Search in sources :

Example 11 with AgendaItem

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

the class ActivationIteratorTest method testAccnSharingWithMixedDormantAndActive.

@Test
public void testAccnSharingWithMixedDormantAndActive() {
    String str = "package org.kie.test \n" + "\n" + "rule rule1 @Propagation(EAGER) when\n" + "    $s1 : Integer() from accumulate( $i : Integer(), sum ( $i ) )    " + "then\n" + "end\n" + "rule rule2 @Propagation(EAGER) when\n" + "    $s1 : Integer() from accumulate( $i : Integer(), sum ( $i ) )    " + "    eval( 1 == 1 ) \n" + "then\n" + "end\n" + "rule rule3  salience 10 when\n" + "    eval( 1 == 1 ) \n" + "    $s1 : Integer() from accumulate( $i : Integer(), sum ( $i ) )    " + "    eval( 1 == 1 ) \n" + "then\n" + "    kcontext.getKieRuntime().halt();\n" + "end\n" + "\n";
    KieSession ksession = new KieHelper().addContent(str, ResourceType.DRL).build().newKieSession();
    ksession.insert(new Integer(1));
    ksession.insert(new Integer(2));
    ksession.insert(new Integer(3));
    ksession.fireAllRules();
    Iterator it = ActivationIterator.iterator(ksession);
    List list = new ArrayList();
    list = new ArrayList();
    for (AgendaItem act = (AgendaItem) it.next(); act != null; act = (AgendaItem) it.next()) {
        list.add(act.getRule().getName() + ":" + act.getDeclarationValue("$s1") + ":" + act.isQueued());
    }
    assertContains(new String[] { "rule1:6:true", "rule2:6:true", "rule3:6:false" }, list);
}
Also used : ActivationIterator(org.drools.core.common.ActivationIterator) Iterator(org.drools.core.util.Iterator) ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) AgendaItem(org.drools.core.common.AgendaItem) Test(org.junit.Test)

Example 12 with AgendaItem

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

the class ActivationIteratorTest method testExistsSharingWithMixedDormantAndActive.

@Test
public void testExistsSharingWithMixedDormantAndActive() {
    String str = "package org.kie.test \n" + "\n" + "rule rule3  @Propagation(EAGER)  salience ( Integer.parseInt( $s1+'1' ) ) when\n" + "    $s1 : String( )\n" + "    exists String( this == '1' )\n" + "    eval( 1 == 1 ) \n" + "    eval( 1 == 1 ) \n" + "then\n" + "    kcontext.getKieRuntime().halt();\n" + "end\n" + "rule rule1  @Propagation(EAGER)  salience 100 when\n" + "    exists String( this == '1' )\n" + "then\n" + "end\n" + "rule rule2   @Propagation(EAGER)  salience ( Integer.parseInt( $s1+'1' ) ) when\n" + "    exists String( this == '1' )\n" + "    $s1 : String( )\n" + "    eval( 1 == 1 ) \n" + "then\n" + "end\n" + "\n";
    KieSession ksession = new KieHelper().addContent(str, ResourceType.DRL).build().newKieSession();
    ksession.insert("0");
    ksession.insert("1");
    ksession.insert("2");
    ksession.fireAllRules();
    Iterator it = ActivationIterator.iterator(ksession);
    List list = new ArrayList();
    for (AgendaItem act = (AgendaItem) it.next(); act != null; act = (AgendaItem) it.next()) {
        if (act.getRule().getName().equals("rule3")) {
            list.add(act.getRule().getName() + ":" + act.getDeclarationValue("$s1") + ":" + act.isQueued());
        } else if (act.getRule().getName().equals("rule1")) {
            list.add(act.getRule().getName() + ":" + act.isQueued());
        } else if (act.getRule().getName().equals("rule2")) {
            list.add(act.getRule().getName() + ":" + act.getDeclarationValue("$s1") + ":" + act.isQueued());
        }
    }
    assertContains(new String[] { "rule1:false", "rule2:0:true", "rule2:1:true", "rule2:2:true", "rule3:2:false" }, list);
}
Also used : ActivationIterator(org.drools.core.common.ActivationIterator) Iterator(org.drools.core.util.Iterator) ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) AgendaItem(org.drools.core.common.AgendaItem) Test(org.junit.Test)

Example 13 with AgendaItem

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

the class LinkingTest method testSubnetwork.

