Search in sources :

Example 21 with ClassObjectType

use of org.drools.core.base.ClassObjectType 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 22 with ClassObjectType

use of org.drools.core.base.ClassObjectType 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)

Example 23 with ClassObjectType

use of org.drools.core.base.ClassObjectType 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 ClassObjectType

use of org.drools.core.base.ClassObjectType in project drools by kiegroup.

the class PropertySpecificTest method testComplexBetaSharedAlphaWithWatchesRemoveR2.

@Test
public void testComplexBetaSharedAlphaWithWatchesRemoveR2() {
    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", "r1");
    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", "j"), sp), alphaNode1.getInferredMask());
    // first split
    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());
    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());
    // fist share, second alpha
    AlphaNode alphaNode1_2 = (AlphaNode) alphaNode1.getObjectSinkPropagator().getSinks()[1];
    assertEquals(calculatePositiveMask(classType, list("i"), sp), alphaNode1_2.getDeclaredMask());
    assertEquals(calculatePositiveMask(classType, list("a", "i", "b", "j"), sp), alphaNode1_2.getInferredMask());
    AlphaNode alphaNode1_3 = (AlphaNode) alphaNode1_2.getObjectSinkPropagator().getSinks()[0];
    assertEquals(calculatePositiveMask(classType, list("b"), sp), alphaNode1_3.getDeclaredMask());
    assertEquals(calculatePositiveMask(classType, list("a", "i", "b", "j"), sp), alphaNode1_3.getInferredMask());
    BetaNode betaNode2 = (BetaNode) alphaNode1_3.getObjectSinkPropagator().getSinks()[0];
    assertEquals(calculatePositiveMask(classType, list("j"), sp), betaNode2.getRightDeclaredMask());
    assertEquals(calculatePositiveMask(classType, list("a", "i", "b", "j"), sp), betaNode2.getRightInferredMask());
    assertEquals(calculatePositiveMask(classType, list("k"), sp), betaNode2.getLeftDeclaredMask());
    assertEquals(calculatePositiveMask(classType, list("c", "k"), sp), betaNode2.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) AlphaNode(org.drools.core.reteoo.AlphaNode) Test(org.junit.Test)

Example 25 with ClassObjectType

use of org.drools.core.base.ClassObjectType in project drools by kiegroup.

the class PropertySpecificTest method testComplexBetaSharedAlphaWithWatches.

@Test
public void testComplexBetaSharedAlphaWithWatches() {
    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());
    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", "j"), 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());
    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 share
    AlphaNode alphaNode1_2 = (AlphaNode) alphaNode1.getObjectSinkPropagator().getSinks()[1];
    assertEquals(calculatePositiveMask(classType, list("i"), sp), alphaNode1_2.getDeclaredMask());
    assertEquals(calculatePositiveMask(classType, list("a", "b", "i", "s", "j"), sp), alphaNode1_2.getInferredMask());
    BetaNode betaNode2 = (BetaNode) alphaNode1_2.getObjectSinkPropagator().getSinks()[1];
    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());
    // third share
    AlphaNode alphaNode1_4 = (AlphaNode) alphaNode1_2.getObjectSinkPropagator().getSinks()[0];
    assertEquals(calculatePositiveMask(classType, list("b"), sp), alphaNode1_4.getDeclaredMask());
    assertEquals(calculatePositiveMask(classType, list("a", "b", "i", "j"), sp), alphaNode1_4.getInferredMask());
    BetaNode betaNode3 = (BetaNode) alphaNode1_4.getObjectSinkPropagator().getSinks()[0];
    assertEquals(calculatePositiveMask(classType, list("j"), sp), betaNode3.getRightDeclaredMask());
    assertEquals(calculatePositiveMask(classType, list("a", "i", "b", "j"), sp), betaNode3.getRightInferredMask());
    assertEquals(calculatePositiveMask(classType, list("k"), sp), betaNode3.getLeftDeclaredMask());
    assertEquals(calculatePositiveMask(classType, list("c", "k"), sp), betaNode3.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) AlphaNode(org.drools.core.reteoo.AlphaNode) Test(org.junit.Test)

Aggregations

ClassObjectType (org.drools.core.base.ClassObjectType)123 Test (org.junit.Test)76 Pattern (org.drools.core.rule.Pattern)37 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)34 KieBase (org.kie.api.KieBase)32 Declaration (org.drools.core.rule.Declaration)28 ObjectType (org.drools.core.spi.ObjectType)28 InternalReadAccessor (org.drools.core.spi.InternalReadAccessor)24 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)23 BetaNode (org.drools.core.reteoo.BetaNode)17 AlphaNode (org.drools.core.reteoo.AlphaNode)15 ArrayList (java.util.ArrayList)14 List (java.util.List)13 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)13 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)13 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)13 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)13 Cheese (org.drools.core.test.model.Cheese)13 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)12 InternalFactHandle (org.drools.core.common.InternalFactHandle)12