Search in sources :

Example 21 with RuleTerminalNode

use of org.drools.core.reteoo.RuleTerminalNode in project drools by kiegroup.

the class PropertySpecificTest method testRtnSharedAlphaWithWatches.

@Test
public void testRtnSharedAlphaWithWatches() {
    String rule1 = "A( a == 10, b == 15 ) @watch(c, !a)";
    String rule2 = "A( a == 10, i == 20 ) @watch(s, !i)";
    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", "c", "s", "i"), 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());
    LeftInputAdapterNode liaNode1 = (LeftInputAdapterNode) alphaNode1_1.getObjectSinkPropagator().getSinks()[0];
    RuleTerminalNode rtNode1 = (RuleTerminalNode) liaNode1.getSinkPropagator().getSinks()[0];
    assertEquals(calculatePositiveMask(classType, list("c"), sp), rtNode1.getDeclaredMask());
    assertEquals(calculatePositiveMask(classType, list("b", "c"), sp), rtNode1.getInferredMask());
    assertEquals(calculateNegativeMask(classType, list("!a"), sp), rtNode1.getNegativeMask());
    // 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", "s"), sp), alphaNode1_2.getInferredMask());
    LeftInputAdapterNode liaNode2 = (LeftInputAdapterNode) alphaNode1_2.getObjectSinkPropagator().getSinks()[0];
    RuleTerminalNode rtNode2 = (RuleTerminalNode) liaNode2.getSinkPropagator().getSinks()[0];
    assertEquals(calculatePositiveMask(classType, list("s"), sp), rtNode2.getDeclaredMask());
    assertEquals(calculatePositiveMask(classType, list("a", "s"), sp), rtNode2.getInferredMask());
    assertEquals(calculateNegativeMask(classType, list("!i"), sp), rtNode2.getNegativeMask());
    // test rule removal
    kbase.removeRule("org.drools.compiler.integrationtests", "r0");
    assertEquals(calculatePositiveMask(classType, list("a"), sp), alphaNode1.getDeclaredMask());
    assertEquals(calculatePositiveMask(classType, list("a", "i", "s"), sp), alphaNode1.getInferredMask());
    assertEquals(calculatePositiveMask(classType, list("i"), sp), alphaNode1_2.getDeclaredMask());
    assertEquals(calculatePositiveMask(classType, list("a", "i", "s"), sp), alphaNode1_2.getInferredMask());
    assertEquals(calculatePositiveMask(classType, list("s"), sp), rtNode2.getDeclaredMask());
    assertEquals(calculatePositiveMask(classType, list("a", "s"), sp), rtNode2.getInferredMask());
    assertEquals(calculateNegativeMask(classType, list("!i"), sp), rtNode2.getNegativeMask());
    // 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", "b", "c"), sp), alphaNode1.getInferredMask());
    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());
    liaNode1 = (LeftInputAdapterNode) alphaNode1_1.getObjectSinkPropagator().getSinks()[0];
    rtNode1 = (RuleTerminalNode) liaNode1.getSinkPropagator().getSinks()[0];
    assertEquals(calculatePositiveMask(classType, list("c"), sp), rtNode1.getDeclaredMask());
    assertEquals(calculatePositiveMask(classType, list("b", "c"), sp), rtNode1.getInferredMask());
    assertEquals(calculateNegativeMask(classType, list("!a"), sp), rtNode1.getNegativeMask());
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) ClassObjectType(org.drools.core.base.ClassObjectType) KieBase(org.kie.api.KieBase) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) AlphaNode(org.drools.core.reteoo.AlphaNode) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Test(org.junit.Test)

Example 22 with RuleTerminalNode

use of org.drools.core.reteoo.RuleTerminalNode in project drools by kiegroup.

the class PropertySpecificTest method testRTNodeWithConstraintsNoPropertySpecific.

