Search in sources :

Example 6 with ClassFieldReader

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

the class ClassFieldAccessorTest method testBasic.

@Test
public void testBasic() throws Exception {
    final Object[] objArray = new Object[1];
    final TestBean obj = new TestBean();
    obj.setBlah(false);
    obj.setSomething("no");
    obj.setObjArray(objArray);
    final ClassFieldReader ext = store.getReader(TestBean.class, "blah");
    assertEquals(false, ((Boolean) ext.getValue(null, obj)).booleanValue());
    final ClassFieldReader ext2 = store.getReader(TestBean.class, "fooBar");
    assertEquals("fooBar", ext2.getValue(null, obj));
    final ClassFieldReader ext3 = store.getReader(TestBean.class, "objArray");
    assertEquals(objArray, ext3.getValue(null, obj));
}
Also used : TestBean(org.drools.core.util.asm.TestBean) ClassFieldReader(org.drools.mvel.accessors.ClassFieldReader) Test(org.junit.Test)

Example 7 with ClassFieldReader

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

the class ClassFieldAccessorTest method testLong.

@Test
public void testLong() throws Exception {
    final ClassFieldReader ext = store.getReader(TestBean.class, "longField");
    final TestBean bean = new TestBean();
    assertEquals(424242, ((Number) ext.getValue(null, bean)).longValue());
}
Also used : ClassFieldReader(org.drools.mvel.accessors.ClassFieldReader) TestBean(org.drools.core.util.asm.TestBean) Test(org.junit.Test)

Example 8 with ClassFieldReader

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

the class MVELTest method testNewConstructor.

@Test
public void testNewConstructor() {
    final String str = "" + "package org.drools.mvel.compiler.test \n" + "import " + Person.class.getCanonicalName() + "\n" + "import " + Address.class.getCanonicalName() + "\n" + "global java.util.List list \n" + "rule \"show\" \n" + "when  \n" + "    $m : Person( address == new Address('s1')) \n" + "then \n" + "    list.add('r1'); \n" + "end \n";
    KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, str);
    KieSession ksession = kbase.newKieSession();
    final List list = new ArrayList();
    ksession.setGlobal("list", list);
    final Person p = new Person("yoda");
    p.setAddress(new Address("s1"));
    ksession.insert(p);
    ksession.fireAllRules();
    assertEquals("r1", list.get(0));
    // Check it was built with MVELReturnValueExpression constraint
    final List<ObjectTypeNode> nodes = ((InternalKnowledgeBase) kbase).getRete().getObjectTypeNodes();
    ObjectTypeNode node = null;
    for (final ObjectTypeNode n : nodes) {
        if (((ClassObjectType) n.getObjectType()).getClassType() == Person.class) {
            node = n;
            break;
        }
    }
    final AlphaNode alphanode = (AlphaNode) node.getObjectSinkPropagator().getSinks()[0];
    final AlphaNodeFieldConstraint constraint = alphanode.getConstraint();
    if (constraint instanceof MVELConstraint) {
        assertTrue(((MVELConstraint) constraint).getFieldExtractor() instanceof ClassFieldReader);
    }
}
Also used : Address(org.drools.mvel.compiler.Address) ArrayList(java.util.ArrayList) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) AlphaNode(org.drools.core.reteoo.AlphaNode) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) MVELConstraint(org.drools.mvel.MVELConstraint) MVELObjectClassFieldReader(org.drools.mvel.extractors.MVELObjectClassFieldReader) ClassFieldReader(org.drools.mvel.accessors.ClassFieldReader) KieBase(org.kie.api.KieBase) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Person(org.drools.mvel.compiler.Person) Test(org.junit.Test)

Example 9 with ClassFieldReader

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

the class ClassFieldAccessorTest method testInherited.

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

Example 10 with ClassFieldReader

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

the class ClassFieldAccessorTest method testInterface.

@Test
public void testInterface() throws Exception {
    final TestInterface obj = new TestInterfaceImpl();
    final ClassFieldReader ext = store.getReader(TestInterface.class, "something");
    assertEquals("foo", (String) ext.getValue(null, obj));
}
Also used : TestInterfaceImpl(org.drools.core.util.asm.TestInterfaceImpl) ClassFieldReader(org.drools.mvel.accessors.ClassFieldReader) TestInterface(org.drools.core.util.asm.TestInterface) 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