Skip to content

Commit

Permalink
implemented parsing without query flattening
Browse files Browse the repository at this point in the history
  • Loading branch information
lc0197 committed Jun 15, 2020
1 parent 43fde72 commit baeeadb
Show file tree
Hide file tree
Showing 26 changed files with 724 additions and 85 deletions.
1 change: 0 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
<version>0.3.5-SNAPSHOT</version>



<name>GDL - Graph Definition Language</name>
<description>ANTLR Grammar for the definition of extended property graphs.</description>
<url>http://github.com/s1ck/gdl.git</url>
Expand Down
13 changes: 11 additions & 2 deletions src/main/antlr4/org/s1ck/gdl/GDL.g4
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@ timeSelector
;

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

complexTimePointArgument
Expand All @@ -215,6 +215,7 @@ intervalFunc
| immediatelyPrecedesOperator
| immediatelySucceedsOperator
| equalsOperator
| longerThanOperator
;
overlapsIntervallOperator
: 'overlaps(' interval ')'
Expand Down Expand Up @@ -254,6 +255,14 @@ equalsOperator
: 'equals(' interval ')'
;

longerThanOperator
: 'longerThan(' timeConstant ')'
;

timeConstant
: IntegerLiteral ('days'|'hours'|'minutes'|'seconds'|'millis')
;

