Search in sources :

Example 11 with Cheese

use of org.drools.mvel.model.Cheese in project drools by kiegroup.

the class LambdaConstraintTestUtil method createCheeseCharObjectTypeEqualsConstraint.

public static LambdaConstraint createCheeseCharObjectTypeEqualsConstraint(final char rightValue, int indexId) {
    // Typical LambdaConstraint used in drools-test-coverage. indexId is required when the test uses hashKey
    Pattern pattern = new Pattern(0, new ClassObjectType(Cheese.class));
    Predicate1<Cheese> predicate = new Predicate1.Impl<Cheese>(_this -> EvaluationUtil.areNullSafeEquals(_this.getCharObjectType(), rightValue));
    AlphaIndexImpl<Cheese, Character> index = new AlphaIndexImpl<Cheese, Character>(Character.class, org.drools.model.Index.ConstraintType.EQUAL, indexId, _this -> _this.getCharObjectType(), (char) rightValue);
    return LambdaConstraintTestUtil.createLambdaConstraint1(Cheese.class, pattern, predicate, index);
}
Also used : Pattern(org.drools.core.rule.Pattern) DeclarationImpl(org.drools.model.impl.DeclarationImpl) AlphaIndexImpl(org.drools.model.index.AlphaIndexImpl) AlphaIndexImpl(org.drools.model.index.AlphaIndexImpl) ClassObjectType(org.drools.core.base.ClassObjectType) Cheese(org.drools.mvel.model.Cheese)

Example 12 with Cheese

use of org.drools.mvel.model.Cheese in project drools by kiegroup.

the class LambdaConstraintTestUtil method createCheesePriceEqualsConstraint.

public static LambdaConstraint createCheesePriceEqualsConstraint(final int rightValue, int indexId) {
    // Typical LambdaConstraint used in drools-test-coverage. (price == xxx)
    Pattern pattern = new Pattern(0, new ClassObjectType(Cheese.class));
    Predicate1<Cheese> predicate = new Predicate1.Impl<Cheese>(_this -> EvaluationUtil.areNullSafeEquals(_this.getPrice(), rightValue));
    AlphaIndexImpl<Cheese, Integer> index = new AlphaIndexImpl<Cheese, Integer>(Integer.class, org.drools.model.Index.ConstraintType.EQUAL, indexId, _this -> _this.getPrice(), rightValue);
    return createLambdaConstraint1(Cheese.class, pattern, predicate, index);
}
Also used : Pattern(org.drools.core.rule.Pattern) DeclarationImpl(org.drools.model.impl.DeclarationImpl) AlphaIndexImpl(org.drools.model.index.AlphaIndexImpl) AlphaIndexImpl(org.drools.model.index.AlphaIndexImpl) ClassObjectType(org.drools.core.base.ClassObjectType) Cheese(org.drools.mvel.model.Cheese)

Example 13 with Cheese

use of org.drools.mvel.model.Cheese in project drools by kiegroup.

the class LambdaConstraintTestUtil method createCheeseCharTypeEqualsConstraint.

public static LambdaConstraint createCheeseCharTypeEqualsConstraint(final char rightValue, int indexId) {
    // Typical LambdaConstraint used in drools-test-coverage. indexId is required when the test uses hashKey
    Pattern pattern = new Pattern(0, new ClassObjectType(Cheese.class));
    Predicate1<Cheese> predicate = new Predicate1.Impl<Cheese>(_this -> EvaluationUtil.areNullSafeEquals(_this.getCharType(), rightValue));
    AlphaIndexImpl<Cheese, Character> index = new AlphaIndexImpl<Cheese, Character>(Character.class, org.drools.model.Index.ConstraintType.EQUAL, indexId, _this -> _this.getCharType(), (char) rightValue);
    return LambdaConstraintTestUtil.createLambdaConstraint1(Cheese.class, pattern, predicate, index);
}
Also used : Pattern(org.drools.core.rule.Pattern) DeclarationImpl(org.drools.model.impl.DeclarationImpl) AlphaIndexImpl(org.drools.model.index.AlphaIndexImpl) AlphaIndexImpl(org.drools.model.index.AlphaIndexImpl) ClassObjectType(org.drools.core.base.ClassObjectType) Cheese(org.drools.mvel.model.Cheese)

Example 14 with Cheese

use of org.drools.mvel.model.Cheese in project drools by kiegroup.

the class FieldConstraintTest method testPredicateConstraint.

/**
 * <pre>
 *
 *                (Cheese (price ?price1 )
 *                (Cheese (price ?price2&amp;:(= ?price2 (* 2 ?price1) )
 *
 * </pre>
 */
