Skip to content

Commit

Permalink
[s1ck#49] further cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
lc0197 committed Jun 15, 2020
1 parent 707f682 commit bda6e80
Show file tree
Hide file tree
Showing 23 changed files with 58 additions and 1,336 deletions.
6 changes: 6 additions & 0 deletions src/main/java/org/s1ck/gdl/GDLLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,12 @@ public void enterStmpF(GDLParser.StmpFContext ctx) {
currentPredicates.add(temporalLoader.buildStampFunction(ctx));
}

/**
* Builds a asOf {@code Predicate}.
* E.g.a.asOf(now) = a.tx_from<=now AND a.tx_from>=now
*
* @param ctx asOf context
*/
@Override
public void enterAsOf(GDLParser.AsOfContext ctx) {
currentPredicates.add(temporalLoader.createAsOf(ctx));
Expand Down
54 changes: 32 additions & 22 deletions src/main/java/org/s1ck/gdl/GDLLoaderTemporal.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

import static org.s1ck.gdl.utils.Comparator.*;

/**
* Responsible for processing the temporal aspects of a query within {@link GDLLoader}
*/
public class GDLLoaderTemporal {

/**
Expand Down Expand Up @@ -129,7 +132,7 @@ private TimePoint buildComplexTimePoint(GDLParser.ComplexTimePointContext ctx) {
*/
Predicate buildIntervalFunction(GDLParser.IntvFContext ctx) {
int predicateSizeBefore = predicateStack.size();
TimePoint[] intv = buildIntervall(ctx.interval());
TimePoint[] intv = buildInterval(ctx.interval());
TimePoint from = intv[0];
TimePoint to = intv[1];
Predicate predicate = createIntervalPredicates(from, to, ctx.intervalFunc());
Expand All @@ -142,13 +145,13 @@ Predicate buildIntervalFunction(GDLParser.IntvFContext ctx) {
}

/**
* Creates a new predicate about an interval. There are different types of intervall predicates/
* Creates a new predicate about an interval. There are different types of interval predicates/
* functions: precedes, fromTo,...
*
* @param from represents the start time (from value) of the intervall
* @param to represents the end time (to value) of the intervall
* @param from represents the start time (from value) of the interval
* @param to represents the end time (to value) of the interval
* @param intervalFunc contains the context information needed to create the correct predicate
* @return new predicate (according to {@code intervalFunc}) about the intervall represented by
* @return new predicate (according to {@code intervalFunc}) about the interval represented by
* {@code from} and {@code to}.
*/
private Predicate createIntervalPredicates(TimePoint from, TimePoint to, GDLParser.IntervalFuncContext intervalFunc) {
Expand Down Expand Up @@ -191,7 +194,7 @@ private Predicate createIntervalPredicates(TimePoint from, TimePoint to, GDLPars
* @return overlaps predicate
*/
private Predicate createOverlapsPredicates(TimePoint from, TimePoint to, GDLParser.OverlapsIntervallOperatorContext ctx) {
TimePoint[] arg = buildIntervall(ctx.interval());
TimePoint[] arg = buildInterval(ctx.interval());
TimePoint arg_from = arg[0];
TimePoint arg_to = arg[1];
TimePoint mx = new MaxTimePoint(from, arg_from);
Expand Down Expand Up @@ -244,7 +247,7 @@ private Predicate createBetweenPredicates(TimePoint from, TimePoint to, GDLParse
* @return precedes predicate
*/
private Predicate createPrecedesPredicates(TimePoint to, GDLParser.PrecedesOperatorContext ctx) {
TimePoint[] arg = buildIntervall(ctx.interval());
TimePoint[] arg = buildInterval(ctx.interval());
TimePoint arg_from = arg[0];
return new Comparison(to, LTE, arg_from);
}
Expand All @@ -257,7 +260,7 @@ private Predicate createPrecedesPredicates(TimePoint to, GDLParser.PrecedesOpera
* @return immediatelyPrecedes predicate
*/
private Predicate createImmediatelyPrecedesPredicates(TimePoint to, GDLParser.ImmediatelyPrecedesOperatorContext ctx) {
TimePoint[] arg = buildIntervall(ctx.interval());
TimePoint[] arg = buildInterval(ctx.interval());
TimePoint arg_from = arg[0];
return new Comparison(to, EQ, arg_from);
}
Expand All @@ -272,7 +275,7 @@ private Predicate createImmediatelyPrecedesPredicates(TimePoint to, GDLParser.Im
* @return succeeds predicate
*/
private Predicate createSucceedsPredicates(TimePoint point, GDLParser.SucceedsOperatorContext ctx) {
TimePoint[] arg = buildIntervall(ctx.interval());
TimePoint[] arg = buildInterval(ctx.interval());
TimePoint arg_to = arg[1];
return new Comparison(point, GTE, arg_to);
}
Expand All @@ -288,7 +291,7 @@ private Predicate createSucceedsPredicates(TimePoint point, GDLParser.SucceedsOp
*/
private Predicate createImmediatelySucceedsPredicates(TimePoint from,
GDLParser.ImmediatelySucceedsOperatorContext ctx) {
TimePoint[] arg = buildIntervall(ctx.interval());
TimePoint[] arg = buildInterval(ctx.interval());
TimePoint arg_to = arg[1];
return new Comparison(from, EQ, arg_to);
}
Expand All @@ -303,7 +306,7 @@ private Predicate createImmediatelySucceedsPredicates(TimePoint from,
*/
private Predicate createContainsPredicates(TimePoint from, TimePoint to, GDLParser.ContainsOperatorContext ctx) {
if (ctx.interval() != null) {
TimePoint[] arg = buildIntervall(ctx.interval());
TimePoint[] arg = buildInterval(ctx.interval());
TimePoint arg_from = arg[0];
TimePoint arg_to = arg[1];
return new And(
Expand All @@ -329,7 +332,7 @@ private Predicate createContainsPredicates(TimePoint from, TimePoint to, GDLPars
* @return equals predicate
*/
private Predicate createEqualsPredicates(TimePoint from, TimePoint to, GDLParser.EqualsOperatorContext ctx) {
TimePoint[] arg = buildIntervall(ctx.interval());
TimePoint[] arg = buildInterval(ctx.interval());
TimePoint arg_from = arg[0];
TimePoint arg_to = arg[1];
return new And(
Expand All @@ -356,7 +359,7 @@ private Predicate createLongerThanPredicates(TimePoint from, TimePoint to, GDLPa
durationPredicate = new And(durationPredicate,
new Comparison(rhs, GT, constant));
} else if (ctx.interval() != null) {
TimePoint[] interval = buildIntervall(ctx.interval());
TimePoint[] interval = buildInterval(ctx.interval());
Duration lhs = new Duration(interval[0], interval[1]);
durationPredicate = new And(durationPredicate,
new Comparison(interval[0], LTE, interval[1]));
Expand All @@ -366,7 +369,6 @@ private Predicate createLongerThanPredicates(TimePoint from, TimePoint to, GDLPa
return durationPredicate;
}


/**
* Creates a predicate a.shorterThan(b) = (length(a) < length(b))
* Implicitly adds constraints that ensure that durations are always positive
Expand All @@ -385,7 +387,7 @@ private Predicate createShorterThanPredicates(TimePoint from, TimePoint to, GDLP
durationPredicate = new And(durationPredicate,
new Comparison(rhs, LT, constant));
} else if (ctx.interval() != null) {
TimePoint[] interval = buildIntervall(ctx.interval());
TimePoint[] interval = buildInterval(ctx.interval());
Duration lhs = new Duration(interval[0], interval[1]);
durationPredicate = new And(durationPredicate,
new Comparison(interval[0], LTE, interval[1]));
Expand Down Expand Up @@ -414,7 +416,7 @@ private Predicate createLengthAtLeastPredicates(TimePoint from, TimePoint to,
durationPredicate = new And(durationPredicate,
new Comparison(rhs, GTE, constant));
} else if (ctx.interval() != null) {
TimePoint[] interval = buildIntervall(ctx.interval());
TimePoint[] interval = buildInterval(ctx.interval());
Duration lhs = new Duration(interval[0], interval[1]);
durationPredicate = new And(durationPredicate,
new Comparison(interval[0], LTE, interval[1]));
Expand Down Expand Up @@ -443,7 +445,7 @@ private Predicate createLengthAtMostPredicates(TimePoint from, TimePoint to,
durationPredicate = new And(durationPredicate,
new Comparison(rhs, LTE, constant));
} else if (ctx.interval() != null) {
TimePoint[] interval = buildIntervall(ctx.interval());
TimePoint[] interval = buildInterval(ctx.interval());
Duration lhs = new Duration(interval[0], interval[1]);
durationPredicate = new And(durationPredicate,
new Comparison(interval[0], LTE, interval[1]));
Expand Down Expand Up @@ -483,14 +485,17 @@ private TimeConstant buildTimeConstant(GDLParser.TimeConstantContext ctx) {
* @param ctx context from which to derive {@code from} and {@code to}
* @return {@code {from, to}} representing an interval
*/
private TimePoint[] buildIntervall(GDLParser.IntervalContext ctx) {
private TimePoint[] buildInterval(GDLParser.IntervalContext ctx) {
if (ctx.intervalSelector() != null) {
GDLParser.IntervalSelectorContext selector = ctx.intervalSelector();
// throws exception, if variable invalid
return buildIntervalFromSelector(selector);
} else if (ctx.intervalFromStamps() != null) {
GDLParser.IntervalFromStampsContext fs = ctx.intervalFromStamps();
return buildIntervalFromStamps(fs);
TimePoint[] intv = buildIntervalFromStamps(fs);
// custom interval: make sure that from <= to
predicateStack.add(new Comparison(intv[0], LTE, intv[1]));
return intv;
} else if (ctx.complexInterval() != null) {
GDLParser.ComplexIntervalArgumentContext arg1 = ctx.complexInterval()
.complexIntervalArgument(0);
Expand All @@ -504,7 +509,7 @@ private TimePoint[] buildIntervall(GDLParser.IntervalContext ctx) {

/**
* Creates an interval as an array {@code {from, to}} from a selector context.
* I.e., a intervall like {@code a.val} would result in {@code {a.val_from, a.val_to}}.
* I.e., a interval like {@code a.val} would result in {@code {a.val_from, a.val_to}}.
* What is more, {@code val} results in {@code GLOBAL_SELECTOR.val_from, GLOBAL_SELECTOR.val_to}
*
* @param ctx context from which to derive the interval
Expand All @@ -521,7 +526,7 @@ private TimePoint[] buildIntervalFromSelector(GDLParser.IntervalSelectorContext

/**
* Creates an interval as an array {@code {from, to}} from a interval constant context.
* I.e., a intervall like {@code Interval(1970-01-01, 2020-01-01)} would result in
* I.e., a interval like {@code Interval(1970-01-01, 2020-01-01)} would result in
* {@code {1970-01-01, 2020-01-01}}.
*
* @param ctx context from which to derive the interval
Expand Down Expand Up @@ -566,7 +571,7 @@ private TimePoint[] buildIntervalFromComplex(GDLParser.ComplexIntervalArgumentCo
new MaxTimePoint(i1[0], i2[0]), LTE, new MinTimePoint(i1[1], i2[1])
);
predicateStack.addFirst(constraint);
// now build complex intervall from i1, i2
// now build complex interval from i1, i2
if (join) {
TimePoint start = new MinTimePoint(i1[0], i2[0]);
TimePoint end = new MaxTimePoint(i1[1], i2[1]);
Expand Down Expand Up @@ -637,6 +642,11 @@ private Predicate createAfterPredicates(TimePoint from, GDLParser.AfterPointOper
return new Comparison(from, Comparator.GT, x);
}

/**
* Create asOf predicate: a.tx_from<=point AND a.tx_to>= point
* @param ctx asOf context
* @return asOf predicate
*/
Predicate createAsOf(GDLParser.AsOfContext ctx) {
TimePoint tp = buildTimePoint(ctx.timePoint());
String identifier = loader.resolveIdentifier(ctx.Identifier().getText());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ public boolean equals(Object o) {
ElementSelector that = (ElementSelector) o;

return variable != null ? variable.equals(that.variable) : that.variable == null;

}

@Override
Expand Down
51 changes: 0 additions & 51 deletions src/main/java/org/s1ck/gdl/model/comparables/time/Duration.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.s1ck.gdl.model.comparables.time;

import org.s1ck.gdl.model.comparables.ComparableExpression;
import org.s1ck.gdl.model.predicates.Predicate;

import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -88,56 +87,6 @@ public Optional<Long> evaluate() {
}
}

/*@Override
public long getLowerBound() {
long toLowerBound = to.getLowerBound();
long fromUpperBound = from.getUpperBound();
if(toLowerBound == Long.MIN_VALUE || fromUpperBound==Long.MAX_VALUE){
return 0;
}
return toLowerBound - fromUpperBound;
}
@Override
public long getUpperBound() {
long toUpperBound = to.getUpperBound();
long fromLowerBound = from.getLowerBound();
if(toUpperBound==Long.MAX_VALUE || fromLowerBound == Long.MIN_VALUE){
return Long.MAX_VALUE;
}
return toUpperBound - fromLowerBound;
}*/

@Override
protected Predicate unfoldEQ(TimePoint arg) {
return null;
}

@Override
protected Predicate unfoldNEQ(TimePoint arg) {
return null;
}

@Override
protected Predicate unfoldGT(TimePoint arg) {
return null;
}

@Override
protected Predicate unfoldGTE(TimePoint arg) {
return null;
}

@Override
protected Predicate unfoldLT(TimePoint arg) {
return null;
}

@Override
protected Predicate unfoldLTE(TimePoint arg) {
return null;
}

@Override
public String toString(){
return "Duration("+from.toString()+", "+to.toString()+")";
Expand Down
Loading

0 comments on commit bda6e80

Please sign in to comment.