use of org.kie.api.runtime.rule.Match in project drools by kiegroup.
the class RemoveRuleTest method testPopulatedRuleWithEvals.
@Test
public void testPopulatedRuleWithEvals() throws Exception {
InternalKnowledgeBase kbase1 = buildKnowledgeBase("r1", " a:A() B() eval(1==1) eval(1==1) C(1;) \n");
InternalWorkingMemory wm = ((InternalWorkingMemory) kbase1.newKieSession());
List list = new ArrayList();
wm.setGlobal("list", list);
wm.insert(new A(1));
wm.insert(new A(2));
wm.insert(new A(3));
wm.insert(new B(1));
wm.insert(new C(1));
wm.insert(new C(2));
wm.insert(new D(1));
wm.insert(new E(1));
wm.fireAllRules();
assertEquals(7, countNodeMemories(wm.getNodeMemories()));
kbase1.addPackages(buildKnowledgePackage("r2", " a:A() B() eval(1==1) eval(1==1) C(2;) \n"));
wm.fireAllRules();
ObjectTypeNode aotn = getObjectTypeNode(kbase1, A.class);
LeftInputAdapterNode liaNode = (LeftInputAdapterNode) aotn.getObjectSinkPropagator().getSinks()[0];
JoinNode bNode = (JoinNode) liaNode.getSinkPropagator().getFirstLeftTupleSink();
EvalConditionNode e1 = (EvalConditionNode) bNode.getSinkPropagator().getFirstLeftTupleSink();
EvalConditionNode e2 = (EvalConditionNode) e1.getSinkPropagator().getFirstLeftTupleSink();
JoinNode c1Node = (JoinNode) e2.getSinkPropagator().getFirstLeftTupleSink();
JoinNode c2Node = (JoinNode) e2.getSinkPropagator().getLastLeftTupleSink();
LiaNodeMemory lm = (LiaNodeMemory) wm.getNodeMemory(liaNode);
SegmentMemory sm = lm.getSegmentMemory();
BetaMemory c1Mem = (BetaMemory) wm.getNodeMemory(c1Node);
assertSame(sm.getFirst(), c1Mem.getSegmentMemory());
assertEquals(3, c1Mem.getLeftTupleMemory().size());
assertEquals(1, c1Mem.getRightTupleMemory().size());
BetaMemory c2Mem = (BetaMemory) wm.getNodeMemory(c2Node);
SegmentMemory c2Smem = sm.getFirst().getNext();
assertSame(c2Smem, c2Mem.getSegmentMemory());
assertEquals(3, c2Mem.getLeftTupleMemory().size());
assertEquals(1, c2Mem.getRightTupleMemory().size());
assertEquals(6, list.size());
kbase1.removeRule("org.kie", "r2");
assertEquals(8, countNodeMemories(wm.getNodeMemories()));
assertNull(sm.getFirst());
// c1SMem repoints back to original Smem
assertSame(sm, c1Mem.getSegmentMemory());
wm.insert(new A(1));
wm.fireAllRules();
assertEquals("r1", ((Match) list.get(6)).getRule().getName());
// only one more added, as second rule as removed
assertEquals(7, list.size());
}
use of org.kie.api.runtime.rule.Match in project drools by kiegroup.
the class RemoveRuleTest method testPopulatedSharedToRtn.
@Test
public void testPopulatedSharedToRtn() throws Exception {
InternalKnowledgeBase kbase1 = buildKnowledgeBase("r1", " A() B() C() D() E()\n");
InternalWorkingMemory wm = ((InternalWorkingMemory) kbase1.newKieSession());
List list = new ArrayList();
wm.setGlobal("list", list);
wm.insert(new A(1));
wm.insert(new A(2));
wm.insert(new B(1));
wm.insert(new C(1));
wm.insert(new D(1));
wm.insert(new E(1));
wm.fireAllRules();
assertEquals(2, list.size());
assertEquals(7, countNodeMemories(wm.getNodeMemories()));
kbase1.addPackages(buildKnowledgePackage("r2", " A() B() C() D() E()\n"));
wm.fireAllRules();
assertEquals(8, countNodeMemories(wm.getNodeMemories()));
assertEquals(4, list.size());
RuleTerminalNode rtn1 = getRtn("org.kie.r1", kbase1);
RuleTerminalNode rtn2 = getRtn("org.kie.r2", kbase1);
PathMemory pmem1 = (PathMemory) wm.getNodeMemory(rtn1);
PathMemory pmem2 = (PathMemory) wm.getNodeMemory(rtn2);
SegmentMemory[] smems1 = pmem1.getSegmentMemories();
SegmentMemory[] smems2 = pmem2.getSegmentMemories();
assertEquals(2, smems1.length);
assertEquals(2, smems2.length);
assertSame(smems1[0], smems2[0]);
assertNotSame(smems1[1], smems2[1]);
SegmentMemory sm = smems1[0];
assertEquals(smems1[1], sm.getFirst());
JoinNode eNode1 = (JoinNode) rtn1.getLeftTupleSource();
JoinNode eNode2 = (JoinNode) rtn2.getLeftTupleSource();
assertSame(eNode1, eNode2);
pmem1 = (PathMemory) wm.getNodeMemory(rtn1);
kbase1.removeRule("org.kie", "r2");
System.out.println("---");
assertEquals(7, countNodeMemories(wm.getNodeMemories()));
assertNull(sm.getFirst());
pmem1 = (PathMemory) wm.getNodeMemory(rtn1);
smems1 = pmem1.getSegmentMemories();
assertEquals(1, smems1.length);
assertSame(sm, smems1[0]);
wm.insert(new A(1));
wm.fireAllRules();
assertEquals("r1", ((Match) list.get(4)).getRule().getName());
// only one more added, as second rule as removed
assertEquals(5, list.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";
KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kconf.setOption(DeclarativeAgendaOption.ENABLED);
KieBase kbase = loadKnowledgeBaseFromString(kconf, str);
KieSession ksession = createKnowledgeSession(kbase);
return ksession;
}
use of org.kie.api.runtime.rule.Match in project drools by kiegroup.
the class DeclarativeAgendaTest method testBasicBlockOnAnnotation.
@Test(timeout = 10000)
public void testBasicBlockOnAnnotation() {
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 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";
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());
assertTrue(list.contains("rule1:go2"));
assertTrue(list.contains("rule2:go2"));
assertTrue(list.contains("rule3:go2"));
list.clear();
ksession.retract(go2);
ksession.fireAllRules();
assertEquals(3, list.size());
assertTrue(list.contains("rule1:go1"));
assertTrue(list.contains("rule2:go1"));
assertTrue(list.contains("rule3:go1"));
ksession.dispose();
}
use of org.kie.api.runtime.rule.Match in project drools by kiegroup.
the class DeclarativeAgendaTest method testMultipleBlockers.
@Test(timeout = 10000)
public void testMultipleBlockers() {
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 @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( $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( $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( $i.rule.name + ':' + $s ); \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 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());
assertTrue(list.contains("rule0:go1"));
assertTrue(list.contains("rule0:go2"));
assertTrue(list.contains("rule0:go3"));
list.clear();
ksession.retract(go3);
ksession.fireAllRules();
assertEquals(0, list.size());
ksession.retract(go2);
ksession.fireAllRules();
assertEquals(0, list.size());
ksession.retract(go1);
ksession.fireAllRules();
assertEquals(1, list.size());
assertTrue(list.contains("rule0:go0"));
ksession.dispose();
}
Aggregations