use of org.drools.core.reteoo.BetaMemory in project drools by kiegroup.
the class RemoveRuleTest method testPopulatedRuleMidwayShare.
@Test
public void testPopulatedRuleMidwayShare() throws Exception {
InternalKnowledgeBase kbase1 = buildKnowledgeBase("r1", " A() B() C(1;) 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 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() C(2;) D() E()\n"));
wm.fireAllRules();
ObjectTypeNode aotn = getObjectTypeNode(kbase1, A.class);
LeftInputAdapterNode liaNode = (LeftInputAdapterNode) aotn.getObjectSinkPropagator().getSinks()[0];
JoinNode bNode = (JoinNode) liaNode.getSinkPropagator().getFirstLeftTupleSink();
JoinNode c1Node = (JoinNode) bNode.getSinkPropagator().getFirstLeftTupleSink();
JoinNode c2Node = (JoinNode) bNode.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(10, 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.reteoo.BetaMemory 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.reteoo.BetaMemory in project drools by kiegroup.
the class OOPathReactiveTests method testReactiveDeleteOnLia.
@Test
public void testReactiveDeleteOnLia() {
final String drl = "import org.drools.compiler.oopath.model.*;\n" + "global java.util.List list\n" + "\n" + "rule R when\n" + " Man( $toy: /wife/children[age > 10]/toys )\n" + "then\n" + " list.add( $toy.getName() );\n" + "end\n";
final KieBase kbase = new KieHelper().addContent(drl, ResourceType.DRL).build();
final KieSession ksession = kbase.newKieSession();
final EntryPointNode epn = ((InternalKnowledgeBase) ksession.getKieBase()).getRete().getEntryPointNodes().values().iterator().next();
final ObjectTypeNode otn = epn.getObjectTypeNodes().values().iterator().next();
final LeftInputAdapterNode lian = (LeftInputAdapterNode) otn.getObjectSinkPropagator().getSinks()[0];
final ReactiveFromNode from1 = (ReactiveFromNode) lian.getSinkPropagator().getSinks()[0];
final ReactiveFromNode from2 = (ReactiveFromNode) from1.getSinkPropagator().getSinks()[0];
final ReactiveFromNode from3 = (ReactiveFromNode) from2.getSinkPropagator().getSinks()[0];
final BetaMemory betaMemory = ((InternalWorkingMemory) ksession).getNodeMemory(from3).getBetaMemory();
final List<String> list = new ArrayList<>();
ksession.setGlobal("list", list);
final Woman alice = new Woman("Alice", 38);
final Man bob = new Man("Bob", 40);
bob.setWife(alice);
final Child charlie = new Child("Charles", 12);
final Child debbie = new Child("Debbie", 11);
alice.addChild(charlie);
alice.addChild(debbie);
charlie.addToy(new Toy("car"));
charlie.addToy(new Toy("ball"));
debbie.addToy(new Toy("doll"));
ksession.insert(bob);
ksession.fireAllRules();
Assertions.assertThat(list).containsExactlyInAnyOrder("car", "ball", "doll");
final TupleMemory tupleMemory = betaMemory.getLeftTupleMemory();
Assertions.assertThat(betaMemory.getLeftTupleMemory().size()).isEqualTo(2);
Iterator<LeftTuple> it = tupleMemory.iterator();
for (LeftTuple next = it.next(); next != null; next = it.next()) {
final Object obj = next.getFactHandle().getObject();
Assertions.assertThat(obj == charlie || obj == debbie).isTrue();
}
list.clear();
debbie.setAge(10);
ksession.fireAllRules();
Assertions.assertThat(list).hasSize(0);
;
Assertions.assertThat(betaMemory.getLeftTupleMemory().size()).isEqualTo(1);
it = tupleMemory.iterator();
for (LeftTuple next = it.next(); next != null; next = it.next()) {
final Object obj = next.getFactHandle().getObject();
Assertions.assertThat(obj == charlie).isTrue();
}
}
use of org.drools.core.reteoo.BetaMemory in project drools by kiegroup.
the class IndexingTest method testFullFastIteratorResume.
@Test(timeout = 10000)
public void testFullFastIteratorResume() throws Exception {
String str = "";
str += "package org.drools.compiler.test \n";
str += "import org.drools.compiler.Person \n";
str += "query peeps( String $name, int $age ) \n";
str += " not $p2 : Person( $name := name, age > $age ) \n";
str += "end\n";
KieBase kbase = loadKnowledgeBaseFromString(str);
List<ObjectTypeNode> nodes = ((KnowledgeBaseImpl) kbase).getRete().getObjectTypeNodes();
ObjectTypeNode node = null;
for (ObjectTypeNode n : nodes) {
if (((ClassObjectType) n.getObjectType()).getClassType() == DroolsQuery.class) {
node = n;
break;
}
}
StatefulKnowledgeSessionImpl wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
AlphaNode alphanode = (AlphaNode) node.getObjectSinkPropagator().getSinks()[0];
LeftInputAdapterNode liaNode = (LeftInputAdapterNode) alphanode.getObjectSinkPropagator().getSinks()[0];
NotNode n = (NotNode) liaNode.getSinkPropagator().getSinks()[0];
DoubleNonIndexSkipBetaConstraints c = (DoubleNonIndexSkipBetaConstraints) n.getRawConstraints();
// assertEquals( "$name", ((VariableConstraint)c.getConstraint()).getRequiredDeclarations()[0].getIdentifier() );
assertTrue(c.isIndexed());
BetaMemory bm = (BetaMemory) wm.getNodeMemory(n);
System.out.println(bm.getLeftTupleMemory().getClass());
System.out.println(bm.getRightTupleMemory().getClass());
assertTrue(bm.getLeftTupleMemory() instanceof TupleIndexHashTable);
assertTrue(bm.getRightTupleMemory() instanceof TupleIndexHashTable);
final Map<String, Integer> map = new HashMap<String, Integer>();
map.put("inserted", new Integer(0));
map.put("deleted", new Integer(0));
map.put("updated", new Integer(0));
wm.openLiveQuery("peeps", new Object[] { Variable.v, 99 }, new ViewChangedEventListener() {
@Override
public void rowInserted(Row row) {
}
@Override
public void rowDeleted(Row row) {
}
@Override
public void rowUpdated(Row row) {
}
});
Map<String, InternalFactHandle> peeps = new HashMap<String, InternalFactHandle>();
Person p = new Person("x0", 100);
InternalFactHandle fh = (InternalFactHandle) wm.insert(p);
peeps.put(p.getName(), fh);
for (int i = 1; i < 100; i++) {
p = new Person("x" + i, 101);
fh = (InternalFactHandle) wm.insert(p);
wm.fireAllRules();
peeps.put(p.getName(), fh);
}
List<RightTuple> list = new ArrayList<RightTuple>(100);
FastIterator it = n.getRightIterator(bm.getRightTupleMemory());
for (RightTuple rt = n.getFirstRightTuple(null, bm.getRightTupleMemory(), null, it); rt != null; rt = (RightTuple) it.next(rt)) {
list.add(rt);
}
assertEquals(100, list.size());
// check we can resume from each entry in the list above.
for (int i = 0; i < 100; i++) {
RightTuple rightTuple = list.get(i);
// resumes from the current rightTuple
it = n.getRightIterator(bm.getRightTupleMemory(), rightTuple);
int j = i + 1;
for (RightTuple rt = (RightTuple) it.next(rightTuple); rt != null; rt = (RightTuple) it.next(rt)) {
assertSame(list.get(j), rt);
j++;
}
}
}
use of org.drools.core.reteoo.BetaMemory in project drools by kiegroup.
the class IndexingTest method testIndexingOnQueryUnification.
@Test(timeout = 10000)
public void testIndexingOnQueryUnification() throws Exception {
String str = "";
str += "package org.drools.compiler.test \n";
str += "import org.drools.compiler.Person \n";
str += "query peeps( String $name, String $likes, String $street) \n";
str += " $p : Person( $name := name, $likes := likes, $street := address.street ) \n";
str += "end\n";
KieBase kbase = loadKnowledgeBaseFromString(str);
List<ObjectTypeNode> nodes = ((KnowledgeBaseImpl) kbase).getRete().getObjectTypeNodes();
ObjectTypeNode node = null;
for (ObjectTypeNode n : nodes) {
if (((ClassObjectType) n.getObjectType()).getClassType() == DroolsQuery.class) {
node = n;
break;
}
}
InternalWorkingMemory wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
AlphaNode alphanode = (AlphaNode) node.getObjectSinkPropagator().getSinks()[0];
LeftInputAdapterNode liaNode = (LeftInputAdapterNode) alphanode.getObjectSinkPropagator().getSinks()[0];
// $p2
JoinNode j = (JoinNode) liaNode.getSinkPropagator().getSinks()[0];
TripleNonIndexSkipBetaConstraints c = (TripleNonIndexSkipBetaConstraints) j.getRawConstraints();
// assertEquals( "$name", ((VariableConstraint)c.getConstraint()).getRequiredDeclarations()[0].getIdentifier() );
assertTrue(c.isIndexed());
BetaMemory bm = (BetaMemory) wm.getNodeMemory(j);
assertTrue(bm.getLeftTupleMemory() instanceof TupleIndexHashTable);
assertTrue(bm.getRightTupleMemory() instanceof TupleIndexHashTable);
}
Aggregations