use of org.drools.mvel.compiler.Address in project drools by kiegroup.
the class NullSafeDereferencingTest method testNullSafeNullComparisonReverse.
@Test
public void testNullSafeNullComparisonReverse() {
// DROOLS-82
String str = "import org.drools.mvel.compiler.*;\n" + "rule R1 when\n" + " Person( \"Main Street\".equalsIgnoreCase(address!.street) )\n" + "then\n" + "end";
KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, str);
KieSession ksession = kbase.newKieSession();
ksession.insert(new Person("Mario", 38));
Person mark = new Person("Mark", 37);
mark.setAddress(new Address("Main Street"));
ksession.insert(mark);
Person edson = new Person("Edson", 34);
edson.setAddress(new Address(null));
ksession.insert(edson);
assertEquals(1, ksession.fireAllRules());
ksession.dispose();
}
use of org.drools.mvel.compiler.Address in project drools by kiegroup.
the class NullSafeDereferencingTest method testNullSafeNullComparisonReverseComplex.
@Test
public void testNullSafeNullComparisonReverseComplex() {
// DROOLS-82
String str = "import org.drools.mvel.compiler.*;\n" + "rule R1 when\n" + " Person( \"Main\".equalsIgnoreCase(address!.street!.substring(0, address!.street!.indexOf(' '))) )\n" + "then\n" + "end";
KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, str);
KieSession ksession = kbase.newKieSession();
ksession.insert(new Person("Mario", 38));
Person mark = new Person("Mark", 37);
mark.setAddress(new Address("Main Street"));
ksession.insert(mark);
Person edson = new Person("Edson", 34);
edson.setAddress(new Address(null));
ksession.insert(edson);
assertEquals(1, ksession.fireAllRules());
ksession.dispose();
}
use of org.drools.mvel.compiler.Address in project drools by kiegroup.
the class FieldAccessTest method testGenericsOption.
@Test
public void testGenericsOption() throws Exception {
// JBRULES-3579
final String str = "import org.drools.mvel.compiler.*;\n" + "rule R1 when\n" + " $c : Cheese( $type: type )\n" + " $p : Person( $name : name, addressOption.get.street == $type )\n" + "then\n" + "end\n";
KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, str);
KieSession ksession = kbase.newKieSession();
final Person p = new Person("x");
p.setAddress(new Address("x", "x", "x"));
ksession.insert(p);
ksession.insert(new Cheese("x"));
assertEquals(1, ksession.fireAllRules());
ksession.dispose();
}
use of org.drools.mvel.compiler.Address 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);
}
}
use of org.drools.mvel.compiler.Address in project drools by kiegroup.
the class MVELTest method testArrayAccessorWithGenerics.
@Test
public void testArrayAccessorWithGenerics() {
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( addresses[0] == new Address('s1'), addresses[0].street == new Address('s1').street ) \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.addAddress(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;
}
}
AlphaNode alphanode = (AlphaNode) node.getObjectSinkPropagator().getSinks()[0];
AlphaNodeFieldConstraint constraint = alphanode.getConstraint();
if (constraint instanceof MVELConstraint) {
assertTrue(((MVELConstraint) constraint).getFieldExtractor() instanceof MVELObjectClassFieldReader);
}
alphanode = (AlphaNode) alphanode.getObjectSinkPropagator().getSinks()[0];
constraint = alphanode.getConstraint();
if (constraint instanceof MVELConstraint) {
assertTrue(((MVELConstraint) constraint).getFieldExtractor() instanceof MVELObjectClassFieldReader);
}
}
Aggregations