diff --git a/src/main/antlr4/org/s1ck/gdl/GDL.g4 b/src/main/antlr4/org/s1ck/gdl/GDL.g4 index 8bf2162..33e7079 100644 --- a/src/main/antlr4/org/s1ck/gdl/GDL.g4 +++ b/src/main/antlr4/org/s1ck/gdl/GDL.g4 @@ -256,11 +256,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 diff --git a/src/main/java/org/s1ck/gdl/GDLLoader.java b/src/main/java/org/s1ck/gdl/GDLLoader.java index 34dd4ba..509fc90 100644 --- a/src/main/java/org/s1ck/gdl/GDLLoader.java +++ b/src/main/java/org/s1ck/gdl/GDLLoader.java @@ -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; diff --git a/src/main/java/org/s1ck/gdl/model/comparables/time/Duration.java b/src/main/java/org/s1ck/gdl/model/comparables/time/Duration.java index dcbb083..e8156b8 100644 --- a/src/main/java/org/s1ck/gdl/model/comparables/time/Duration.java +++ b/src/main/java/org/s1ck/gdl/model/comparables/time/Duration.java @@ -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; @@ -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); + } } diff --git a/src/main/java/org/s1ck/gdl/model/comparables/time/TimeConstant.java b/src/main/java/org/s1ck/gdl/model/comparables/time/TimeConstant.java index 10711d4..8fd6053 100644 --- a/src/main/java/org/s1ck/gdl/model/comparables/time/TimeConstant.java +++ b/src/main/java/org/s1ck/gdl/model/comparables/time/TimeConstant.java @@ -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; @@ -139,4 +140,5 @@ public boolean isGlobal() { public ComparableExpression replaceGlobalByLocal(List variables) { return this; } + } diff --git a/src/test/java/org/s1ck/gdl/GDLLoaderTemporalTest.java b/src/test/java/org/s1ck/gdl/GDLLoaderTemporalTest.java index de8ab87..5a39e97 100644 --- a/src/test/java/org/s1ck/gdl/GDLLoaderTemporalTest.java +++ b/src/test/java/org/s1ck/gdl/GDLLoaderTemporalTest.java @@ -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( -// // (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); } /**