Search in sources :

Example 11 with Iterator

use of org.drools.core.util.Iterator in project drools by kiegroup.

the class ActivationIteratorTest method testSingleJoinNodePlusEvalnWithSharingWithMixedDormantAndActive.

@Test
public void testSingleJoinNodePlusEvalnWithSharingWithMixedDormantAndActive() {
    String str = "package org.kie.test \n" + "\n" + "rule rule1  salience ( Integer.parseInt( '1'+$s1+'0'+$s2 ) ) when\n" + "    $s1 : String( )\n" + "    $s2 : String( )\n" + "    eval( 1 == 1 ) \n" + "then\n" + "end\n" + "rule rule2 salience (1020 + Integer.parseInt( $s1 ) ) when\n" + "    $s1 : String( )\n" + "then\n" + "    kcontext.getKieRuntime().halt();\n" + "end\n" + "rule rule3  salience ( Integer.parseInt( '1'+$s1+'1'+$s2  ) ) when\n" + "    $s1 : String( )\n" + "    $s2 : String( )\n" + "    $s3 : String( )\n" + "    eval( 1 == 1 ) \n" + "then\n" + "end\n" + "\n";
    KieSession ksession = new KieHelper().addContent(str, ResourceType.DRL).build().newKieSession();
    for (int i = 0; i < 2; 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()) {
        if (act.getRule().getName().equals("rule3")) {
            list.add(act.getRule().getName() + ":" + act.getDeclarationValue("$s1") + ":" + act.getDeclarationValue("$s2") + ":" + act.getDeclarationValue("$s3") + ":" + act.isQueued());
        } else if (act.getRule().getName().equals("rule1")) {
            list.add(act.getRule().getName() + ":" + act.getDeclarationValue("$s1") + ":" + act.getDeclarationValue("$s2") + ":" + act.isQueued());
        } else if (act.getRule().getName().equals("rule2")) {
            list.add(act.getRule().getName() + ":" + act.getDeclarationValue("$s1") + ":" + act.isQueued());
        }
    }
    assertContains(new String[] { "rule1:0:0:true", "rule1:0:1:true", "rule1:1:0:false", "rule1:1:1:false", "rule2:1:false", "rule2:0:true", "rule3:0:0:0:true", "rule3:0:0:1:true", "rule3:1:0:0:false", "rule3:1:0:1:false", "rule3:0:1:0:true", "rule3:0:1:1:true", "rule3:1:1:0:false", "rule3:1:1:1: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 Iterator

use of org.drools.core.util.Iterator in project drools by kiegroup.

the class ActivationIteratorTest method testFromnSharingWithMixedDormantAndActive.

@Test
public void testFromnSharingWithMixedDormantAndActive() {
    String str = "package org.kie.test \n" + "global java.util.List list \n" + "\n" + "rule rule3 salience ( Integer.parseInt( $s1+'1' ) ) when\n" + "    $s1 : String( ) from list  \n" + "    eval( 1 == 1 ) \n" + "    eval( 1 == 1 ) \n" + "then\n" + "    kcontext.getKieRuntime().halt();\n" + "end\n" + "rule rule1 salience ( Integer.parseInt( $s1+'1' ) )  when\n" + "    $s1 : String( this == '1' )  from list\n" + "then\n" + "end\n" + "rule rule2  salience ( Integer.parseInt( $s1+'1' ) ) when\n" + "    $s1 : String( )  from list \n" + "    eval( 1 == 1 ) \n" + "then\n" + "end\n" + "\n";
    KieSession ksession = new KieHelper().addContent(str, ResourceType.DRL).build().newKieSession();
    List list = new ArrayList();
    list.add("0");
    list.add("1");
    list.add("2");
    ksession.setGlobal("list", list);
    ksession.fireAllRules();
    Iterator it = ActivationIterator.iterator(ksession);
    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:1:true", "rule2:0:true", "rule2:1:true", "rule2:2:true", "rule3:0:true", "rule3:1:true", "rule3:2:false" }, list);
}
Also used : ArrayList(java.util.ArrayList) ActivationIterator(org.drools.core.common.ActivationIterator) Iterator(org.drools.core.util.Iterator) 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 Iterator

use of org.drools.core.util.Iterator 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 14 with Iterator

use of org.drools.core.util.Iterator 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 15 with Iterator

use of org.drools.core.util.Iterator in project drools by kiegroup.

the class TerminalNodeIteratorTest method testTerminalNodeListener.

@Test
public void testTerminalNodeListener() {
    String str = "package org.kie.test \n" + "\n" + "rule rule1 when\n" + "then\n" + "end\n" + "rule rule2 when\n" + "then\n" + "end\n" + "rule rule3 when\n" + "    Object()" + "then\n" + "end\n" + "rule rule4 when\n" + "    Object()" + "then\n" + "end\n" + // this will result in two terminal nodes
    "rule rule5 when\n" + "    Object() or\n" + "    Object()\n" + "then\n" + "end\n";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    List<String> nodes = new ArrayList<String>();
    Iterator it = TerminalNodeIterator.iterator(kbase);
    for (TerminalNode node = (TerminalNode) it.next(); node != null; node = (TerminalNode) it.next()) {
        nodes.add(((RuleTerminalNode) node).getRule().getName());
    }
    assertEquals(6, nodes.size());
    assertTrue(nodes.contains("rule1"));
    assertTrue(nodes.contains("rule2"));
    assertTrue(nodes.contains("rule3"));
    assertTrue(nodes.contains("rule4"));
    assertTrue(nodes.contains("rule5"));
    int first = nodes.indexOf("rule5");
    int second = nodes.lastIndexOf("rule5");
    assertTrue(first != second);
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ArrayList(java.util.ArrayList) TerminalNodeIterator(org.drools.core.common.TerminalNodeIterator) Iterator(org.drools.core.util.Iterator) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) TerminalNode(org.drools.core.reteoo.TerminalNode) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Test(org.junit.Test)

Aggregations

Iterator (org.drools.core.util.Iterator)33 Test (org.junit.Test)24 KieSession (org.kie.api.runtime.KieSession)23 ArrayList (java.util.ArrayList)19 List (java.util.List)13 AgendaItem (org.drools.core.common.AgendaItem)13 ObjectHashMap (org.drools.core.util.ObjectHashMap)13 KieHelper (org.kie.internal.utils.KieHelper)13 ActivationIterator (org.drools.core.common.ActivationIterator)12 EqualityKey (org.drools.core.common.EqualityKey)10 NamedEntryPoint (org.drools.core.common.NamedEntryPoint)10 TruthMaintenanceSystem (org.drools.core.common.TruthMaintenanceSystem)10 FactType (org.kie.api.definition.type.FactType)10 Tuple (org.drools.core.spi.Tuple)4 FastIterator (org.drools.core.util.FastIterator)4 InternalFactHandle (org.drools.core.common.InternalFactHandle)3 ObjectEntry (org.drools.core.util.ObjectHashMap.ObjectEntry)3 FactHandle (org.kie.api.runtime.rule.FactHandle)3 AccumulateNode (org.drools.core.reteoo.AccumulateNode)2 AccumulateContext (org.drools.core.reteoo.AccumulateNode.AccumulateContext)2