Search in sources :

Example 1 with InternalAgendaGroup

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

the class PhreakRuleTerminalNode method doLeftTupleUpdate.

public static void doLeftTupleUpdate(TerminalNode rtnNode, RuleExecutor executor, InternalAgenda agenda, int salienceInt, Salience salience, LeftTuple leftTuple) {
    PropagationContext pctx = leftTuple.getPropagationContext();
    pctx = RuleTerminalNode.findMostRecentPropagationContext(leftTuple, pctx);
    boolean blocked = false;
    RuleTerminalNodeLeftTuple rtnLeftTuple = (RuleTerminalNodeLeftTuple) leftTuple;
    if (executor.isDeclarativeAgendaEnabled()) {
        if (rtnLeftTuple.getBlockers() != null && !rtnLeftTuple.getBlockers().isEmpty()) {
            // declarativeAgenda still blocking LeftTuple, so don't add back ot list
            blocked = true;
        }
    } else {
        blocked = rtnNode.getRule().isNoLoop() && rtnNode.equals(pctx.getTerminalNodeOrigin());
    }
    InternalWorkingMemory wm = agenda.getWorkingMemory();
    if (salience != null) {
        salienceInt = salience.getValue(new DefaultKnowledgeHelper(rtnLeftTuple, wm), rtnNode.getRule(), wm);
    }
    if (agenda.getActivationsFilter() != null && !agenda.getActivationsFilter().accept(rtnLeftTuple, wm, rtnNode)) {
        // only relevant for serialization, to not re-fire Matches already fired
        return;
    }
    if (!blocked) {
        boolean addToExector = true;
        if (rtnNode.getRule().isLockOnActive() && pctx.getType() != PropagationContext.Type.RULE_ADDITION) {
            long handleRecency = pctx.getFactHandle().getRecency();
            InternalAgendaGroup agendaGroup = executor.getRuleAgendaItem().getAgendaGroup();
            if (blockedByLockOnActive(rtnNode.getRule(), pctx, handleRecency, agendaGroup)) {
                addToExector = false;
            }
        }
        if (addToExector) {
            if (!rtnLeftTuple.isQueued()) {
                // not queued, so already fired, so it's effectively recreated
                EventSupport es = (EventSupport) wm;
                es.getAgendaEventSupport().fireActivationCreated(rtnLeftTuple, wm);
                rtnLeftTuple.update(salienceInt, pctx);
                executor.addLeftTuple(leftTuple);
                wm.getRuleEventSupport().onUpdateMatch(rtnLeftTuple);
            }
        }
    } else {
        // LeftTuple is blocked, and thus not queued, so just update it's values
        rtnLeftTuple.update(salienceInt, pctx);
    }
    if (!rtnNode.isFireDirect() && executor.isDeclarativeAgendaEnabled()) {
        agenda.modifyActivation(rtnLeftTuple, rtnLeftTuple.isQueued());
    }
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) InternalAgendaGroup(org.drools.core.common.InternalAgendaGroup) PropagationContext(org.drools.core.spi.PropagationContext) EventSupport(org.drools.core.common.EventSupport) RuleTerminalNodeLeftTuple(org.drools.core.reteoo.RuleTerminalNodeLeftTuple) DefaultKnowledgeHelper(org.drools.core.base.DefaultKnowledgeHelper)

Example 2 with InternalAgendaGroup

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

the class ScenarioRunnerTest method testRuleFlowGroupActivation.

