Search in sources :

Example 31 with RightTupleImpl

use of org.drools.core.reteoo.RightTupleImpl 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 32 with RightTupleImpl

use of org.drools.core.reteoo.RightTupleImpl in project drools by kiegroup.

the class NotTest method getBlockerFactHandle.

private InternalFactHandle getBlockerFactHandle(KieSession ksession) {
    ObjectTypeNode otn = getObjectTypeNode(ksession.getKieBase(), Person.class);
    BetaNode notNode = (BetaNode) ((AlphaNode) otn.getSinks()[0]).getSinks()[0];
    StatefulKnowledgeSessionImpl ksessionImpl = (StatefulKnowledgeSessionImpl) ksession;
    NodeMemories nodeMemories = ksessionImpl.getNodeMemories();
    BetaMemory betaMemory = (BetaMemory) nodeMemories.getNodeMemory(notNode, ksessionImpl);
    TupleMemory rightTupleMemory = betaMemory.getRightTupleMemory();
    Tuple[] tuples = (Tuple[]) rightTupleMemory.toArray();
    for (int i = 0; i < tuples.length; i++) {
        RightTupleImpl tuple = (RightTupleImpl) tuples[i];
        if (tuple.getBlocked() != null) {
            return tuple.getFactHandle();
        }
    }
    fail("Cannot find blocker in BetaMemory");
    return null;
}
Also used : BetaNode(org.drools.core.reteoo.BetaNode) StatefulKnowledgeSessionImpl(org.drools.kiesession.session.StatefulKnowledgeSessionImpl) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) NodeMemories(org.drools.core.common.NodeMemories) BetaMemory(org.drools.core.reteoo.BetaMemory) RightTupleImpl(org.drools.core.reteoo.RightTupleImpl) AlphaNode(org.drools.core.reteoo.AlphaNode) TupleMemory(org.drools.core.reteoo.TupleMemory) Tuple(org.drools.core.spi.Tuple)

Aggregations

RightTupleImpl (org.drools.core.reteoo.RightTupleImpl)32 InternalFactHandle (org.drools.core.common.InternalFactHandle)30 RightTuple (org.drools.core.reteoo.RightTuple)21 Test (org.junit.Test)21 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)16 FieldIndex (org.drools.core.util.AbstractHashTable.FieldIndex)16 LeftTupleImpl (org.drools.core.reteoo.LeftTupleImpl)15 Tuple (org.drools.core.spi.Tuple)15 Cheese (org.drools.core.test.model.Cheese)15 Declaration (org.drools.core.rule.Declaration)14 TupleIndexHashTable (org.drools.core.util.index.TupleIndexHashTable)13 ClassObjectType (org.drools.core.base.ClassObjectType)12 InternalReadAccessor (org.drools.core.spi.InternalReadAccessor)12 Pattern (org.drools.core.rule.Pattern)11 TupleList (org.drools.core.util.index.TupleList)11 LeftTuple (org.drools.core.reteoo.LeftTuple)6 SingleIndex (org.drools.core.util.AbstractHashTable.SingleIndex)6 ArrayList (java.util.ArrayList)5 BetaMemory (org.drools.core.reteoo.BetaMemory)5 PhreakPropagationContext (org.drools.core.common.PhreakPropagationContext)4