use of org.drools.core.phreak.RuleAgendaItem in project drools by kiegroup.
the class DefaultAgenda method clearAndCancelAgendaGroup.
/*
* (non-Javadoc)
*
* @see org.kie.common.AgendaI#clearAgendaGroup(org.kie.common.AgendaGroupImpl)
*/
public void clearAndCancelAgendaGroup(InternalAgendaGroup agendaGroup) {
// enforce materialization of all activations of this group before removing them
for (Activation activation : agendaGroup.getActivations()) {
((RuleAgendaItem) activation).getRuleExecutor().reEvaluateNetwork(this);
}
final EventSupport eventsupport = this.workingMemory;
agendaGroup.setClearedForRecency(this.workingMemory.getFactHandleFactory().getRecency());
// this is thread safe for BinaryHeapQueue
// Binary Heap locks while it returns the array and reset's it's own internal array. Lock is released afer getAndClear()
List<RuleAgendaItem> lazyItems = new ArrayList<RuleAgendaItem>();
for (Activation aQueueable : agendaGroup.getAndClear()) {
final AgendaItem item = (AgendaItem) aQueueable;
if (item.isRuleAgendaItem()) {
lazyItems.add((RuleAgendaItem) item);
((RuleAgendaItem) item).getRuleExecutor().cancel(workingMemory, eventsupport);
continue;
}
// this must be set false before removal from the activationGroup.
// Otherwise the activationGroup will also try to cancel the Actvation
// Also modify won't work properly
item.setQueued(false);
if (item.getActivationGroupNode() != null) {
item.getActivationGroupNode().getActivationGroup().removeActivation(item);
}
eventsupport.getAgendaEventSupport().fireActivationCancelled(item, this.workingMemory, MatchCancelledCause.CLEAR);
}
// restore lazy items
for (RuleAgendaItem lazyItem : lazyItems) {
agendaGroup.add(lazyItem);
}
}
use of org.drools.core.phreak.RuleAgendaItem in project drools by kiegroup.
the class DefaultAgenda method createRuleAgendaItem.
public RuleAgendaItem createRuleAgendaItem(final int salience, final PathMemory rs, final TerminalNode rtn) {
String agendaGroupName = rtn.getRule().getAgendaGroup();
String ruleFlowGroupName = rtn.getRule().getRuleFlowGroup();
RuleAgendaItem lazyAgendaItem;
if (!StringUtils.isEmpty(ruleFlowGroupName)) {
lazyAgendaItem = new RuleAgendaItem(activationCounter++, null, salience, null, rs, rtn, isDeclarativeAgenda(), (InternalAgendaGroup) getAgendaGroup(ruleFlowGroupName));
} else {
lazyAgendaItem = new RuleAgendaItem(activationCounter++, null, salience, null, rs, rtn, isDeclarativeAgenda(), (InternalAgendaGroup) getRuleFlowGroup(agendaGroupName));
}
return lazyAgendaItem;
}
use of org.drools.core.phreak.RuleAgendaItem 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;
}
}
}
use of org.drools.core.phreak.RuleAgendaItem in project drools by kiegroup.
the class ExecutionFlowControlTest method testLockOnActiveWithModify.
@Test
public void testLockOnActiveWithModify() throws Exception {
KieBase kbase = loadKnowledgeBase("test_LockOnActiveWithUpdate.drl");
KieSession ksession = kbase.newKieSession();
final List list = new ArrayList();
ksession.setGlobal("list", list);
final Cheese brie = new Cheese("brie", 13);
final Person bob = new Person("bob");
bob.setCheese(brie);
final Person mic = new Person("mic");
mic.setCheese(brie);
final Person mark = new Person("mark");
mark.setCheese(brie);
final FactHandle brieHandle = (FactHandle) ksession.insert(brie);
ksession.insert(bob);
ksession.insert(mic);
ksession.insert(mark);
InternalWorkingMemory wm = (InternalWorkingMemory) ksession;
wm.flushPropagations();
final InternalAgenda agenda = (InternalAgenda) ksession.getAgenda();
final AgendaGroup group1 = agenda.getAgendaGroup("group1");
agenda.setFocus(group1);
assertEquals(1, group1.size());
RuleAgendaItem ruleItem1 = (RuleAgendaItem) group1.getActivations()[0];
ruleItem1.getRuleExecutor().evaluateNetwork(wm.getAgenda());
assertEquals(3, ruleItem1.getRuleExecutor().getLeftTupleList().size());
agenda.fireNextItem(null, 0, 0);
assertEquals(1, group1.size());
assertEquals(2, ruleItem1.getRuleExecutor().getLeftTupleList().size());
ksession.update(brieHandle, brie);
assertEquals(1, group1.size());
ruleItem1.getRuleExecutor().evaluateNetwork(wm.getAgenda());
assertEquals(2, ruleItem1.getRuleExecutor().getLeftTupleList().size());
AgendaGroup group2 = agenda.getAgendaGroup("group2");
agenda.setFocus(group2);
assertEquals(1, group2.size());
RuleAgendaItem ruleItem2 = (RuleAgendaItem) group2.getActivations()[0];
ruleItem2.getRuleExecutor().evaluateNetwork(wm.getAgenda());
assertEquals(3, ruleItem2.getRuleExecutor().getLeftTupleList().size());
agenda.fireNextItem(null, 0, 0);
assertEquals(1, group2.size());
assertEquals(2, ruleItem2.getRuleExecutor().getLeftTupleList().size());
ksession.update(brieHandle, brie);
assertEquals(1, group2.size());
assertEquals(2, ruleItem2.getRuleExecutor().getLeftTupleList().size());
}
use of org.drools.core.phreak.RuleAgendaItem 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());
}
Aggregations