stampFunc
: beforePointOperator
| afterPointOperator
Expand Down
72 changes: 71 additions & 1 deletion src/main/java/org/s1ck/gdl/GDLLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.s1ck.gdl.model.*;
import org.s1ck.gdl.model.comparables.ElementSelector;
import org.s1ck.gdl.model.comparables.time.*;
import org.s1ck.gdl.model.comparables.time.util.TimeConstant;
import org.s1ck.gdl.model.predicates.booleans.And;
import org.s1ck.gdl.model.predicates.expressions.Comparison;
import org.s1ck.gdl.model.predicates.Predicate;
Expand Down Expand Up @@ -91,6 +92,9 @@ class GDLLoader extends GDLBaseListener {
private static final String ANONYMOUS_VERTEX_VARIABLE = "__v%d";
private static final String ANONYMOUS_EDGE_VARIABLE = "__e%d";

// should predicates be reformulated to simple comparisons?
private boolean processPredicates = true;


/**
* Initializes a new GDL Loader.
Expand All @@ -104,6 +108,20 @@ class GDLLoader extends GDLBaseListener {
true, true, true);
}

/**
* Initializes a new GDL Loader.
*
* @param defaultGraphLabel graph label to be used if no label is given in the GDL script
* @param defaultVertexLabel vertex label to be used if no label is given in the GDL script
* @param defaultEdgeLabel edge label to be used if no label is given in the GDL script
* @param processPredicates true iff predicates should be reformulated to simple (atomic) comparisons
*/
GDLLoader(String defaultGraphLabel, String defaultVertexLabel, String defaultEdgeLabel,
boolean processPredicates) {
this(defaultGraphLabel, defaultVertexLabel, defaultEdgeLabel);
this.processPredicates = processPredicates;
}

/**
* Initializes a new GDL Loader.
*
Expand Down Expand Up @@ -142,6 +160,25 @@ class GDLLoader extends GDLBaseListener {
currentPredicates = new ArrayDeque<>();
}

/**
* Initializes a new GDL Loader.
*
* @param defaultGraphLabel graph label to be used if no label is given in the GDL script
* @param defaultVertexLabel vertex label to be used if no label is given in the GDL script
* @param defaultEdgeLabel edge label to be used if no label is given in the GDL script
* @param useDefaultGraphLabel enable default graph label
* @param useDefaultVertexLabel enable default vertex label
* @param useDefaultEdgeLabel enable default edge label
*/
GDLLoader(String defaultGraphLabel, String defaultVertexLabel, String defaultEdgeLabel,
boolean useDefaultGraphLabel, boolean useDefaultVertexLabel, boolean useDefaultEdgeLabel,
boolean processPredicates) {

this(defaultGraphLabel, defaultVertexLabel, defaultEdgeLabel, useDefaultGraphLabel, useDefaultVertexLabel,
useDefaultEdgeLabel);
this.processPredicates = processPredicates;
}

/**
* Returns the default graph label.
*
Expand Down Expand Up @@ -312,7 +349,9 @@ public void exitGraph(GDLParser.GraphContext ctx) {
*/
@Override
public void exitQuery(GDLParser.QueryContext ctx) {
postprocessPredicates();
if(processPredicates) {
postprocessPredicates();
}
for(Vertex v : vertices) {
addPredicates(Predicate.fromGraphElement(v, getDefaultVertexLabel()));
}
Expand Down Expand Up @@ -502,6 +541,9 @@ else if(intervalFunc.immediatelySucceedsOperator()!=null){
else if(intervalFunc.equalsOperator()!=null){
return createEqualsPredicates(from, to, intervalFunc.equalsOperator());
}
else if(intervalFunc.longerThanOperator()!=null){
return createLongerThanPredicates(from, to, intervalFunc.longerThanOperator());
}
return null;
}

Expand Down Expand Up @@ -631,6 +673,34 @@ private Predicate createEqualsPredicates(TimePoint from, TimePoint to, GDLParser
);
}

private Predicate createLongerThanPredicates(TimePoint from, TimePoint to, GDLParser.LongerThanOperatorContext ctx){
TimeConstant constant = buildTimeConstant(ctx.timeConstant());
Comparison c = new Comparison(new PlusTimePoint(from, constant), LT, to);
return new Comparison(
new PlusTimePoint(from, constant), LT, to
);
}

private TimeConstant buildTimeConstant(GDLParser.TimeConstantContext ctx){
int value = Integer.parseInt(ctx.IntegerLiteral().getText());
if(ctx.getText().contains("days")){
return new TimeConstant(value,0,0,0,0);
}
else if(ctx.getText().contains("hours")){
return new TimeConstant(0, value, 0, 0, 0);
}
else if(ctx.getText().contains("minutes")){
return new TimeConstant(0, 0, value, 0, 0);
}
else if(ctx.getText().contains("seconds")){
return new TimeConstant(0, 0, 0, value, 0);
}
else if(ctx.getText().contains("milliseconds") || ctx.getText().contains("millis")){
return new TimeConstant(0, 0, 0, 0, value);
}
return null;
}

/**
* Creates an array {@code {from, to}} representing an intervall.
* @param ctx context from which to derive {@code from} and {@code to}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,10 @@ public interface ComparableExpression extends Serializable{
*/
boolean containsSelectorType(TimeSelector.TimeField type);

/**
* Checks whether a global time selector is contained.
* @return true iff a global time selector is contained
*/
boolean isGlobal();

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ public boolean containsSelectorType(TimeSelector.TimeField type){
return false;
}

@Override
public boolean isGlobal(){
return false;
}

@Override
public String toString() {
return variable;
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/s1ck/gdl/model/comparables/Literal.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ public boolean containsSelectorType(TimeSelector.TimeField type){
return false;
}

@Override
public boolean isGlobal(){
return false;
}

@Override
public String toString() {
return value.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ public boolean containsSelectorType(TimeSelector.TimeField type){
return false;
}

@Override
public boolean isGlobal(){
return false;
}

@Override
public String toString() {
return variable + "." + propertyName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,14 @@ public boolean equals(Object o) {
return true;
}

@Override
public boolean isGlobal(){
for(TimePoint arg: args){
if(arg.isGlobal()){
return true;
}
}
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -213,5 +213,15 @@ public boolean equals(Object o) {
return true;
}

@Override
public boolean isGlobal(){
for(TimePoint arg: args){
if(arg.isGlobal()){
return true;
}
}
return false;
}


}
Loading

0 comments on commit baeeadb

Please sign in to comment.