Skip to content

Commit

Permalink
[DROOLS-2634] fix for several cep use cases in executable model
Browse files Browse the repository at this point in the history
  • Loading branch information
mariofusco committed Jun 28, 2018
1 parent 0c825e9 commit 2fd3ac3
Show file tree
Hide file tree
Showing 17 changed files with 334 additions and 63 deletions.
Expand Up @@ -20,9 +20,11 @@
import org.drools.model.view.Expr1ViewItemImpl; import org.drools.model.view.Expr1ViewItemImpl;
import org.drools.model.view.Expr2ViewItemImpl; import org.drools.model.view.Expr2ViewItemImpl;
import org.drools.model.view.ExprViewItem; import org.drools.model.view.ExprViewItem;
import org.drools.model.view.FixedTemporalExprViewItem;
import org.drools.model.view.InputViewItem; import org.drools.model.view.InputViewItem;
import org.drools.model.view.InputViewItemImpl; import org.drools.model.view.InputViewItemImpl;
import org.drools.model.view.TemporalExprViewItem; import org.drools.model.view.TemporalExprViewItem;
import org.drools.model.view.VariableTemporalExprViewItem;
import org.drools.model.view.ViewItem; import org.drools.model.view.ViewItem;
import org.drools.model.view.ViewItemBuilder; import org.drools.model.view.ViewItemBuilder;