@Test
public void testRuleFlowGroupActivation() throws Exception {
    Scenario scenario = new Scenario();
    scenario.getImports().addImport(new Import("foo.bar.Coolness"));
    Fixture[] given = new Fixture[] { new FactData("Coolness", "c", Arrays.<Field>asList(new FieldData("num", "42"), new FieldData("name", "mic")), false) };
    scenario.getFixtures().addAll(Arrays.asList(given));
    ExecutionTrace executionTrace = new ExecutionTrace();
    scenario.getRules().add("rule1");
    scenario.setInclusive(true);
    scenario.getFixtures().add(executionTrace);
    Expectation[] assertions = new Expectation[2];
    assertions[0] = new VerifyFact("c", ls(new VerifyField("num", "42", "==")));
    assertions[1] = new VerifyRuleFired("rule1", 1, null);
    scenario.getFixtures().addAll(Arrays.asList(assertions));
    KieSession ksession = getKieSession("rule_flow_actication.drl");
    ClassLoader classLoader = ((KnowledgeBaseImpl) ksession.getKieBase()).getRootClassLoader();
    HashSet<String> imports = new HashSet<String>();
    imports.add("foo.bar.*");
    TypeResolver resolver = new ClassTypeResolver(imports, classLoader);
    Class<?> coolnessClass = classLoader.loadClass("foo.bar.Coolness");
    assertNotNull(coolnessClass);
    ClassLoader cl_ = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(classLoader);
    // resolver will need to have generated beans in it - possibly using a composite classloader from the package,
    // including whatever CL has the generated beans...
    ScenarioRunner scenarioRunner = new ScenarioRunner(ksession);
    scenarioRunner.run(scenario);
    assertEquals(0, executionTrace.getNumberOfRulesFired().intValue());
    assertFalse(scenario.wasSuccessful());
    // Activate rule flow
    scenario.getFixtures().clear();
    given = new Fixture[] { new FactData("Coolness", "c", Arrays.<Field>asList(new FieldData("num", "42"), new FieldData("name", "mic")), false), new ActivateRuleFlowGroup("asdf") };
    scenario.getFixtures().addAll(Arrays.asList(given));
    scenario.getFixtures().add(executionTrace);
    ((InternalAgendaGroup) ksession.getAgenda().getRuleFlowGroup("asdf")).setAutoDeactivate(false);
    scenarioRunner = new ScenarioRunner(ksession);
    scenarioRunner.run(scenario);
    assertTrue(scenario.wasSuccessful());
    Thread.currentThread().setContextClassLoader(cl_);
}
Also used : Import(org.kie.soup.project.datamodel.imports.Import) VerifyField(org.drools.workbench.models.testscenarios.shared.VerifyField) ClassTypeResolver(org.kie.soup.project.datamodel.commons.types.ClassTypeResolver) TypeResolver(org.kie.soup.project.datamodel.commons.types.TypeResolver) ActivateRuleFlowGroup(org.drools.workbench.models.testscenarios.shared.ActivateRuleFlowGroup) VerifyField(org.drools.workbench.models.testscenarios.shared.VerifyField) Field(org.drools.workbench.models.testscenarios.shared.Field) FactData(org.drools.workbench.models.testscenarios.shared.FactData) ProjectClassLoader(org.drools.core.common.ProjectClassLoader) KieSession(org.kie.api.runtime.KieSession) Fixture(org.drools.workbench.models.testscenarios.shared.Fixture) VerifyFact(org.drools.workbench.models.testscenarios.shared.VerifyFact) HashSet(java.util.HashSet) InternalAgendaGroup(org.drools.core.common.InternalAgendaGroup) VerifyRuleFired(org.drools.workbench.models.testscenarios.shared.VerifyRuleFired) ExecutionTrace(org.drools.workbench.models.testscenarios.shared.ExecutionTrace) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) Scenario(org.drools.workbench.models.testscenarios.shared.Scenario) FieldData(org.drools.workbench.models.testscenarios.shared.FieldData) Expectation(org.drools.workbench.models.testscenarios.shared.Expectation) ClassTypeResolver(org.kie.soup.project.datamodel.commons.types.ClassTypeResolver) Test(org.junit.Test)

Example 3 with InternalAgendaGroup

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

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

the class RuleUnitExecutorSession method bindRuleUnit.

private RuleUnitDescr bindRuleUnit(RuleUnit ruleUnit) {
    suspended.set(false);
    currentRuleUnit = ruleUnit;
    currentRuleUnit.onStart();
    factHandlesMap.computeIfAbsent(ruleUnit.getClass(), x -> session.getEntryPoint(RULE_UNIT_ENTRY_POINT).insert(ruleUnit));
    RuleUnitDescr ruDescr = session.kBase.getRuleUnitRegistry().getRuleUnitDescr(ruleUnit);
    ruDescr.bindDataSources(session, ruleUnit);
    ((Globals) session.getGlobalResolver()).setDelegate(new RuleUnitGlobals(ruDescr, ruleUnit));
    InternalAgendaGroup unitGroup = (InternalAgendaGroup) session.getAgenda().getAgendaGroup(ruleUnit.getClass().getName());
    unitGroup.setAutoDeactivate(false);
    unitGroup.setFocus();
    return ruDescr;
}
Also used : Globals(org.kie.api.runtime.Globals) InternalAgendaGroup(org.drools.core.common.InternalAgendaGroup) RuleUnitDescr(org.drools.core.ruleunit.RuleUnitDescr)

