Search in sources :

Example 1 with Match

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

the class RuleEventListenerTest method testRuleEventListener.

@Test
public void testRuleEventListener() {
    String str = "import " + Person.class.getCanonicalName() + "\n" + "global java.util.List list\n" + "rule R when\n" + "  $p: Person( $age: age < 20 )\n" + "then\n" + "  modify($p) { setAge( $age + 1 ) };" + "end\n";
    KieSession ksession = new KieHelper().addContent(str, ResourceType.DRL).build().newKieSession();
    List<String> list = new ArrayList<>();
    ((RuleEventManager) ksession).addEventListener(new RuleEventListener() {

        @Override
        public void onBeforeMatchFire(Match match) {
            list.add("onBeforeMatchFire: " + match.getDeclarationValue("$age"));
        }

        @Override
        public void onAfterMatchFire(Match match) {
            list.add("onAfterMatchFire: " + match.getDeclarationValue("$age"));
        }

        @Override
        public void onDeleteMatch(Match match) {
            list.add("onDeleteMatch: " + match.getDeclarationValue("$age"));
        }

        @Override
        public void onUpdateMatch(Match match) {
            list.add("onUpdateMatch: " + match.getDeclarationValue("$age"));
        }
    });
    ksession.insert(new Person("John Smith", 18));
    ksession.fireAllRules();
    List<String> expected = Arrays.asList("onBeforeMatchFire: 18", "onAfterMatchFire: 19", "onUpdateMatch: 19", "onBeforeMatchFire: 19", "onAfterMatchFire: 20", "onDeleteMatch: 20");
    assertEquals(expected, list);
}
Also used : RuleEventManager(org.kie.internal.event.rule.RuleEventManager) ArrayList(java.util.ArrayList) RuleEventListener(org.kie.internal.event.rule.RuleEventListener) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.compiler.Person) Match(org.kie.api.runtime.rule.Match) Test(org.junit.Test)

Example 2 with Match

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

the class AddRuleTest method testPopulatedSharedLiaNode.

@Test
public void testPopulatedSharedLiaNode() throws Exception {
    InternalKnowledgeBase kbase1 = buildKnowledgeBase("r1", "   A() B(1;) 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 A(3));
    wm.insert(new B(1));
    wm.insert(new B(2));
    wm.insert(new C(1));
    wm.insert(new D(1));
    wm.insert(new E(1));
    wm.fireAllRules();
    assertEquals(3, list.size());
    kbase1.addPackages(buildKnowledgePackage("r2", "   a : A() B(2;) C() D() E()\n"));
    ObjectTypeNode aotn = getObjectTypeNode(kbase1, A.class);
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) aotn.getObjectSinkPropagator().getSinks()[0];
    JoinNode bNode1 = (JoinNode) liaNode.getSinkPropagator().getFirstLeftTupleSink();
    JoinNode bNode2 = (JoinNode) liaNode.getSinkPropagator().getLastLeftTupleSink();
    BetaMemory bm = (BetaMemory) wm.getNodeMemory(bNode2);
    SegmentMemory sm = bm.getSegmentMemory();
    assertNotNull(sm.getStagedLeftTuples().getInsertFirst());
    assertNotNull(sm.getStagedLeftTuples().getInsertFirst().getStagedNext());
    assertNotNull(sm.getStagedLeftTuples().getInsertFirst().getStagedNext().getStagedNext());
    assertNull(sm.getStagedLeftTuples().getInsertFirst().getStagedNext().getStagedNext().getStagedNext());
    wm.fireAllRules();
    assertNull(sm.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("r2", ((Match) list.get(4)).getRule().getName());
    assertEquals("r2", ((Match) list.get(5)).getRule().getName());
    List results = new ArrayList();
    results.add(((A) ((Match) list.get(3)).getDeclarationValue("a")).getObject());
    results.add(((A) ((Match) list.get(4)).getDeclarationValue("a")).getObject());
    results.add(((A) ((Match) list.get(5)).getDeclarationValue("a")).getObject());
    assertTrue(results.containsAll(asList(1, 2, 3)));
}
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) 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 3 with Match

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

the class AddRuleTest method testPopulatedMultipleShares.

