Search in sources :

Example 21 with Cheese

use of org.drools.core.test.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 = (InternalKnowledgeBase) 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, WorkingMemory workingMemory, Object context) {
            int price1 = previousDeclarations[0].getIntValue((InternalWorkingMemory) workingMemory, tuple.getObject(previousDeclarations[0]));
            int price2 = localDeclarations[0].getIntValue((InternalWorkingMemory) workingMemory, 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 : ClassObjectType(org.drools.core.base.ClassObjectType) ObjectOutput(java.io.ObjectOutput) WorkingMemory(org.drools.core.WorkingMemory) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) Cheese(org.drools.core.test.model.Cheese) PredicateExpression(org.drools.core.spi.PredicateExpression) RightTupleImpl(org.drools.core.reteoo.RightTupleImpl) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) PredicateContextEntry(org.drools.core.rule.PredicateConstraint.PredicateContextEntry) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) LeftTupleImpl(org.drools.core.reteoo.LeftTupleImpl) ObjectInput(java.io.ObjectInput) InternalFactHandle(org.drools.core.common.InternalFactHandle) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Tuple(org.drools.core.spi.Tuple) Test(org.junit.Test)

Example 22 with Cheese

use of org.drools.core.test.model.Cheese in project drools by kiegroup.

the class FieldConstraintTest method testPrimitiveLiteralConstraint.

/**
 * <pre>
 *
 *                Cheese( price == 5 )
 *
 * </pre>
 */
@Test
public void testPrimitiveLiteralConstraint() {
    InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase();
    StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
    final ClassFieldReader extractor = store.getReader(Cheese.class, "price");
    final MvelConstraint constraint = new MvelConstraintTestUtil("price == 5", FieldFactory.getInstance().getFieldValue(5), extractor);
    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", 10);
    final InternalFactHandle stiltonHandle = (InternalFactHandle) ksession.insert(stilton);
    // check constraint
    assertFalse(constraint.isAllowed(stiltonHandle, ksession));
}
Also used : ClassFieldReader(org.drools.core.base.ClassFieldReader) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) Cheese(org.drools.core.test.model.Cheese) InternalFactHandle(org.drools.core.common.InternalFactHandle) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 23 with Cheese

use of org.drools.core.test.model.Cheese in project drools by kiegroup.

the class PatternTest method testDeclarationsObjectType.

@Test
public void testDeclarationsObjectType() throws Exception {
    final ObjectType type = new ClassObjectType(Cheese.class);
    final Pattern col = new Pattern(0, type, "foo");
    final Declaration dec = col.getDeclaration();
    final InternalReadAccessor ext = dec.getExtractor();
    assertEquals(Cheese.class, ext.getExtractToClass());
    final Cheese stilton = new Cheese("stilton", 42);
    assertEquals(stilton, dec.getValue(null, stilton));
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) ObjectType(org.drools.core.spi.ObjectType) FactTemplateObjectType(org.drools.core.facttemplates.FactTemplateObjectType) ClassObjectType(org.drools.core.base.ClassObjectType) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) Cheese(org.drools.core.test.model.Cheese) Test(org.junit.Test)

Example 24 with Cheese

use of org.drools.core.test.model.Cheese in project drools by kiegroup.

the class FieldIndexEntryTest method testThreeEntries.

@Test
public void testThreeEntries() {
    final ClassFieldReader extractor = store.getReader(Cheese.class, "type");
    final FieldIndex fieldIndex = new FieldIndex(extractor, null, MvelConstraint.INDEX_EVALUATOR);
    final SingleIndex singleIndex = new SingleIndex(new FieldIndex[] { fieldIndex }, 1);
    Tuple tuple = new RightTupleImpl(new DefaultFactHandle(1, new Cheese("stilton", 10)));
    final TupleList index = new AbstractHashTable.SingleIndexTupleList(singleIndex, tuple, "stilton".hashCode(), false);
    final Cheese stilton1 = new Cheese("stilton", 35);
    final InternalFactHandle h1 = new DefaultFactHandle(1, stilton1);
    final Cheese stilton2 = new Cheese("stilton", 59);
    final InternalFactHandle h2 = new DefaultFactHandle(2, stilton2);
    final Cheese stilton3 = new Cheese("stilton", 59);
    final InternalFactHandle h3 = new DefaultFactHandle(3, stilton3);
    RightTuple h1RightTuple = new RightTupleImpl(h1, null);
    RightTuple h2RightTuple = new RightTupleImpl(h2, null);
    RightTuple h3RightTuple = new RightTupleImpl(h3, null);
    // test add
    index.add(h1RightTuple);
    index.add(h2RightTuple);
    index.add(h3RightTuple);
    assertEquals(h1, index.getFirst().getFactHandle());
    assertEquals(h2, ((RightTuple) index.getFirst().getNext()).getFactHandle());
    assertEquals(h3, ((RightTuple) index.getFirst().getNext().getNext()).getFactHandle());
    // test get
    assertEquals(h1, index.get(h1).getFactHandle());
    assertEquals(h2, index.get(h2).getFactHandle());
    assertEquals(h3, index.get(h3).getFactHandle());
    // test removal for combinations
    // remove first
    index.remove(h3RightTuple);
    assertEquals(h1, index.getFirst().getFactHandle());
    assertEquals(h2, ((RightTuple) index.getFirst().getNext()).getFactHandle());
    index.add(h3RightTuple);
    index.remove(h2RightTuple);
    assertEquals(h1, index.getFirst().getFactHandle());
    assertEquals(h3, ((RightTuple) index.getFirst().getNext()).getFactHandle());
    index.add(h2RightTuple);
    index.remove(h1RightTuple);
    assertEquals(h3, index.getFirst().getFactHandle());
    assertEquals(h2, ((RightTuple) index.getFirst().getNext()).getFactHandle());
    index.remove(index.getFirst());
    // check index type does not change, as this fact is removed
    stilton2.setType("cheddar");
}
Also used : TupleList(org.drools.core.util.index.TupleList) SingleIndex(org.drools.core.util.AbstractHashTable.SingleIndex) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) FieldIndex(org.drools.core.util.AbstractHashTable.FieldIndex) ClassFieldReader(org.drools.core.base.ClassFieldReader) Cheese(org.drools.core.test.model.Cheese) RightTupleImpl(org.drools.core.reteoo.RightTupleImpl) InternalFactHandle(org.drools.core.common.InternalFactHandle) RightTuple(org.drools.core.reteoo.RightTuple) RightTuple(org.drools.core.reteoo.RightTuple) Tuple(org.drools.core.spi.Tuple) Test(org.junit.Test)

