Search in sources :

Example 11 with ClassFieldReader

use of org.drools.mvel.accessors.ClassFieldReader in project drools by kiegroup.

the class ClassFieldAccessorTest method testMultipleInterfaces.

@Test
public void testMultipleInterfaces() throws Exception {
    final ConcreteChild obj = new ConcreteChild();
    final ClassFieldReader ext = store.getReader(InterfaceChild.class, "foo");
    assertEquals(42, ((Number) ext.getValue(null, obj)).intValue());
}
Also used : ClassFieldReader(org.drools.mvel.accessors.ClassFieldReader) ConcreteChild(org.drools.core.base.ConcreteChild) Test(org.junit.Test)

Example 12 with ClassFieldReader

use of org.drools.mvel.accessors.ClassFieldReader 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, new Declaration("id", extractor, null));
    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.IndexTupleList(singleIndex, new AbstractHashTable.SingleHashEntry("stilton".hashCode(), "stilton"));
    // Test initial construction
    assertNull(index.getFirst());
    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 : FieldIndex(org.drools.core.util.AbstractHashTable.FieldIndex) Cheese(org.drools.core.test.model.Cheese) RightTupleImpl(org.drools.core.reteoo.RightTupleImpl) RightTuple(org.drools.core.reteoo.RightTuple) TupleList(org.drools.core.util.index.TupleList) SingleIndex(org.drools.core.util.AbstractHashTable.SingleIndex) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) AbstractHashTable(org.drools.core.util.AbstractHashTable) ClassFieldReader(org.drools.mvel.accessors.ClassFieldReader) Declaration(org.drools.core.rule.Declaration) InternalFactHandle(org.drools.core.common.InternalFactHandle) RightTuple(org.drools.core.reteoo.RightTuple) Tuple(org.drools.core.spi.Tuple) Test(org.junit.Test)

Example 13 with ClassFieldReader

use of org.drools.mvel.accessors.ClassFieldReader 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

ClassFieldReader (org.drools.mvel.accessors.ClassFieldReader)13 Test (org.junit.Test)13 InternalFactHandle (org.drools.core.common.InternalFactHandle)5 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)3 RightTuple (org.drools.core.reteoo.RightTuple)3 RightTupleImpl (org.drools.core.reteoo.RightTupleImpl)3 Declaration (org.drools.core.rule.Declaration)3 AlphaNodeFieldConstraint (org.drools.core.spi.AlphaNodeFieldConstraint)3 Tuple (org.drools.core.spi.Tuple)3 Cheese (org.drools.core.test.model.Cheese)3 AbstractHashTable (org.drools.core.util.AbstractHashTable)3 FieldIndex (org.drools.core.util.AbstractHashTable.FieldIndex)3 SingleIndex (org.drools.core.util.AbstractHashTable.SingleIndex)3 TestBean (org.drools.core.util.asm.TestBean)3 TupleList (org.drools.core.util.index.TupleList)3 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)2 StatefulKnowledgeSessionImpl (org.drools.kiesession.session.StatefulKnowledgeSessionImpl)2 Cheese (org.drools.mvel.model.Cheese)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1