use of org.drools.mvel.compiler.Person in project drools by kiegroup.
the class Misc2Test method testLockOnActiveWithModifyNoEager.
@Test
public void testLockOnActiveWithModifyNoEager() {
// DROOLS-280
String drl = "" + "package org.drools.test; \n" + "import org.drools.mvel.compiler.Person; \n" + "" + "rule \"Rule1\" \n" + "salience 1 \n" + "lock-on-active true\n" + "when\n" + " $p: Person()\n" + "then\n" + " System.out.println( \"Rule1\" ); \n" + " modify( $p ) { setAge( 44 ); }\n" + "end;\n" + "\n" + "rule \"Rule2\"\n" + "lock-on-active true\n" + "when\n" + " $p: Person() \n" + " String() from $p.getName() \n" + "then\n" + " System.out.println( \"Rule2\" + $p ); " + " modify ( $p ) { setName( \"john\" ); } \n" + "end";
KieBase kb = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, drl);
KieSession ks = kb.newKieSession();
ks.addEventListener(new DebugAgendaEventListener());
ks.fireAllRules();
Person p = new Person("mark", 76);
ks.insert(p);
ks.fireAllRules();
assertEquals(44, p.getAge());
assertEquals("john", p.getName());
}
use of org.drools.mvel.compiler.Person 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.Person 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);
}
}
use of org.drools.mvel.compiler.Person in project drools by kiegroup.
the class MVELTest method testLocalVariableMVELConsequence.
@Test
public void testLocalVariableMVELConsequence() {
KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources(getClass(), kieBaseTestConfiguration, "test_LocalVariableMVELConsequence.drl");
final KieSession ksession = kbase.newKieSession();
final List list = new ArrayList();
ksession.setGlobal("results", list);
ksession.insert(new Person("bob", "stilton"));
ksession.insert(new Person("mark", "brie"));
try {
ksession.fireAllRules();
assertEquals("should have fired twice", 2, list.size());
} catch (final Exception e) {
e.printStackTrace();
fail("Should not raise any exception");
}
}
use of org.drools.mvel.compiler.Person in project drools by kiegroup.
the class MVELTest method testMapAccessorWithStaticFieldAccess.
@Test
public void testMapAccessorWithStaticFieldAccess() {
final String str = "" + "package org.drools.mvel.compiler.test \n" + "import " + Person.class.getCanonicalName() + "\n" + "import " + Address.class.getCanonicalName() + "\n" + "import " + TestEnum.class.getCanonicalName() + "\n" + "global java.util.List list \n" + "rule \"show\" \n" + "when \n" + " $m : Person( namedAddresses[TestEnum.ONE] == new Address('s1'), namedAddresses[TestEnum.ONE].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.getNamedAddresses().put(TestEnum.ONE, 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) alphanode.getConstraint()).getFieldExtractor() instanceof MVELObjectClassFieldReader);
}
alphanode = (AlphaNode) alphanode.getObjectSinkPropagator().getSinks()[0];
constraint = alphanode.getConstraint();
if (constraint instanceof MVELConstraint) {
assertTrue(((MVELConstraint) alphanode.getConstraint()).getFieldExtractor() instanceof MVELObjectClassFieldReader);
}
}
Aggregations