Search in sources :

Example 1 with ClassFieldReader

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

the class FieldConstraintTest method testPrimitiveLiteralConstraint.

/**
 * <pre>
 *
 *                Cheese( price == 5 )
 *
 * </pre>
 */
@Test
public void testPrimitiveLiteralConstraint() {
    InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
    ;
    StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
    final ClassFieldReader extractor = store.getReader(Cheese.class, "price");
    AlphaNodeFieldConstraint constraint = ConstraintTestUtil.createCheesePriceEqualsConstraint(extractor, 5, 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", 10);
    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)

Example 2 with ClassFieldReader

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

the class FieldIndexEntryTest method testTwoEntries.

@Test
public void testTwoEntries() {
    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"));
    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);
    RightTuple h1RightTuple = new RightTupleImpl(h1, null);
    RightTuple h2RightTuple = new RightTupleImpl(h2, null);
    // test add
    index.add(h1RightTuple);
    index.add(h2RightTuple);
    assertEquals(h1, index.getFirst().getFactHandle());
    assertEquals(h2, ((RightTuple) index.getFirst().getNext()).getFactHandle());
    // test get
    assertEquals(h1, index.get(h1).getFactHandle());
    assertEquals(h2, index.get(h2).getFactHandle());
    // test removal for combinations
    // remove first
    index.remove(h2RightTuple);
    assertEquals(h1RightTuple.getFactHandle(), index.getFirst().getFactHandle());
    // remove second
    index.add(h2RightTuple);
    index.remove(h1RightTuple);
    assertEquals(h2RightTuple.getFactHandle(), index.getFirst().getFactHandle());
    // check index type does not change, as this fact is removed
    stilton1.setType("cheddar");
}
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 3 with ClassFieldReader

use of org.drools.mvel.accessors.ClassFieldReader 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, 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"));
    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 : 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 4 with ClassFieldReader

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

the class ClassFieldAccessorTest method testAbstract.

@Test
public void testAbstract() throws Exception {
    final ClassFieldReader ext = store.getReader(TestAbstract.class, "something");
    final TestAbstract obj = new TestAbstractImpl();
    assertEquals("foo", (String) ext.getValue(null, obj));
}
Also used : ClassFieldReader(org.drools.mvel.accessors.ClassFieldReader) TestAbstractImpl(org.drools.core.util.asm.TestAbstractImpl) TestAbstract(org.drools.core.util.asm.TestAbstract) Test(org.junit.Test)

Example 5 with ClassFieldReader

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

the class ClassFieldAccessorTest method testNonExistentField.

@Test
public void testNonExistentField() throws Exception {
    final Object[] objArray = new Object[1];
    final TestBean obj = new TestBean();
    obj.setBlah(false);
    obj.setSomething("no");
    obj.setObjArray(objArray);
    ClassFieldReader ext = store.getReader(TestBean.class, "xyz");
    assertNull(ext);
}
Also used : TestBean(org.drools.core.util.asm.TestBean) ClassFieldReader(org.drools.mvel.accessors.ClassFieldReader) 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