Example 25 with Cheese

use of org.drools.core.test.model.Cheese in project drools by kiegroup.

the class FieldIndexEntryTest method testSingleEntry.

@Test
public void testSingleEntry() {
    final ClassFieldReader extractor = store.getReader(Cheese.class, "type");
    final FieldIndex fieldIndex = new FieldIndex(extractor, null, MvelConstraint.INDEX_EVALUATOR);
    final SingleIndex singleIndex = new SingleIndex(new FieldIndex[] { fieldIndex }, 1);
    Tuple tuple = new RightTupleImpl(new DefaultFactHandle(1, new Cheese("stilton", 10)));
    final TupleList index = new AbstractHashTable.SingleIndexTupleList(singleIndex, tuple, "stilton".hashCode(), false);
    // Test initial construction
    assertNull(index.getFirst());
    assertEquals("stilton".hashCode(), index.hashCode());
    final Cheese stilton1 = new Cheese("stilton", 35);
    final InternalFactHandle h1 = new DefaultFactHandle(1, stilton1);
    // test add
    RightTuple h1RightTuple = new RightTupleImpl(h1, null);
    index.add(h1RightTuple);
    final Tuple entry1 = index.getFirst();
    assertSame(h1, entry1.getFactHandle());
    assertNull(entry1.getNext());
    assertSame(entry1, index.get(h1));
    // test get
    final Tuple entry2 = index.get(new RightTupleImpl(h1, null));
    assertSame(entry1, entry2);
    // test remove
    index.remove(h1RightTuple);
    assertNull(index.getFirst());
}
Also used : TupleList(org.drools.core.util.index.TupleList) SingleIndex(org.drools.core.util.AbstractHashTable.SingleIndex) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) FieldIndex(org.drools.core.util.AbstractHashTable.FieldIndex) ClassFieldReader(org.drools.core.base.ClassFieldReader) Cheese(org.drools.core.test.model.Cheese) RightTupleImpl(org.drools.core.reteoo.RightTupleImpl) InternalFactHandle(org.drools.core.common.InternalFactHandle) RightTuple(org.drools.core.reteoo.RightTuple) RightTuple(org.drools.core.reteoo.RightTuple) Tuple(org.drools.core.spi.Tuple) Test(org.junit.Test)

Aggregations

Cheese (org.drools.core.test.model.Cheese)35 Test (org.junit.Test)35 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)14 InternalFactHandle (org.drools.core.common.InternalFactHandle)14 InternalReadAccessor (org.drools.core.spi.InternalReadAccessor)14 ClassObjectType (org.drools.core.base.ClassObjectType)12 MvelConstraint (org.drools.core.rule.constraint.MvelConstraint)10 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)9 FieldIndex (org.drools.core.util.AbstractHashTable.FieldIndex)9 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)8 RightTupleImpl (org.drools.core.reteoo.RightTupleImpl)8 FactHandle (org.kie.api.runtime.rule.FactHandle)8 ClassFieldReader (org.drools.core.base.ClassFieldReader)7 RightTuple (org.drools.core.reteoo.RightTuple)7 MvelConstraintTestUtil (org.drools.core.rule.MvelConstraintTestUtil)7 Pattern (org.drools.core.rule.Pattern)7 Tuple (org.drools.core.spi.Tuple)7 LeftTupleImpl (org.drools.core.reteoo.LeftTupleImpl)6 Declaration (org.drools.core.rule.Declaration)6 TupleIndexHashTable (org.drools.core.util.index.TupleIndexHashTable)6