Search in sources :

Example 6 with AgendaItem

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

the class DefaultKnowledgeHelper method blockMatch.

public void blockMatch(Match act) {
    AgendaItem targetMatch = (AgendaItem) act;
    // iterate to find previous equal logical insertion
    LogicalDependency<SimpleMode> dep = null;
    if (this.previousBlocked != null) {
        for (dep = this.previousBlocked.getFirst(); dep != null; dep = dep.getNext()) {
            if (targetMatch == dep.getJustified()) {
                this.previousBlocked.remove(dep);
                break;
            }
        }
    }
    if (dep == null) {
        SimpleMode mode = new SimpleMode();
        dep = new SimpleLogicalDependency(activation, targetMatch, mode);
        mode.setObject(dep);
    }
    this.activation.addBlocked(dep);
    if (targetMatch.getBlockers().size() == 1 && targetMatch.isQueued()) {
        if (targetMatch.getRuleAgendaItem() == null) {
            // it wasn't blocked before, but is now, so we must remove it from all groups, so it cannot be executed.
            targetMatch.remove();
        } else {
            targetMatch.getRuleAgendaItem().getRuleExecutor().removeLeftTuple(targetMatch.getTuple());
        }
        if (targetMatch.getActivationGroupNode() != null) {
            targetMatch.getActivationGroupNode().getActivationGroup().removeActivation(targetMatch);
        }
        if (targetMatch.getActivationNode() != null) {
            final InternalRuleFlowGroup ruleFlowGroup = (InternalRuleFlowGroup) targetMatch.getActivationNode().getParentContainer();
            ruleFlowGroup.remove(targetMatch);
        }
    }
}
Also used : SimpleLogicalDependency(org.drools.core.beliefsystem.simple.SimpleLogicalDependency) RuleAgendaItem(org.drools.core.phreak.RuleAgendaItem) AgendaItem(org.drools.core.common.AgendaItem) InternalRuleFlowGroup(org.drools.core.common.InternalRuleFlowGroup) SimpleMode(org.drools.core.beliefsystem.simple.SimpleMode)

Example 7 with AgendaItem

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

the class DefaultKnowledgeHelper method cancelRemainingPreviousLogicalDependencies.

public void cancelRemainingPreviousLogicalDependencies() {
    if (this.previousJustified != null) {
        for (LogicalDependency<T> dep = this.previousJustified.getFirst(); dep != null; dep = dep.getNext()) {
            TruthMaintenanceSystemHelper.removeLogicalDependency(dep, activation.getPropagationContext());
        }
    }
    if (this.previousBlocked != null) {
        for (LogicalDependency<SimpleMode> dep = this.previousBlocked.getFirst(); dep != null; ) {
            LogicalDependency<SimpleMode> tmp = dep.getNext();
            this.previousBlocked.remove(dep);
            AgendaItem justified = (AgendaItem) dep.getJustified();
            justified.getBlockers().remove((SimpleMode) dep.getMode());
            if (justified.getBlockers().isEmpty()) {
                RuleAgendaItem ruleAgendaItem = justified.getRuleAgendaItem();
                workingMemory.getAgenda().stageLeftTuple(ruleAgendaItem, justified);
            }
            dep = tmp;
        }
    }
}
Also used : RuleAgendaItem(org.drools.core.phreak.RuleAgendaItem) RuleAgendaItem(org.drools.core.phreak.RuleAgendaItem) AgendaItem(org.drools.core.common.AgendaItem) SimpleMode(org.drools.core.beliefsystem.simple.SimpleMode)

Example 8 with AgendaItem

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

the class ActivationIteratorTest method testSingleJoinNode.

@Test
public void testSingleJoinNode() {
    String str = "package org.kie.test \n" + "\n" + "rule rule1  @Propagation(EAGER)  when\n" + "    $s1 : String( )\n" + "    $s2 : String( )\n" + "then\n" + "end\n";
    KieSession ksession = new KieHelper().addContent(str, ResourceType.DRL).build().newKieSession();
    for (int i = 0; i < 2; i++) {
        ksession.insert(new String("" + i));
    }
    evaluateEagerList(ksession);
    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("$s1") + ":" + act.getDeclarationValue("$s2") + ":" + act.isQueued());
    }
    assertContains(new String[] { "rule1:0:1:true", "rule1:1:0:true", "rule1:1:1:true", "rule1:0:0:true" }, list);
    ksession.fireAllRules();
    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.getDeclarationValue("$s2") + ":" + act.isQueued());
    }
    assertContains(new String[] { "rule1:0:1:false", "rule1:1:0:false", "rule1:1:1:false", "rule1:0:0: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 9 with AgendaItem

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

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

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