@Test
public void testSubnetwork() throws Exception {
    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 += "import " + F.class.getCanonicalName() + "\n";
    str += "import " + G.class.getCanonicalName() + "\n";
    str += "global java.util.List list \n";
    str += "rule rule1 when \n";
    str += "   $a : A() \n";
    str += "   exists ( B() and C() ) \n";
    str += "   $e : D() \n";
    str += "then \n";
    str += "  list.add( 'x' ); \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 aotn = getObjectTypeNode(kbase, A.class);
    ObjectTypeNode botn = getObjectTypeNode(kbase, A.class);
    ObjectTypeNode cotn = getObjectTypeNode(kbase, A.class);
    InternalWorkingMemory wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
    List list = new ArrayList();
    wm.setGlobal("list", list);
    wm.insert(new A());
    wm.insert(new B());
    for (int i = 0; i < 28; i++) {
        wm.insert(new C());
    }
    wm.insert(new D());
    wm.flushPropagations();
    InternalAgenda agenda = (InternalAgenda) wm.getAgenda();
    InternalAgendaGroup group = (InternalAgendaGroup) agenda.getNextFocus();
    AgendaItem item = (AgendaItem) group.remove();
    RuleExecutor ruleExecutor = ((RuleAgendaItem) item).getRuleExecutor();
    int count = ruleExecutor.evaluateNetworkAndFire(wm, null, 0, -1);
    // assertEquals(3, count );
    wm.fireAllRules();
    assertEquals(1, list.size());
    wm.fireAllRules();
    // check it doesn't double fire
    assertEquals(1, list.size());
}
Also used : InternalAgendaGroup(org.drools.core.common.InternalAgendaGroup) RuleExecutor(org.drools.core.phreak.RuleExecutor) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) ArrayList(java.util.ArrayList) AgendaItem(org.drools.core.common.AgendaItem) RuleAgendaItem(org.drools.core.phreak.RuleAgendaItem) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) InternalAgenda(org.drools.core.common.InternalAgenda) RuleAgendaItem(org.drools.core.phreak.RuleAgendaItem) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ArrayList(java.util.ArrayList) List(java.util.List) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 14 with AgendaItem

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

the class ActiveActivationsIteratorTest method testActiveActivationsIteratorTest.

@Test
public void testActiveActivationsIteratorTest() {
    String str = "package org.kie.test \n" + "\n" + "rule rule0 @Propagation(EAGER) agenda-group 'a1' salience ( Integer.parseInt('1'+$s) ) when\n" + "    $s : String( this != 'xx' )\n" + "then\n" + "end\n" + "rule rule1 @Propagation(EAGER) agenda-group 'a2' salience ( Integer.parseInt('1'+$s)) when\n" + "    $s : String( this != 'xx' )\n" + "    eval( Integer.parseInt( $s ) <= 2 ) \n" + "then\n" + "end\n" + "rule rule2 @Propagation(EAGER) agenda-group 'a3' salience ( Integer.parseInt('1'+$s)) when\n" + "    $s : String( this != 'xx' )\n" + "    eval( Integer.parseInt( $s ) <= 2 ) \n" + "then\n" + "    kcontext.getKieRuntime().halt();\n" + "end\n" + "rule rule3 @Propagation(EAGER) ruleflow-group 'r1' salience ( Integer.parseInt('1'+$s)) when\n" + "    $s : String( this != 'xx' )\n" + "    eval( Integer.parseInt( $s ) > 2 ) \n" + "then\n" + "end\n" + "rule rule4 @Propagation(EAGER) ruleflow-group 'r1' salience ( Integer.parseInt('1'+$s) ) when\n" + "    $s : String( this != 'xx' )\n" + "    eval( Integer.parseInt( $s ) > 2 ) \n" + "    eval( Integer.parseInt( $s ) > 3 ) \n" + "then\n" + "end\n" + "rule rule6 @Propagation(EAGER) when\n" + "     java.util.Map()\n" + "then\n" + "end\n" + "\n" + "rule rule7 @Propagation(EAGER) when\n" + "    $s : String( this != 'xx' )\n" + "then\n" + "end\n" + "\n";
    KieSession ksession = new KieHelper().addContent(str, ResourceType.DRL).build().newKieSession();
    for (int i = 0; i < 3; i++) {
        ksession.insert(new String("" + i));
    }
    ((InternalWorkingMemory) ksession).flushPropagations();
    ((InternalAgenda) ksession.getAgenda()).evaluateEagerList();
    Iterator it = ActiveActivationIterator.iterator(ksession);
    List list = new ArrayList();
    for (AgendaItem act = (AgendaItem) it.next(); act != null; act = (AgendaItem) it.next()) {
        list.add(act.getRule().getName() + ":" + act.getDeclarationValue("$s") + ":" + act.isQueued());
    }
    assertContains(new String[] { "rule7:2:true", "rule7:0:true", "rule7:1:true", "rule0:2:true", "rule0:0:true", "rule0:1:true", "rule1:2:true", "rule1:0:true", "rule1:1:true", "rule2:2:true", "rule2:0:true", "rule2:1:true" }, list);
    ksession.fireAllRules();
    it = ActiveActivationIterator.iterator(ksession);
    list = new ArrayList();
    for (AgendaItem act = (AgendaItem) it.next(); act != null; act = (AgendaItem) it.next()) {
        list.add(act.getRule().getName() + ":" + act.getDeclarationValue("$s") + ":" + act.isQueued());
    }
    assertContains(new String[] { "rule0:2:true", "rule0:0:true", "rule0:1:true", "rule1:2:true", "rule1:0:true", "rule1:1:true", "rule2:2:true", "rule2:0:true", "rule2:1:true" }, list);
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) InternalAgenda(org.drools.core.common.InternalAgenda) ActiveActivationIterator(org.drools.core.common.ActiveActivationIterator) Iterator(org.drools.core.util.Iterator) ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) AgendaItem(org.drools.core.common.AgendaItem) Test(org.junit.Test)

