Skip to content

Commit

Permalink
[s1ck#49] implemented duration type
Browse files Browse the repository at this point in the history
  • Loading branch information
lc0197 committed Jun 15, 2020
1 parent 255a9a0 commit 9fa3a7d
Show file tree
Hide file tree
Showing 16 changed files with 468 additions and 325 deletions.
5 changes: 2 additions & 3 deletions src/main/java/org/s1ck/gdl/GDLLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import org.s1ck.gdl.model.*;
import org.s1ck.gdl.model.comparables.ElementSelector;
import org.s1ck.gdl.model.comparables.time.*;
import org.s1ck.gdl.model.comparables.time.util.TimeConstant;
import org.s1ck.gdl.model.comparables.time.TimeConstant;
import org.s1ck.gdl.model.predicates.booleans.And;
import org.s1ck.gdl.model.predicates.expressions.Comparison;
import org.s1ck.gdl.model.predicates.Predicate;
Expand Down Expand Up @@ -684,9 +684,8 @@ private Predicate createEqualsPredicates(TimePoint from, TimePoint to, GDLParser

private Predicate createLongerThanPredicates(TimePoint from, TimePoint to, GDLParser.LongerThanOperatorContext ctx){
TimeConstant constant = buildTimeConstant(ctx.timeConstant());
Comparison c = new Comparison(new PlusTimePoint(from, constant), LT, to);
return new Comparison(
new PlusTimePoint(from, constant), LT, to
new Duration(from, to), GT, constant
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
* Represents a MAX(p1,...,pn) term, where p1...pn are TimePoints
Expand All @@ -29,18 +30,18 @@ public MaxTimePoint(TimePoint...args){


@Override
public long evaluate(){
long mn = Long.MIN_VALUE;
public Optional<Long> evaluate(){
long mx = Long.MIN_VALUE;
for (TimePoint p:args){
long eval = p.evaluate();
if(eval== UNDEFINED){
return UNDEFINED;
Optional<Long> eval = p.evaluate();
if(!eval.isPresent()){
return Optional.empty();
}
if(eval > mn){
mn = eval;
if(eval.get() > mx){
mx = eval.get();
}
}
return mn;
return Optional.of(mx);
}

@Override
Expand All @@ -49,7 +50,7 @@ public long getLowerBound(){
long res = Long.MIN_VALUE;
for (TimePoint p: args){
long val = p.getLowerBound();
if(val!=UNDEFINED && val>res){
if(val>res){
res = val;
}
}
Expand Down
21 changes: 11 additions & 10 deletions src/main/java/org/s1ck/gdl/model/comparables/time/MinTimePoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
* Represents a MAX(p1,...,pn) term, where p1...pn are TimePoints
Expand All @@ -26,18 +27,18 @@ public MinTimePoint(TimePoint...args){
}

@Override
public long evaluate(){
public Optional<Long> evaluate(){
long mn = Long.MAX_VALUE;
for (TimePoint p:args){
long eval = p.evaluate();
if (eval==UNDEFINED){
return UNDEFINED;
Optional<Long> eval = p.evaluate();
if (!eval.isPresent()){
return Optional.empty();
}
if (eval < mn){
mn = eval;
if (eval.get() < mn){
mn = eval.get();
}
}
return mn;
return Optional.of(mn);
}

@Override
Expand All @@ -46,8 +47,8 @@ public long getLowerBound(){
long res = Long.MAX_VALUE;
for (TimePoint p: args){
long val = p.getLowerBound();
if(val==0){
return 0;
if(val==Long.MIN_VALUE){
return Long.MIN_VALUE;
}
if(val< res){
res = val;
Expand All @@ -62,7 +63,7 @@ public long getUpperBound(){
long res = Long.MAX_VALUE;
for (TimePoint p: args){
long val = p.getUpperBound();
if(val!=UNDEFINED && val < res){
if(val!=Long.MAX_VALUE && val < res){
res = val;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/*
package org.s1ck.gdl.model.comparables.time;
import org.s1ck.gdl.model.comparables.ComparableExpression;
import org.s1ck.gdl.model.comparables.time.util.TimeConstant;
import org.s1ck.gdl.model.comparables.time.TimeConstant;
import org.s1ck.gdl.model.predicates.Predicate;
import org.s1ck.gdl.model.predicates.booleans.And;
import org.s1ck.gdl.model.predicates.booleans.Or;
Expand All @@ -16,26 +17,34 @@
import static org.s1ck.gdl.utils.Comparator.*;
import static org.s1ck.gdl.utils.Comparator.GTE;
*/
/**
* Represents an addition of a constant to a given TimePoint
*/
*//*
public class PlusTimePoint extends TimeAtom{
/**
*/
/**
* The wrapped TimePoint
*/
*//*
private TimePoint timePoint;
/**
*/
/**
* The constant to be added to the wrapped TimePoint
*/
*//*
private TimeConstant constant;
/**
*/
/**
* Initializes a Sum of a TimePoint and a constant
* @param timePoint the TimePoint
* @param constant the constant to be added to the TimePoint
*/
*//*
public PlusTimePoint(TimePoint timePoint, TimeConstant constant){
this.timePoint = timePoint;
this.constant = constant;
Expand Down Expand Up @@ -126,14 +135,16 @@ else if(comp.equals(Comparator.GTE)){
return null;
}
/**
*/
/**
* Translates a comparison {@code (this == rhs)} into an equivalent predicate that does not contain
* global time selectors/intervals anymore.
* Basically the same method as in {@link TimeSelector}, but adapted for {@link PlusTimePoint}.
* @param rhs the right hand side of the comparison to translate
* @param variables all query variables
* @return translated comparison
*/
*//*
private Predicate unfoldGlobalEQ(ComparableExpression rhs, List<String> variables){
// exists var: var.from==rhs
Predicate exists = existsVariable(EQ, rhs, variables);
Expand All @@ -151,26 +162,30 @@ private Predicate unfoldGlobalEQ(ComparableExpression rhs, List<String> variable
}
}
/**
*/
/**
* Translates a comparison {@code (this != rhs)} into an equivalent predicate that does not contain
* global time selectors/intervals anymore.
* Basically the same method as in {@link TimeSelector}, but adapted for {@link PlusTimePoint}
* @param rhs the right hand side of the comparison to translate
* @param variables all query variables
* @return translated comparison
*/
*//*
private Predicate unfoldGlobalNEQ(ComparableExpression rhs, List<String> variables){
return forAllVariables(NEQ, rhs, variables);
}
/**
*/
/**
* Translates a comparison {@code (this < rhs)} into an equivalent predicate that does not contain
* global time selectors/intervals anymore.
* Basically the same method as in {@link TimeSelector}, but adapted for {@link PlusTimePoint}
* @param rhs the right hand side of the comparison to translate
* @param variables all query variables
* @return translated comparison
*/
*//*
private Predicate unfoldGlobalLT(ComparableExpression rhs, List<String> variables){
TimeSelector.TimeField timeProp = ((TimeSelector)timePoint).getTimeProp();
if(timeProp.equals(TimeSelector.TimeField.TX_FROM) || timeProp.equals(TimeSelector.TimeField.VAL_FROM)){
Expand All @@ -183,14 +198,16 @@ private Predicate unfoldGlobalLT(ComparableExpression rhs, List<String> variable
}
}
/**
*/
/**
* Translates a comparison {@code (this <= rhs)} into an equivalent predicate that does not contain
* global time selectors/intervals anymore.
* Basically the same method as in {@link TimeSelector}, but adapted for {@link PlusTimePoint}
* @param rhs the right hand side of the comparison to translate
* @param variables all query variables
* @return translated comparison
*/
*//*
private Predicate unfoldGlobalLTE(ComparableExpression rhs, List<String> variables){
TimeSelector.TimeField timeProp = ((TimeSelector)timePoint).getTimeProp();
if(timeProp.equals(TimeSelector.TimeField.TX_FROM) || timeProp.equals(TimeSelector.TimeField.VAL_FROM)){
Expand All @@ -203,14 +220,16 @@ private Predicate unfoldGlobalLTE(ComparableExpression rhs, List<String> variabl
}
}
/**
*/
/**
* Translates a comparison {@code (this > rhs)} into an equivalent predicate that does not contain
* global time selectors/intervals anymore.
* Basically the same method as in {@link TimeSelector}, but adapted for {@link PlusTimePoint}
* @param rhs the right hand side of the comparison to translate
* @param variables all query variables
* @return translated comparison
*/
*//*
private Predicate unfoldGlobalGT(ComparableExpression rhs, List<String> variables){
TimeSelector.TimeField timeProp = ((TimeSelector)timePoint).getTimeProp();
if(timeProp.equals(TimeSelector.TimeField.TX_FROM) || timeProp.equals(TimeSelector.TimeField.VAL_FROM)){
Expand All @@ -223,14 +242,16 @@ private Predicate unfoldGlobalGT(ComparableExpression rhs, List<String> variable
}
}
/**
*/
/**
* Translates a comparison {@code (this >= rhs)} into an equivalent predicate that does not contain
* global time selectors/intervals anymore.
* Basically the same method as in {@link TimeSelector}, but adapted for {@link PlusTimePoint}
* @param rhs the right hand side of the comparison to translate
* @param variables all query variables
* @return translated comparison
*/
*//*
private Predicate unfoldGlobalGTE(ComparableExpression rhs, List<String> variables){
TimeSelector.TimeField timeProp = ((TimeSelector)timePoint).getTimeProp();
if(timeProp.equals(TimeSelector.TimeField.TX_FROM) || timeProp.equals(TimeSelector.TimeField.VAL_FROM)){
Expand All @@ -243,14 +264,16 @@ private Predicate unfoldGlobalGTE(ComparableExpression rhs, List<String> variabl
}
}
/**
*/
/**
* Returns a predicate equivalent to {@code exists v in variables s.t. (v comp rhs) holds}
* Basically the same method as in {@link TimeSelector}, but adapted for {@link PlusTimePoint}
* @param comp the comparator
* @param rhs the rhs in the comparison
* @param variables the query variables to "iterate" over (the domain)
* @return predicate equivalent to {@code exists v in variables s.t. (v comp rhs) holds}
*/
*//*
private Predicate existsVariable(Comparator comp, ComparableExpression rhs, List<String> variables){
TimeSelector.TimeField timeProp = ((TimeSelector)timePoint).getTimeProp();
Comparison c0 = new Comparison(
Expand All @@ -267,14 +290,16 @@ private Predicate existsVariable(Comparator comp, ComparableExpression rhs, List
return exists;
}
/**
*/
/**
* Returns a predicate equivalent to {@code forall v in variables: (v comp rhs) holds}
* Basically the same method as in {@link TimeSelector}, but adapted for {@link PlusTimePoint}
* @param comp the comparator
* @param rhs the rhs in the comparison
* @param variables the query variables to "iterate" over (the domain)
* @return predicate equivalent to {@code forall v in variables: (v comp rhs) holds}
*/
*//*
private Predicate forAllVariables(Comparator comp, ComparableExpression rhs, List<String> variables){
TimeSelector.TimeField timeProp = ((TimeSelector)timePoint).getTimeProp();
Comparison c0 = new Comparison(
Expand Down Expand Up @@ -305,3 +330,4 @@ public ComparableExpression replaceGlobalByLocal(List<String> variables) {
return new PlusTimePoint((TimePoint)timePoint.replaceGlobalByLocal(variables), constant);
}
}
*/
Loading

0 comments on commit 9fa3a7d

Please sign in to comment.