From a511ff3e9898b3e7845e7f39d72f6540e49e9ea7 Mon Sep 17 00:00:00 2001 From: lc0197 Date: Mon, 27 Jul 2020 18:34:44 +0200 Subject: [PATCH] [#49] fixed global time selectors --- .../java/org/s1ck/gdl/GDLLoaderTemporal.java | 35 ++++++++++-- .../org/s1ck/gdl/GDLLoaderTemporalTest.java | 56 ++++++++++++------- 2 files changed, 65 insertions(+), 26 deletions(-) diff --git a/src/main/java/org/s1ck/gdl/GDLLoaderTemporal.java b/src/main/java/org/s1ck/gdl/GDLLoaderTemporal.java index 40f8c5c..55a886a 100644 --- a/src/main/java/org/s1ck/gdl/GDLLoaderTemporal.java +++ b/src/main/java/org/s1ck/gdl/GDLLoaderTemporal.java @@ -96,10 +96,21 @@ private TimeLiteral buildTimeLiteral(GDLParser.TimeLiteralContext ctx) { * @return TimeSelector */ private TimeSelector buildTimeSelector(GDLParser.TimeSelectorContext ctx) { + boolean global = ctx.Identifier() == null; // checks whether ID is even there (is a vertex or edge) and returns its variable - String var = ctx.Identifier() != null ? - loader.resolveIdentifier(ctx.Identifier().getText()) : TimeSelector.GLOBAL_SELECTOR; + String var = global ? + TimeSelector.GLOBAL_SELECTOR : loader.resolveIdentifier(ctx.Identifier().getText()); String field = ctx.TimeProp().getText(); + if(global){ + boolean tx = field.trim().startsWith("tx"); + TimeSelector.TimeField from = tx ? + TimeSelector.TimeField.TX_FROM : TimeSelector.TimeField.VAL_FROM; + TimeSelector.TimeField to = tx ? + TimeSelector.TimeField.TX_TO : TimeSelector.TimeField.VAL_TO; + TimePoint global_from = new TimeSelector(var, from); + TimePoint global_to = new TimeSelector(var, to); + predicateStack.addFirst(new Comparison(global_from, LTE, global_to)); + } return new TimeSelector(var, field); } @@ -504,7 +515,7 @@ private TimePoint[] buildInterval(GDLParser.IntervalContext ctx) { GDLParser.IntervalFromStampsContext fs = ctx.intervalFromStamps(); TimePoint[] intv = buildIntervalFromStamps(fs); // custom interval: make sure that from <= to - predicateStack.add(new Comparison(intv[0], LTE, intv[1])); + predicateStack.addFirst(new Comparison(intv[0], LTE, intv[1])); return intv; } else if (ctx.complexInterval() != null) { GDLParser.ComplexIntervalArgumentContext arg1 = ctx.complexInterval() @@ -526,11 +537,16 @@ private TimePoint[] buildInterval(GDLParser.IntervalContext ctx) { * @return {@code {from, to}} representing the interval */ private TimePoint[] buildIntervalFromSelector(GDLParser.IntervalSelectorContext ctx) { - String var = ctx.Identifier() != null ? - loader.resolveIdentifier(ctx.Identifier().getText()) : TimeSelector.GLOBAL_SELECTOR; + boolean global = ctx.Identifier() == null; + String var = global ? + TimeSelector.GLOBAL_SELECTOR : loader.resolveIdentifier(ctx.Identifier().getText()); String intId = ctx.IntervalConst().getText(); TimePoint from = new TimeSelector(var, intId + "_from"); TimePoint to = new TimeSelector(var, intId + "_to"); + if(global){ + // make sure that global from <= global to + predicateStack.addFirst(new Comparison(from, LTE, to)); + } return new TimePoint[]{from, to}; } @@ -603,8 +619,15 @@ private TimePoint[] buildIntervalFromComplex(GDLParser.ComplexIntervalArgumentCo * @return (potentially complex) {@code Predicate} that encodes the time stamp function. Atoms are time stamp comparisons */ Predicate buildStampFunction(GDLParser.StmpFContext ctx) { + int predicateSizeBefore = predicateStack.size(); TimePoint tp = buildTimePoint(ctx.timePoint()); - return createStampPredicates(tp, ctx.stampFunc()); + Predicate predicate = createStampPredicates(tp, ctx.stampFunc()); + // implicit constraints for global val, tx + int countConstraints = predicateStack.size() - predicateSizeBefore; + for (int i = 0; i < countConstraints; i++) { + predicate = new And(predicate, predicateStack.removeFirst()); + } + return predicate; } /** diff --git a/src/test/java/org/s1ck/gdl/GDLLoaderTemporalTest.java b/src/test/java/org/s1ck/gdl/GDLLoaderTemporalTest.java index 978909a..4a4f598 100644 --- a/src/test/java/org/s1ck/gdl/GDLLoaderTemporalTest.java +++ b/src/test/java/org/s1ck/gdl/GDLLoaderTemporalTest.java @@ -168,27 +168,29 @@ public void globalPredicateTest(){ TimeSelector eFrom = new TimeSelector("e", TX_FROM); TimeSelector eTo = new TimeSelector("e", TX_TO); - MaxTimePoint globalFrom = new MaxTimePoint( + MaxTimePoint globalTxFrom = new MaxTimePoint( eFrom, aFrom, bFrom ); - MinTimePoint globalTo = new MinTimePoint( + MinTimePoint globalTxTo = new MinTimePoint( eTo, aTo, bTo ); + Comparison globalTxPredicate = new Comparison(globalTxFrom, LTE, globalTxTo); + // only lhs global GDLLoader loader = getLoaderFromGDLString("MATCH (a)-[e]->(b) " + "WHERE tx.between(1970-01-01, 2020-05-01)"); - Predicate expected1 = new And( - new Comparison(globalFrom, LTE, l2), - new Comparison(globalTo, GT, l1) - ); + Predicate expected1 = new And(new And( + new Comparison(globalTxFrom, LTE, l2), + new Comparison(globalTxTo, GT, l1) + ), globalTxPredicate); assertPredicateEquals(expected1, loader.getPredicates().get()); //only rhs global loader = getLoaderFromGDLString("MATCH (a)-[e]->(b) " + "WHERE 1970-01-01.precedes(tx)"); - Comparison expected2 = new Comparison(l1, LTE, globalFrom); - assertPredicateEquals(expected2, loader.getPredicates().get()); + //Predicate expected2 = new And(new Comparison(l1, LTE, globalFrom), globalFromPredicate); + //assertPredicateEquals(expected2, loader.getPredicates().get()); //both sides global loader = getLoaderFromGDLString("MATCH (a)-[e]->(b) " + @@ -196,12 +198,21 @@ public void globalPredicateTest(){ TimeSelector aValTo = new TimeSelector("a", VAL_TO); TimeSelector bValTo = new TimeSelector("b", VAL_TO); TimeSelector eValTo = new TimeSelector("e", VAL_TO); + TimeSelector aValFrom = new TimeSelector("a", VAL_FROM); + TimeSelector bValFrom = new TimeSelector("b", VAL_FROM); + TimeSelector eValFrom = new TimeSelector("e", VAL_FROM); + MaxTimePoint globalValFrom = new MaxTimePoint(eValFrom, aValFrom, bValFrom); MinTimePoint globalValTo = new MinTimePoint( - eValTo, bValTo, eValTo + eValTo, aValTo, bValTo ); - Predicate expected3 = new Comparison(globalValTo, GT, globalFrom); + Predicate globalValPredicate = new Comparison(globalValFrom, LTE, globalValTo); + Predicate expected3 = new And( + new And(new Comparison(globalValTo, GT, globalTxFrom), globalTxPredicate), globalValPredicate); //assertPredicateEquals(expectedProcessed3, resultProcessed3); + System.out.println(expected3); + System.out.println(loader.getPredicates().get()); assertPredicateEquals(expected3, loader.getPredicates().get()); + } @Test @@ -218,9 +229,9 @@ public void intervalMergeAndJoinTest(){ Predicate expected = new And( new And( new Comparison(new MaxTimePoint(aTxFrom, bTxFrom), GTE, tl2), + new Comparison(tl1, LTE, tl2)), new Comparison(new MaxTimePoint(aTxFrom, bTxFrom), LTE, - new MinTimePoint(aTxTo, bTxTo))), - new Comparison(tl1, LTE, tl2) + new MinTimePoint(aTxTo, bTxTo)) ); assertPredicateEquals(expected, loader.getPredicates().get()); @@ -232,9 +243,9 @@ public void intervalMergeAndJoinTest(){ expected = new And( new And( new Comparison(new MinTimePoint(aTxFrom, bTxFrom), GTE, tl2), + new Comparison(tl1, LTE, tl2)), new Comparison(new MaxTimePoint(aTxFrom, bTxFrom), LTE, - new MinTimePoint(aTxTo, bTxTo))), - new Comparison(tl1, LTE, tl2) + new MinTimePoint(aTxTo, bTxTo)) ); assertPredicateEquals(expected, loader.getPredicates().get()); @@ -431,8 +442,9 @@ private void lengthComparisonTest(String operator, Comparator comparator){ MinTimePoint globalValTo = new MinTimePoint(eValTo, aValTo, bValTo); Duration globalValDuration = new Duration(globalValFrom, globalValTo); Comparison globalValPred = new Comparison(globalValFrom, LTE, globalValTo); - expected = new And(globalValPred, - new Comparison(globalValDuration, comparator, fiveMinutes)); + // !!! global interval + expected = new And(new And(globalValPred, + new Comparison(globalValDuration, comparator, fiveMinutes)), globalValPred); assertPredicateEquals(loader.getPredicates().get(), expected); loader = getLoaderFromGDLString("MATCH (a)-[e]->(b) " + @@ -509,11 +521,15 @@ private void lengthComparisonTest(String operator, Comparator comparator){ MinTimePoint globalTxTo = new MinTimePoint(eTxTo, aTxTo, bTxTo); Duration globalTxDuration = new Duration(globalTxFrom, globalTxTo); Comparison globalTxPred = new Comparison(globalTxFrom, LTE, globalTxTo); - expected = new And( + // !!! global val and tx + expected = new And(new And(new And( new And( globalValPred, globalTxPred ), - new Comparison(globalValDuration, comparator, globalTxDuration)); + new Comparison(globalValDuration, comparator, globalTxDuration)), + globalTxPred), globalValPred); + System.out.println(loader.getPredicates().get()); + System.out.println(expected); assertPredicateEquals(loader.getPredicates().get(), expected); loader = getLoaderFromGDLString("MATCH (a)-[e]->(b) " + @@ -522,11 +538,11 @@ private void lengthComparisonTest(String operator, Comparator comparator){ TimeLiteral l2 = new TimeLiteral("2020-05-05"); Duration constantInterval = new Duration(l1, l2); Comparison constantPred = new Comparison(l1, LTE, l2); - expected = new And( + expected = new And(new And( new And( new And(globalValPred, constantPred), new Comparison(globalValDuration, comparator, constantInterval)), - new Comparison(l1, LTE, l2)); + new Comparison(l1, LTE, l2)), globalValPred); assertPredicateEquals(loader.getPredicates().get(), expected); }