use of org.drools.core.reteoo.AlphaNode 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.AlphaNode in project drools by kiegroup.
the class PropertySpecificTest method testRtnWithConstraintsNoWatches.
@Test
public void testRtnWithConstraintsNoWatches() {
String rule1 = "A( a == 10 )";
KieBase kbase = getKnowledgeBase(rule1);
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 alphaNode = (AlphaNode) otn.getObjectSinkPropagator().getSinks()[0];
assertEquals(calculatePositiveMask(classType, list("a"), sp), alphaNode.getDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a"), sp), alphaNode.getInferredMask());
LeftInputAdapterNode liaNode = (LeftInputAdapterNode) alphaNode.getObjectSinkPropagator().getSinks()[0];
RuleTerminalNode rtNode = (RuleTerminalNode) liaNode.getSinkPropagator().getSinks()[0];
// rtn declares nothing
assertEquals(EmptyBitMask.get(), rtNode.getDeclaredMask());
// rtn infers from alpha
assertEquals(calculatePositiveMask(classType, list("a"), sp), rtNode.getInferredMask());
}
use of org.drools.core.reteoo.AlphaNode 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.AlphaNode in project drools by kiegroup.
the class MemoryLeakTest method testStagedLeftTupleLeak.
@Test
public void testStagedLeftTupleLeak() throws Exception {
// BZ-1058874
String str = "rule R1 when\n" + " String( this == \"this\" )\n" + " String( this == \"that\" )\n" + "then\n" + "end\n";
KieBase kbase = new KieHelper().addContent(str, ResourceType.DRL).build();
KieSession ksession = kbase.newKieSession();
ksession.fireAllRules();
for (int i = 0; i < 10; i++) {
FactHandle fh = ksession.insert("this");
ksession.fireAllRules();
ksession.delete(fh);
ksession.fireAllRules();
}
Rete rete = ((KnowledgeBaseImpl) kbase).getRete();
LeftInputAdapterNode liaNode = null;
for (ObjectTypeNode otn : rete.getObjectTypeNodes()) {
if (String.class == otn.getObjectType().getValueType().getClassType()) {
AlphaNode alphaNode = (AlphaNode) otn.getObjectSinkPropagator().getSinks()[0];
liaNode = (LeftInputAdapterNode) alphaNode.getObjectSinkPropagator().getSinks()[0];
break;
}
}
assertNotNull(liaNode);
InternalWorkingMemory wm = (InternalWorkingMemory) ksession;
LeftInputAdapterNode.LiaNodeMemory memory = (LeftInputAdapterNode.LiaNodeMemory) wm.getNodeMemory(liaNode);
TupleSets<LeftTuple> stagedLeftTuples = memory.getSegmentMemory().getStagedLeftTuples();
assertNull(stagedLeftTuples.getDeleteFirst());
assertNull(stagedLeftTuples.getInsertFirst());
}
use of org.drools.core.reteoo.AlphaNode in project drools by kiegroup.
the class JavaDialectTest method testEvalDetectionInAlphaNode.
@Test
public void testEvalDetectionInAlphaNode() {
// Tests evals are generated and executed with Java dialect
String drl = "";
drl += "package org.drools.compiler.test\n";
drl += "import org.drools.compiler.Person\n";
drl += "global java.util.List list\n";
drl += "rule test1\n";
drl += "when\n";
drl += " $p1 : Person( eval( name \n != null ), name == ( new String(\"xxx\") ) )\n";
drl += "then\n";
drl += "end\n";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newReaderResource(new StringReader(drl)), ResourceType.DRL);
KnowledgeBuilderErrors errors = kbuilder.getErrors();
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
assertFalse(kbuilder.hasErrors());
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
List<ObjectTypeNode> nodes = ((KnowledgeBaseImpl) kbase).getRete().getObjectTypeNodes();
ObjectTypeNode node = null;
for (ObjectTypeNode n : nodes) {
if (((ClassObjectType) n.getObjectType()).getClassType() == Person.class) {
node = n;
break;
}
}
AlphaNode alphanode = (AlphaNode) node.getObjectSinkPropagator().getSinks()[0];
PredicateConstraint c = (PredicateConstraint) alphanode.getConstraint();
assertTrue(c.getPredicateExpression() instanceof PredicateExpression);
assertTrue(c.getPredicateExpression() instanceof CompiledInvoker);
alphanode = (AlphaNode) alphanode.getObjectSinkPropagator().getSinks()[0];
AlphaNodeFieldConstraint constraint = alphanode.getConstraint();
if (constraint instanceof MvelConstraint) {
FieldValue fieldVal = ((MvelConstraint) constraint).getField();
assertEquals("xxx", fieldVal.getValue());
}
}
Aggregations