@Test
public void testPopulatedMultipleShares() throws Exception {
    InternalKnowledgeBase kbase1 = buildKnowledgeBase("r1", "   A(1;)  A(2;) B(1;) B(2;) 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(2));
    wm.insert(new A(3));
    wm.insert(new B(1));
    wm.insert(new B(2));
    wm.insert(new C(1));
    wm.insert(new C(2));
    wm.insert(new D(1));
    wm.insert(new E(1));
    wm.fireAllRules();
    assertEquals(2, list.size());
    kbase1.addPackages(buildKnowledgePackage("r2", "   A(1;)  A(2;) B(1;) B(2;) C(2;) D() E()\n"));
    kbase1.addPackages(buildKnowledgePackage("r3", "   A(1;)  A(3;) B(1;) B(2;) C(2;) D() E()\n"));
    wm.fireAllRules();
    System.out.println(list);
    assertEquals(5, list.size());
    assertEquals("r1", ((Match) list.get(0)).getRule().getName());
    assertEquals("r1", ((Match) list.get(1)).getRule().getName());
    // only one A3
    assertEquals("r3", ((Match) list.get(2)).getRule().getName());
    assertEquals("r2", ((Match) list.get(3)).getRule().getName());
    assertEquals("r2", ((Match) list.get(4)).getRule().getName());
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Match(org.kie.api.runtime.rule.Match) Test(org.junit.Test)

Example 4 with Match

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

the class AddRuleTest method testPopulatedRuleWithEvals.

@Test
public void testPopulatedRuleWithEvals() throws Exception {
    InternalKnowledgeBase kbase1 = buildKnowledgeBase("r1", "   a:A() B() eval(1==1) eval(1==1) C(1;) \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.fireAllRules();
    assertEquals(3, list.size());
    kbase1.addPackages(buildKnowledgePackage("r2", "   a:A() B() eval(1==1) eval(1==1) C(2;) \n"));
    ObjectTypeNode aotn = getObjectTypeNode(kbase1, A.class);
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) aotn.getObjectSinkPropagator().getSinks()[0];
    JoinNode bNode = (JoinNode) liaNode.getSinkPropagator().getFirstLeftTupleSink();
    EvalConditionNode e1 = (EvalConditionNode) bNode.getSinkPropagator().getFirstLeftTupleSink();
    EvalConditionNode e2 = (EvalConditionNode) e1.getSinkPropagator().getFirstLeftTupleSink();
    JoinNode c1Node = (JoinNode) e2.getSinkPropagator().getFirstLeftTupleSink();
    JoinNode c2Node = (JoinNode) e2.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) EvalConditionNode(org.drools.core.reteoo.EvalConditionNode) 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 5 with Match

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

the class RemoveRuleTest method testPopulatedRuleMidwayShare.

@Test
public void testPopulatedRuleMidwayShare() throws Exception {
    InternalKnowledgeBase kbase1 = buildKnowledgeBase("r1", "   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(7, countNodeMemories(wm.getNodeMemories()));
    kbase1.addPackages(buildKnowledgePackage("r2", "   a : A() B() C(2;) D() E()\n"));
    wm.fireAllRules();
    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(3, c2Mem.getLeftTupleMemory().size());
    assertEquals(1, c2Mem.getRightTupleMemory().size());
    assertEquals(6, list.size());
    kbase1.removeRule("org.kie", "r2");
    assertEquals(10, countNodeMemories(wm.getNodeMemories()));
    assertNull(sm.getFirst());
    // c1SMem repoints back to original Smem
    assertSame(sm, c1Mem.getSegmentMemory());
    wm.insert(new A(1));
    wm.fireAllRules();
    assertEquals("r1", ((Match) list.get(6)).getRule().getName());
    // only one more added, as second rule as removed
    assertEquals(7, list.size());
}
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) 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)68 Test (org.junit.Test)60 ArrayList (java.util.ArrayList)57 List (java.util.List)52 KieSession (org.kie.api.runtime.KieSession)37 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)26 KieBase (org.kie.api.KieBase)26 FactHandle (org.kie.api.runtime.rule.FactHandle)24 SegmentMemory (org.drools.core.reteoo.SegmentMemory)22 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)20 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)18 JoinNode (org.drools.core.reteoo.JoinNode)16 LiaNodeMemory (org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory)16 Arrays.asList (java.util.Arrays.asList)15 BetaMemory (org.drools.core.reteoo.BetaMemory)14 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)13 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)11 RuleEventListener (org.kie.internal.event.rule.RuleEventListener)11 RuleEventManager (org.kie.internal.event.rule.RuleEventManager)11 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)10