use of org.kie.api.event.rule.MatchCancelledEvent in project drools by kiegroup.
the class AgendaEventSupportTest method testAgendaEventListener.
// public void testIsSerializable() {
// assertTrue( Serializable.class.isAssignableFrom( AgendaEventSupport.class ) );
// }
@Test
@Ignore
public void testAgendaEventListener() throws Exception {
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
// create a simple package with one rule to test the events
InternalKnowledgePackage pkg = new KnowledgePackageImpl("org.drools.test");
final RuleImpl rule = new RuleImpl("test1");
rule.setEager(true);
rule.setAgendaGroup("test group");
final ClassObjectType cheeseObjectType = new ClassObjectType(Cheese.class);
final Pattern pattern = new Pattern(0, cheeseObjectType);
pkg.setClassFieldAccessorCache(new ClassFieldAccessorCache(Thread.currentThread().getContextClassLoader()));
pkg.getClassFieldAccessorStore().setEagerWire(true);
final ClassFieldReader extractor = pkg.getClassFieldAccessorStore().getReader(Cheese.class, "type");
final FieldValue field = FieldFactory.getInstance().getFieldValue("cheddar");
final MvelConstraint constraint = new MvelConstraintTestUtil("type == \"cheddar\"", field, extractor);
pattern.addConstraint(constraint);
rule.addPattern(pattern);
rule.setConsequence(new Consequence() {
public void evaluate(final KnowledgeHelper knowledgeHelper, final WorkingMemory workingMemory) throws Exception {
}
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
}
public void writeExternal(ObjectOutput out) throws IOException {
}
public String getName() {
// TODO Auto-generated method stub
return null;
}
});
pkg.addRule(rule);
kbase.addPackages(Collections.singleton(pkg));
// create a new working memory and add an AgendaEventListener
KieSession ksession = kbase.newKieSession();
final List agendaList = new ArrayList();
final AgendaEventListener agendaEventListener = new AgendaEventListener() {
public void matchCancelled(MatchCancelledEvent event) {
assertNotNull(event.getKieRuntime());
agendaList.add(event);
}
public void matchCreated(MatchCreatedEvent event) {
assertNotNull(event.getKieRuntime());
agendaList.add(event);
}
public void afterMatchFired(AfterMatchFiredEvent event) {
assertNotNull(event.getKieRuntime());
agendaList.add(event);
}
public void agendaGroupPopped(AgendaGroupPoppedEvent event) {
assertNotNull(event.getKieRuntime());
agendaList.add(event);
}
public void agendaGroupPushed(AgendaGroupPushedEvent event) {
assertNotNull(event.getKieRuntime());
agendaList.add(event);
}
public void beforeMatchFired(BeforeMatchFiredEvent event) {
assertNotNull(event.getKieRuntime());
agendaList.add(event);
}
public void beforeRuleFlowGroupActivated(RuleFlowGroupActivatedEvent event) {
assertNotNull(event.getKieRuntime());
agendaList.add(event);
}
public void afterRuleFlowGroupActivated(RuleFlowGroupActivatedEvent event) {
assertNotNull(event.getKieRuntime());
agendaList.add(event);
}
public void beforeRuleFlowGroupDeactivated(RuleFlowGroupDeactivatedEvent event) {
assertNotNull(event.getKieRuntime());
agendaList.add(event);
}
public void afterRuleFlowGroupDeactivated(RuleFlowGroupDeactivatedEvent event) {
assertNotNull(event.getKieRuntime());
agendaList.add(event);
}
};
ksession.addEventListener(agendaEventListener);
assertEquals(1, ksession.getAgendaEventListeners().size());
// assert the cheese fact
final Cheese cheddar = new Cheese("cheddar", 15);
FactHandle cheddarHandle = ksession.insert(cheddar);
InternalAgenda agenda = (InternalAgenda) ksession.getAgenda();
agenda.evaluateEagerList();
// should be one MatchCreatedEvent
assertEquals(1, agendaList.size());
MatchCreatedEvent createdEvent = (MatchCreatedEvent) agendaList.get(0);
assertSame(cheddarHandle, createdEvent.getMatch().getFactHandles().toArray()[0]);
// clear the agenda to check CLEAR events occur
ksession.getAgenda().clear();
MatchCancelledEvent cancelledEvent = (MatchCancelledEvent) agendaList.get(1);
assertEquals(MatchCancelledCause.CLEAR, cancelledEvent.getCause());
agendaList.clear();
// update results in an MatchCreatedEvent
cheddar.setPrice(14);
ksession.update(cheddarHandle, cheddar);
agenda.evaluateEagerList();
assertEquals(1, agendaList.size());
createdEvent = (MatchCreatedEvent) agendaList.get(0);
assertSame(cheddarHandle, createdEvent.getMatch().getFactHandles().toArray()[0]);
agendaList.clear();
// update should not result in cancelation+activation events
cheddar.setPrice(14);
ksession.update(cheddarHandle, cheddar);
assertEquals(0, agendaList.size());
// cancelledEvent = (ActivationCancelledEvent) agendaList.get( 0 );
// assertEquals( ActivationCancelledCause.WME_MODIFY, cancelledEvent.getCause() );
// assertSame( cheddarHandle,
// cancelledEvent.getActivation().toFactHandles().toArray()[0] );
// createdEvent = (ActivationCreatedEvent) agendaList.get( 1 );
// assertSame( cheddarHandle,
// createdEvent.getActivation().toFactHandles().toArray()[0] );
// agendaList.clear();
// retract results in a ActivationCancelledEvent, note the object is not resolveable now as it no longer exists
ksession.retract(cheddarHandle);
assertEquals(1, agendaList.size());
cancelledEvent = (MatchCancelledEvent) agendaList.get(0);
// invalidated handles no longer set the object to null
assertNotNull(((InternalFactHandle) cancelledEvent.getMatch().getFactHandles().toArray()[0]).getObject());
// re-assert the fact so we can test the agenda group events
cheddarHandle = ksession.insert(cheddar);
agendaList.clear();
// setFocus results in an AgendaGroupPushedEvent
ksession.getAgenda().getAgendaGroup("test group").setFocus();
assertEquals(1, agendaList.size());
final AgendaGroupPushedEvent pushedEvent = (AgendaGroupPushedEvent) agendaList.get(0);
assertEquals("test group", pushedEvent.getAgendaGroup().getName());
agendaList.clear();
// fireAllRules results in a BeforeActivationFiredEvent and an AfterActivationFiredEvent
// the AgendaGroup becomes empty, which results in a popped event.
ksession.fireAllRules();
assertEquals(3, agendaList.size());
final BeforeMatchFiredEvent beforeEvent = (BeforeMatchFiredEvent) agendaList.get(0);
assertSame(cheddarHandle, beforeEvent.getMatch().getFactHandles().toArray()[0]);
final AfterMatchFiredEvent afterEvent = (AfterMatchFiredEvent) agendaList.get(1);
assertSame(cheddarHandle, afterEvent.getMatch().getFactHandles().toArray()[0]);
final AgendaGroupPoppedEvent poppedEvent = (AgendaGroupPoppedEvent) agendaList.get(2);
assertEquals("test group", poppedEvent.getAgendaGroup().getName());
}
use of org.kie.api.event.rule.MatchCancelledEvent in project drools by kiegroup.
the class SequentialTest method testEvents.
@Test
public void testEvents() throws Exception {
String str = "";
str += "package org.drools.compiler.test\n";
str += "import org.drools.compiler.Message\n";
str += "rule \"Hello World\"\n";
str += "when\n";
str += " Message( )\n";
str += "then\n";
str += " System.out.println( drools.getKieRuntime() );\n";
str += "end\n";
KieBase kbase = loadKnowledgeBaseFromString(kconf, str);
StatelessKieSession ksession = createStatelessKnowledgeSession(kbase);
final List list = new ArrayList();
ksession.addEventListener(new AgendaEventListener() {
public void matchCancelled(MatchCancelledEvent event) {
assertNotNull(event.getKieRuntime());
list.add(event);
}
public void matchCreated(MatchCreatedEvent event) {
assertNotNull(event.getKieRuntime());
list.add(event);
}
public void afterMatchFired(AfterMatchFiredEvent event) {
assertNotNull(event.getKieRuntime());
list.add(event);
}
public void agendaGroupPopped(AgendaGroupPoppedEvent event) {
assertNotNull(event.getKieRuntime());
list.add(event);
}
public void agendaGroupPushed(AgendaGroupPushedEvent event) {
assertNotNull(event.getKieRuntime());
list.add(event);
}
public void beforeMatchFired(BeforeMatchFiredEvent event) {
assertNotNull(event.getKieRuntime());
list.add(event);
}
public void beforeRuleFlowGroupActivated(RuleFlowGroupActivatedEvent event) {
assertNotNull(event.getKieRuntime());
list.add(event);
}
public void afterRuleFlowGroupActivated(RuleFlowGroupActivatedEvent event) {
assertNotNull(event.getKieRuntime());
list.add(event);
}
public void beforeRuleFlowGroupDeactivated(RuleFlowGroupDeactivatedEvent event) {
assertNotNull(event.getKieRuntime());
list.add(event);
}
public void afterRuleFlowGroupDeactivated(RuleFlowGroupDeactivatedEvent event) {
assertNotNull(event.getKieRuntime());
list.add(event);
}
});
ksession.addEventListener(new RuleRuntimeEventListener() {
public void objectInserted(ObjectInsertedEvent event) {
assertNotNull(event.getKieRuntime());
list.add(event);
}
public void objectDeleted(ObjectDeletedEvent event) {
assertNotNull(event.getKieRuntime());
list.add(event);
}
public void objectUpdated(ObjectUpdatedEvent event) {
assertNotNull(event.getKieRuntime());
list.add(event);
}
});
ksession.execute(new Message("help"));
assertEquals(4, list.size());
}
use of org.kie.api.event.rule.MatchCancelledEvent in project drools by kiegroup.
the class Misc2Test method testMatchingEventsInStreamMode.
@Test
public void testMatchingEventsInStreamMode() {
// DROOLS-338
String drl = "import org.drools.mvel.integrationtests.Misc2Test.SimpleEvent\n" + "declare SimpleEvent\n" + " @role(event)\n" + "end\n" + "\n" + "rule \"RuleA\"\n" + "salience 5\n" + "when\n" + " $f : SimpleEvent( )\n" + "then\n" + " delete ($f);\n" + "end\n" + "\n" + "rule \"RuleB\"\n" + "when\n" + " $f : SimpleEvent( )\n" + "then\n" + "end\n";
KieBaseTestConfiguration streamConfig = TestParametersUtil.getStreamInstanceOf(kieBaseTestConfiguration);
KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", streamConfig, drl);
KieSession ksession = kbase.newKieSession();
final AtomicInteger i = new AtomicInteger(0);
ksession.addEventListener(new DefaultAgendaEventListener() {
public void matchCreated(MatchCreatedEvent event) {
i.incrementAndGet();
}
public void matchCancelled(MatchCancelledEvent event) {
i.decrementAndGet();
}
});
ksession.insert(new SimpleEvent());
ksession.fireAllRules();
assertEquals(1, i.get());
}
use of org.kie.api.event.rule.MatchCancelledEvent in project drools by kiegroup.
the class SequentialTest method testEvents.
@Test
public void testEvents() throws Exception {
String str = "";
str += "package org.drools.mvel.compiler.test\n";
str += "import org.drools.mvel.compiler.Message\n";
str += "rule \"Hello World\"\n";
str += "when\n";
str += " Message( )\n";
str += "then\n";
str += " System.out.println( drools.getKieRuntime() );\n";
str += "end\n";
final KieModule kieModule = KieUtil.getKieModuleFromDrls("test", kieBaseTestConfiguration, str);
final KieBase kbase = KieBaseUtil.newKieBaseFromKieModuleWithAdditionalOptions(kieModule, kieBaseTestConfiguration, SequentialOption.YES);
StatelessKieSession ksession = kbase.newStatelessKieSession();
final List list = new ArrayList();
ksession.addEventListener(new AgendaEventListener() {
public void matchCancelled(MatchCancelledEvent event) {
assertNotNull(event.getKieRuntime());
list.add(event);
}
public void matchCreated(MatchCreatedEvent event) {
assertNotNull(event.getKieRuntime());
list.add(event);
}
public void afterMatchFired(AfterMatchFiredEvent event) {
assertNotNull(event.getKieRuntime());
list.add(event);
}
public void agendaGroupPopped(AgendaGroupPoppedEvent event) {
assertNotNull(event.getKieRuntime());
list.add(event);
}
public void agendaGroupPushed(AgendaGroupPushedEvent event) {
assertNotNull(event.getKieRuntime());
list.add(event);
}
public void beforeMatchFired(BeforeMatchFiredEvent event) {
assertNotNull(event.getKieRuntime());
list.add(event);
}
public void beforeRuleFlowGroupActivated(RuleFlowGroupActivatedEvent event) {
assertNotNull(event.getKieRuntime());
list.add(event);
}
public void afterRuleFlowGroupActivated(RuleFlowGroupActivatedEvent event) {
assertNotNull(event.getKieRuntime());
list.add(event);
}
public void beforeRuleFlowGroupDeactivated(RuleFlowGroupDeactivatedEvent event) {
assertNotNull(event.getKieRuntime());
list.add(event);
}
public void afterRuleFlowGroupDeactivated(RuleFlowGroupDeactivatedEvent event) {
assertNotNull(event.getKieRuntime());
list.add(event);
}
});
ksession.addEventListener(new RuleRuntimeEventListener() {
public void objectInserted(ObjectInsertedEvent event) {
assertNotNull(event.getKieRuntime());
list.add(event);
}
public void objectDeleted(ObjectDeletedEvent event) {
assertNotNull(event.getKieRuntime());
list.add(event);
}
public void objectUpdated(ObjectUpdatedEvent event) {
assertNotNull(event.getKieRuntime());
list.add(event);
}
});
ksession.execute(new Message("help"));
assertEquals(4, list.size());
}
use of org.kie.api.event.rule.MatchCancelledEvent in project drools by kiegroup.
the class ExecutionFlowControlTest method testUpdateActivationCreationNoLoop.
@Test
public void testUpdateActivationCreationNoLoop() throws Exception {
// JBRULES-787, no-loop blocks all dependant tuples for update
KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources(this.getClass(), kieBaseTestConfiguration, "test_UpdateActivationCreationNoLoop.drl");
KieSession ksession = kbase.newKieSession();
final List created = new ArrayList();
final List cancelled = new ArrayList();
final AgendaEventListener l = new DefaultAgendaEventListener() {
@Override
public void matchCreated(MatchCreatedEvent event) {
created.add(event);
}
@Override
public void matchCancelled(MatchCancelledEvent event) {
cancelled.add(event);
}
};
ksession.addEventListener(l);
final Cheese stilton = new Cheese("stilton", 15);
final FactHandle stiltonHandle = ksession.insert(stilton);
final Person p1 = new Person("p1");
p1.setCheese(stilton);
ksession.insert(p1);
final Person p2 = new Person("p2");
p2.setCheese(stilton);
ksession.insert(p2);
final Person p3 = new Person("p3");
p3.setCheese(stilton);
ksession.insert(p3);
ksession.fireAllRules();
assertEquals(3, created.size());
assertEquals(0, cancelled.size());
// simulate a modify inside a consequence
ksession.update(stiltonHandle, stilton);
// with true modify, no reactivations should be triggered
assertEquals(3, created.size());
assertEquals(0, cancelled.size());
}
Aggregations