Search in sources :

Example 26 with Match

use of org.kie.api.runtime.rule.Match in project drools by kiegroup.

the class DefaultAgenda method isRuleInstanceAgendaItem.

public boolean isRuleInstanceAgendaItem(String ruleflowGroupName, String ruleName, long processInstanceId) {
    propagationList.flush();
    RuleFlowGroup systemRuleFlowGroup = this.getRuleFlowGroup(ruleflowGroupName);
    Match[] matches = ((InternalAgendaGroup) systemRuleFlowGroup).getActivations();
    for (Match match : matches) {
        Activation act = (Activation) match;
        if (act.isRuleAgendaItem()) {
            // The lazy RuleAgendaItem must be fully evaluated, to see if there is a rule match
            RuleExecutor ruleExecutor = ((RuleAgendaItem) act).getRuleExecutor();
            ruleExecutor.evaluateNetwork(this);
            TupleList list = ruleExecutor.getLeftTupleList();
            for (RuleTerminalNodeLeftTuple lt = (RuleTerminalNodeLeftTuple) list.getFirst(); lt != null; lt = (RuleTerminalNodeLeftTuple) lt.getNext()) {
                if (ruleName.equals(lt.getRule().getName())) {
                    if (checkProcessInstance(lt, processInstanceId)) {
                        return true;
                    }
                }
            }
        } else {
            if (ruleName.equals(act.getRule().getName())) {
                if (checkProcessInstance(act, processInstanceId)) {
                    return true;
                }
            }
        }
    }
    return false;
}
Also used : RuleFlowGroup(org.drools.core.spi.RuleFlowGroup) TupleList(org.drools.core.util.index.TupleList) RuleAgendaItem(org.drools.core.phreak.RuleAgendaItem) RuleTerminalNodeLeftTuple(org.drools.core.reteoo.RuleTerminalNodeLeftTuple) RuleExecutor(org.drools.core.phreak.RuleExecutor) Activation(org.drools.core.spi.Activation) Match(org.kie.api.runtime.rule.Match)

Example 27 with Match

use of org.kie.api.runtime.rule.Match in project drools by kiegroup.

the class AddRuleTest method testPopulatedSingleRuleNoSharingWithSubnetworkAtStart.

@Test
public void testPopulatedSingleRuleNoSharingWithSubnetworkAtStart() throws Exception {
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    InternalWorkingMemory wm = ((InternalWorkingMemory) kbase.newKieSession());
    wm.insert(new A(1));
    wm.insert(new A(2));
    wm.insert(new D(1));
    wm.insert(new E(1));
    wm.insert(new C(2));
    wm.fireAllRules();
    kbase.addPackages(buildKnowledgePackage("r1", "   A() not( B() and C() ) D() E()\n"));
    List list = new ArrayList();
    wm.setGlobal("list", list);
    ObjectTypeNode aotn = getObjectTypeNode(kbase, A.class);
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) aotn.getObjectSinkPropagator().getSinks()[0];
    LiaNodeMemory lm = (LiaNodeMemory) wm.getNodeMemory(liaNode);
    SegmentMemory sm = lm.getSegmentMemory();
    assertNull(sm.getStagedLeftTuples().getInsertFirst());
    SegmentMemory subSm = sm.getFirst();
    SegmentMemory mainSm = subSm.getNext();
    assertNotNull(subSm.getStagedLeftTuples().getInsertFirst());
    assertNotNull(subSm.getStagedLeftTuples().getInsertFirst().getStagedNext());
    assertNull(subSm.getStagedLeftTuples().getInsertFirst().getStagedNext().getStagedNext());
    assertNotNull(mainSm.getStagedLeftTuples().getInsertFirst());
    assertNotNull(mainSm.getStagedLeftTuples().getInsertFirst().getStagedNext());
    assertNull(mainSm.getStagedLeftTuples().getInsertFirst().getStagedNext().getStagedNext());
    wm.fireAllRules();
    assertNull(subSm.getStagedLeftTuples().getInsertFirst());
    assertNull(mainSm.getStagedLeftTuples().getInsertFirst());
    assertEquals(2, list.size());
    assertEquals("r1", ((Match) list.get(0)).getRule().getName());
}
Also used : SegmentMemory(org.drools.core.reteoo.SegmentMemory) ArrayList(java.util.ArrayList) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) LiaNodeMemory(org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory) Match(org.kie.api.runtime.rule.Match) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Test(org.junit.Test)

Example 28 with Match

use of org.kie.api.runtime.rule.Match in project drools by kiegroup.