@Test
public void testRTNodeWithConstraintsNoPropertySpecific() {
    String rule = "package org.drools.compiler.integrationtests\n" + "import " + Person.class.getCanonicalName() + "\n" + "rule r1\n" + "when\n" + "   Person( name == 'bobba')\n" + "then\n" + "end\n";
    KieBase kbase = new KieHelper(PropertySpecificOption.ALLOWED).addContent(rule, ResourceType.DRL).build();
    ObjectTypeNode otn = getObjectTypeNode(kbase, "Person");
    assertNotNull(otn);
    AlphaNode alphaNode = (AlphaNode) otn.getObjectSinkPropagator().getSinks()[0];
    assertEquals(AllSetBitMask.get(), alphaNode.getDeclaredMask());
    assertEquals(AllSetBitMask.get(), alphaNode.getInferredMask());
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) alphaNode.getObjectSinkPropagator().getSinks()[0];
    RuleTerminalNode rtNode = (RuleTerminalNode) liaNode.getSinkPropagator().getSinks()[0];
    assertEquals(AllSetBitMask.get(), rtNode.getDeclaredMask());
    assertEquals(AllSetBitMask.get(), rtNode.getInferredMask());
}
Also used : KieBase(org.kie.api.KieBase) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) KieHelper(org.kie.internal.utils.KieHelper) Person(org.drools.compiler.Person) AlphaNode(org.drools.core.reteoo.AlphaNode) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Test(org.junit.Test)

Example 23 with RuleTerminalNode

use of org.drools.core.reteoo.RuleTerminalNode in project drools by kiegroup.

the class PropertySpecificTest method testRtnSharedAlphaNoWatches.

@Test
public void testRtnSharedAlphaNoWatches() {
    String rule1 = "A( a == 10, b == 15 )";
    String rule2 = "A( a == 10, i == 20 )";
    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", "i"), 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"), sp), alphaNode1_1.getInferredMask());
    LeftInputAdapterNode liaNode1 = (LeftInputAdapterNode) alphaNode1_1.getObjectSinkPropagator().getSinks()[0];
    RuleTerminalNode rtNode1 = (RuleTerminalNode) liaNode1.getSinkPropagator().getSinks()[0];
    assertEquals(EmptyBitMask.get(), rtNode1.getDeclaredMask());
    assertEquals(calculatePositiveMask(classType, list("a", "b"), sp), rtNode1.getInferredMask());
    // 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"), sp), alphaNode1_2.getInferredMask());
    LeftInputAdapterNode liaNode2 = (LeftInputAdapterNode) alphaNode1_2.getObjectSinkPropagator().getSinks()[0];
    RuleTerminalNode rtNode2 = (RuleTerminalNode) liaNode2.getSinkPropagator().getSinks()[0];
    assertEquals(EmptyBitMask.get(), rtNode2.getDeclaredMask());
    assertEquals(calculatePositiveMask(classType, list("a", "i"), sp), rtNode2.getInferredMask());
    // test rule removal
    kbase.removeRule("org.drools.compiler.integrationtests", "r0");
    assertEquals(calculatePositiveMask(classType, list("a"), sp), alphaNode1.getDeclaredMask());
    assertEquals(calculatePositiveMask(classType, list("a", "i"), sp), alphaNode1.getInferredMask());
    assertEquals(calculatePositiveMask(classType, list("i"), sp), alphaNode1_2.getDeclaredMask());
    assertEquals(calculatePositiveMask(classType, list("a", "i"), sp), alphaNode1_2.getInferredMask());
    assertEquals(EmptyBitMask.get(), rtNode2.getDeclaredMask());
    assertEquals(calculatePositiveMask(classType, list("a", "i"), sp), rtNode2.getInferredMask());
    // 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", "b"), sp), alphaNode1.getInferredMask());
    alphaNode1_1 = (AlphaNode) alphaNode1.getObjectSinkPropagator().getSinks()[0];
    assertEquals(calculatePositiveMask(classType, list("b"), sp), alphaNode1_1.getDeclaredMask());
    assertEquals(calculatePositiveMask(classType, list("a", "b"), sp), alphaNode1_1.getInferredMask());
    liaNode1 = (LeftInputAdapterNode) alphaNode1_1.getObjectSinkPropagator().getSinks()[0];
    rtNode1 = (RuleTerminalNode) liaNode1.getSinkPropagator().getSinks()[0];
    assertEquals(EmptyBitMask.get(), rtNode1.getDeclaredMask());
    assertEquals(calculatePositiveMask(classType, list("a", "b"), sp), rtNode1.getInferredMask());
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) ClassObjectType(org.drools.core.base.ClassObjectType) KieBase(org.kie.api.KieBase) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) AlphaNode(org.drools.core.reteoo.AlphaNode) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Test(org.junit.Test)

