use of org.drools.core.reteoo.BetaNode in project drools by kiegroup.
the class PropertySpecificTest method testBetaSharedAlphaNoWatches.
@Test
public void testBetaSharedAlphaNoWatches() {
String rule1 = "$b : B( a == 15) @watch(c, !a) A( a == 10, s == 15, b == $b.b )";
String rule2 = "$b : B( a == 15) @watch(j, !i) A( a == 10, i == 20, b == $b.b )";
KieBase kbase = getKnowledgeBase(rule1, rule2);
InternalWorkingMemory wm = ((InternalWorkingMemory) kbase.newKieSession());
ObjectTypeNode otn = getObjectTypeNode(kbase, "A");
assertNotNull(otn);
Class classType = ((ClassObjectType) otn.getObjectType()).getClassType();
List<String> sp = getSettableProperties(wm, otn);
AlphaNode alphaNode1 = (AlphaNode) otn.getObjectSinkPropagator().getSinks()[0];
assertEquals(calculatePositiveMask(classType, list("a"), sp), alphaNode1.getDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a", "b", "s", "i"), sp), alphaNode1.getInferredMask());
// first share
AlphaNode alphaNode1_1 = (AlphaNode) alphaNode1.getObjectSinkPropagator().getSinks()[0];
assertEquals(calculatePositiveMask(classType, list("s"), sp), alphaNode1_1.getDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a", "s", "b"), sp), alphaNode1_1.getInferredMask());
BetaNode betaNode1 = (BetaNode) alphaNode1_1.getObjectSinkPropagator().getSinks()[0];
assertEquals(calculatePositiveMask(classType, list("b"), sp), betaNode1.getRightDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a", "s", "b"), sp), betaNode1.getRightInferredMask());
assertEquals(calculatePositiveMask(classType, list("b", "c"), sp), betaNode1.getLeftDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("b", "c"), sp), betaNode1.getLeftInferredMask());
assertEquals(calculateNegativeMask(classType, list("!a"), sp), betaNode1.getLeftNegativeMask());
// second share
AlphaNode alphaNode1_2 = (AlphaNode) alphaNode1.getObjectSinkPropagator().getSinks()[1];
assertEquals(calculatePositiveMask(classType, list("i"), sp), alphaNode1_2.getDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a", "i", "b"), sp), alphaNode1_2.getInferredMask());
BetaNode betaNode2 = (BetaNode) alphaNode1_2.getObjectSinkPropagator().getSinks()[0];
assertEquals(calculatePositiveMask(classType, list("b"), sp), betaNode2.getRightDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a", "i", "b"), sp), betaNode2.getRightInferredMask());
assertEquals(calculatePositiveMask(classType, list("b", "j"), sp), betaNode2.getLeftDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a", "b", "j"), sp), betaNode2.getLeftInferredMask());
assertEquals(calculateNegativeMask(classType, list("!i"), sp), betaNode2.getLeftNegativeMask());
// test rule removal
kbase.removeRule("org.drools.compiler.integrationtests", "r0");
assertEquals(calculatePositiveMask(classType, list("a"), sp), alphaNode1.getDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a", "i", "b"), sp), alphaNode1.getInferredMask());
assertEquals(calculatePositiveMask(classType, list("i"), sp), alphaNode1_2.getDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a", "i", "b"), sp), alphaNode1_2.getInferredMask());
assertEquals(calculatePositiveMask(classType, list("b"), sp), betaNode2.getRightDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a", "i", "b"), sp), betaNode2.getRightInferredMask());
assertEquals(calculatePositiveMask(classType, list("b", "c"), sp), betaNode1.getLeftDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("b", "c"), sp), betaNode1.getLeftInferredMask());
assertEquals(calculateNegativeMask(classType, list("!a"), sp), betaNode1.getLeftNegativeMask());
// have to rebuild to remove r1
kbase = getKnowledgeBase(rule1, rule2);
kbase.removeRule("org.drools.compiler.integrationtests", "r1");
otn = getObjectTypeNode(kbase, "A");
alphaNode1 = (AlphaNode) otn.getObjectSinkPropagator().getSinks()[0];
assertEquals(calculatePositiveMask(classType, list("a"), sp), alphaNode1.getDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a", "s", "b"), sp), alphaNode1.getInferredMask());
alphaNode1_1 = (AlphaNode) alphaNode1.getObjectSinkPropagator().getSinks()[0];
assertEquals(calculatePositiveMask(classType, list("s"), sp), alphaNode1_1.getDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a", "s", "b"), sp), alphaNode1_1.getInferredMask());
betaNode1 = (BetaNode) alphaNode1_1.getObjectSinkPropagator().getSinks()[0];
assertEquals(calculatePositiveMask(classType, list("b"), sp), betaNode1.getRightDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a", "s", "b"), sp), betaNode1.getRightInferredMask());
assertEquals(calculatePositiveMask(classType, list("b", "j"), sp), betaNode2.getLeftDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a", "b", "j"), sp), betaNode2.getLeftInferredMask());
assertEquals(calculateNegativeMask(classType, list("!i"), sp), betaNode2.getLeftNegativeMask());
}
use of org.drools.core.reteoo.BetaNode in project drools by kiegroup.
the class PropertySpecificTest method testBetaNodeNoConstraintsNoPropertySpecific.
@Test
public void testBetaNodeNoConstraintsNoPropertySpecific() {
String rule = "package org.drools.compiler.integrationtests\n" + "import " + Person.class.getCanonicalName() + "\n" + "import " + Cheese.class.getCanonicalName() + "\n" + "rule r1\n" + "when\n" + " Person()\n" + " Cheese()\n" + "then\n" + "end\n";
KieBase kbase = new KieHelper(PropertySpecificOption.ALLOWED).addContent(rule, ResourceType.DRL).build();
ObjectTypeNode otn = getObjectTypeNode(kbase, "Cheese");
assertNotNull(otn);
BetaNode betaNode = (BetaNode) otn.getObjectSinkPropagator().getSinks()[0];
assertEquals(AllSetBitMask.get(), betaNode.getRightDeclaredMask());
assertEquals(AllSetBitMask.get(), betaNode.getRightInferredMask());
}
use of org.drools.core.reteoo.BetaNode in project drools by kiegroup.
the class PropertySpecificTest method testComplexBetaSharedAlphaWithWatchesRemoveR3.
@Test
public void testComplexBetaSharedAlphaWithWatchesRemoveR3() {
String rule1 = "$b : B( b == 15) @watch(i) A( a == 10, b == 15 ) @watch(c)";
String rule2 = "$b : B( b == 15) @watch(j) A( a == 10, i == 20 ) @watch(s)";
String rule3 = "$b : B( c == 15) @watch(k) A( a == 10, i == 20, b == 10 ) @watch(j)";
KieBase kbase = getKnowledgeBase(rule1, rule2, rule3);
InternalWorkingMemory wm = ((InternalWorkingMemory) kbase.newKieSession());
kbase.removeRule("org.drools.compiler.integrationtests", "r2");
ObjectTypeNode otn = getObjectTypeNode(kbase, "A");
assertNotNull(otn);
Class classType = ((ClassObjectType) otn.getObjectType()).getClassType();
List<String> sp = getSettableProperties(wm, otn);
AlphaNode alphaNode1 = (AlphaNode) otn.getObjectSinkPropagator().getSinks()[0];
assertEquals(calculatePositiveMask(classType, list("a"), sp), alphaNode1.getDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a", "b", "c", "i", "s"), sp), alphaNode1.getInferredMask());
// first share
AlphaNode alphaNode1_1 = (AlphaNode) alphaNode1.getObjectSinkPropagator().getSinks()[0];
assertEquals(calculatePositiveMask(classType, list("b"), sp), alphaNode1_1.getDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a", "b", "c"), sp), alphaNode1_1.getInferredMask());
// first split
BetaNode betaNode1 = (BetaNode) alphaNode1_1.getObjectSinkPropagator().getSinks()[0];
assertEquals(calculatePositiveMask(classType, list("c"), sp), betaNode1.getRightDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a", "b", "c"), sp), betaNode1.getRightInferredMask());
assertEquals(calculatePositiveMask(classType, list("i"), sp), betaNode1.getLeftDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("b", "i"), sp), betaNode1.getLeftInferredMask());
// second split
AlphaNode alphaNode1_2 = (AlphaNode) alphaNode1.getObjectSinkPropagator().getSinks()[1];
assertEquals(calculatePositiveMask(classType, list("i"), sp), alphaNode1_2.getDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a", "i", "s"), sp), alphaNode1_2.getInferredMask());
BetaNode betaNode2 = (BetaNode) alphaNode1_2.getObjectSinkPropagator().getSinks()[0];
assertEquals(calculatePositiveMask(classType, list("s"), sp), betaNode2.getRightDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a", "i", "s"), sp), betaNode2.getRightInferredMask());
assertEquals(calculatePositiveMask(classType, list("j"), sp), betaNode2.getLeftDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("b", "j"), sp), betaNode2.getLeftInferredMask());
}
use of org.drools.core.reteoo.BetaNode in project drools by kiegroup.
the class ProtobufOutputMarshaller method writeRIANodeMemory.
private static ProtobufMessages.NodeMemory writeRIANodeMemory(final int nodeId, final BaseNode node, final NodeMemories memories) {
RightInputAdapterNode riaNode = (RightInputAdapterNode) node;
ObjectSink[] sinks = riaNode.getObjectSinkPropagator().getSinks();
BetaNode betaNode = (BetaNode) sinks[0];
Memory betaMemory = memories.peekNodeMemory(betaNode);
if (betaMemory == null) {
return null;
}
BetaMemory bm;
if (betaNode.getType() == NodeTypeEnums.AccumulateNode) {
bm = ((AccumulateMemory) betaMemory).getBetaMemory();
} else {
bm = (BetaMemory) betaMemory;
}
// for RIA nodes, we need to store the ID of the created handles
bm.getRightTupleMemory().iterator();
if (bm.getRightTupleMemory().size() > 0) {
ProtobufMessages.NodeMemory.RIANodeMemory.Builder _ria = ProtobufMessages.NodeMemory.RIANodeMemory.newBuilder();
final org.drools.core.util.Iterator it = bm.getRightTupleMemory().iterator();
// iterates over all propagated handles and assert them to the new sink
for (RightTuple entry = (RightTuple) it.next(); entry != null; entry = (RightTuple) it.next()) {
LeftTuple leftTuple = entry instanceof LeftTuple ? // with phreak the entry is always both a right and a left tuple
(LeftTuple) entry : // this is necessary only for reteoo
(LeftTuple) entry.getFactHandle().getObject();
InternalFactHandle handle = (InternalFactHandle) leftTuple.getFactHandle();
if (handle == null) {
continue;
}
FactHandle _handle = ProtobufMessages.FactHandle.newBuilder().setId(handle.getId()).setRecency(handle.getRecency()).build();
_ria.addContext(ProtobufMessages.NodeMemory.RIANodeMemory.RIAContext.newBuilder().setTuple(PersisterHelper.createTuple(leftTuple)).setResultHandle(_handle).build());
}
return ProtobufMessages.NodeMemory.newBuilder().setNodeId(nodeId).setNodeType(ProtobufMessages.NodeMemory.NodeType.RIA).setRia(_ria.build()).build();
}
return null;
}
use of org.drools.core.reteoo.BetaNode in project drools by kiegroup.
the class AddRemoveRule method deleteRightInputData.
private static void deleteRightInputData(LeftTupleSink node, InternalWorkingMemory wm) {
if (wm.getNodeMemories().peekNodeMemory(node) != null) {
BetaNode bn = (BetaNode) node;
BetaMemory bm;
if (bn.getType() == NodeTypeEnums.AccumulateNode) {
bm = ((AccumulateMemory) wm.getNodeMemory(bn)).getBetaMemory();
} else {
bm = (BetaMemory) wm.getNodeMemory(bn);
}
TupleMemory rtm = bm.getRightTupleMemory();
FastIterator it = rtm.fullFastIterator();
for (Tuple rightTuple = BetaNode.getFirstTuple(rtm, it); rightTuple != null; ) {
Tuple next = (Tuple) it.next(rightTuple);
rtm.remove(rightTuple);
rightTuple.unlinkFromRightParent();
rightTuple = next;
}
if (!bm.getStagedRightTuples().isEmpty()) {
bm.setNodeDirtyWithoutNotify();
}
TupleSets<RightTuple> srcRightTuples = bm.getStagedRightTuples().takeAll();
unlinkRightTuples(srcRightTuples.getInsertFirst());
unlinkRightTuples(srcRightTuples.getUpdateFirst());
unlinkRightTuples(srcRightTuples.getDeleteFirst());
deleteFactsFromRightInput(bn, wm);
}
}
Aggregations