Example 15 with AgendaItem

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

the class ActivationIteratorTest method testLianPlusEvalnWithSharingWithMixedDormantAndActive.

@Test
public void testLianPlusEvalnWithSharingWithMixedDormantAndActive() {
    // Rule 0 single LiaNode
    // Rule 1 and 2 are shared
    // Rule 3 shares the LIANode with 1 and 2
    // Rule 4 Shares the eval with 3
    String str = "package org.kie.test \n" + "\n" + "rule rule2 salience ( Integer.parseInt('1'+$s)) when\n" + "    $s : String( this != 'xx' )\n" + "    eval( Integer.parseInt( $s ) <= 2 ) \n" + "then\n" + "    kcontext.getKieRuntime().halt();\n" + "end\n" + "rule rule0 salience ( Integer.parseInt('1'+$s) ) when\n" + "    $s : String( this != 'xx' )\n" + "then\n" + "end\n" + "rule rule1 salience ( Integer.parseInt('1'+$s)) when\n" + "    $s : String( this != 'xx' )\n" + "    eval( Integer.parseInt( $s ) <= 2 ) \n" + "then\n" + "end\n" + "rule rule3 salience ( Integer.parseInt('1'+$s)) when\n" + "    $s : String( this != 'xx' )\n" + "    eval( Integer.parseInt( $s ) > 2 ) \n" + "then\n" + "end\n" + "rule rule4 salience ( Integer.parseInt('1'+$s) ) when\n" + "    $s : String( this != 'xx' )\n" + "    eval( Integer.parseInt( $s ) > 2 ) \n" + "    eval( Integer.parseInt( $s ) > 3 ) \n" + "then\n" + "end\n" + "\n" + "rule rule6 when\n" + "     java.util.Map()\n" + "then\n" + "end\n" + "\n";
    KieSession ksession = new KieHelper().addContent(str, ResourceType.DRL).build().newKieSession();
    for (int i = 0; i < 5; i++) {
        ksession.insert(new String("" + i));
    }
    ksession.fireAllRules();
    Iterator it = ActivationIterator.iterator(ksession);
    List list = new ArrayList();
    for (AgendaItem act = (AgendaItem) it.next(); act != null; act = (AgendaItem) it.next()) {
        list.add(act.getRule().getName() + ":" + act.getDeclarationValue("$s") + ":" + act.isQueued());
    }
    assertContains(new String[] { "rule0:0:true", "rule0:1:true", "rule0:2:true", "rule0:3:false", "rule0:4:false", "rule1:0:true", "rule1:1:true", "rule1:2:true", "rule2:0:true", "rule2:1:true", "rule2:2:false", "rule3:3:false", "rule3:4:false", "rule3:4:false" }, list);
}
Also used : ActivationIterator(org.drools.core.common.ActivationIterator) Iterator(org.drools.core.util.Iterator) ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) AgendaItem(org.drools.core.common.AgendaItem) Test(org.junit.Test)

Aggregations

AgendaItem (org.drools.core.common.AgendaItem)27 Test (org.junit.Test)17 ArrayList (java.util.ArrayList)15 List (java.util.List)14 Iterator (org.drools.core.util.Iterator)13 KieSession (org.kie.api.runtime.KieSession)13 KieHelper (org.kie.internal.utils.KieHelper)13 ActivationIterator (org.drools.core.common.ActivationIterator)12 RuleAgendaItem (org.drools.core.phreak.RuleAgendaItem)6 Declaration (org.drools.core.rule.Declaration)6 InternalFactHandle (org.drools.core.common.InternalFactHandle)5 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)5 HashMap (java.util.HashMap)3 DefaultKnowledgeHelper (org.drools.core.base.DefaultKnowledgeHelper)3 AgendaItemImpl (org.drools.core.common.AgendaItemImpl)3 InternalAgenda (org.drools.core.common.InternalAgenda)3 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)3 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)3 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)3 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)3