Search in sources :

Example 6 with BetaNode

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);
}
Also used : BetaNode(org.drools.core.reteoo.BetaNode) KnowledgeBuilderErrors(org.kie.internal.builder.KnowledgeBuilderErrors) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) PredicateExpression(org.drools.core.spi.PredicateExpression) BetaNodeFieldConstraint(org.drools.core.spi.BetaNodeFieldConstraint) CompiledInvoker(org.drools.core.spi.CompiledInvoker) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) StringReader(java.io.StringReader) PredicateConstraint(org.drools.core.rule.PredicateConstraint) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 7 with BetaNode

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);
}
Also used : Rete(org.drools.core.reteoo.Rete) BetaNode(org.drools.core.reteoo.BetaNode) ClassObjectType(org.drools.core.base.ClassObjectType) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) LeftTupleSink(org.drools.core.reteoo.LeftTupleSink) KieHelper(org.kie.internal.utils.KieHelper) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) ExistsNode(org.drools.core.reteoo.ExistsNode) KieBase(org.kie.api.KieBase) LeftTupleNode(org.drools.core.reteoo.LeftTupleNode) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) RightInputAdapterNode(org.drools.core.reteoo.RightInputAdapterNode) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Test(org.junit.Test)

Example 8 with BetaNode

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());
    }
}
Also used : EntryPointNode(org.drools.core.reteoo.EntryPointNode) LeftTupleSource(org.drools.core.reteoo.LeftTupleSource) BetaNode(org.drools.core.reteoo.BetaNode) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) ObjectSource(org.drools.core.reteoo.ObjectSource) TerminalNode(org.drools.core.reteoo.TerminalNode)

Example 9 with BetaNode

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());
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) BetaNode(org.drools.core.reteoo.BetaNode) ClassObjectType(org.drools.core.base.ClassObjectType) KieBase(org.kie.api.KieBase) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) Test(org.junit.Test)

Example 10 with BetaNode

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());
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) BetaNode(org.drools.core.reteoo.BetaNode) ClassObjectType(org.drools.core.base.ClassObjectType) KieBase(org.kie.api.KieBase) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) Test(org.junit.Test)

Aggregations

BetaNode (org.drools.core.reteoo.BetaNode)38 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)26 Test (org.junit.Test)25 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)23 KieBase (org.kie.api.KieBase)23 ClassObjectType (org.drools.core.base.ClassObjectType)17 AlphaNode (org.drools.core.reteoo.AlphaNode)13 BetaMemory (org.drools.core.reteoo.BetaMemory)11 RightInputAdapterNode (org.drools.core.reteoo.RightInputAdapterNode)7 AccumulateMemory (org.drools.core.reteoo.AccumulateNode.AccumulateMemory)6 KieHelper (org.kie.internal.utils.KieHelper)6 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)5 LeftTuple (org.drools.core.reteoo.LeftTuple)5 RightTuple (org.drools.core.reteoo.RightTuple)5 Person (org.drools.compiler.Person)4 InternalFactHandle (org.drools.core.common.InternalFactHandle)4 Memory (org.drools.core.common.Memory)4 ArrayList (java.util.ArrayList)3 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)3 FromMemory (org.drools.core.reteoo.FromNode.FromMemory)3