use of org.drools.core.common.InternalAgenda in project drools by kiegroup.
the class ExecutionFlowControlTest method testLockOnActive.
@Test
public void testLockOnActive() throws Exception {
KieBase kbase = loadKnowledgeBase("test_LockOnActive.drl");
KieSession ksession = kbase.newKieSession();
final List list = new ArrayList();
ksession.setGlobal("list", list);
// AgendaGroup "group1" is not active, so should receive activation
final Cheese brie12 = new Cheese("brie", 12);
ksession.insert(brie12);
((InternalWorkingMemory) ksession).flushPropagations();
InternalAgenda agenda = ((InternalAgenda) ksession.getAgenda());
final AgendaGroup group1 = agenda.getAgendaGroup("group1");
assertEquals(1, group1.size());
ksession.getAgenda().getAgendaGroup("group1").setFocus();
// AgendaqGroup "group1" is now active, so should not receive activations
final Cheese brie10 = new Cheese("brie", 10);
ksession.insert(brie10);
assertEquals(1, group1.size());
final Cheese cheddar20 = new Cheese("cheddar", 20);
ksession.insert(cheddar20);
final AgendaGroup group2 = agenda.getAgendaGroup("group1");
assertEquals(1, group2.size());
agenda.setFocus(group2);
final Cheese cheddar17 = new Cheese("cheddar", 17);
ksession.insert(cheddar17);
assertEquals(1, group2.size());
}
use of org.drools.core.common.InternalAgenda in project drools by kiegroup.
the class ExecutionFlowControlTest method testRuleFlowGroupInActiveMode.
@Test(timeout = 10000)
public void testRuleFlowGroupInActiveMode() throws Exception {
KieBase kbase = loadKnowledgeBase("ruleflowgroup.drl");
final KieSession ksession = kbase.newKieSession();
final List list = new ArrayList();
ksession.setGlobal("list", list);
final AtomicBoolean fired = new AtomicBoolean(false);
ksession.addEventListener(new org.kie.api.event.rule.DefaultAgendaEventListener() {
@Override
public void afterMatchFired(AfterMatchFiredEvent event) {
synchronized (fired) {
fired.set(true);
fired.notifyAll();
}
}
});
new Thread(new Runnable() {
public void run() {
ksession.fireUntilHalt();
}
}).start();
ksession.insert("Test");
assertEquals(0, list.size());
((InternalAgenda) ksession.getAgenda()).activateRuleFlowGroup("Group1");
synchronized (fired) {
if (!fired.get()) {
fired.wait();
}
}
assertEquals(1, list.size());
ksession.halt();
}
use of org.drools.core.common.InternalAgenda 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.common.InternalAgenda in project drools by kiegroup.
the class ExecutionFlowControlTest method testRuleFlowGroupDeactivate.
@Test
public void testRuleFlowGroupDeactivate() throws Exception {
// need to make eager, for cancel to work, (mdp)
KieBase kbase = loadKnowledgeBase("ruleflowgroup2.drl");
KieSession ksession = kbase.newKieSession();
final List list = new ArrayList();
ksession.setGlobal("list", list);
ksession.insert("Test");
ksession.fireAllRules();
assertEquals(0, list.size());
assertEquals(2, ((InternalAgenda) ksession.getAgenda()).getRuleFlowGroup("Group1").size());
((InternalAgenda) ksession.getAgenda()).activateRuleFlowGroup("Group1");
ksession.fireAllRules();
assertEquals(0, list.size());
}
use of org.drools.core.common.InternalAgenda 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