use of org.drools.core.reteoo.BetaNode in project drools by kiegroup.
the class JavaDialectTest method testEvalDetectionInBetaNode.
@Test
public void testEvalDetectionInBetaNode() {
// 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 += " $s : String()\n";
drl += " $p1 : Person( eval( name \n != $s ), name == ( new String($s+\"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;
}
}
BetaNode betaanode = (BetaNode) node.getObjectSinkPropagator().getSinks()[0];
BetaNodeFieldConstraint[] constraint = betaanode.getConstraints();
PredicateConstraint c = (PredicateConstraint) constraint[0];
assertTrue(c.getPredicateExpression() instanceof PredicateExpression);
assertTrue(c.getPredicateExpression() instanceof CompiledInvoker);
}
use of org.drools.core.reteoo.BetaNode in project drools by kiegroup.
the class NodePositionInPathTest method test.
@Test
public void test() {
String drl = "rule R1 when\n" + " Integer()\n" + " exists( Integer() and Integer() )\n" + " String()\n" + "then\n" + "end\n" + "rule R2 when\n" + " Integer()\n" + " exists( Integer() and String() )\n" + "then\n" + "end\n";
KieBase kbase = new KieHelper().addContent(drl, ResourceType.DRL).build();
ReteDumper.dumpRete(((InternalKnowledgeBase) kbase));
Rete rete = ((KnowledgeBaseImpl) kbase).getRete();
LeftInputAdapterNode liaNode = null;
for (ObjectTypeNode otn : rete.getObjectTypeNodes()) {
Class<?> otnType = ((ClassObjectType) otn.getObjectType()).getClassType();
if (Integer.class == otnType) {
liaNode = (LeftInputAdapterNode) otn.getObjectSinkPropagator().getSinks()[0];
}
}
assertEquals(0, liaNode.getPositionInPath());
LeftTupleSink[] liaSinks = liaNode.getSinkPropagator().getSinks();
BetaNode join1 = (BetaNode) liaSinks[0];
assertEquals(1, join1.getPositionInPath());
ExistsNode ex1 = (ExistsNode) liaSinks[1];
assertEquals(1, ex1.getPositionInPath());
BetaNode join2 = (BetaNode) ex1.getSinkPropagator().getSinks()[0];
assertEquals(2, join2.getPositionInPath());
RuleTerminalNode rtn1 = (RuleTerminalNode) join2.getSinkPropagator().getSinks()[0];
assertEquals(3, rtn1.getPositionInPath());
ExistsNode ex2 = (ExistsNode) liaSinks[2];
assertEquals(1, ex2.getPositionInPath());
RuleTerminalNode rtn2 = (RuleTerminalNode) ex2.getSinkPropagator().getSinks()[0];
assertEquals(2, rtn2.getPositionInPath());
BetaNode join3 = (BetaNode) join1.getSinkPropagator().getSinks()[0];
assertEquals(2, join3.getPositionInPath());
RightInputAdapterNode ria1 = (RightInputAdapterNode) join3.getSinkPropagator().getSinks()[0];
assertEquals(3, ria1.getPositionInPath());
BetaNode join4 = (BetaNode) join1.getSinkPropagator().getSinks()[1];
assertEquals(2, join4.getPositionInPath());
RightInputAdapterNode ria2 = (RightInputAdapterNode) join4.getSinkPropagator().getSinks()[0];
assertEquals(3, ria2.getPositionInPath());
LeftTupleNode[] rtn1PathNodes = rtn1.getPathNodes();
assertEquals(4, rtn1PathNodes.length);
checkNodePosition(rtn1PathNodes, liaNode);
checkNodePosition(rtn1PathNodes, ex1);
checkNodePosition(rtn1PathNodes, join2);
checkNodePosition(rtn1PathNodes, rtn1);
LeftTupleNode[] rtn2PathNodes = rtn2.getPathNodes();
assertEquals(3, rtn2PathNodes.length);
checkNodePosition(rtn2PathNodes, liaNode);
checkNodePosition(rtn2PathNodes, ex2);
checkNodePosition(rtn2PathNodes, rtn2);
LeftTupleNode[] ria1PathNodes = ria1.getPathNodes();
assertEquals(4, ria1PathNodes.length);
checkNodePosition(ria1PathNodes, liaNode);
checkNodePosition(ria1PathNodes, join1);
checkNodePosition(ria1PathNodes, join3);
checkNodePosition(ria1PathNodes, ria1);
LeftTupleNode[] ria2PathNodes = ria2.getPathNodes();
assertEquals(4, ria2PathNodes.length);
checkNodePosition(ria2PathNodes, liaNode);
checkNodePosition(ria2PathNodes, join1);
checkNodePosition(ria2PathNodes, join4);
checkNodePosition(ria2PathNodes, ria2);
}
use of org.drools.core.reteoo.BetaNode in project drools by kiegroup.
the class NodesPartitioningTest method checkNode.
private void checkNode(NetworkNode node) {
if (node instanceof EntryPointNode) {
assertSame(RuleBasePartitionId.MAIN_PARTITION, node.getPartitionId());
} else if (node instanceof ObjectTypeNode) {
assertSame(RuleBasePartitionId.MAIN_PARTITION, node.getPartitionId());
checkPartitionedSinks((ObjectTypeNode) node);
} else if (node instanceof ObjectSource) {
ObjectSource source = ((ObjectSource) node).getParentObjectSource();
if (!(source instanceof ObjectTypeNode)) {
assertSame(source.getPartitionId(), node.getPartitionId());
}
} else if (node instanceof BetaNode) {
ObjectSource rightInput = ((BetaNode) node).getRightInput();
if (!(rightInput instanceof ObjectTypeNode)) {
assertSame(rightInput.getPartitionId(), node.getPartitionId());
}
LeftTupleSource leftInput = ((BetaNode) node).getLeftTupleSource();
assertSame(leftInput.getPartitionId(), node.getPartitionId());
} else if (node instanceof TerminalNode) {
LeftTupleSource leftInput = ((TerminalNode) node).getLeftTupleSource();
assertSame(leftInput.getPartitionId(), node.getPartitionId());
}
}
use of org.drools.core.reteoo.BetaNode in project drools by kiegroup.
the class PropertySpecificTest method testBetaAfterBetaWithWatch.
@Test
public void testBetaAfterBetaWithWatch() {
String rule1 = "$b : B(a == 15) @watch(k) A(i == $b.j) @watch(b, c) C()";
KieBase kbase = getKnowledgeBase(rule1);
InternalWorkingMemory wm = ((InternalWorkingMemory) kbase.newKieSession());
ObjectTypeNode otnA = getObjectTypeNode(kbase, "A");
Class classType = ((ClassObjectType) otnA.getObjectType()).getClassType();
ObjectTypeNode otnC = getObjectTypeNode(kbase, "C");
List<String> sp = getSettableProperties(wm, otnA);
BetaNode betaNodeA = (BetaNode) otnA.getObjectSinkPropagator().getSinks()[0];
assertEquals(calculatePositiveMask(classType, list("i", "b", "c"), sp), betaNodeA.getRightDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("i", "b", "c"), sp), betaNodeA.getRightInferredMask());
assertEquals(calculatePositiveMask(classType, list("j", "k"), sp), betaNodeA.getLeftDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a", "j", "k"), sp), betaNodeA.getLeftInferredMask());
BetaNode betaNodeC = (BetaNode) otnC.getObjectSinkPropagator().getSinks()[0];
assertEquals(EmptyBitMask.get(), betaNodeC.getRightDeclaredMask());
assertEquals(EmptyBitMask.get(), betaNodeC.getRightInferredMask());
assertEquals(AllSetBitMask.get(), betaNodeC.getLeftDeclaredMask());
assertEquals(AllSetBitMask.get(), betaNodeC.getLeftInferredMask());
}
use of org.drools.core.reteoo.BetaNode in project drools by kiegroup.
the class PropertySpecificTest method testBetaNoConstraintsWithWatches.
@Test
public void testBetaNoConstraintsWithWatches() {
String rule1 = "B() @watch(a) A() @watch(a)";
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);
BetaNode betaNode = (BetaNode) otn.getObjectSinkPropagator().getSinks()[0];
assertEquals(calculatePositiveMask(classType, list("a"), sp), betaNode.getRightDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a"), sp), betaNode.getRightInferredMask());
assertEquals(calculatePositiveMask(classType, list("a"), sp), betaNode.getLeftDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a"), sp), betaNode.getLeftInferredMask());
}
Aggregations