use of org.kie.api.runtime.rule.Match 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.api.runtime.rule.Match in project drools by kiegroup.
the class RuleCoverageListenerTest method testCoverage.
@Test
public void testCoverage() throws Exception {
// configuring mock event
AfterMatchFiredEvent amfe = mock(AfterMatchFiredEvent.class);
Match match = mock(Match.class);
Rule rule = mock(Rule.class);
when(amfe.getMatch()).thenReturn(match);
when(match.getRule()).thenReturn(rule);
when(rule.getName()).thenReturn("rule1").thenReturn("rule2").thenReturn("rule3");
HashSet<String> rules = new HashSet<String>();
rules.add("rule1");
rules.add("rule2");
rules.add("rule3");
RuleCoverageListener ls = new RuleCoverageListener(rules);
Assert.assertEquals(3, ls.rules.size());
Assert.assertEquals(0, ls.getPercentCovered());
ls.afterMatchFired(amfe);
Assert.assertEquals(2, ls.rules.size());
assertTrue(ls.rules.contains("rule2"));
assertTrue(ls.rules.contains("rule3"));
assertFalse(ls.rules.contains("rule1"));
Assert.assertEquals(33, ls.getPercentCovered());
ls.afterMatchFired(amfe);
Assert.assertEquals(1, ls.rules.size());
assertFalse(ls.rules.contains("rule2"));
assertFalse(ls.rules.contains("rule1"));
assertTrue(ls.rules.contains("rule3"));
Assert.assertEquals(66, ls.getPercentCovered());
ls.afterMatchFired(amfe);
Assert.assertEquals(0, ls.rules.size());
assertFalse(ls.rules.contains("rule2"));
assertFalse(ls.rules.contains("rule1"));
assertFalse(ls.rules.contains("rule3"));
Assert.assertEquals(100, ls.getPercentCovered());
}
use of org.kie.api.runtime.rule.Match 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.api.runtime.rule.Match 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.api.runtime.rule.Match 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