use of org.kie.internal.event.rule.RuleEventManager 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.event.rule.RuleEventManager in project drools by kiegroup.
the class Misc2Test method testQueryAndRetract.
@Test
public void testQueryAndRetract() {
// DROOLS-7
String str = "global java.util.List list\n" + "\n" + "query q (String $s)\n" + " String( this == $s )\n" + "end" + "\n" + "rule R1 when\n" + " $x : String( this == \"x\" )\n" + " ?q( \"x\"; )\n" + "then\n" + " delete( \"x\" );\n" + "end\n";
KieSession ksession = new KieHelper().addContent(str, ResourceType.DRL).build().newKieSession();
List list = new ArrayList();
ksession.setGlobal("list", list);
((RuleEventManager) ksession).addEventListener(new RuleEventListener() {
@Override
public void onDeleteMatch(Match match) {
list.add("test");
}
});
ksession.insert("x");
ksession.fireAllRules();
assertEquals(1, list.size());
}
use of org.kie.internal.event.rule.RuleEventManager in project drools by kiegroup.
the class JTMSTest method testPosNegNonConflictingInsertions.
@Test(timeout = 10000)
public void testPosNegNonConflictingInsertions() {
String s = "package org.drools.core.beliefsystem.jtms;\n" + "\n" + "import java.util.List \n" + "import org.drools.core.common.AgendaItem;" + "global java.util.List list;\n" + "\n" + "rule \"go1\"\n" + "when\n" + " String( this == 'go1' )\n" + "then\n" + " insertLogical( 'neg', 'neg' );\n" + "end\n" + "\n" + "rule \"go2\"\n" + "when\n" + " String( this == 'go2' )\n" + "then\n" + " insertLogical( 'pos' );\n" + "end\n" + "\n" + "rule \"Positive\"\n" + "when\n" + " $n : String( this != 'go1' || == 'go2' ) \n" + "then\n" + " final String s = '+' + $n;" + " final List l = list;" + " l.add( s );\n" + "end\n" + "rule \"Negative\"\n" + "when\n" + " $n : String( _.neg, this != 'go1' || == 'go2' ) \n" + "then\n" + " final String s = '-' + $n; \n" + " final List l = list; \n" + " l.add( s ); \n" + "end\n";
KieSession kSession = getSessionFromString(s);
List list = new ArrayList();
kSession.setGlobal("list", list);
((RuleEventManager) kSession).addEventListener(new RuleEventListener() {
@Override
public void onDeleteMatch(Match match) {
String rule = match.getRule().getName();
if (rule.equals("Positive")) {
list.remove("+" + match.getDeclarationValue("$n"));
} else if (rule.equals("Negative")) {
list.remove("-" + match.getDeclarationValue("$n"));
}
}
});
FactHandle fhGo1 = kSession.insert("go1");
kSession.fireAllRules();
assertTrue(list.contains("-neg"));
// just go1
assertEquals(1, kSession.getEntryPoint("DEFAULT").getObjects().size());
assertEquals(1, getNegativeObjects(kSession).size());
FactHandle fhGo2 = kSession.insert("go2");
kSession.fireAllRules();
assertTrue(list.contains("-neg"));
assertTrue(list.contains("+pos"));
// go1, go2, pos
assertEquals(3, kSession.getEntryPoint("DEFAULT").getObjects().size());
assertEquals(1, getNegativeObjects(kSession).size());
kSession.retract(fhGo1);
kSession.fireAllRules();
assertFalse(list.contains("-neg"));
assertTrue(list.contains("+pos"));
// go2, pos
assertEquals(2, kSession.getEntryPoint("DEFAULT").getObjects().size());
assertEquals(0, getNegativeObjects(kSession).size());
kSession.retract(fhGo2);
kSession.fireAllRules();
assertFalse(list.contains("-neg"));
assertFalse(list.contains("+pos"));
assertEquals(0, kSession.getEntryPoint("DEFAULT").getObjects().size());
assertEquals(0, getNegativeObjects(kSession).size());
}
use of org.kie.internal.event.rule.RuleEventManager in project drools by kiegroup.
the class JTMSTest method testConflictToggleWithoutGoingEmpty.
@Test(timeout = 10000)
public void testConflictToggleWithoutGoingEmpty() {
String s = "package org.drools.core.beliefsystem.jtms;\n" + "\n" + "import java.util.List \n" + "import org.drools.core.common.AgendaItem;" + "global java.util.List list;\n" + "\n" + "rule \"go1\"\n" + "when\n" + " String( this == 'go1' )\n" + "then\n" + " insertLogical( 'xxx' );\n" + "end\n" + "rule \"go2\"\n" + "when\n" + " String( this == 'go2' )\n" + "then\n" + " insertLogical( 'xxx');\n" + "end\n" + "rule \"go3\"\n" + "when\n" + " String( this == 'go3' )\n" + "then\n" + " insertLogical( 'xxx');\n" + "end\n" + "\n" + "rule \"go4\"\n" + "when\n" + " String( this == 'go4' )\n" + "then\n" + " insertLogical( 'xxx', 'neg' );\n" + "end\n" + "\n" + "rule \"Positive\"\n" + "when\n" + " $n : String( this == 'xxx' ) \n" + "then\n" + " final String s = '+' + $n;" + " final List l = list;" + " l.add( s );\n" + "end\n" + "rule \"Negative\"\n" + "when\n" + " $n : String( _.neg, this == 'xxx' )\n" + "then\n" + " final String s = '-' + $n; \n" + " final List l = list; \n" + " l.add( s ); \n" + "end\n" + "";
KieSession kSession = getSessionFromString(s);
List list = new ArrayList();
kSession.setGlobal("list", list);
((RuleEventManager) kSession).addEventListener(new RuleEventListener() {
@Override
public void onDeleteMatch(Match match) {
String rule = match.getRule().getName();
if (rule.equals("Positive")) {
list.remove("+" + match.getDeclarationValue("$n"));
} else if (rule.equals("Negative")) {
list.remove("-" + match.getDeclarationValue("$n"));
}
}
});
FactHandle fhGo1 = kSession.insert("go1");
FactHandle fhGo2 = kSession.insert("go2");
FactHandle fhGo3 = kSession.insert("go3");
kSession.fireAllRules();
System.out.println(list);
assertTrue(list.contains("+xxx"));
FactHandle fhGo4 = kSession.insert("go4");
kSession.fireAllRules();
assertTrue(list.isEmpty());
kSession.delete(fhGo4);
kSession.fireAllRules();
assertTrue(list.contains("+xxx"));
}
use of org.kie.internal.event.rule.RuleEventManager in project drools by kiegroup.
the class JTMSTest method testRetractHandleWhenOnlyNeg.
@Test(timeout = 10000)
@Ignore("Currently cannot support updates")
public void testRetractHandleWhenOnlyNeg() {
String s = "package org.drools.core.beliefsystem.jtms;\n" + "\n" + "import java.util.List \n" + "import org.drools.core.common.AgendaItem;" + "global java.util.List list;\n" + "\n" + "rule \"go1_1\"\n" + "when\n" + " String( this == 'go1' )\n" + "then\n" + " insertLogical( new String( 'neg' ), 'neg' );\n" + "end\n" + "rule \"go1_2\"\n" + "when\n" + " String( this == 'go1' )\n" + "then\n" + " insertLogical( new String( 'neg' ), 'neg' );\n" + "end\n" + "rule \"go1_3\"\n" + "when\n" + " String( this == 'go1' )\n" + "then\n" + " insertLogical( new String( 'neg' ), 'neg' );\n" + "end\n" + "\n" + "rule \"Negative\"\n" + "when\n" + " $n : String( _.neg, this != 'go1' || == 'go2' ) \n" + "then\n" + " final String s = '-' + $n; \n" + " final List l = list; \n" + " l.add( s ); \n" + "end\n";
KieSession kSession = getSessionFromString(s);
List list = new ArrayList();
kSession.setGlobal("list", list);
((RuleEventManager) kSession).addEventListener(new RuleEventListener() {
@Override
public void onDeleteMatch(Match match) {
String rule = match.getRule().getName();
if (rule.equals("Negative")) {
list.remove("-" + match.getDeclarationValue("$n"));
}
}
});
FactHandle fhGo1 = kSession.insert("go1");
kSession.fireAllRules();
assertTrue(list.contains("-neg"));
// just go1
assertEquals(1, kSession.getEntryPoint("DEFAULT").getObjects().size());
assertEquals(1, getNegativeObjects(kSession).size());
NamedEntryPoint ep = (NamedEntryPoint) ((StatefulKnowledgeSessionImpl) kSession).getEntryPoint("DEFAULT");
ObjectHashMap equalityMap = ep.getTruthMaintenanceSystem().getEqualityKeyMap();
// go1, neg are two different strings.
assertEquals(2, equalityMap.size());
org.drools.core.util.Iterator it = equalityMap.iterator();
EqualityKey key = (EqualityKey) ((ObjectEntry) it.next()).getValue();
while (!key.getFactHandle().getObject().equals("neg")) {
key = (EqualityKey) ((ObjectEntry) it.next()).getValue();
}
assertEquals(3, key.getBeliefSet().size());
ep.getTruthMaintenanceSystem().delete(key.getLogicalFactHandle());
assertEquals(0, key.getBeliefSet().size());
// just go1
assertEquals(1, kSession.getEntryPoint("DEFAULT").getObjects().size());
assertEquals(0, getNegativeObjects(kSession).size());
assertEquals(0, key.getBeliefSet().size());
assertEquals(1, ep.getTruthMaintenanceSystem().getEqualityKeyMap().size());
}
Aggregations