the class AddRuleTest method testPopulatedRuleMidwayShare.

@Test
public void testPopulatedRuleMidwayShare() throws Exception {
    InternalKnowledgeBase kbase1 = buildKnowledgeBase("r1", "   a : A() B() C(1;) D() E()\n");
    InternalWorkingMemory wm = ((InternalWorkingMemory) kbase1.newKieSession());
    List list = new ArrayList();
    wm.setGlobal("list", list);
    wm.insert(new A(1));
    wm.insert(new A(2));
    wm.insert(new A(3));
    wm.insert(new B(1));
    wm.insert(new C(1));
    wm.insert(new C(2));
    wm.insert(new D(1));
    wm.insert(new E(1));
    wm.fireAllRules();
    assertEquals(3, list.size());
    kbase1.addPackages(buildKnowledgePackage("r2", "   a : A() B() C(2;) D() E()\n"));
    ObjectTypeNode aotn = getObjectTypeNode(kbase1, A.class);
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) aotn.getObjectSinkPropagator().getSinks()[0];
    JoinNode bNode = (JoinNode) liaNode.getSinkPropagator().getFirstLeftTupleSink();
    JoinNode c1Node = (JoinNode) bNode.getSinkPropagator().getFirstLeftTupleSink();
    JoinNode c2Node = (JoinNode) bNode.getSinkPropagator().getLastLeftTupleSink();
    LiaNodeMemory lm = (LiaNodeMemory) wm.getNodeMemory(liaNode);
    SegmentMemory sm = lm.getSegmentMemory();
    BetaMemory c1Mem = (BetaMemory) wm.getNodeMemory(c1Node);
    assertSame(sm.getFirst(), c1Mem.getSegmentMemory());
    assertEquals(3, c1Mem.getLeftTupleMemory().size());
    assertEquals(1, c1Mem.getRightTupleMemory().size());
    BetaMemory c2Mem = (BetaMemory) wm.getNodeMemory(c2Node);
    SegmentMemory c2Smem = sm.getFirst().getNext();
    assertSame(c2Smem, c2Mem.getSegmentMemory());
    assertEquals(0, c2Mem.getLeftTupleMemory().size());
    assertEquals(0, c2Mem.getRightTupleMemory().size());
    assertNotNull(c2Smem.getStagedLeftTuples().getInsertFirst());
    assertNotNull(c2Smem.getStagedLeftTuples().getInsertFirst().getStagedNext());
    assertNotNull(c2Smem.getStagedLeftTuples().getInsertFirst().getStagedNext().getStagedNext());
    assertNull(c2Smem.getStagedLeftTuples().getInsertFirst().getStagedNext().getStagedNext().getStagedNext());
    wm.fireAllRules();
    assertEquals(3, c2Mem.getLeftTupleMemory().size());
    assertEquals(1, c2Mem.getRightTupleMemory().size());
    assertNull(c2Smem.getStagedLeftTuples().getInsertFirst());
    assertEquals(6, list.size());
    assertEquals("r1", ((Match) list.get(0)).getRule().getName());
    assertEquals("r1", ((Match) list.get(1)).getRule().getName());
    assertEquals("r1", ((Match) list.get(2)).getRule().getName());
    assertEquals("r2", ((Match) list.get(3)).getRule().getName());
    assertEquals(3, ((A) ((Match) list.get(3)).getDeclarationValue("a")).getObject());
    assertEquals("r2", ((Match) list.get(4)).getRule().getName());
    assertEquals(2, ((A) ((Match) list.get(4)).getDeclarationValue("a")).getObject());
    assertEquals("r2", ((Match) list.get(5)).getRule().getName());
    assertEquals(1, ((A) ((Match) list.get(5)).getDeclarationValue("a")).getObject());
}
Also used : SegmentMemory(org.drools.core.reteoo.SegmentMemory) JoinNode(org.drools.core.reteoo.JoinNode) ArrayList(java.util.ArrayList) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) BetaMemory(org.drools.core.reteoo.BetaMemory) LiaNodeMemory(org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory) Match(org.kie.api.runtime.rule.Match) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Test(org.junit.Test)

Example 29 with Match

use of org.kie.api.runtime.rule.Match in project drools by kiegroup.

the class AddRuleTest method testPopulatedSharedToRtn.

