Skip to content

Commit

Permalink
Temporal Predicates for OOPathcepTest (apache#1692)
Browse files Browse the repository at this point in the history
* Slightly better fix for missing class OOPathExpression

* Let's not merge the two expressions

* Restore substitution of last OOPath chunk

* Do not overwrite last declaration in OOPath

* All temporal operators

* Avoid collision while finding the binding ID in a OOPathChunk expr

* Fix Interval Min Value and testTemporalOperatorAfterWithOOPath

* Temporal operators use TemporalOperatorSpec

* Coincides Predicate

* Coincides predicate

* Coincides Predicate

* Overlaps predicate

* Metby predicate

* FinishedBy

* Meets

* Refactor to intoduce end timestamp in the evaluate method

* During Predicate

* Startedby predicate

* Overlapped by

* Includes Predicate

* StartsPredicate

* Finishes Predicate

* Coincides

* Fix After Predicate

* Fix DuringPredicate

* Disable tests

* Moved OperatorSpec to separated package
  • Loading branch information
lucamolteni authored and mariofusco committed Jan 12, 2018
1 parent db135c5 commit 95eb2b6
Show file tree
Hide file tree
Showing 22 changed files with 697 additions and 74 deletions.
Expand Up @@ -20,7 +20,18 @@
import org.drools.model.functions.accumulate.AccumulateFunction;
import org.drools.model.functions.temporal.AfterPredicate;
import org.drools.model.functions.temporal.BeforePredicate;
import org.drools.model.functions.temporal.CoincidesPredicate;
import org.drools.model.functions.temporal.DuringPredicate;
import org.drools.model.functions.temporal.FinishedbyPredicate;
import org.drools.model.functions.temporal.FinishesPredicate;
import org.drools.model.functions.temporal.IncludesPredicate;
import org.drools.model.functions.temporal.Interval;
import org.drools.model.functions.temporal.MeetsPredicate;
import org.drools.model.functions.temporal.MetbyPredicate;
import org.drools.model.functions.temporal.OverlappedbyPredicate;
import org.drools.model.functions.temporal.OverlapsPredicate;
import org.drools.model.functions.temporal.StartsPredicate;
import org.drools.model.functions.temporal.StartedbyPredicate;
import org.drools.model.functions.temporal.TemporalPredicate;
import org.drools.model.impl.AnnotationValueImpl;
import org.drools.model.impl.DataSourceDefinitionImpl;
Expand Down Expand Up @@ -373,6 +384,58 @@ public static TemporalPredicate before( long lowerBound, TimeUnit lowerUnit, lon
return new BeforePredicate( new Interval( lowerBound, lowerUnit, upperBound, upperUnit ) );
}

public static TemporalPredicate coincides( long dev, TimeUnit devUnit) {
return new CoincidesPredicate(dev, devUnit );
}

public static TemporalPredicate overlaps( long dev, TimeUnit devUnit) {
return new OverlapsPredicate(dev, devUnit );
}

public static TemporalPredicate metby( long dev, TimeUnit devUnit) {
return new MetbyPredicate(dev, devUnit );
}

public static TemporalPredicate finishedby() {
return new FinishedbyPredicate();
}

public static TemporalPredicate finishedby( long dev, TimeUnit devUnit) {
return new FinishedbyPredicate(dev, devUnit );
}

public static TemporalPredicate meets( long dev, TimeUnit devUnit) {
return new MeetsPredicate(dev, devUnit );
}

public static TemporalPredicate during() {
return new DuringPredicate();
}

public static TemporalPredicate startedby( long dev, TimeUnit devUnit) {
return new StartedbyPredicate(dev, devUnit);
}

public static TemporalPredicate overlappedby( long dev, TimeUnit devUnit) {
return new OverlappedbyPredicate(dev, devUnit);
}

public static TemporalPredicate includes() {
return new IncludesPredicate();
}

public static TemporalPredicate starts( long dev, TimeUnit devUnit) {
return new StartsPredicate(dev, devUnit);
}

public static TemporalPredicate finishes() {
return new FinishesPredicate();
}

public static TemporalPredicate finishes( long dev, TimeUnit devUnit) {
return new FinishesPredicate(dev, devUnit);
}

// -- Accumulate Functions --

public static AccumulateFunction accFunction(Class<?> accFunctionClass, Variable source) {
Expand Down
Expand Up @@ -19,7 +19,7 @@
public class AfterPredicate extends AbstractTemporalPredicate {

public AfterPredicate() {
this( new Interval() );
this( new Interval(1, Interval.MAX) );
}

public AfterPredicate( Interval interval ) {
Expand All @@ -32,8 +32,7 @@ public String toString() {
}

@Override
public boolean evaluate(long start1, long duration1, long start2, long duration2) {
long end2 = start2 + duration2;
public boolean evaluate(long start1, long duration1, long end1, long start2, long duration2, long end2) {
long diff = start1 - end2;
return diff >= interval.getLowerBound() && diff <= interval.getUpperBound();
}
Expand Down
Expand Up @@ -19,7 +19,7 @@
public class BeforePredicate extends AbstractTemporalPredicate {

public BeforePredicate() {
this( new Interval() );
this( new Interval(0, Interval.MAX) );
}

public BeforePredicate( Interval interval ) {
Expand All @@ -32,8 +32,7 @@ public String toString() {
}

@Override
public boolean evaluate(long start1, long duration1, long start2, long duration2) {
long end1 = start1 + duration1;
public boolean evaluate(long start1, long duration1, long end1, long start2, long duration2, long end2) {
long diff = start2 - end1;
return diff >= interval.getLowerBound() && diff <= interval.getUpperBound();
}
Expand Down
@@ -0,0 +1,46 @@
/*
* Copyright 2005 JBoss Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.drools.model.functions.temporal;

import java.util.concurrent.TimeUnit;

import static org.drools.model.functions.temporal.TimeUtil.unitToLong;

public class CoincidesPredicate extends AbstractTemporalPredicate {

private final long startDevLong;
private final long endDevLong;

public CoincidesPredicate(long dev, TimeUnit devUnit) {
super(new Interval(Interval.MIN, Interval.MAX));
this.startDevLong = unitToLong(dev, devUnit);
this.endDevLong = unitToLong(dev, devUnit);
}


@Override
public String toString() {
return "coincides" + interval;
}

@Override
public boolean evaluate(long start1, long duration1, long end1, long start2, long duration2, long end2) {
long distStart = Math.abs( start2 - start1 );
long distEnd = Math.abs( end2 - end1 );
return (distStart <= this.startDevLong && distEnd <= this.endDevLong);
}
}
@@ -0,0 +1,43 @@
/*
* Copyright 2005 JBoss Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.drools.model.functions.temporal;

public class DuringPredicate extends AbstractTemporalPredicate {

private final long startMinDev, startMaxDev;
private final long endMinDev, endMaxDev;

public DuringPredicate() {
super(new Interval(1, Interval.MAX));
this.startMinDev = 1;
this.startMaxDev = Long.MAX_VALUE;
this.endMinDev = 1;
this.endMaxDev = Long.MAX_VALUE;
}

@Override
public String toString() {
return "during" + interval;
}

@Override
public boolean evaluate(long start1, long duration1, long end1, long start2, long duration2, long end2) {
long distStart = start1 - start2;
long distEnd = end2 - end1;
return (distStart >= this.startMinDev && distStart <= this.startMaxDev && distEnd >= this.endMinDev && distEnd <= this.endMaxDev);
}
}
@@ -0,0 +1,48 @@
/*
* Copyright 2005 JBoss Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.drools.model.functions.temporal;

import java.util.concurrent.TimeUnit;

import static org.drools.model.functions.temporal.TimeUtil.unitToLong;

public class FinishedbyPredicate extends AbstractTemporalPredicate {

long endDev;

public FinishedbyPredicate() {
super(new Interval(Interval.MIN, 0));
this.endDev = 0;
}

public FinishedbyPredicate(long endDev, TimeUnit endDevTimeUnit) {
super(new Interval(Interval.MIN, 0));
this.endDev = unitToLong(endDev, endDevTimeUnit);
}

@Override
public String toString() {
return "finishedby" + interval;
}

@Override
public boolean evaluate(long start1, long duration1, long end1, long start2, long duration2, long end2) {
long distStart = start2 - start1;
long distEnd = Math.abs(end2 - end1);
return (distStart > 0 && distEnd <= this.endDev);
}
}
@@ -0,0 +1,49 @@
/*
* Copyright 2005 JBoss Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.drools.model.functions.temporal;

import java.util.concurrent.TimeUnit;

import static org.drools.model.functions.temporal.TimeUtil.unitToLong;

public class FinishesPredicate extends AbstractTemporalPredicate {

private final long endDev;

public FinishesPredicate() {
super(new Interval(0, Interval.MAX));
this.endDev = 0;
}

public FinishesPredicate(long endDev, TimeUnit endDevLongUnit) {
super(new Interval(0, Interval.MAX));
this.endDev = unitToLong(endDev, endDevLongUnit);
}

@Override
public String toString() {
return "finishes" + interval;
}

@Override
public boolean evaluate(long start1, long duration1, long end1, long start2, long duration2, long end2) {

long distStart = start1 - start2;
long distEnd = Math.abs( end2 - end1 );
return (distStart > 0 && distEnd <= this.endDev);
}
}
@@ -0,0 +1,43 @@
/*
* Copyright 2005 JBoss Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.drools.model.functions.temporal;

public class IncludesPredicate extends AbstractTemporalPredicate {

private final long startMinDev, startMaxDev;
private final long endMinDev, endMaxDev;

public IncludesPredicate() {
super(new Interval(Interval.MIN, 0));
this.startMinDev = 1;
this.startMaxDev = Long.MAX_VALUE;
this.endMinDev = 1;
this.endMaxDev = Long.MAX_VALUE;
}

@Override
public String toString() {
return "during " + interval;
}

@Override
public boolean evaluate(long start1, long duration1, long end1, long start2, long duration2, long end2) {
long distStart = start2 - start1;
long distEnd = end1 - end2;
return (distStart >= this.startMinDev && distStart <= this.startMaxDev && distEnd >= this.endMinDev && distEnd <= this.endMaxDev);
}
}
Expand Up @@ -27,10 +27,6 @@ public class Interval {
private final long lowerBound;
private final long upperBound;

public Interval() {
this( MIN, MAX );
}

public Interval( long lowerBound, long upperBound ) {
this.lowerBound = lowerBound;
this.upperBound = upperBound;
Expand All @@ -40,6 +36,7 @@ public Interval( long lowerBound, TimeUnit lowerUnit, long upperBound, TimeUnit
this( unitToLong( lowerBound, lowerUnit), unitToLong( upperBound, upperUnit) );
}


public long getLowerBound() {
return lowerBound;
}
Expand Down

0 comments on commit 95eb2b6

Please sign in to comment.