use of org.drools.core.impl.InternalKnowledgeBase in project drools by kiegroup.
the class RemoveRuleTest method testPopulatedMultipleSharesRemoveLast.
@Test
public void testPopulatedMultipleSharesRemoveLast() throws Exception {
InternalKnowledgeBase kbase1 = buildKnowledgeBase("r1", " A(1;) A(2;) B(1;) B(2;) C(1;) D() E()\n");
InternalWorkingMemory wm = ((InternalWorkingMemory) kbase1.newKieSession());
List list = new ArrayList();
wm.setGlobal("list", list);
InternalFactHandle fh1 = (InternalFactHandle) wm.insert(new A(1));
InternalFactHandle fh2 = (InternalFactHandle) wm.insert(new A(2));
InternalFactHandle fh3 = (InternalFactHandle) wm.insert(new A(2));
InternalFactHandle fh4 = (InternalFactHandle) wm.insert(new A(3));
InternalFactHandle fh5 = (InternalFactHandle) wm.insert(new B(1));
InternalFactHandle fh6 = (InternalFactHandle) wm.insert(new B(2));
InternalFactHandle fh7 = (InternalFactHandle) wm.insert(new C(1));
InternalFactHandle fh8 = (InternalFactHandle) wm.insert(new C(2));
InternalFactHandle fh9 = (InternalFactHandle) wm.insert(new D(1));
InternalFactHandle fh10 = (InternalFactHandle) wm.insert(new E(1));
wm.fireAllRules();
assertEquals(2, list.size());
kbase1.addPackages(buildKnowledgePackage("r2", " A(1;) A(2;) B(1;) B(2;) C(2;) D() E()\n"));
kbase1.addPackages(buildKnowledgePackage("r3", " A(1;) A(3;) B(1;) B(2;) C(2;) D() E()\n"));
wm.fireAllRules();
assertEquals(5, list.size());
kbase1.removeRule("org.kie", "r3");
list.clear();
wm.update(fh1, fh1.getObject());
wm.update(fh2, fh2.getObject());
wm.update(fh3, fh3.getObject());
wm.update(fh4, fh4.getObject());
wm.update(fh5, fh5.getObject());
wm.update(fh6, fh6.getObject());
wm.update(fh7, fh7.getObject());
wm.update(fh8, fh8.getObject());
wm.update(fh9, fh9.getObject());
wm.update(fh10, fh10.getObject());
wm.fireAllRules();
assertEquals(4, list.size());
}
use of org.drools.core.impl.InternalKnowledgeBase 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.drools.core.impl.InternalKnowledgeBase in project drools by kiegroup.
the class RemoveRuleTest method buildKnowledgeBase.
private InternalKnowledgeBase buildKnowledgeBase(String ruleName, String rule) {
String str = "";
str += "package org.kie \n";
str += "import " + A.class.getCanonicalName() + "\n";
str += "import " + B.class.getCanonicalName() + "\n";
str += "import " + C.class.getCanonicalName() + "\n";
str += "import " + D.class.getCanonicalName() + "\n";
str += "import " + E.class.getCanonicalName() + "\n";
str += "global java.util.List list \n";
int i = 0;
str += "rule " + ruleName + " when \n";
str += rule;
str += "then \n";
str += " list.add( kcontext.getMatch() );\n";
str += "end \n";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
return kbase;
}
use of org.drools.core.impl.InternalKnowledgeBase 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.drools.core.impl.InternalKnowledgeBase in project drools by kiegroup.
the class ReteooBuilderTest method checkRuleBase.
private void checkRuleBase(final String name) throws Exception {
final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
builder.addPackageFromDrl(new InputStreamReader(getClass().getResourceAsStream("test_" + name + ".drl")));
InternalKnowledgePackage pkg = builder.getPackage("org.drools.compiler.test");
final InternalKnowledgeBase kBase = (InternalKnowledgeBase) getKnowledgeBase();
kBase.addPackage(pkg);
if (this.writeTree) {
writeRuleBase(kBase, name);
}
final XStream xstream = createTrustingXStream();
final InternalKnowledgeBase goodKBase = (InternalKnowledgeBase) xstream.fromXML(getClass().getResourceAsStream(name));
nodesEquals(goodKBase.getRete(), kBase.getRete());
}
Aggregations