use of org.drools.mvel.compiler.Person in project drools by kiegroup.
the class SegmentMemoryPrototypeTest method testEnsureRiaSegmentCreationUsingPrototypes.
@Test
public void testEnsureRiaSegmentCreationUsingPrototypes() {
// DROOLS-1739
String str = "import " + Person.class.getCanonicalName() + "\n" + "import " + Address.class.getCanonicalName() + "\n" + "import java.util.List;\n" + "\n" + "rule rule1 when\n" + " $personE : List()\n" + " Person( ) from $personE\n" + "then end\n" + "\n" + "rule rule2 when\n" + " $personE : List()\n" + " Person( $addresses : addresses ) from $personE\n" + " $address : Address( ) from $addresses\n" + " not (Address( this != $address ) from $addresses)\n" + " String( )\n" + "then end";
KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, str);
KieSessionConfiguration conf = RuleBaseFactory.newKnowledgeSessionConfiguration();
conf.setOption(ForceEagerActivationOption.YES);
KieSession ksession = kbase.newKieSession(conf, null);
try {
ksession.insert(asList(new Person()));
ksession.insert("test");
assertEquals(1, ksession.fireAllRules());
} finally {
ksession.dispose();
try {
ksession = kbase.newKieSession(conf, null);
ksession.insert(asList(new Person()));
ksession.insert("test");
assertEquals(1, ksession.fireAllRules());
} finally {
ksession.dispose();
}
}
}
use of org.drools.mvel.compiler.Person in project drools by kiegroup.
the class RuleEventListenerTest method testRuleEventListener.
@Test
public void testRuleEventListener() {
String str = "import " + Person.class.getCanonicalName() + "\n" + "global java.util.List list\n" + "rule R when\n" + " $p: Person( $age: age < 20 )\n" + "then\n" + " modify($p) { setAge( $age + 1 ) };" + "end\n";
KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, str);
KieSession ksession = kbase.newKieSession();
List<String> list = new ArrayList<>();
((RuleEventManager) ksession).addEventListener(new RuleEventListener() {
@Override
public void onBeforeMatchFire(Match match) {
list.add("onBeforeMatchFire: " + match.getDeclarationValue("$age"));
}
@Override
public void onAfterMatchFire(Match match) {
list.add("onAfterMatchFire: " + match.getDeclarationValue("$age"));
}
@Override
public void onDeleteMatch(Match match) {
list.add("onDeleteMatch: " + match.getDeclarationValue("$age"));
}
@Override
public void onUpdateMatch(Match match) {
list.add("onUpdateMatch: " + match.getDeclarationValue("$age"));
}
});
ksession.insert(new Person("John Smith", 18));
ksession.fireAllRules();
List<String> expected = Arrays.asList("onBeforeMatchFire: 18", "onAfterMatchFire: 19", "onUpdateMatch: 19", "onBeforeMatchFire: 19", "onAfterMatchFire: 20", "onDeleteMatch: 20");
assertEquals(expected, list);
}
use of org.drools.mvel.compiler.Person in project drools by kiegroup.
the class UpdateTest method testJavaModifyBlock.
// this test requires mvel 1.2.19. Leaving it commented until mvel is released.
@Test
public void testJavaModifyBlock() throws Exception {
KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources(getClass(), kieBaseTestConfiguration, "test_JavaModifyBlock.drl");
KieSession ksession = kbase.newKieSession();
final List list = new ArrayList();
ksession.setGlobal("results", list);
final Person bob = new Person("Bob", 30);
bob.setStatus("hungry");
ksession.insert(bob);
ksession.insert(new Cheese());
ksession.insert(new Cheese());
ksession.insert(new OuterClass.InnerClass(1));
ksession.fireAllRules();
assertEquals(2, list.size());
assertEquals("full", bob.getStatus());
assertEquals(31, bob.getAge());
assertEquals(2, ((OuterClass.InnerClass) list.get(1)).getIntAttr());
}
use of org.drools.mvel.compiler.Person in project drools by kiegroup.
the class UpdateTest method doModifyTest.
private void doModifyTest(final String drlResource) throws Exception {
KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources(getClass(), kieBaseTestConfiguration, drlResource);
KieSession ksession = kbase.newKieSession();
final List list = new ArrayList();
ksession.setGlobal("results", list);
final Person bob = new Person("Bob");
bob.setStatus("hungry");
final Cheese c = new Cheese();
ksession.insert(bob);
ksession.insert(c);
ksession.fireAllRules();
assertEquals(10, c.getPrice());
assertEquals("fine", bob.getStatus());
}
use of org.drools.mvel.compiler.Person in project drools by kiegroup.
the class UpdateTest method testModifySimple.
@Test
public void testModifySimple() {
final String str = "package org.drools.mvel.compiler;\n" + "\n" + "rule \"test modify block\"\n" + "when\n" + " $p: Person( name == \"hungry\" )\n" + "then\n" + " modify( $p ) { setName(\"fine\") }\n" + "end\n" + "\n" + "rule \"Log\"\n" + "when\n" + " $o: Object()\n" + "then\n" + " System.out.println( $o );\n" + "end";
KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, str);
KieSession ksession = kbase.newKieSession();
final Person p = new Person("hungry");
ksession.insert(p);
ksession.fireAllRules();
ksession.dispose();
}
Aggregations