use of org.kie.api.runtime.rule.Match in project drools by kiegroup.
the class DeclarativeAgendaTest method testActiveInActiveChanges.
@Test(timeout = 10000)
public void testActiveInActiveChanges() {
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 rule2 @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 rule3 @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 countActivateInActive @activationListener('direct') \n";
str += "when \n";
str += " $s : String( this == 'go2' ) \n";
str += " $active : Number( this == 1 ) from accumulate( $a : Match( department == 'sales', active == true ), count( $a ) )\n";
str += " $inActive : Number( this == 2 ) from accumulate( $a : Match( department == 'sales', active == false ), count( $a ) )\n";
str += "then \n";
str += " list.add( $active + ':' + $inActive ); \n";
str += " kcontext.halt( ); \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");
FactHandle go2 = ksession.insert("go2");
ksession.fireAllRules();
assertEquals(3, list.size());
System.out.println(list);
assertTrue(list.contains("1:2"));
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 DeclarativeAgendaTest method testMultipleBlockersWithUnblockAll.
@Test(timeout = 10000)
public void testMultipleBlockersWithUnblockAll() {
// This test is a bit wierd as it recurses. Maybe unblockAll is not feasible...
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 @Propagation(EAGER) @department(sales) \n";
str += "when \n";
str += " $s : String( this == 'go0' ) \n";
str += "then \n";
str += " list.add( kcontext.rule.name + ':' + $s ); \n";
str += "end \n";
str += "rule blockerAllSalesRules1 @activationListener('direct') \n";
str += "when \n";
str += " $s : String( this == 'go1' ) \n";
str += " $i : Match( department == 'sales' ) \n";
str += "then \n";
str += " list.add( kcontext.rule.name + ':' + $i.rule.name + ':' + $s ); \n";
str += " kcontext.blockMatch( $i ); \n";
str += "end \n";
str += "rule blockerAllSalesRules2 @activationListener('direct') \n";
str += "when \n";
str += " $s : String( this == 'go2' ) \n";
str += " $i : Match( department == 'sales' ) \n";
str += "then \n";
str += " list.add( kcontext.rule.name + ':' + $i.rule.name + ':' + $s ); \n";
str += " kcontext.blockMatch( $i ); \n";
str += "end \n";
str += "rule blockerAllSalesRules3 @activationListener('direct') \n";
str += "when \n";
str += " $s : String( this == 'go3' ) \n";
str += " $i : Match( department == 'sales' ) \n";
str += "then \n";
str += " list.add( kcontext.rule.name + ':' + $i.rule.name + ':' + $s ); \n";
str += " kcontext.blockMatch( $i ); \n";
str += "end \n";
str += "rule unblockAll @activationListener('direct') \n";
str += "when \n";
str += " $s : String( this == 'go4' ) \n";
str += " $i : Match( department == 'sales', active == true ) \n";
str += "then \n";
str += " list.add( kcontext.rule.name + ':' + $i.rule.name + ':' + $s ); \n";
str += " kcontext.unblockAllMatches( $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 go0 = ksession.insert("go0");
FactHandle go1 = ksession.insert("go1");
FactHandle go2 = ksession.insert("go2");
FactHandle go3 = ksession.insert("go3");
ksession.fireAllRules();
assertEquals(3, list.size());
System.out.println(list);
assertTrue(list.contains("blockerAllSalesRules1:rule0:go1"));
assertTrue(list.contains("blockerAllSalesRules2:rule0:go2"));
assertTrue(list.contains("blockerAllSalesRules3:rule0:go3"));
list.clear();
FactHandle go4 = ksession.insert("go4");
ksession.fireAllRules();
System.out.println(list);
assertEquals(5, list.size());
assertTrue(list.contains("unblockAll:rule0:go4"));
assertTrue(list.contains("rule0:go0"));
assertTrue(list.contains("blockerAllSalesRules1:rule0:go1"));
assertTrue(list.contains("blockerAllSalesRules2:rule0:go2"));
assertTrue(list.contains("blockerAllSalesRules3:rule0:go3"));
}
use of org.kie.api.runtime.rule.Match in project drools by kiegroup.
the class AccumulateTest method execTestAccumulateMultipleFunctions.
public void execTestAccumulateMultipleFunctions(String fileName) throws Exception {
KieSession ksession = getKieSessionFromResources(fileName);
AgendaEventListener ael = mock(AgendaEventListener.class);
ksession.addEventListener(ael);
final Cheese[] cheese = new Cheese[] { new Cheese("stilton", 10), new Cheese("stilton", 3), new Cheese("stilton", 5), new Cheese("brie", 15), new Cheese("brie", 17), new Cheese("provolone", 8) };
final Person bob = new Person("Bob", "stilton");
final FactHandle[] cheeseHandles = new FactHandle[cheese.length];
for (int i = 0; i < cheese.length; i++) {
cheeseHandles[i] = (FactHandle) ksession.insert(cheese[i]);
}
final FactHandle bobHandle = (FactHandle) ksession.insert(bob);
// ---------------- 1st scenario
ksession.fireAllRules();
ArgumentCaptor<AfterMatchFiredEvent> cap = ArgumentCaptor.forClass(AfterMatchFiredEvent.class);
Mockito.verify(ael).afterMatchFired(cap.capture());
Match activation = cap.getValue().getMatch();
assertThat(((Number) activation.getDeclarationValue("$sum")).intValue(), is(18));
assertThat(((Number) activation.getDeclarationValue("$min")).intValue(), is(3));
assertThat(((Number) activation.getDeclarationValue("$avg")).intValue(), is(6));
Mockito.reset(ael);
// ---------------- 2nd scenario
final int index = 1;
cheese[index].setPrice(9);
ksession.update(cheeseHandles[index], cheese[index]);
ksession.fireAllRules();
Mockito.verify(ael).afterMatchFired(cap.capture());
activation = cap.getValue().getMatch();
assertThat(((Number) activation.getDeclarationValue("$sum")).intValue(), is(24));
assertThat(((Number) activation.getDeclarationValue("$min")).intValue(), is(5));
assertThat(((Number) activation.getDeclarationValue("$avg")).intValue(), is(8));
Mockito.reset(ael);
// ---------------- 3rd scenario
bob.setLikes("brie");
ksession.update(bobHandle, bob);
ksession.fireAllRules();
Mockito.verify(ael).afterMatchFired(cap.capture());
activation = cap.getValue().getMatch();
assertThat(((Number) activation.getDeclarationValue("$sum")).intValue(), is(32));
assertThat(((Number) activation.getDeclarationValue("$min")).intValue(), is(15));
assertThat(((Number) activation.getDeclarationValue("$avg")).intValue(), is(16));
Mockito.reset(ael);
// ---------------- 4th scenario
ksession.delete(cheeseHandles[3]);
ksession.fireAllRules();
Mockito.verify(ael).afterMatchFired(cap.capture());
activation = cap.getValue().getMatch();
assertThat(((Number) activation.getDeclarationValue("$sum")).intValue(), is(17));
assertThat(((Number) activation.getDeclarationValue("$min")).intValue(), is(17));
assertThat(((Number) activation.getDeclarationValue("$avg")).intValue(), is(17));
}
use of org.kie.api.runtime.rule.Match in project drools by kiegroup.
the class AccumulateTest method execTestAccumulateMultipleFunctionsConstraint.
public void execTestAccumulateMultipleFunctionsConstraint(String fileName) throws Exception {
KieSession ksession = getKieSessionFromResources(fileName);
AgendaEventListener ael = mock(AgendaEventListener.class);
ksession.addEventListener(ael);
final Cheese[] cheese = new Cheese[] { new Cheese("stilton", 10), new Cheese("stilton", 3), new Cheese("stilton", 5), new Cheese("brie", 3), new Cheese("brie", 17), new Cheese("provolone", 8) };
final Person bob = new Person("Bob", "stilton");
final FactHandle[] cheeseHandles = new FactHandle[cheese.length];
for (int i = 0; i < cheese.length; i++) {
cheeseHandles[i] = (FactHandle) ksession.insert(cheese[i]);
}
final FactHandle bobHandle = (FactHandle) ksession.insert(bob);
// ---------------- 1st scenario
ksession.fireAllRules();
ArgumentCaptor<AfterMatchFiredEvent> cap = ArgumentCaptor.forClass(AfterMatchFiredEvent.class);
Mockito.verify(ael).afterMatchFired(cap.capture());
Match activation = cap.getValue().getMatch();
assertThat(((Number) activation.getDeclarationValue("$sum")).intValue(), is(18));
assertThat(((Number) activation.getDeclarationValue("$min")).intValue(), is(3));
assertThat(((Number) activation.getDeclarationValue("$avg")).intValue(), is(6));
Mockito.reset(ael);
// ---------------- 2nd scenario
final int index = 1;
cheese[index].setPrice(9);
ksession.update(cheeseHandles[index], cheese[index]);
ksession.fireAllRules();
Mockito.verify(ael, Mockito.never()).afterMatchFired(Mockito.any(AfterMatchFiredEvent.class));
Mockito.reset(ael);
// ---------------- 3rd scenario
bob.setLikes("brie");
ksession.update(bobHandle, bob);
ksession.fireAllRules();
Mockito.verify(ael).afterMatchFired(cap.capture());
activation = cap.getValue().getMatch();
assertThat(((Number) activation.getDeclarationValue("$sum")).intValue(), is(20));
assertThat(((Number) activation.getDeclarationValue("$min")).intValue(), is(3));
assertThat(((Number) activation.getDeclarationValue("$avg")).intValue(), is(10));
ksession.dispose();
}
use of org.kie.api.runtime.rule.Match in project drools by kiegroup.
the class DeclarativeAgendaTest method testCancelActivation.
@Test(timeout = 10000)
public void testCancelActivation() {
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 += " kcontext.cancelMatch( $i ); \n";
str += "end \n";
KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kconf.setOption(DeclarativeAgendaOption.ENABLED);
KieBase kbase = loadKnowledgeBaseFromString(kconf, str);
KieSession ksession = createKnowledgeSession(kbase);
final List cancelled = new ArrayList();
ksession.addEventListener(new AgendaEventListener() {
public void beforeMatchFired(BeforeMatchFiredEvent event) {
}
public void agendaGroupPushed(AgendaGroupPushedEvent event) {
}
public void agendaGroupPopped(AgendaGroupPoppedEvent event) {
}
public void afterMatchFired(AfterMatchFiredEvent event) {
}
public void matchCreated(MatchCreatedEvent event) {
}
public void beforeRuleFlowGroupActivated(RuleFlowGroupActivatedEvent event) {
}
public void afterRuleFlowGroupActivated(RuleFlowGroupActivatedEvent event) {
}
public void beforeRuleFlowGroupDeactivated(RuleFlowGroupDeactivatedEvent event) {
}
public void afterRuleFlowGroupDeactivated(RuleFlowGroupDeactivatedEvent event) {
}
public void matchCancelled(MatchCancelledEvent event) {
cancelled.add(event);
}
});
List list = new ArrayList();
ksession.setGlobal("list", list);
ksession.insert("go1");
FactHandle go2 = ksession.insert("go2");
ksession.fireAllRules();
assertEquals(0, list.size());
assertEquals(1, cancelled.size());
assertEquals("rule1", ((MatchCancelledEvent) cancelled.get(0)).getMatch().getRule().getName());
ksession.dispose();
}
Aggregations