@Test
public void testPopulatedSharedToRtn() throws Exception {
    InternalKnowledgeBase kbase1 = buildKnowledgeBase("r1", "   A() B() C() D() E()\n");
    InternalWorkingMemory wm = ((InternalWorkingMemory) kbase1.newKieSession());
    List list = new ArrayList();
    wm.setGlobal("list", list);
    wm.insert(new A(1));
    wm.insert(new A(2));
    wm.insert(new B(1));
    wm.insert(new C(1));
    wm.insert(new D(1));
    wm.insert(new E(1));
    wm.fireAllRules();
    assertEquals(2, list.size());
    kbase1.addPackages(buildKnowledgePackage("r2", "   A() B() C() D() E()\n"));
    ObjectTypeNode eotn = getObjectTypeNode(kbase1, E.class);
    JoinNode eNode = (JoinNode) eotn.getObjectSinkPropagator().getSinks()[0];
    RuleTerminalNode rtn = (RuleTerminalNode) eNode.getSinkPropagator().getLastLeftTupleSink();
    PathMemory pm = (PathMemory) wm.getNodeMemory(rtn);
    SegmentMemory sm = pm.getSegmentMemory();
    assertNotNull(sm.getStagedLeftTuples().getInsertFirst());
    assertNotNull(sm.getStagedLeftTuples().getInsertFirst().getStagedNext());
    assertNull(sm.getStagedLeftTuples().getInsertFirst().getStagedNext().getStagedNext());
    wm.fireAllRules();
    assertNull(sm.getStagedLeftTuples().getInsertFirst());
    assertEquals(4, list.size());
    assertEquals("r1", ((Match) list.get(0)).getRule().getName());
    assertEquals("r1", ((Match) list.get(1)).getRule().getName());
    assertEquals("r2", ((Match) list.get(2)).getRule().getName());
    assertEquals("r2", ((Match) list.get(3)).getRule().getName());
}
Also used : SegmentMemory(org.drools.core.reteoo.SegmentMemory) JoinNode(org.drools.core.reteoo.JoinNode) ArrayList(java.util.ArrayList) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) Match(org.kie.api.runtime.rule.Match) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) PathMemory(org.drools.core.reteoo.PathMemory) Test(org.junit.Test)

Example 30 with Match

use of org.kie.api.runtime.rule.Match in project drools by kiegroup.

the class AddRuleTest method testPopulatedSingleRuleNoSharing.

@Test
public void testPopulatedSingleRuleNoSharing() throws Exception {
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    InternalWorkingMemory wm = ((InternalWorkingMemory) kbase.newKieSession());
    wm.insert(new A(1));
    wm.insert(new B(1));
    wm.insert(new C(1));
    wm.insert(new C(2));
    wm.insert(new D(1));
    wm.insert(new E(1));
    wm.fireAllRules();
    kbase.addPackages(buildKnowledgePackage("r1", "   A() B() C(object == 2) D() E()\n"));
    List list = new ArrayList();
    wm.setGlobal("list", list);
    ObjectTypeNode aotn = getObjectTypeNode(kbase, A.class);
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) aotn.getObjectSinkPropagator().getSinks()[0];
    LiaNodeMemory lm = (LiaNodeMemory) wm.getNodeMemory(liaNode);
    SegmentMemory sm = lm.getSegmentMemory();
    assertNotNull(sm.getStagedLeftTuples().getInsertFirst());
    wm.fireAllRules();
    assertNull(sm.getStagedLeftTuples().getInsertFirst());
    assertEquals(1, list.size());
    assertEquals("r1", ((Match) list.get(0)).getRule().getName());
}
Also used : SegmentMemory(org.drools.core.reteoo.SegmentMemory) ArrayList(java.util.ArrayList) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) LiaNodeMemory(org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory) Match(org.kie.api.runtime.rule.Match) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Test(org.junit.Test)

Aggregations

Match (org.kie.api.runtime.rule.Match)33 Test (org.junit.Test)29 ArrayList (java.util.ArrayList)27 List (java.util.List)26 KieSession (org.kie.api.runtime.KieSession)18 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)13 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)13 FactHandle (org.kie.api.runtime.rule.FactHandle)12 SegmentMemory (org.drools.core.reteoo.SegmentMemory)11 KieBase (org.kie.api.KieBase)11 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)11 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)10 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)9 Arrays.asList (java.util.Arrays.asList)8 JoinNode (org.drools.core.reteoo.JoinNode)8 LiaNodeMemory (org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory)8 BetaMemory (org.drools.core.reteoo.BetaMemory)7 AfterMatchFiredEvent (org.kie.api.event.rule.AfterMatchFiredEvent)5 RuleEventListener (org.kie.internal.event.rule.RuleEventListener)5 RuleEventManager (org.kie.internal.event.rule.RuleEventManager)5