use of org.kie.internal.utils.KieHelper in project drools by kiegroup.
the class QueryTest method testQueryWithClassArg.
@Test
public void testQueryWithClassArg() {
// DROOLS-590
String drl = "global java.util.List list; " + "" + "declare Foo end " + "" + "query bar( Class $c ) " + " Class( this.getName() == $c.getName() ) " + "end " + "query bar2( Class $c ) " + " Class( this == $c ) " + "end " + "" + "rule Init when then insert( Foo.class ); end " + "" + "rule React1 " + "when " + " bar( Foo.class ; ) " + "then " + " list.add( 'aa' ); " + "end " + "rule React2 " + "when\n" + " bar2( Foo.class ; ) " + "then " + " list.add( 'bb' ); " + "end";
List list = new ArrayList();
KieHelper helper = new KieHelper();
helper.addContent(drl, ResourceType.DRL);
KieSession ks = helper.build().newKieSession();
ks.setGlobal("list", list);
ks.fireAllRules();
assertEquals(Arrays.asList("aa", "bb"), list);
}
use of org.kie.internal.utils.KieHelper 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";
KieSession ksession = new KieHelper().addContent(str, ResourceType.DRL).build().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.kie.internal.utils.KieHelper in project drools by kiegroup.
the class RuleExecutionTest method testAll.
@Test
public void testAll() throws Exception {
String str = "global java.util.List list\n" + "rule R1 when\n" + " $i : Integer()\n" + " $s : String( this == $i.toString() )\n" + "then\n" + " list.add( $i );\n" + "end\n" + "\n" + "rule R2 @All when\n" + " $i : Integer()\n" + " $s : Long( intValue == $i )\n" + "then\n" + " insert( \"\" + $i );\n" + " list.add( -$i );\n" + "end\n";
KieSession ksession = new KieHelper().addContent(str, ResourceType.DRL).build().newKieSession();
List<Integer> list = new ArrayList<Integer>();
ksession.setGlobal("list", list);
ksession.insert(1);
ksession.insert(2);
ksession.insert(3);
ksession.insert(1L);
ksession.insert(2L);
ksession.insert(3L);
ksession.fireAllRules();
assertEquals(asList(-1, -2, -3, 1, 2, 3), list);
}
use of org.kie.internal.utils.KieHelper in project drools by kiegroup.
the class RuleExecutionTest method testOnDeleteMatchConsequence.
@Test
public void testOnDeleteMatchConsequence() throws Exception {
String str = "import " + Person.class.getCanonicalName() + ";\n" + "rule R1 when\n" + " $p : Person( age > 30 )\n" + "then\n" + " $p.setStatus(\"in\");\n" + "then[$onDeleteMatch$]\n" + " $p.setStatus(\"out\");\n" + "end\n";
KieSession ksession = new KieHelper().addContent(str, ResourceType.DRL).build().newKieSession();
Person mario = new Person("Mario", 40);
FactHandle fact = ksession.insert(mario);
ksession.fireAllRules();
assertEquals("in", mario.getStatus());
ksession.delete(fact);
ksession.fireAllRules();
assertEquals("out", mario.getStatus());
}
use of org.kie.internal.utils.KieHelper in project drools by kiegroup.
the class OOPathOnGraphTest method testOOPathOnGraphWithReactiveContentModification.
@Test
public void testOOPathOnGraphWithReactiveContentModification() {
String drl = "import org.drools.compiler.oopath.graph.*;\n" + "import " + Library.class.getCanonicalName() + ";\n" + "import " + Book.class.getCanonicalName() + ";\n" + "import " + Person.class.getCanonicalName() + ";\n" + "global java.util.List list\n" + "\n" + "rule R when\n" + " Vertex( it instanceof Library, $a : /outVs/outVs/it#Person[ age > 25 ] )\n" + "then\n" + " list.add( $a.getName() );\n" + "end\n";
KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
List<String> list = new ArrayList<String>();
ksession.setGlobal("list", list);
Vertex<Library> library = getGraph();
ksession.insert(library);
ksession.fireAllRules();
assertEquals(1, list.size());
assertTrue(list.contains("Mario"));
list.clear();
Person raoul = (Person) library.getOutVs().get(0).getOutVs().get(0).getIt();
assertEquals("Raoul", raoul.getName());
raoul.setAge(raoul.getAge() + 1);
ksession.fireAllRules();
assertEquals(1, list.size());
assertTrue(list.contains("Raoul"));
}
Aggregations