Search in sources :

Example 36 with LeftTupleImpl

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

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

the class LeftTupleIndexHashTableIteratorTest method testLastBucketInTheTable.

@Test
public void testLastBucketInTheTable() {
    // JBRULES-2574
    // setup the entry array with an element in the first bucket, one
    // in the middle and one in the last bucket
    Entry[] entries = new Entry[10];
    entries[0] = mock(TupleList.class);
    entries[5] = mock(TupleList.class);
    entries[9] = mock(TupleList.class);
    LeftTupleImpl[] tuples = new LeftTupleImpl[] { mock(LeftTupleImpl.class), mock(LeftTupleImpl.class), mock(LeftTupleImpl.class) };
    // set return values for methods
    when(entries[0].getNext()).thenReturn(null);
    when(((TupleList) entries[0]).getFirst()).thenReturn(tuples[0]);
    when(entries[5].getNext()).thenReturn(null);
    when(((TupleList) entries[5]).getFirst()).thenReturn(tuples[1]);
    when(entries[9].getNext()).thenReturn(null);
    when(((TupleList) entries[9]).getFirst()).thenReturn(tuples[2]);
    // create the mock table for the iterator
    AbstractHashTable table = mock(AbstractHashTable.class);
    when(table.getTable()).thenReturn(entries);
    // create the iterator
    FieldIndexHashTableFullIterator iterator = new FieldIndexHashTableFullIterator(table);
    // test it
    assertThat(iterator.next(), sameInstance((Object) tuples[0]));
    assertThat(iterator.next(), sameInstance((Object) tuples[1]));
    assertThat(iterator.next(), sameInstance((Object) tuples[2]));
    assertThat(iterator.next(), is((Object) null));
}
Also used : TupleList(org.drools.core.util.index.TupleList) Entry(org.drools.core.util.Entry) AbstractHashTable(org.drools.core.util.AbstractHashTable) FieldIndexHashTableFullIterator(org.drools.core.util.index.TupleIndexHashTable.FieldIndexHashTableFullIterator) LeftTupleImpl(org.drools.core.reteoo.LeftTupleImpl) Test(org.junit.Test)

Aggregations

LeftTupleImpl (org.drools.core.reteoo.LeftTupleImpl)37 Test (org.junit.Test)31 InternalFactHandle (org.drools.core.common.InternalFactHandle)30 Declaration (org.drools.core.rule.Declaration)17 ClassObjectType (org.drools.core.base.ClassObjectType)16 RightTupleImpl (org.drools.core.reteoo.RightTupleImpl)15 Pattern (org.drools.core.rule.Pattern)15 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)12 InternalReadAccessor (org.drools.core.spi.InternalReadAccessor)12 TupleIndexHashTable (org.drools.core.util.index.TupleIndexHashTable)12 HashMap (java.util.HashMap)10 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)10 Cheese (org.drools.core.test.model.Cheese)10 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)8 RuleBuildContext (org.drools.compiler.rule.builder.RuleBuildContext)8 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)8 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)8 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)8 BuildContext (org.drools.core.reteoo.builder.BuildContext)8 StatefulKnowledgeSessionImpl (org.drools.kiesession.session.StatefulKnowledgeSessionImpl)8