Skip to content

Commit

Permalink
[s1ck#49] implemented min and max
Browse files Browse the repository at this point in the history
  • Loading branch information
lc0197 committed May 5, 2020
1 parent 95c244c commit c67362d
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 7 deletions.
11 changes: 11 additions & 0 deletions src/main/antlr4/org/s1ck/gdl/GDL.g4
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ complexIntervalArgument
timePoint
: timeLiteral
| timeSelector
| complexTimePoint
;

timeLiteral
Expand All @@ -190,6 +191,16 @@ timeSelector
| TimeProp
;

complexTimePoint
: 'MAX(' complexTimePointArgument ',' complexTimePointArgument (','complexTimePointArgument) ')'
| 'MIN(' complexTimePointArgument ',' complexTimePointArgument (','complexTimePointArgument) ')'
;

complexTimePointArgument
: timeLiteral
| timeSelector
;

intervalFunc
: overlapsIntervallOperator
| fromToOperator
Expand Down
49 changes: 42 additions & 7 deletions src/main/java/org/s1ck/gdl/GDLLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -806,15 +806,50 @@ private Predicate createAfterPredicates(TimePoint from, GDLParser.AfterPointOper
*/
private TimePoint buildTimePoint(GDLParser.TimePointContext ctx) {
if (ctx.timeLiteral() != null){
return new TimeLiteral(ctx.getText());
return buildTimeLiteral(ctx.timeLiteral());
}
else if (ctx.timeSelector()!=null){
GDLParser.TimeSelectorContext ts = ctx.timeSelector();
// checks whether ID is even there (is a vertex or edge) and returns its variable
String var = ts.Identifier()!=null ?
resolveIdentifier(ts.Identifier().getText()) : TimeSelector.GLOBAL_SELECTOR;
String field = ts.TimeProp().getText();
return new TimeSelector(var, field);
return buildTimeSelector(ctx.timeSelector());
}
else if (ctx.complexTimePoint()!=null){
return buildComplexTimePoint(ctx.complexTimePoint());
}
return null;
}

private TimeLiteral buildTimeLiteral(GDLParser.TimeLiteralContext ctx){
return new TimeLiteral(ctx.getText().trim());
}

private TimeSelector buildTimeSelector(GDLParser.TimeSelectorContext ctx){
// checks whether ID is even there (is a vertex or edge) and returns its variable
String var = ctx.Identifier()!=null ?
resolveIdentifier(ctx.Identifier().getText()) : TimeSelector.GLOBAL_SELECTOR;
String field = ctx.TimeProp().getText();
return new TimeSelector(var, field);
}

private TimePoint buildComplexTimePoint(GDLParser.ComplexTimePointContext ctx){

List<GDLParser.ComplexTimePointArgumentContext> argumentContexts =
ctx.complexTimePointArgument();
TimePoint[] args = new TimePoint[argumentContexts.size()];

for(int i=0; i<argumentContexts.size(); i++){
GDLParser.ComplexTimePointArgumentContext argumentContext = argumentContexts.get(i);
if(argumentContext.timeLiteral()!=null){
args[i]= buildTimeLiteral(argumentContext.timeLiteral());
}
else if(argumentContext.timeSelector()!=null){
args[i]= buildTimeSelector(argumentContext.timeSelector());
}
}
// available complex timepoints: min and max
if(ctx.getText().startsWith("MAX(")){
return new MaxTimePoint(args);
}
else if(ctx.getText().startsWith("MIN(")){
return new MinTimePoint(args);
}
return null;
}
Expand Down
82 changes: 82 additions & 0 deletions src/test/java/org/s1ck/gdl/GDLLoaderTemporalTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import org.s1ck.gdl.model.predicates.expressions.Comparison;
import org.s1ck.gdl.utils.Comparator;

import java.sql.Time;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.s1ck.gdl.model.comparables.time.TimeSelector.TimeField.*;
Expand Down Expand Up @@ -485,6 +487,86 @@ public void equalsTest(){
assertPredicateEquals(loader.getPredicates().get(), expected);
}

@Test
public void minMaxTest(){
TimeSelector aTxFrom = new TimeSelector("a", TX_FROM);
TimeSelector bTxFrom = new TimeSelector("b", TX_FROM);
TimeSelector eTxFrom = new TimeSelector("e", TX_FROM);
TimeSelector aValTo = new TimeSelector("a", VAL_TO);
TimeSelector bValTo = new TimeSelector("b", VAL_TO);
TimeSelector eValTo = new TimeSelector("e", VAL_TO);
TimeLiteral literal1 = new TimeLiteral("2020-05-05");
GDLLoader loader = getLoaderFromGDLString("MATCH (a)-[e]->(b) " +
"WHERE MIN(a.tx_from, b.tx_from, e.tx_from).before(2020-05-05)");
Predicate expected = new Or(
new Or(
new Comparison(aTxFrom, LT, literal1),
new Comparison(bTxFrom, LT, literal1)
),
new Comparison(eTxFrom, LT, literal1)
);
assertPredicateEquals(loader.getPredicates().get(), expected);

loader = getLoaderFromGDLString("MATCH (a)-[e]->(b) " +
"WHERE MAX(a.tx_from, b.tx_from, e.tx_from).before(2020-05-05)");
expected = new And(
new And(
new Comparison(aTxFrom, LT, literal1),
new Comparison(bTxFrom, LT, literal1)
),
new Comparison(eTxFrom, LT, literal1)
);
assertPredicateEquals(loader.getPredicates().get(), expected);
loader = getLoaderFromGDLString("MATCH (a)-[e]->(b) " +
"WHERE MAX(a.tx_from, b.tx_from, e.tx_from).after(" +
"MIN(a.val_to, b.val_to, e.val_to))");
expected = new Or(
//a, b
new Or(
// a
new Or(new Or(
new Comparison(aTxFrom, GT, aValTo),
new Comparison(aTxFrom, GT, bValTo)
),
new Comparison(aTxFrom, GT, eValTo)
),
// b
new Or(
new Or(
new Comparison(bTxFrom, GT, aValTo),
new Comparison(bTxFrom, GT, bValTo)
),
new Comparison(bTxFrom, GT, eValTo)
)
),
//e
new Or(
new Or(
new Comparison(eTxFrom, GT, aValTo),
new Comparison(eTxFrom, GT, bValTo)
),
new Comparison(eTxFrom, GT, eValTo)
)
);
assertPredicateEquals(loader.getPredicates().get(), expected);
loader = getLoaderFromGDLString("MATCH (a)-[e]->(b) " +
"WHERE MIN(a.tx_from, b.tx_from, e.tx_from).before(2020-05-05) AND" +
" a.tx.succeeds(b.tx)");
expected = new And(
new Or(
new Or(
new Comparison(aTxFrom, LT, literal1),
new Comparison(bTxFrom, LT, literal1)
),
new Comparison(eTxFrom, LT, literal1)
),
new Comparison(aTxFrom, GTE, new TimeSelector("b", TX_TO))
);
assertPredicateEquals(loader.getPredicates().get(), expected);
System.out.println(loader.getPredicates().get());

}

/**
* Does not fail iff {@code result==expected} or {@code result.switchSides()==expected}
* @param result predicate to compare
Expand Down

0 comments on commit c67362d

Please sign in to comment.