Example 24 with RuleTerminalNode

use of org.drools.core.reteoo.RuleTerminalNode in project drools by kiegroup.

the class PropertySpecificTest method testRtnNoConstraintsWithWatches.

@Test
public void testRtnNoConstraintsWithWatches() {
    String rule1 = "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();
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) otn.getObjectSinkPropagator().getSinks()[0];
    List<String> sp = getSettableProperties(wm, otn);
    RuleTerminalNode rtNode = (RuleTerminalNode) liaNode.getSinkPropagator().getSinks()[0];
    assertEquals(calculatePositiveMask(classType, list("a"), sp), rtNode.getDeclaredMask());
    assertEquals(calculatePositiveMask(classType, list("a"), sp), rtNode.getInferredMask());
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) ClassObjectType(org.drools.core.base.ClassObjectType) KieBase(org.kie.api.KieBase) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Test(org.junit.Test)

Example 25 with RuleTerminalNode

use of org.drools.core.reteoo.RuleTerminalNode in project drools by kiegroup.

the class LinkingTest method testSubNetworkSharingMemories.

@Test
public void testSubNetworkSharingMemories() throws Exception {
    // checks the memory sharing works, and linking, uses the already checked network from testSubNetworkSharing
    String str = "";
    str += "package org.kie \n";
    str += "import " + A.class.getCanonicalName() + "\n";
    str += "import " + B.class.getCanonicalName() + "\n";
    str += "import " + C.class.getCanonicalName() + "\n";
    str += "import " + D.class.getCanonicalName() + "\n";
    str += "import " + E.class.getCanonicalName() + "\n";
    str += "global java.util.List list \n";
    str += "rule rule1 when \n";
    str += "   A() \n";
    str += "   B() \n";
    str += "   C() \n";
    str += "   D() \n";
    str += "then \n";
    str += "end \n";
    str += "rule rule2 when \n";
    str += "   A() \n";
    str += "   exists( B() and C() ) \n";
    str += "   D() \n";
    str += "then \n";
    str += "end \n";
    str += "rule rule3 when \n";
    str += "   A() \n";
    str += "   exists( B() and C() and D() ) \n";
    str += "   E() \n";
    str += "then \n";
    str += "end \n";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    ObjectTypeNode node = getObjectTypeNode(kbase, A.class);
    InternalWorkingMemory wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
    LeftInputAdapterNode liaNodeA = (LeftInputAdapterNode) node.getObjectSinkPropagator().getSinks()[0];
    ExistsNode existsNode2 = (ExistsNode) liaNodeA.getSinkPropagator().getSinks()[1];
    ExistsNode existsNode3 = (ExistsNode) liaNodeA.getSinkPropagator().getSinks()[2];
    JoinNode joinNodeB = (JoinNode) liaNodeA.getSinkPropagator().getSinks()[0];
    JoinNode joinNodeC = (JoinNode) joinNodeB.getSinkPropagator().getSinks()[0];
    JoinNode joinNodeD1 = (JoinNode) joinNodeC.getSinkPropagator().getSinks()[0];
    JoinNode joinNodeD2 = (JoinNode) existsNode2.getSinkPropagator().getSinks()[0];
    JoinNode joinNodeE = (JoinNode) existsNode3.getSinkPropagator().getSinks()[0];
    RuleTerminalNode rtn1 = (RuleTerminalNode) joinNodeD1.getSinkPropagator().getSinks()[0];
    RuleTerminalNode rtn2 = (RuleTerminalNode) joinNodeD2.getSinkPropagator().getSinks()[0];
    RuleTerminalNode rtn3 = (RuleTerminalNode) joinNodeE.getSinkPropagator().getSinks()[0];
    FactHandle fha = wm.insert(new A());
    wm.insert(new B());
    wm.insert(new C());
    wm.insert(new D());
    wm.flushPropagations();
    BetaMemory bm = null;
    LiaNodeMemory liam = (LiaNodeMemory) wm.getNodeMemory(liaNodeA);
    BetaMemory bm1 = (BetaMemory) wm.getNodeMemory(joinNodeB);
    BetaMemory bm2 = (BetaMemory) wm.getNodeMemory(joinNodeC);
    BetaMemory bm3 = (BetaMemory) wm.getNodeMemory(joinNodeD1);
    assertEquals(1, liam.getNodePosMaskBit());
    assertEquals(1, bm1.getNodePosMaskBit());
    assertEquals(2, bm2.getNodePosMaskBit());
    assertEquals(1, bm3.getNodePosMaskBit());
    assertNotSame(liam.getSegmentMemory(), bm1.getSegmentMemory());
    assertSame(bm1.getSegmentMemory(), bm2.getSegmentMemory());
    assertNotSame(bm2.getSegmentMemory(), bm3.getSegmentMemory());
    BetaMemory bm4 = (BetaMemory) wm.getNodeMemory(existsNode2);
    BetaMemory bm5 = (BetaMemory) wm.getNodeMemory(joinNodeD2);
    assertEquals(1, bm4.getNodePosMaskBit());
    assertEquals(2, bm5.getNodePosMaskBit());
    assertSame(bm4.getSegmentMemory(), bm5.getSegmentMemory());
    PathMemory rs1 = (PathMemory) wm.getNodeMemory(rtn1);
    PathMemory rs2 = (PathMemory) wm.getNodeMemory(rtn2);
    PathMemory rs3 = (PathMemory) wm.getNodeMemory(rtn3);
    assertTrue(rs1.isRuleLinked());
    assertTrue(rs2.isRuleLinked());
    // no E yet
    assertFalse(rs3.isRuleLinked());
    wm.insert(new E());
    wm.flushPropagations();
    BetaMemory bm6 = (BetaMemory) wm.getNodeMemory(existsNode3);
    BetaMemory bm7 = (BetaMemory) wm.getNodeMemory(joinNodeE);
    assertEquals(1, bm6.getNodePosMaskBit());
    assertEquals(2, bm7.getNodePosMaskBit());
    assertSame(bm6.getSegmentMemory(), bm7.getSegmentMemory());
    assertTrue(rs1.isRuleLinked());
    assertTrue(rs2.isRuleLinked());
    assertTrue(rs3.isRuleLinked());
    wm.retract(fha);
    // need to have rules evalulated, for unlinking to happen
    wm.fireAllRules();
    assertFalse(rs1.isRuleLinked());
    assertFalse(rs2.isRuleLinked());
    assertFalse(rs3.isRuleLinked());
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) JoinNode(org.drools.core.reteoo.JoinNode) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) BetaMemory(org.drools.core.reteoo.BetaMemory) ExistsNode(org.drools.core.reteoo.ExistsNode) LiaNodeMemory(org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) PathMemory(org.drools.core.reteoo.PathMemory) Test(org.junit.Test)

Aggregations

RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)69 Test (org.junit.Test)54 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)38 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)30 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)28 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)23 KieBase (org.kie.api.KieBase)23 SegmentMemory (org.drools.core.reteoo.SegmentMemory)18 JoinNode (org.drools.core.reteoo.JoinNode)16 PathMemory (org.drools.core.reteoo.PathMemory)16 ArrayList (java.util.ArrayList)14 ClassObjectType (org.drools.core.base.ClassObjectType)13 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)13 List (java.util.List)12 LiaNodeMemory (org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory)11 WorkingMemory (org.drools.core.WorkingMemory)10 RightInputAdapterNode (org.drools.core.reteoo.RightInputAdapterNode)9 FactHandle (org.kie.api.runtime.rule.FactHandle)9 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)9 KnowledgeBaseImpl (org.drools.core.impl.KnowledgeBaseImpl)8