Skip to content

Commit

Permalink
[s1ck#49] implemented contains function
Browse files Browse the repository at this point in the history
  • Loading branch information
lc0197 committed Jun 15, 2020
1 parent 7266a30 commit 042d670
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 13 deletions.
6 changes: 6 additions & 0 deletions src/main/antlr4/org/s1ck/gdl/GDL.g4
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ intervalFunc
| betweenOperator
| precedesOperator
| succeedsOperator
| containsOperator
;
overlapsIntervallOperator
: 'overlaps(' interval ')'
Expand All @@ -218,6 +219,11 @@ succeedsOperator
: 'succeeds(' interval ')'
;

containsOperator
: 'contains(' interval ')'
| 'contains(' timePoint ')'
;

stampFunc
: beforePointOperator
| afterPointOperator
Expand Down
38 changes: 34 additions & 4 deletions src/main/java/org/s1ck/gdl/GDLLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.util.*;
import java.util.stream.Collectors;

import static org.s1ck.gdl.utils.Comparator.GTE;
import static org.s1ck.gdl.utils.Comparator.LTE;

class GDLLoader extends GDLBaseListener {
Expand Down Expand Up @@ -472,6 +473,9 @@ else if(intervalFunc.precedesOperator()!=null){
else if(intervalFunc.succeedsOperator()!=null){
predicate = createSucceedsPredicates(from, intervalFunc.succeedsOperator());
}
else if(intervalFunc.containsOperator()!=null){
predicate = createContainsPredicates(from, to, intervalFunc.containsOperator());
}
// additional constraints added during interval processing?
return predicate;
}
Expand Down Expand Up @@ -550,7 +554,33 @@ private Predicate createPrecedesPredicates(TimePoint point, GDLParser.PrecedesOp
private Predicate createSucceedsPredicates(TimePoint point, GDLParser.SucceedsOperatorContext ctx){
TimePoint[] arg = buildIntervall(ctx.interval());
TimePoint arg_to = arg[1];
return new Comparison(point, Comparator.GTE, arg_to);
return new Comparison(point, GTE, arg_to);
}

/**
* Creates a predicate a.contains(b) = a.from<=b.from AND a.to>=b.to
* @param from from value of the calling interval
* @param to to value of the calling interval
* @param ctx context of the call, containing b
* @return contains predicate
*/
private Predicate createContainsPredicates(TimePoint from, TimePoint to, GDLParser.ContainsOperatorContext ctx){
if(ctx.interval()!=null){
TimePoint[] arg = buildIntervall(ctx.interval());
TimePoint arg_from = arg[0];
TimePoint arg_to = arg[1];
return new And(
new Comparison(from, LTE, arg_from),
new Comparison(to, GTE, arg_to)
);
}
// argument is only a timestamp
else{
TimePoint arg = buildTimePoint(ctx.timePoint());
return new And(
new Comparison(from, LTE, arg), new Comparison(to, GTE, arg)
);
}
}

/**
Expand Down Expand Up @@ -726,7 +756,7 @@ public void enterAsOf(GDLParser.AsOfContext ctx){
tp),
new Comparison(
new TimeSelector(identifier, TimeSelector.TimeField.TX_TO),
Comparator.GTE,
GTE,
tp)
)
);
Expand Down Expand Up @@ -759,7 +789,7 @@ private Predicate createDefaultAsOf(){
),
new Comparison(
new TimeSelector(variables.get(0), TimeSelector.TimeField.TX_TO),
Comparator.GTE,
GTE,
now
)
);
Expand All @@ -774,7 +804,7 @@ private Predicate createDefaultAsOf(){
),
new Comparison(
new TimeSelector(variables.get(i), TimeSelector.TimeField.TX_TO),
Comparator.GTE,
GTE,
now
)
)
Expand Down
22 changes: 13 additions & 9 deletions src/test/java/org/s1ck/gdl/GDLLoaderTemporalTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.s1ck.gdl.model.comparables.time.TimeSelector.TimeField.TX_FROM;
import static org.s1ck.gdl.model.comparables.time.TimeSelector.TimeField.TX_TO;
import static org.s1ck.gdl.model.comparables.time.TimeSelector.TimeField.*;
import static org.s1ck.gdl.utils.Comparator.GTE;
import static org.s1ck.gdl.utils.Comparator.LTE;

Expand Down Expand Up @@ -177,7 +176,7 @@ public void succeedsTest(){
expected = new Comparison(
new TimeSelector("a", TX_TO),
GTE,
new TimeSelector("b", TimeSelector.TimeField.VAL_TO)
new TimeSelector("b", VAL_TO)
);
assertPredicateEquals(result, expected);
}
Expand Down Expand Up @@ -253,9 +252,9 @@ public void globalPredicateTest(){

loader = getLoaderFromGDLString("MATCH (a)-[e]->(b) " +
"WHERE val_to.after(tx_from)");
TimeSelector aValTo = new TimeSelector("a", TimeSelector.TimeField.VAL_TO);
TimeSelector bValTo = new TimeSelector("b", TimeSelector.TimeField.VAL_TO);
TimeSelector eValTo = new TimeSelector("e", TimeSelector.TimeField.VAL_TO);
TimeSelector aValTo = new TimeSelector("a", VAL_TO);
TimeSelector bValTo = new TimeSelector("b", VAL_TO);
TimeSelector eValTo = new TimeSelector("e", VAL_TO);
Predicate result3 = loader.getPredicates().get();
Predicate expected3 = new And(
new And(
Expand Down Expand Up @@ -429,10 +428,15 @@ public void intervallMergeAndJoinTest(){
System.out.println(loader.getPredicates());
}


@Test
public void graphTimeStampTest(){

public void containsTest(){
GDLLoader loader = getLoaderFromGDLString("MATCH (a)-[e]->(b) " +
"WHERE a.tx.contains(b.val)");
Predicate expected = new And(
new Comparison(new TimeSelector("a", TX_FROM), LTE, new TimeSelector("b", VAL_FROM)),
new Comparison(new TimeSelector("a", TX_TO), GTE, new TimeSelector("b", VAL_TO))
);
assertPredicateEquals(expected, loader.getPredicates().get());
}

/**
Expand Down

0 comments on commit 042d670

Please sign in to comment.