Skip to content

Commit

Permalink
[s1ck#49] implemented longerThan
Browse files Browse the repository at this point in the history
  • Loading branch information
lc0197 committed May 7, 2020
1 parent fe25846 commit 3b933b1
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 73 deletions.
8 changes: 6 additions & 2 deletions src/main/antlr4/org/s1ck/gdl/GDL.g4
Original file line number Diff line number Diff line change
Expand Up @@ -252,11 +252,15 @@ equalsOperator
;

longerThanOperator
: 'longerThan(' timeConstant ')'
: 'longerThan(' (interval | timeConstant) ')'
;

timeConstant
: IntegerLiteral ('days'|'hours'|'minutes'|'seconds'|'millis')
: 'Millis(' IntegerLiteral ')'
| 'Seconds(' IntegerLiteral ')'
| 'Minutes(' IntegerLiteral ')'
| 'Hours(' IntegerLiteral ')'
| 'Days(' IntegerLiteral ')'
;

stampFunc
Expand Down
25 changes: 16 additions & 9 deletions src/main/java/org/s1ck/gdl/GDLLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -683,27 +683,34 @@ private Predicate createEqualsPredicates(TimePoint from, TimePoint to, GDLParser
}

private Predicate createLongerThanPredicates(TimePoint from, TimePoint to, GDLParser.LongerThanOperatorContext ctx){
TimeConstant constant = buildTimeConstant(ctx.timeConstant());
return new Comparison(
new Duration(from, to), GT, constant
);
Duration rhs = new Duration(from, to);
if(ctx.timeConstant()!=null) {
TimeConstant constant = buildTimeConstant(ctx.timeConstant());
return new Comparison(rhs, GT, constant);
}
else if(ctx.interval()!=null){
TimePoint[] interval = buildIntervall(ctx.interval());
Duration lhs = new Duration(interval[0], interval[1]);
return new Comparison(rhs, GT, lhs);
}
return null;
}

private TimeConstant buildTimeConstant(GDLParser.TimeConstantContext ctx){
int value = Integer.parseInt(ctx.IntegerLiteral().getText());
if(ctx.getText().contains("days")){
if(ctx.getText().startsWith("Days(")){
return new TimeConstant(value,0,0,0,0);
}
else if(ctx.getText().contains("hours")){
else if(ctx.getText().startsWith("Hours(")){
return new TimeConstant(0, value, 0, 0, 0);
}
else if(ctx.getText().contains("minutes")){
else if(ctx.getText().startsWith("Minutes(")){
return new TimeConstant(0, 0, value, 0, 0);
}
else if(ctx.getText().contains("seconds")){
else if(ctx.getText().startsWith("Seconds(")){
return new TimeConstant(0, 0, 0, value, 0);
}
else if(ctx.getText().contains("milliseconds") || ctx.getText().contains("millis")){
else if(ctx.getText().startsWith("Millis(")){
return new TimeConstant(0, 0, 0, 0, value);
}
return null;
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/org/s1ck/gdl/model/comparables/time/Duration.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.s1ck.gdl.model.comparables.ComparableExpression;
import org.s1ck.gdl.model.predicates.Predicate;
import org.s1ck.gdl.model.predicates.expressions.Comparison;

import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -137,4 +138,18 @@ protected Predicate unfoldLT(TimePoint arg) {
protected Predicate unfoldLTE(TimePoint arg) {
return null;
}

@Override
public String toString(){
return "Duration("+from.toString()+", "+to.toString()+")";
}

@Override
public boolean equals(Object o){
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

Duration that = (Duration) o;
return that.from.equals(from) && that.to.equals(to);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.s1ck.gdl.model.comparables.ComparableExpression;
import org.s1ck.gdl.model.predicates.Predicate;
import org.s1ck.gdl.model.predicates.expressions.Comparison;

import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -139,4 +140,5 @@ public boolean isGlobal() {
public ComparableExpression replaceGlobalByLocal(List<String> variables) {
return this;
}

}
133 changes: 71 additions & 62 deletions src/test/java/org/s1ck/gdl/GDLLoaderTemporalTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -717,68 +717,77 @@ public void minMaxTest(){

@Test
public void longerThanTest(){
// GDLLoader loaderDoProcess = getLoaderFromGDLString("MATCH (a)-[e]->(b) " +
// "WHERE a.val.longerThan(10 days)");
// TimeSelector aValFrom = new TimeSelector("a", VAL_FROM);
// TimeSelector aValTo = new TimeSelector("a", VAL_TO);
// TimeSelector bValFrom = new TimeSelector("b", VAL_FROM);
// TimeSelector bValTo = new TimeSelector("b", VAL_TO);
// TimeSelector eValFrom = new TimeSelector("e", VAL_FROM);
// TimeSelector eValTo = new TimeSelector("e", VAL_TO);
// TimeConstant tenDays = new TimeConstant(10,0,0,0,0);
// Predicate expected = new Comparison(new PlusTimePoint(aValFrom, tenDays), LT, aValTo);
// assertPredicateEquals(loaderDoProcess.getPredicates().get(), expected);
//
// loaderDoProcess = getLoaderFromGDLString("MATCH (a)-[e]->(b) " +
// "WHERE a.val.longerThan(12 hours)");
// TimeConstant twelveHours = new TimeConstant(0,12,0,0,0);
// expected = new Comparison(new PlusTimePoint(aValFrom, twelveHours), LT, aValTo);
// assertPredicateEquals(loaderDoProcess.getPredicates().get(), expected);
// System.out.println(loaderDoProcess.getPredicates());
//
// loaderDoProcess = getLoaderFromGDLString("MATCH (a)-[e]->(b) " +
// "WHERE val.longerThan(5 minutes)");
// TimeConstant fiveMinutes = new TimeConstant(0,0,5,0,0);
// expected = new And(
// // <e.val_to, <a.val_to
// new And(
// //e
// new And(
// // e,a
// new And(
// new Comparison(
// new PlusTimePoint(eValFrom, fiveMinutes), LT, eValTo),
// new Comparison(new PlusTimePoint(aValFrom, fiveMinutes), LT, eValTo)
// ),
// //b
// new Comparison(new PlusTimePoint(bValFrom, fiveMinutes), LT, eValTo)
// ),
// //a
// new And(
// // e,a
// new And(
// new Comparison(new PlusTimePoint(eValFrom, fiveMinutes), LT, aValTo),
// new Comparison(new PlusTimePoint(aValFrom, fiveMinutes), LT, aValTo)
// ),
// //b
// new Comparison(new PlusTimePoint(bValFrom, fiveMinutes), LT, aValTo)
// )
// ),
// // <b.val_to
// new And(
// // e,a
// new And(
// new Comparison(new PlusTimePoint(eValFrom, fiveMinutes), LT, bValTo),
// new Comparison(new PlusTimePoint(aValFrom, fiveMinutes), LT, bValTo)
// ),
// //b
// new Comparison(new PlusTimePoint(bValFrom, fiveMinutes), LT, bValTo)
// )
// );
// System.out.println(expected);
// System.out.println(loaderDoProcess.getPredicates().get());
// assertPredicateEquals(loaderDoProcess.getPredicates().get(), expected);
// System.out.println(loaderDoProcess.getPredicates());
GDLLoader loader = getLoaderFromGDLString("MATCH (a)-[e]->(b) " +
"WHERE a.val.longerThan(Days(10))", false);

TimeSelector aValFrom = new TimeSelector("a", VAL_FROM);
TimeSelector aValTo = new TimeSelector("a", VAL_TO);
TimeSelector bValFrom = new TimeSelector("b", VAL_FROM);
TimeSelector bValTo = new TimeSelector("b", VAL_TO);
TimeSelector eValFrom = new TimeSelector("e", VAL_FROM);
TimeSelector eValTo = new TimeSelector("e", VAL_TO);
TimeConstant tenDays = new TimeConstant(10,0,0,0,0);

Duration valDuration = new Duration(aValFrom, aValTo);

Predicate expected = new Comparison(valDuration, GT, tenDays);
assertPredicateEquals(loader.getPredicates().get(), expected);

loader = getLoaderFromGDLString("MATCH (a)-[e]->(b) " +
"WHERE a.val.longerThan(Hours(12))", false);
TimeConstant twelveHours = new TimeConstant(0,12,0,0,0);
expected = new Comparison(valDuration, GT, twelveHours);
assertPredicateEquals(loader.getPredicates().get(), expected);
System.out.println(loader.getPredicates());

loader = getLoaderFromGDLString("MATCH (a)-[e]->(b) " +
"WHERE val.longerThan(Minutes(5))", false);
TimeConstant fiveMinutes = new TimeConstant(0,0,5,0,0);
MaxTimePoint globalValFrom = new MaxTimePoint(eValFrom, aValFrom, bValFrom);
MinTimePoint globalValTo = new MinTimePoint(eValTo, aValTo, bValTo);
Duration globalValDuration = new Duration(globalValFrom, globalValTo);
expected = new Comparison(globalValDuration, GT, fiveMinutes);
assertPredicateEquals(loader.getPredicates().get(), expected);

loader = getLoaderFromGDLString("MATCH (a)-[e]->(b) " +
"WHERE a.val.merge(b.val).longerThan(Hours(20))", false);
TimeConstant twentyHours = new TimeConstant(0,20,0,0,0);
MaxTimePoint mergeFrom = new MaxTimePoint(aValFrom, bValFrom);
MinTimePoint mergeTo = new MinTimePoint(aValTo, bValTo);
Duration mergeDuration = new Duration(mergeFrom, mergeTo);
expected = new And(
new Comparison(mergeDuration, GT, twentyHours),
new Comparison(mergeFrom, LTE, mergeTo)
);
assertPredicateEquals(loader.getPredicates().get(), expected);

loader = getLoaderFromGDLString("MATCH (a)-[e]->(b) " +
"WHERE Interval(a.val_from, b.val_to).longerThan(Days(4))", false);
TimeConstant fourDays = new TimeConstant(4,0,0,0,0);
Duration intervalDuration = new Duration(aValFrom, bValTo);
expected = new Comparison(intervalDuration, GT, fourDays);
assertPredicateEquals(loader.getPredicates().get(), expected);

loader = getLoaderFromGDLString("MATCH (a)-[e]->(b) " +
"WHERE a.val.longerThan(b.val)", false);
Duration aVal = new Duration(aValFrom, aValTo);
Duration bVal = new Duration(bValFrom, bValTo);
expected = new Comparison(aVal, GT, bVal);
assertPredicateEquals(loader.getPredicates().get(), expected);

loader = getLoaderFromGDLString("MATCH (a)-[e]->(b) " +
"WHERE val.longerThan(tx)", false);
TimeSelector eTxFrom = new TimeSelector("e", TX_FROM);
TimeSelector aTxFrom = new TimeSelector("a", TX_FROM);
TimeSelector bTxFrom = new TimeSelector("b", TX_FROM);
TimeSelector eTxTo = new TimeSelector("e", TX_TO);
TimeSelector aTxTo = new TimeSelector("a", TX_TO);
TimeSelector bTxTo = new TimeSelector("b", TX_TO);
MaxTimePoint globalTxFrom = new MaxTimePoint(eTxFrom, aTxFrom, bTxFrom);
MinTimePoint globalTxTo = new MinTimePoint(eTxTo, aTxTo, bTxTo);
Duration globalTxDuration = new Duration(globalTxFrom, globalTxTo);
expected = new Comparison(globalValDuration, GT, globalTxDuration);
assertPredicateEquals(loader.getPredicates().get(), expected);
}

/**
Expand Down

0 comments on commit 3b933b1

Please sign in to comment.