@Test
public void testPredicateConstraint() {
    InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
    ;
    StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
    final InternalReadAccessor priceExtractor = store.getReader(Cheese.class, "price");
    Pattern pattern = new Pattern(0, new ClassObjectType(Cheese.class));
    // Bind the extractor to a decleration
    // Declarations know the pattern they derive their value form
    final Declaration price1Declaration = new Declaration("price1", priceExtractor, pattern);
    pattern = new Pattern(1, new ClassObjectType(Cheese.class));
    // Bind the extractor to a decleration
    // Declarations know the pattern they derive their value form
    final Declaration price2Declaration = new Declaration("price2", priceExtractor, pattern);
    final PredicateExpression evaluator = new PredicateExpression() {

        private static final long serialVersionUID = 510l;

        public boolean evaluate(InternalFactHandle handle, Tuple tuple, Declaration[] previousDeclarations, Declaration[] localDeclarations, ReteEvaluator reteEvaluator, Object context) {
            int price1 = previousDeclarations[0].getIntValue(reteEvaluator, tuple.getObject(previousDeclarations[0]));
            int price2 = localDeclarations[0].getIntValue(reteEvaluator, handle.getObject());
            return (price2 == (price1 * 2));
        }

        public Object createContext() {
            return null;
        }

        public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        }

        public void writeExternal(ObjectOutput out) throws IOException {
        }
    };
    final PredicateConstraint constraint1 = new PredicateConstraint(evaluator, new Declaration[] { price1Declaration }, new Declaration[] { price2Declaration });
    final Cheese cheddar0 = new Cheese("cheddar", 5);
    final InternalFactHandle f0 = (InternalFactHandle) ksession.insert(cheddar0);
    LeftTupleImpl tuple = new LeftTupleImpl(f0, null, true);
    final Cheese cheddar1 = new Cheese("cheddar", 10);
    final InternalFactHandle f1 = (InternalFactHandle) ksession.insert(cheddar1);
    tuple = new LeftTupleImpl(tuple, new RightTupleImpl(f1, null), null, true);
    final PredicateContextEntry context = (PredicateContextEntry) constraint1.createContextEntry();
    context.updateFromTuple(ksession, tuple);
    assertTrue(constraint1.isAllowedCachedLeft(context, f1));
}
Also used : ReteEvaluator(org.drools.core.common.ReteEvaluator) Pattern(org.drools.core.rule.Pattern) ClassObjectType(org.drools.core.base.ClassObjectType) ObjectOutput(java.io.ObjectOutput) Cheese(org.drools.mvel.model.Cheese) PredicateExpression(org.drools.core.spi.PredicateExpression) RightTupleImpl(org.drools.core.reteoo.RightTupleImpl) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) PredicateConstraint(org.drools.core.rule.PredicateConstraint) PredicateContextEntry(org.drools.core.rule.PredicateConstraint.PredicateContextEntry) StatefulKnowledgeSessionImpl(org.drools.kiesession.session.StatefulKnowledgeSessionImpl) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) LeftTupleImpl(org.drools.core.reteoo.LeftTupleImpl) Declaration(org.drools.core.rule.Declaration) ObjectInput(java.io.ObjectInput) PredicateConstraint(org.drools.core.rule.PredicateConstraint) InternalFactHandle(org.drools.core.common.InternalFactHandle) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Tuple(org.drools.core.spi.Tuple) Test(org.junit.Test)

Example 15 with Cheese

use of org.drools.mvel.model.Cheese in project drools by kiegroup.

the class FieldConstraintTest method testLiteralConstraint.

/**
 * <pre>
 *
 *                ( Cheese (type &quot;cheddar&quot;) )
 *
 * </pre>
 *
 * This is currently the same as using a ReturnValueConstraint just that it
 * doesn't need any requiredDeclarations
 */
@Test
public void testLiteralConstraint() {
    InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
    ;
    StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
    final ClassFieldReader extractor = store.getReader(Cheese.class, "type");
    AlphaNodeFieldConstraint constraint = ConstraintTestUtil.createCheeseTypeEqualsConstraint(extractor, "cheddar", useLambdaConstraint);
    final Cheese cheddar = new Cheese("cheddar", 5);
    final InternalFactHandle cheddarHandle = (InternalFactHandle) ksession.insert(cheddar);
    // check constraint
    assertTrue(constraint.isAllowed(cheddarHandle, ksession));
    final Cheese stilton = new Cheese("stilton", 5);
    final InternalFactHandle stiltonHandle = (InternalFactHandle) ksession.insert(stilton);
    // check constraint
    assertFalse(constraint.isAllowed(stiltonHandle, ksession));
}
Also used : AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) ClassFieldReader(org.drools.mvel.accessors.ClassFieldReader) StatefulKnowledgeSessionImpl(org.drools.kiesession.session.StatefulKnowledgeSessionImpl) Cheese(org.drools.mvel.model.Cheese) InternalFactHandle(org.drools.core.common.InternalFactHandle) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Test(org.junit.Test)

Aggregations

Cheese (org.drools.mvel.model.Cheese)16 Test (org.junit.Test)8 ClassObjectType (org.drools.core.base.ClassObjectType)7 Pattern (org.drools.core.rule.Pattern)7 AlphaNodeFieldConstraint (org.drools.core.spi.AlphaNodeFieldConstraint)6 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)6 StatefulKnowledgeSessionImpl (org.drools.kiesession.session.StatefulKnowledgeSessionImpl)6 AlphaNode (org.drools.core.reteoo.AlphaNode)5 DeclarationImpl (org.drools.model.impl.DeclarationImpl)5 AlphaIndexImpl (org.drools.model.index.AlphaIndexImpl)5 InternalFactHandle (org.drools.core.common.InternalFactHandle)4 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)3 PropagationContextFactory (org.drools.core.common.PropagationContextFactory)3 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)3 MockObjectSink (org.drools.core.reteoo.MockObjectSink)3 BuildContext (org.drools.core.reteoo.builder.BuildContext)3 PropagationContext (org.drools.core.spi.PropagationContext)3 MockObjectSource (org.drools.mvel.model.MockObjectSource)3 HashKey (org.drools.core.reteoo.CompositeObjectSinkAdapter.HashKey)2 Declaration (org.drools.core.rule.Declaration)2