use of org.kie.api.runtime.rule.Match in project drools by kiegroup.
the class CepEspTest method testMultipleSlidingWindows.
@Test(timeout = 10000)
public void testMultipleSlidingWindows() {
final String drl = "declare A\n" + " @role( event )\n" + " id : int\n" + "end\n" + "declare B\n" + " @role( event )\n" + " id : int\n" + "end\n" + "rule launch\n" + "when\n" + "then\n" + " insert( new A( 1 ) );\n" + " insert( new A( 2 ) );\n" + " insert( new B( 1 ) );\n" + " insert( new A( 3 ) );\n" + " insert( new B( 2 ) );\n" + "end\n" + "rule \"ab\" \n" + "when\n" + " A( $a : id ) over window:length( 1 )\n" + " B( $b : id ) over window:length( 1 )\n" + "then\n" + " System.out.println(\"AB: ( \"+$a+\", \"+$b+\" )\");\n" + "end\n" + "rule \"ba\" salience 10\n" + "when\n" + " B( $b : id ) over window:length( 1 )\n" + " A( $a : id ) over window:length( 1 )\n" + "then\n" + " System.out.println(\"BA: ( \"+$b+\", \"+$a+\" )\");\n" + "end";
final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("cep-esp-test", kieBaseTestConfiguration, drl);
final KieSession ksession = kbase.newKieSession();
try {
final AgendaEventListener ael = mock(AgendaEventListener.class);
ksession.addEventListener(ael);
ksession.fireAllRules();
final ArgumentCaptor<AfterMatchFiredEvent> captor = ArgumentCaptor.forClass(AfterMatchFiredEvent.class);
verify(ael, times(3)).afterMatchFired(captor.capture());
final List<AfterMatchFiredEvent> values = captor.getAllValues();
// first rule
Match act = values.get(0).getMatch();
assertThat(act.getRule().getName(), is("launch"));
// second rule
act = values.get(1).getMatch();
assertThat(act.getRule().getName(), is("ba"));
assertThat(((Number) act.getDeclarationValue("$a")).intValue(), is(3));
assertThat(((Number) act.getDeclarationValue("$b")).intValue(), is(2));
// third rule
act = values.get(2).getMatch();
assertThat(act.getRule().getName(), is("ab"));
assertThat(((Number) act.getDeclarationValue("$a")).intValue(), is(3));
assertThat(((Number) act.getDeclarationValue("$b")).intValue(), is(2));
} finally {
ksession.dispose();
}
}
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 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 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");
TruthMaintenanceSystem tms = TruthMaintenanceSystemFactory.get().getOrCreateTruthMaintenanceSystem(ep);
ObjectHashMap equalityMap = tms.getEqualityKeyMap();
// go1, neg are two different strings.
assertEquals(2, equalityMap.size());
org.drools.core.util.Iterator it = equalityMap.iterator();
TruthMaintenanceSystemEqualityKey key = (TruthMaintenanceSystemEqualityKey) ((ObjectEntry) it.next()).getValue();
while (!key.getFactHandle().getObject().equals("neg")) {
key = (TruthMaintenanceSystemEqualityKey) ((ObjectEntry) it.next()).getValue();
}
assertEquals(3, key.getBeliefSet().size());
tms.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, tms.getEqualityKeyMap().size());
}
use of org.kie.api.runtime.rule.Match in project drools by kiegroup.
the class DeclarativeAgendaTest method getStatefulKnowledgeSession.
public KieSession getStatefulKnowledgeSession() {
String str = "";
str += "package org.domain.test \n";
str += "import " + Match.class.getName() + "\n";
str += "global java.util.List list \n";
str += "dialect 'mvel' \n";
str += "rule rule1 @department(sales) \n";
str += "when \n";
str += " $s : String( this == 'go1' ) \n";
str += "then \n";
str += " list.add( kcontext.rule.name + ':' + $s ); \n";
str += "end \n";
str += "rule blockerAllSalesRules @activationListener('direct') \n";
str += "when \n";
str += " $s : String( this == 'go2' ) \n";
str += " $i : Match( department == 'sales' ) \n";
str += "then \n";
str += " list.add( $i.rule.name + ':' + $s ); \n";
str += " kcontext.blockMatch( $i ); \n";
str += "end \n";
final KieModule kieModule = KieUtil.getKieModuleFromDrls("test", kieBaseTestConfiguration, str);
final KieBase kbase = KieBaseUtil.newKieBaseFromKieModuleWithAdditionalOptions(kieModule, kieBaseTestConfiguration, DeclarativeAgendaOption.ENABLED);
KieSession ksession = kbase.newKieSession();
return ksession;
}
Aggregations