use of org.kie.api.runtime.rule.Match in project drools by kiegroup.
the class DeclarativeAgendaTest method testIterativeUpdate.
@Test(timeout = 10000)
public void testIterativeUpdate() {
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 rule0 \n";
str += "when \n";
str += " $s : String( this == 'rule0' ) \n";
str += "then \n";
str += " list.add( kcontext.rule.name ); \n";
str += "end \n";
str += "rule rule1 \n";
str += "when \n";
str += " $s : String( this == 'rule1' ) \n";
str += "then \n";
str += " list.add( kcontext.rule.name ); \n";
str += "end \n";
str += "rule rule2 \n";
str += "when \n";
str += " $s : String( this == 'rule2' ) \n";
str += "then \n";
str += " list.add( kcontext.rule.name ); \n";
str += "end \n";
str += "rule blockerAllSalesRules1 @activationListener('direct') \n";
str += "when \n";
str += " $l : List( ) \n";
str += " $i : Match( rule.name == $l[0] ) \n";
str += "then \n";
// str += " System.out.println( kcontext.rule.name + ':' + $i ); \n";
str += " list.add( 'block:' + $i.rule.name ); \n";
str += " kcontext.blockMatch( $i ); \n";
str += "end \n";
KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kconf.setOption(DeclarativeAgendaOption.ENABLED);
KieBase kbase = loadKnowledgeBaseFromString(kconf, str);
KieSession ksession = createKnowledgeSession(kbase);
List list = new ArrayList();
ksession.setGlobal("list", list);
FactHandle rule0 = ksession.insert("rule0");
FactHandle rule1 = ksession.insert("rule1");
FactHandle rule2 = ksession.insert("rule2");
ksession.fireAllRules();
assertEquals(3, list.size());
assertTrue(list.contains("rule0"));
assertTrue(list.contains("rule1"));
assertTrue(list.contains("rule2"));
list.clear();
ArrayList l = new ArrayList();
l.add("rule0");
FactHandle lh = ksession.insert(l);
ksession.update(rule0, "rule0");
ksession.update(rule1, "rule1");
ksession.update(rule2, "rule2");
ksession.fireAllRules();
assertEquals(4, list.size());
assertTrue(list.contains("block:rule0"));
assertTrue(list.contains("rule1"));
assertTrue(list.contains("rule2"));
assertFalse(list.contains("rule0"));
list.clear();
ksession.update(rule0, "rule0");
ksession.update(rule1, "rule1");
ksession.update(rule2, "rule2");
ksession.fireAllRules();
assertEquals(3, list.size());
assertTrue(list.contains("block:rule0"));
assertTrue(list.contains("rule1"));
assertTrue(list.contains("rule2"));
assertFalse(list.contains("rule0"));
list.clear();
l.set(0, "rule1");
ksession.update(lh, l);
ksession.fireAllRules();
System.out.println(list);
assertEquals(2, list.size());
assertTrue(list.contains("rule0"));
assertTrue(list.contains("block:rule1"));
list.clear();
ksession.update(rule0, "rule0");
ksession.update(rule1, "rule1");
ksession.update(rule2, "rule2");
ksession.fireAllRules();
assertEquals(3, list.size());
assertTrue(list.contains("block:rule1"));
assertTrue(list.contains("rule0"));
assertTrue(list.contains("rule2"));
assertFalse(list.contains("rule1"));
list.clear();
l.set(0, "rule2");
ksession.update(lh, l);
ksession.fireAllRules();
assertEquals(2, list.size());
assertTrue(list.contains("rule1"));
assertTrue(list.contains("block:rule2"));
}
use of org.kie.api.runtime.rule.Match in project drools by kiegroup.
the class DeclarativeAgendaTest method testCancelMultipleActivations.
@Test(timeout = 10000)
public void testCancelMultipleActivations() {
String str = "package org.domain.test\n" + "import " + Match.class.getName() + "\n" + "global java.util.List list\n" + "rule sales1 @department('sales')\n" + "when\n" + " String( this == 'fireRules' )\n" + "then\n" + " list.add(\"sales1\");\n" + "end\n" + "\n" + "rule sales2 @department('sales') \n" + "when\n" + " String( this == 'fireRules' )\n" + "then\n" + " list.add(\"sales2\");\n" + "end\n" + "\n" + "rule salesCancel @activationListener('direct')\n" + "when\n" + " $i : Match( department == 'sales' )\n" + "then\n" + " kcontext.cancelMatch($i);\n" + "end";
KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kconf.setOption(DeclarativeAgendaOption.ENABLED);
KieBase kbase = loadKnowledgeBaseFromString(kconf, str);
KieSession ksession = createKnowledgeSession(kbase);
List list = new ArrayList();
ksession.setGlobal("list", list);
ksession.insert("fireRules");
ksession.fireAllRules();
System.out.println(list);
assertEquals(0, list.size());
ksession.dispose();
}
use of org.kie.api.runtime.rule.Match in project drools by kiegroup.
the class DeclarativeAgendaTest method testCancelActivationOnInsertAndUpdate.
@Test(timeout = 10000)
public void testCancelActivationOnInsertAndUpdate() {
String str = "package org.domain.test\n" + "import " + Match.class.getName() + "\n" + "global java.util.List list\n" + "rule sales1 @department('sales') @category('special')\n" + "salience 10\n" + "when\n" + " String( this == 'fireRules' )\n" + "then\n" + " list.add(\"sales1\");\n" + "end\n" + "\n" + "rule sales2 @department('sales') \n" + "when\n" + " String( this == 'fireRules' )\n" + "then\n" + " list.add(\"sales2\");\n" + "end\n" + "\n" + "rule salesCancel @activationListener('direct')\n" + "when\n" + " String(this == 'fireCancelRule')\n" + " $i : Match( department == 'sales', category == 'special' )\n" + "then\n" + " kcontext.cancelMatch($i);\n" + "end";
KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kconf.setOption(DeclarativeAgendaOption.ENABLED);
KieBase kbase = loadKnowledgeBaseFromString(kconf, str);
KieSession ksession = createKnowledgeSession(kbase);
List list = new ArrayList();
ksession.setGlobal("list", list);
FactHandle fireRules = ksession.insert("fireRules");
FactHandle fireCancelRule = ksession.insert("fireCancelRule");
ksession.fireAllRules();
assertEquals(1, list.size());
ksession.update(fireRules, "fireRules");
ksession.update(fireCancelRule, "fireCancelRule");
ksession.fireAllRules();
assertEquals(2, list.size());
ksession.dispose();
}
use of org.kie.api.runtime.rule.Match in project drools by kiegroup.
the class DeclarativeAgendaTest method testSimpleBlockingUsingForall.
@Test(timeout = 10000)
public void testSimpleBlockingUsingForall() {
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) salience -100 \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 rule2 salience 200\n";
str += "when \n";
str += " $s : String( this == 'go1' ) \n";
str += " exists Match( department == 'sales' ) \n";
str += " forall ( $a : Match( department == 'sales' ) Match( this == $a, active == false ) ) \n";
str += "then \n";
str += " list.add( kcontext.rule.name + ':' + $s ); \n";
str += "end \n";
KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kconf.setOption(DeclarativeAgendaOption.ENABLED);
KieBase kbase = loadKnowledgeBaseFromString(kconf, str);
KieSession ksession = createKnowledgeSession(kbase);
List list = new ArrayList();
ksession.setGlobal("list", list);
ksession.insert("go1");
ksession.fireAllRules();
assertEquals(2, list.size());
assertTrue(list.contains("rule1:go1"));
assertTrue(list.contains("rule2:go1"));
ksession.dispose();
}
use of org.kie.api.runtime.rule.Match in project drools by kiegroup.
the class CepEspTest method testMultipleSlidingWindows.
@Test(timeout = 10000)
public void testMultipleSlidingWindows() throws IOException, ClassNotFoundException {
String str = "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";
KieBaseConfiguration config = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
config.setOption(EventProcessingOption.STREAM);
KieBase kbase = loadKnowledgeBaseFromString(config, str);
KieSession ksession = createKnowledgeSession(kbase);
AgendaEventListener ael = mock(AgendaEventListener.class);
ksession.addEventListener(ael);
ksession.fireAllRules();
ArgumentCaptor<AfterMatchFiredEvent> captor = ArgumentCaptor.forClass(AfterMatchFiredEvent.class);
verify(ael, times(3)).afterMatchFired(captor.capture());
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));
}
Aggregations