Expand Down Expand Up @@ -144,7 +146,11 @@ public ViewItem<T> get() {
// -- Temporal Constraints -- // -- Temporal Constraints --


public static <T> TemporalExprViewItem<T> expr( String exprId, Variable<T> var1, Variable<?> var2, TemporalPredicate temporalPredicate ) { public static <T> TemporalExprViewItem<T> expr( String exprId, Variable<T> var1, Variable<?> var2, TemporalPredicate temporalPredicate ) {
return new TemporalExprViewItem<>( exprId, var1, var2, temporalPredicate); return new VariableTemporalExprViewItem<>( exprId, var1, var2, temporalPredicate);
}

public static <T> TemporalExprViewItem<T> expr( String exprId, Variable<T> var1, long value, TemporalPredicate temporalPredicate ) {
return new FixedTemporalExprViewItem<>( exprId, var1, value, temporalPredicate);
} }


// -- Conditional Named Consequnce -- // -- Conditional Named Consequnce --
Expand Down
Expand Up @@ -20,14 +20,15 @@
import java.util.List; import java.util.List;


import org.drools.model.consequences.ConditionalConsequenceBuilder; import org.drools.model.consequences.ConditionalConsequenceBuilder;
import org.drools.model.constraints.FixedTemporalConstraint;
import org.drools.model.constraints.SingleConstraint1; import org.drools.model.constraints.SingleConstraint1;
import org.drools.model.constraints.SingleConstraint2; import org.drools.model.constraints.SingleConstraint2;
import org.drools.model.constraints.SingleConstraint3; import org.drools.model.constraints.SingleConstraint3;
import org.drools.model.constraints.SingleConstraint4; import org.drools.model.constraints.SingleConstraint4;
import org.drools.model.constraints.SingleConstraint5; import org.drools.model.constraints.SingleConstraint5;
import org.drools.model.constraints.SingleConstraint6; import org.drools.model.constraints.SingleConstraint6;
import org.drools.model.constraints.SingleConstraint7; import org.drools.model.constraints.SingleConstraint7;
import org.drools.model.constraints.TemporalConstraint; import org.drools.model.constraints.VariableTemporalConstraint;
import org.drools.model.functions.Function1; import org.drools.model.functions.Function1;
import org.drools.model.functions.Function2; import org.drools.model.functions.Function2;
import org.drools.model.functions.Predicate1; import org.drools.model.functions.Predicate1;
Expand Down Expand Up @@ -109,6 +110,7 @@ public interface PatternDef<T> extends ViewItem<T> {
<A, B, C, D, E, F> PatternDef<T> expr(String exprId, Variable<A> var2, Variable<B> var3, Variable<C> var4, Variable<D> var5, Variable<E> var6, Variable<F> var7, Predicate7<T, A, B, C, D, E, F> predicate, ReactOn reactOn ); <A, B, C, D, E, F> PatternDef<T> expr(String exprId, Variable<A> var2, Variable<B> var3, Variable<C> var4, Variable<D> var5, Variable<E> var6, Variable<F> var7, Predicate7<T, A, B, C, D, E, F> predicate, ReactOn reactOn );


<U> PatternDef<T> expr( String exprId, Variable<U> var1, TemporalPredicate temporalPredicate ); <U> PatternDef<T> expr( String exprId, Variable<U> var1, TemporalPredicate temporalPredicate );
PatternDef<T> expr( String exprId, long value, TemporalPredicate temporalPredicate );


<A> PatternDef<T> bind( Variable<A> boundVar, Function1<T, A> f ); <A> PatternDef<T> bind( Variable<A> boundVar, Function1<T, A> f );
<A> PatternDef<T> bind( Variable<A> boundVar, Function1<T, A> f, ReactOn reactOn ); <A> PatternDef<T> bind( Variable<A> boundVar, Function1<T, A> f, ReactOn reactOn );
Expand Down Expand Up @@ -276,6 +278,12 @@ public <U> PatternDef<T> expr( String exprId, Variable<U> var1, TemporalPredicat
return this; return this;
} }


@Override
public PatternDef<T> expr( String exprId, long value, TemporalPredicate temporalPredicate ) {
items.add( new FixedTemporalPatternExpr<>( exprId, value, temporalPredicate) );
return this;
}

@Override @Override
public <A> PatternDef<T> bind( Variable<A> boundVar, Function1<T, A> f ) { public <A> PatternDef<T> bind( Variable<A> boundVar, Function1<T, A> f ) {
items.add( new PatternBinding1<>(boundVar, f, null) ); items.add( new PatternBinding1<>(boundVar, f, null) );
Expand Down Expand Up @@ -584,7 +592,27 @@ public TemporalPatternExpr( String exprId, Variable<U> var2, TemporalPredicate t


@Override @Override
public Constraint asConstraint(PatternDefImpl patternDef) { public Constraint asConstraint(PatternDefImpl patternDef) {
return new TemporalConstraint(getExprId(), patternDef.getFirstVariable(), var2, temporalPredicate); return new VariableTemporalConstraint(getExprId(), patternDef.getFirstVariable(), var2, temporalPredicate);
}
}

public static class FixedTemporalPatternExpr<T> extends PatternExprImpl<T> {
private final long value;
private final TemporalPredicate temporalPredicate;

public FixedTemporalPatternExpr(long value, TemporalPredicate temporalPredicate) {
this(randomUUID().toString(), value, temporalPredicate);
}

public FixedTemporalPatternExpr( String exprId, long value, TemporalPredicate temporalPredicate) {
super( exprId, null );
this.value = value;
this.temporalPredicate = temporalPredicate;
}

@Override
public Constraint asConstraint(PatternDefImpl patternDef) {
return new FixedTemporalConstraint(getExprId(), patternDef.getFirstVariable(), value, temporalPredicate);
} }
} }


Expand Down
@@ -0,0 +1,57 @@
/*
* 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.constraints;

import org.drools.model.Variable;
import org.drools.model.functions.temporal.TemporalPredicate;
import org.drools.model.impl.ModelComponent;
import org.drools.model.view.FixedTemporalExprViewItem;

public class FixedTemporalConstraint<A> extends TemporalConstraint {

private final long value;

public FixedTemporalConstraint( String exprId, Variable<A> var1, long value, TemporalPredicate temporalPredicate ) {
super(exprId, var1, temporalPredicate);
this.value = value;
}

public FixedTemporalConstraint( FixedTemporalExprViewItem<A> expr ) {
this( expr.getExprId(), expr.getFirstVariable(), expr.getValue(), expr.getTemporalPredicate() );
}

@Override
public Variable[] getVariables() {
return new Variable[] { var1 };
}

public long getValue() {
return value;
}

@Override
public boolean isEqualTo( ModelComponent o ) {
if ( this == o ) return true;
if ( o == null || getClass() != o.getClass() ) return false;

FixedTemporalConstraint<?> that = ( FixedTemporalConstraint<?> ) o;

if ( !ModelComponent.areEqualInModel( var1, that.var1 ) ) return false;
if ( value != that.value ) return false;
return temporalPredicate.equals( that.temporalPredicate );
}
}
Expand Up @@ -19,31 +19,21 @@
import org.drools.model.Variable; import org.drools.model.Variable;
import org.drools.model.functions.PredicateN; import org.drools.model.functions.PredicateN;
import org.drools.model.functions.temporal.TemporalPredicate; import org.drools.model.functions.temporal.TemporalPredicate;
import org.drools.model.impl.ModelComponent; import org.drools.model.view.FixedTemporalExprViewItem;
import org.drools.model.view.TemporalExprViewItem; import org.drools.model.view.TemporalExprViewItem;
import org.drools.model.view.VariableTemporalExprViewItem;


public class TemporalConstraint<A> extends AbstractSingleConstraint { public abstract class TemporalConstraint<A> extends AbstractSingleConstraint {


private final Variable<A> var1; protected final Variable<A> var1;
private final Variable<?> var2; protected final TemporalPredicate temporalPredicate;
private final TemporalPredicate temporalPredicate;


public TemporalConstraint(String exprId, Variable<A> var1, Variable<?> var2, TemporalPredicate temporalPredicate ) { public TemporalConstraint( String exprId, Variable<A> var1, TemporalPredicate temporalPredicate ) {
super(exprId); super(exprId);
this.var1 = var1; this.var1 = var1;
this.var2 = var2;
this.temporalPredicate = temporalPredicate; this.temporalPredicate = temporalPredicate;
} }


public TemporalConstraint(TemporalExprViewItem<A> expr ) {
this( expr.getExprId(), expr.getFirstVariable(), expr.getSecondVariable(), expr.getTemporalPredicate() );
}

@Override
public Variable[] getVariables() {
return new Variable[] { var1, var2 };
}

@Override @Override
public PredicateN getPredicate() { public PredicateN getPredicate() {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
Expand All @@ -58,16 +48,13 @@ public TemporalPredicate getTemporalPredicate() {
return temporalPredicate; return temporalPredicate;
} }



public static <A> TemporalConstraint<A> createTemporalConstraint( TemporalExprViewItem<A> expr ) {
@Override if (expr instanceof FixedTemporalExprViewItem) {
public boolean isEqualTo( ModelComponent o ) { return new FixedTemporalConstraint<A>( (( FixedTemporalExprViewItem<A> ) expr) );
if ( this == o ) return true; }
if ( o == null || getClass() != o.getClass() ) return false; if (expr instanceof VariableTemporalExprViewItem ) {

return new VariableTemporalConstraint<A>( (( VariableTemporalExprViewItem<A> ) expr) );
TemporalConstraint<?> that = ( TemporalConstraint<?> ) o; }

throw new UnsupportedOperationException("Unknown expression " + expr);
if ( !ModelComponent.areEqualInModel( var1, that.var1 ) ) return false;
if ( !ModelComponent.areEqualInModel( var2, that.var2 ) ) return false;
return temporalPredicate.equals( that.temporalPredicate );
} }
} }
@@ -0,0 +1,53 @@
/*
* 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.constraints;

import org.drools.model.Variable;
import org.drools.model.functions.temporal.TemporalPredicate;
import org.drools.model.impl.ModelComponent;
import org.drools.model.view.VariableTemporalExprViewItem;

public class VariableTemporalConstraint<A> extends TemporalConstraint {

private final Variable<?> var2;

public VariableTemporalConstraint( String exprId, Variable<A> var1, Variable<?> var2, TemporalPredicate temporalPredicate ) {
super(exprId, var1, temporalPredicate);
this.var2 = var2;
}

public VariableTemporalConstraint( VariableTemporalExprViewItem<A> expr ) {
this( expr.getExprId(), expr.getFirstVariable(), expr.getSecondVariable(), expr.getTemporalPredicate() );
}

@Override
public Variable[] getVariables() {
return new Variable[] { var1, var2 };
}

@Override
public boolean isEqualTo( ModelComponent o ) {
if ( this == o ) return true;
if ( o == null || getClass() != o.getClass() ) return false;

VariableTemporalConstraint<?> that = ( VariableTemporalConstraint<?> ) o;

if ( !ModelComponent.areEqualInModel( var1, that.var1 ) ) return false;
if ( !ModelComponent.areEqualInModel( var2, that.var2 ) ) return false;
return temporalPredicate.equals( that.temporalPredicate );
}
}
Expand Up @@ -327,7 +327,7 @@ private static Condition viewItem2Condition( ViewItem viewItem, Condition condit


if (viewItem instanceof TemporalExprViewItem) { if (viewItem instanceof TemporalExprViewItem) {
TemporalExprViewItem expr = (TemporalExprViewItem)viewItem; TemporalExprViewItem expr = (TemporalExprViewItem)viewItem;
( (PatternImpl) condition ).addConstraint( new TemporalConstraint( expr ) ); ( (PatternImpl) condition ).addConstraint( TemporalConstraint.createTemporalConstraint( expr ) );
return condition; return condition;
} }


Expand Down
@@ -0,0 +1,44 @@
/*
* 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.view;

import org.drools.model.Variable;
import org.drools.model.functions.temporal.TemporalPredicate;

public class FixedTemporalExprViewItem<T> extends TemporalExprViewItem<T> {

private final long value;

public FixedTemporalExprViewItem( Variable<T> var1, long value, TemporalPredicate temporalPredicate ) {
super(var1, value, temporalPredicate);
this.value = value;
}

public FixedTemporalExprViewItem( String exprId, Variable<T> var1, long value, TemporalPredicate temporalPredicate ) {
super(exprId, var1, temporalPredicate);
this.value = value;
}

public long getValue() {
return value;
}

@Override
public Variable<?>[] getVariables() {
return new Variable[] { getFirstVariable() };
}
}
Expand Up @@ -20,28 +20,20 @@
import org.drools.model.Variable; import org.drools.model.Variable;
import org.drools.model.functions.temporal.TemporalPredicate; import org.drools.model.functions.temporal.TemporalPredicate;


public class TemporalExprViewItem<T> extends AbstractExprViewItem<T> { public abstract class TemporalExprViewItem<T> extends AbstractExprViewItem<T> {

private final Variable<?> var2;


private final TemporalPredicate temporalPredicate; private final TemporalPredicate temporalPredicate;


public TemporalExprViewItem( Variable<T> var1, Variable<?> var2, TemporalPredicate temporalPredicate ) { public TemporalExprViewItem( Variable<T> var1, Object arg, TemporalPredicate temporalPredicate ) {
super(getConstraintId(temporalPredicate, var1, var2), var1); super(getConstraintId(temporalPredicate, var1, arg), var1);
this.var2 = var2;
this.temporalPredicate = temporalPredicate; this.temporalPredicate = temporalPredicate;
} }


public TemporalExprViewItem( String exprId, Variable<T> var1, Variable<?> var2, TemporalPredicate temporalPredicate ) { public TemporalExprViewItem( String exprId, Variable<T> var1, TemporalPredicate temporalPredicate ) {
super(exprId, var1); super(exprId, var1);
this.var2 = var2;
this.temporalPredicate = temporalPredicate; this.temporalPredicate = temporalPredicate;
} }


public Variable<?> getSecondVariable() {
return var2;
}

public TemporalPredicate getTemporalPredicate() { public TemporalPredicate getTemporalPredicate() {
return temporalPredicate; return temporalPredicate;
} }
Expand All @@ -51,12 +43,7 @@ public Type getType() {
return Type.TEMPORAL; return Type.TEMPORAL;
} }


@Override private static String getConstraintId(TemporalPredicate temporalPredicate, Variable<?> var1, Object arg) {
public Variable<?>[] getVariables() { return temporalPredicate + "_" + var1 + "_" + arg;
return new Variable[] { getFirstVariable(), getSecondVariable() };
}

private static String getConstraintId(TemporalPredicate temporalPredicate, Variable<?> var1, Variable<?> var2) {
return temporalPredicate + "_" + var1 + "_" + var2;
} }
} }

0 comments on commit 2fd3ac3

Please sign in to comment.