Example 5 with InternalAgendaGroup

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

the class PhreakRuleTerminalNode method doLeftTupleInsert.

public static void doLeftTupleInsert(TerminalNode rtnNode, RuleExecutor executor, InternalAgenda agenda, RuleAgendaItem ruleAgendaItem, int salienceInt, Salience salience, LeftTuple leftTuple) {
    PropagationContext pctx = leftTuple.getPropagationContext();
    pctx = RuleTerminalNode.findMostRecentPropagationContext(leftTuple, pctx);
    if (rtnNode.getRule().isNoLoop() && rtnNode.equals(pctx.getTerminalNodeOrigin())) {
        return;
    }
    InternalWorkingMemory wm = agenda.getWorkingMemory();
    if (salience != null) {
        salienceInt = salience.getValue(new DefaultKnowledgeHelper((AgendaItem) leftTuple, wm), rtnNode.getRule(), wm);
    }
    RuleTerminalNodeLeftTuple rtnLeftTuple = (RuleTerminalNodeLeftTuple) leftTuple;
    agenda.createAgendaItem(rtnLeftTuple, salienceInt, pctx, ruleAgendaItem, ruleAgendaItem.getAgendaGroup());
    EventSupport es = (EventSupport) wm;
    es.getAgendaEventSupport().fireActivationCreated(rtnLeftTuple, wm);
    if (rtnNode.getRule().isLockOnActive() && leftTuple.getPropagationContext().getType() != PropagationContext.Type.RULE_ADDITION) {
        long handleRecency = pctx.getFactHandle().getRecency();
        InternalAgendaGroup agendaGroup = executor.getRuleAgendaItem().getAgendaGroup();
        if (blockedByLockOnActive(rtnNode.getRule(), pctx, handleRecency, agendaGroup)) {
            es.getAgendaEventSupport().fireActivationCancelled(rtnLeftTuple, wm, MatchCancelledCause.FILTER);
            return;
        }
    }
    if (agenda.getActivationsFilter() != null && !agenda.getActivationsFilter().accept(rtnLeftTuple, wm, rtnNode)) {
        // only relevant for seralization, to not refire Matches already fired
        return;
    }
    agenda.addItemToActivationGroup(rtnLeftTuple);
    executor.addLeftTuple(leftTuple);
    // increased here, decreased in Agenda's cancelActivation and fireActivation
    leftTuple.increaseActivationCountForEvents();
    if (!rtnNode.isFireDirect() && executor.isDeclarativeAgendaEnabled()) {
        agenda.insertAndStageActivation(rtnLeftTuple);
    }
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) InternalAgendaGroup(org.drools.core.common.InternalAgendaGroup) PropagationContext(org.drools.core.spi.PropagationContext) EventSupport(org.drools.core.common.EventSupport) DefaultKnowledgeHelper(org.drools.core.base.DefaultKnowledgeHelper) RuleTerminalNodeLeftTuple(org.drools.core.reteoo.RuleTerminalNodeLeftTuple)

Aggregations

InternalAgendaGroup (org.drools.core.common.InternalAgendaGroup)7 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)3 DefaultKnowledgeHelper (org.drools.core.base.DefaultKnowledgeHelper)2 EventSupport (org.drools.core.common.EventSupport)2 RuleTerminalNodeLeftTuple (org.drools.core.reteoo.RuleTerminalNodeLeftTuple)2 PropagationContext (org.drools.core.spi.PropagationContext)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 ActivationsFilter (org.drools.core.common.ActivationsFilter)1 AgendaItem (org.drools.core.common.AgendaItem)1 InternalAgenda (org.drools.core.common.InternalAgenda)1 ProjectClassLoader (org.drools.core.common.ProjectClassLoader)1 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)1 KnowledgeBaseImpl (org.drools.core.impl.KnowledgeBaseImpl)1 RuleAgendaItem (org.drools.core.phreak.RuleAgendaItem)1 RuleExecutor (org.drools.core.phreak.RuleExecutor)1 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)1 RuleUnitDescr (org.drools.core.ruleunit.RuleUnitDescr)1