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());
}
}
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_);
}
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());
}
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;
}
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);
}
}
Aggregations