use of org.drools.core.event.DefaultAgendaEventListener in project drools by kiegroup.
the class IncrementalCompilationTest method testKJarUpgradeDRLWithSpace3.
@Test
public void testKJarUpgradeDRLWithSpace3() throws Exception {
// DROOLS-1399 ter
String drl_1 = "package org.drools.compiler\n" + "rule Rs when $s : String() then System.out.println($s); end\n" + "rule Rx when\n" + " $m : Message( message == \"Hello World\" )\n" + "then\n" + " System.out.println($m); \n" + "end\n";
String drl_2 = "package org.drools.compiler\n" + // <<- notice rule changed
"rule Rs when $s : String( this == \"x\") then System.out.println($s); end\n" + "rule Rx when\n" + // <<- notice the EXTRA SPACE is the an ADDITIONAL change in this other version.
" $m : Message( message == \"Hello World\" )\n" + "then\n" + " System.out.println($m); \n" + "end\n";
KieServices ks = KieServices.Factory.get();
ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.0.0");
KieModule km = createAndDeployJar(ks, releaseId1, drl_1);
KieContainer kc = ks.newKieContainer(km.getReleaseId());
KieSession ksession = kc.newKieSession();
final List<String> fired = new ArrayList<>();
ksession.addEventListener(new DefaultAgendaEventListener() {
@Override
public void afterMatchFired(AfterMatchFiredEvent event) {
fired.add(event.getMatch().getRule().getName());
}
});
ksession.insert(new Message("Hello World"));
ksession.insert("x");
assertEquals(2, ksession.fireAllRules());
assertTrue(fired.contains("Rs"));
assertTrue(fired.contains("Rx"));
fired.clear();
ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.0");
km = createAndDeployJar(ks, releaseId2, drl_2);
kc.updateToVersion(releaseId2);
// rule Rx is UNchanged and should NOT fire again
// rule Rs is changed and should match again, and fire again.
assertEquals(1, ksession.fireAllRules());
assertTrue(fired.contains("Rs"));
assertFalse(fired.contains("Rx"));
}
use of org.drools.core.event.DefaultAgendaEventListener in project drools by kiegroup.
the class ActivateAndDeleteOnListenerTest method testActivateOnMatchAndUpdate.
@Test
public void testActivateOnMatchAndUpdate() throws Exception {
String str = "";
str += "package org.drools.compiler.integrationtests \n";
str += "import " + Alarm.class.getCanonicalName() + " \n";
str += "import " + Sensor.class.getCanonicalName() + " \n";
str += "rule StringRule @Propagation(EAGER) ruleflow-group \"DROOLS_SYSTEM\"\n";
str += " when \n";
str += " $a : Alarm() \n";
str += " $s : Sensor() \n";
str += " then \n";
str += "end \n";
KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
conf.setOption(ForceEagerActivationOption.YES);
KieSession ksession = new KieHelper().addContent(str, ResourceType.DRL).build().newKieSession(conf, null);
ksession.addEventListener(new DefaultAgendaEventListener() {
@Override
public void matchCreated(MatchCreatedEvent event) {
Collection<? extends FactHandle> alarms = event.getKieRuntime().getFactHandles(new ClassObjectFilter(Alarm.class));
for (FactHandle alarm : alarms) {
event.getKieRuntime().update(alarm, new Alarm());
}
}
});
// go !
Alarm alarm = new Alarm();
alarm.setMessage("test");
alarm.setNumber(123);
ksession.insert(alarm);
Sensor sensor = new Sensor();
sensor.setPressure(1);
sensor.setTemperature(25);
ksession.insert(sensor);
}
use of org.drools.core.event.DefaultAgendaEventListener in project drools by kiegroup.
the class ActivateAndDeleteOnListenerTest method testActivateOnMatchAndDelete.
@Test
public void testActivateOnMatchAndDelete() throws Exception {
String str = "";
str += "package org.drools.compiler.integrationtests \n";
str += "import " + Alarm.class.getCanonicalName() + " \n";
str += "import " + Sensor.class.getCanonicalName() + " \n";
str += "rule StringRule @Propagation(EAGER) ruleflow-group \"DROOLS_SYSTEM\"\n";
str += " when \n";
str += " $a : Alarm() \n";
str += " $s : Sensor() \n";
str += " then \n";
str += "end \n";
KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
conf.setOption(ForceEagerActivationOption.YES);
KieSession ksession = new KieHelper().addContent(str, ResourceType.DRL).build().newKieSession(conf, null);
ksession.addEventListener(new DefaultAgendaEventListener() {
@Override
public void matchCreated(MatchCreatedEvent event) {
Collection<? extends FactHandle> alarms = event.getKieRuntime().getFactHandles(new ClassObjectFilter(Alarm.class));
for (FactHandle alarm : alarms) {
event.getKieRuntime().delete(alarm);
}
}
});
// go !
Alarm alarm = new Alarm();
alarm.setMessage("test");
alarm.setNumber(123);
ksession.insert(alarm);
Sensor sensor = new Sensor();
sensor.setPressure(1);
sensor.setTemperature(25);
ksession.insert(sensor);
ksession.fireAllRules();
}
use of org.drools.core.event.DefaultAgendaEventListener in project drools by kiegroup.
the class ReteooMannersTest method testManners.
@Test
public void testManners() throws Exception {
InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase();
kBase.addPackage(this.pkg);
KieSession kSession = kBase.newKieSession();
final DefaultAgendaEventListener listener = new DefaultAgendaEventListener() {
private int counter = 0;
// public void matchCreated(ActivationCreatedEvent event) {
// super.matchCreated( event );
// System.out.println( event );
// }
//
// public void matchCancelled(ActivationCancelledEvent event) {
// super.matchCancelled( event );
// System.out.println( event );
// }
//
// public void beforeMatchFired(BeforeActivationFiredEvent event) {
// super.beforeMatchFired( event );
// System.out.println( event );
// }
public void afterActivationFired(AfterActivationFiredEvent event) {
this.counter++;
// super.afterMatchFired( event );
// System.out.println( event );
}
public String toString() {
return "fired : " + this.counter;
}
};
// workingMemory.addEventListener(listener );
final InputStream is = getClass().getResourceAsStream("/manners5.dat");
final List list = getInputObjects(is);
for (final Iterator it = list.iterator(); it.hasNext(); ) {
final Object object = it.next();
kSession.insert(object);
}
kSession.insert(new Count(1));
final long start = System.currentTimeMillis();
kSession.fireAllRules();
// System.err.println( System.currentTimeMillis() - start );
// System.out.println( listener );
// while (1==1){
// Thread.yield();
// Thread.sleep( 2000 );
// }
// final MemoryVisitor visitor = new MemoryVisitor( (InternalWorkingMemory) workingMemory );
// visitor.visit( ruleBase );
// final ReteooJungViewer viewer = new ReteooJungViewer(ruleBase);
//
// javax.swing.SwingUtilities.invokeLater(new Runnable() {
// public void run() {
// viewer.showGUI();
// }
// });
//
// Thread.sleep( 10000 );
}
use of org.drools.core.event.DefaultAgendaEventListener in project jbpm by kiegroup.
the class ProcessFlowControlTest method testRuleFlowClear.
@Test
public void testRuleFlowClear() throws Exception {
builder.addPackageFromDrl(new InputStreamReader(getClass().getResourceAsStream("test_ruleflowClear.drl")));
builder.addRuleFlow(new InputStreamReader(getClass().getResourceAsStream("test_ruleflowClear.rfm")));
KieSession workingMemory = createKieSession(true, builder.getPackages());
final List<String> list = new ArrayList<String>();
workingMemory.setGlobal("list", list);
final List<Match> activations = new ArrayList<Match>();
AgendaEventListener listener = new DefaultAgendaEventListener() {
public void matchCancelled(MatchCancelledEvent event) {
activations.add(event.getMatch());
}
};
workingMemory.addEventListener(listener);
InternalAgenda agenda = (InternalAgenda) workingMemory.getAgenda();
// assertEquals( 0,
// agenda.getRuleFlowGroup( "flowgroup-1" ).size() );
// We need to call fireAllRules here to get the InitialFact into the system, to the eval(true)'s kick in
workingMemory.fireAllRules();
agenda.evaluateEagerList();
// Now we have 4 in the RuleFlow, but not yet in the agenda
assertEquals(4, agenda.sizeOfRuleFlowGroup("flowgroup-1"));
// Check they aren't in the Agenda
assertEquals(0, agenda.getAgendaGroup("MAIN").size());
// Check we have 0 activation cancellation events
assertEquals(0, activations.size());
((InternalAgenda) workingMemory.getAgenda()).clearAndCancelRuleFlowGroup("flowgroup-1");
// Check the AgendaGroup and RuleFlowGroup are now empty
assertEquals(0, agenda.getAgendaGroup("MAIN").size());
assertEquals(0, agenda.sizeOfRuleFlowGroup("flowgroup-1"));
// Check we have four activation cancellation events
assertEquals(4, activations.size());
}
Aggregations