Skip to content

Commit

Permalink
[s1ck#49] implemented lengthAtLeast, lengthAtMost
Browse files Browse the repository at this point in the history
  • Loading branch information
lc0197 committed May 8, 2020
1 parent b89604a commit 3dae776
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/main/antlr4/org/s1ck/gdl/GDL.g4
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ intervalFunc
| equalsOperator
| longerThanOperator
| shorterThanOperator
| lengthAtLeastOperator
| lengthAtMostOperator
;
overlapsIntervallOperator
: 'overlaps(' interval ')'
Expand Down Expand Up @@ -260,6 +262,14 @@ shorterThanOperator
: 'shorterThan(' (interval | timeConstant) ')'
;

lengthAtLeastOperator
: 'lengthAtLeast(' (interval | timeConstant) ')'
;

lengthAtMostOperator
: 'lengthAtMost(' (interval | timeConstant) ')'
;

timeConstant
: 'Millis(' IntegerLiteral ')'
| 'Seconds(' IntegerLiteral ')'
Expand Down
50 changes: 50 additions & 0 deletions src/main/java/org/s1ck/gdl/GDLLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,12 @@ else if(intervalFunc.longerThanOperator()!=null){
else if(intervalFunc.shorterThanOperator()!=null){
return createShorterThanPredicates(from, to, intervalFunc.shorterThanOperator());
}
else if(intervalFunc.lengthAtLeastOperator()!=null){
return createLengthAtLeastPredicates(from, to, intervalFunc.lengthAtLeastOperator());
}
else if(intervalFunc.lengthAtMostOperator()!=null){
return createLengthAtMostPredicates(from, to, intervalFunc.lengthAtMostOperator());
}
return null;
}

Expand Down Expand Up @@ -748,6 +754,50 @@ else if(ctx.interval()!=null){
return null;
}

/**
* Creates a predicate a.lengthAtLeast(b) = (length(a) >= length(b))
* @param from from value of the calling interval
* @param to to value of the calling interval
* @param ctx context containing the callee interval
* @return lengthAtLeast predicate
*/
private Predicate createLengthAtLeastPredicates(TimePoint from, TimePoint to,
GDLParser.LengthAtLeastOperatorContext ctx){
Duration rhs = new Duration(from, to);
if(ctx.timeConstant()!=null) {
TimeConstant constant = buildTimeConstant(ctx.timeConstant());
return new Comparison(rhs, GTE, constant);
}
else if(ctx.interval()!=null){
TimePoint[] interval = buildIntervall(ctx.interval());
Duration lhs = new Duration(interval[0], interval[1]);
return new Comparison(rhs, GTE, lhs);
}
return null;
}

/**
* Creates a predicate a.lengthAtMost(b) = (length(a) <= length(b))
* @param from from value of the calling interval
* @param to to value of the calling interval
* @param ctx context containing the callee interval
* @return lengthAtMost predicate
*/
private Predicate createLengthAtMostPredicates(TimePoint from, TimePoint to,
GDLParser.LengthAtMostOperatorContext ctx){
Duration rhs = new Duration(from, to);
if(ctx.timeConstant()!=null) {
TimeConstant constant = buildTimeConstant(ctx.timeConstant());
return new Comparison(rhs, LTE, constant);
}
else if(ctx.interval()!=null){
TimePoint[] interval = buildIntervall(ctx.interval());
Duration lhs = new Duration(interval[0], interval[1]);
return new Comparison(rhs, LTE, lhs);
}
return null;
}

/**
* Creates a TimeConstant given a suitable context. Constants can be a constant number
* of days ({@code Days(n)}), hours ({@code Hours(n)}), minutes ({@code Minutes(n)}),
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/org/s1ck/gdl/GDLLoaderTemporalTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,16 @@ public void shorterThanTest(){
lengthComparisonTest("shorterThan", LT);
}

@Test
public void lengthAtLeastTest(){
lengthComparisonTest("lengthAtLeast", GTE);
}

@Test
public void lengthAtMostTest(){
lengthComparisonTest("lengthAtMost", LTE);
}

private void lengthComparisonTest(String operator, Comparator comparator){
GDLLoader loader = getLoaderFromGDLString("MATCH (a)-[e]->(b) " +
"WHERE a.val."+operator+"(Days(80))", false);
Expand Down

0 comments on commit 3dae776

Please sign in to comment.