Search in sources :

Example 41 with InternalWorkingMemory

use of org.drools.core.common.InternalWorkingMemory in project drools by kiegroup.

the class ParallelEvaluationTest method testWithUpdates.

@Test(timeout = 10000L)
public void testWithUpdates() {
    StringBuilder sb = new StringBuilder(400);
    sb.append("global java.util.List list;\n");
    for (int i = 0; i < 10; i++) {
        sb.append(getRule(i, ""));
    }
    KieSession ksession = new KieHelper().addContent(sb.toString(), ResourceType.DRL).build(MultithreadEvaluationOption.YES).newKieSession();
    assertTrue(((InternalWorkingMemory) ksession).getAgenda().isParallelAgenda());
    List<Integer> list = new DebugList<Integer>();
    ksession.setGlobal("list", list);
    FactHandle[] fhs = new FactHandle[10];
    for (int i = 0; i < 10; i++) {
        fhs[i] = ksession.insert(i);
        ksession.insert("" + i);
    }
    ksession.fireAllRules();
    assertEquals(10, list.size());
    list.clear();
    for (int i = 0; i < 10; i++) {
        ksession.update(fhs[i], i);
    }
    ksession.fireAllRules();
    assertEquals(10, list.size());
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FactHandle(org.kie.api.runtime.rule.FactHandle) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) DebugList(org.drools.compiler.util.debug.DebugList) Test(org.junit.Test)

Example 42 with InternalWorkingMemory

use of org.drools.core.common.InternalWorkingMemory in project drools by kiegroup.

the class PropertySpecificTest method testRTNodeEmptyLHS.

@Test
public void testRTNodeEmptyLHS() {
    String rule = "package org.drools.compiler.integrationtests\n" + "rule r1\n" + "when\n" + "then\n" + "end\n";
    KieBase kbase = loadKnowledgeBaseFromString(rule);
    InternalWorkingMemory wm = ((InternalWorkingMemory) kbase.newKieSession());
    ObjectTypeNode otn = getObjectTypeNode(kbase, "InitialFactImpl");
    assertNotNull(otn);
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) otn.getObjectSinkPropagator().getSinks()[0];
    RuleTerminalNode rtNode = (RuleTerminalNode) liaNode.getSinkPropagator().getSinks()[0];
    assertEquals(AllSetBitMask.get(), rtNode.getDeclaredMask());
    assertEquals(AllSetBitMask.get(), rtNode.getInferredMask());
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) 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 43 with InternalWorkingMemory

use of org.drools.core.common.InternalWorkingMemory 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 44 with InternalWorkingMemory

use of org.drools.core.common.InternalWorkingMemory 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 45 with InternalWorkingMemory

use of org.drools.core.common.InternalWorkingMemory 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

InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)147 Test (org.junit.Test)107 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)72 KieBase (org.kie.api.KieBase)53 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)47 ArrayList (java.util.ArrayList)46 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)45 SegmentMemory (org.drools.core.reteoo.SegmentMemory)42 List (java.util.List)39 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)36 BetaMemory (org.drools.core.reteoo.BetaMemory)33 KieSession (org.kie.api.runtime.KieSession)32 JoinNode (org.drools.core.reteoo.JoinNode)30 PathMemory (org.drools.core.reteoo.PathMemory)29 ClassObjectType (org.drools.core.base.ClassObjectType)26 LiaNodeMemory (org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory)25 FactHandle (org.kie.api.runtime.rule.FactHandle)23 BetaNode (org.drools.core.reteoo.BetaNode)22 KieHelper (org.kie.internal.utils.KieHelper)22 AlphaNode (org.drools.core.reteoo.AlphaNode)19