use of org.kie.api.event.rule.AfterMatchFiredEvent in project drools by kiegroup.
the class WorkingMemoryActionsSerializationTest method before.
@Before
public void before() {
ruleCalls.clear();
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(drl.getBytes()), ResourceType.DRL);
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
ksession = kbase.newKieSession();
ksession.addEventListener(new DefaultAgendaEventListener() {
@Override
public void afterMatchFired(final AfterMatchFiredEvent event) {
final String name = event.getMatch().getRule().getName();
System.out.println(name + " fired!");
synchronized (ruleCalls) {
Integer calls = ruleCalls.get(name);
if (calls == null) {
calls = 1;
} else {
calls++;
}
ruleCalls.put(name, calls);
}
}
});
// Using fire until halt. If firaAllRules is called it works. But for other reasons, I need to run fireUntilHalt
new Thread(new Runnable() {
@Override
public void run() {
ksession.fireUntilHalt();
}
}).start();
}
use of org.kie.api.event.rule.AfterMatchFiredEvent 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.kie.api.event.rule.AfterMatchFiredEvent in project drools by kiegroup.
the class StreamsTest method testMultipleWindows.
@Test(timeout = 10000)
public void testMultipleWindows() throws Exception {
String drl = "package org.drools.compiler\n" + "declare StockTick\n" + " @role(event)\n" + "end\n" + "rule FaultsCoincide\n" + "when\n" + " f1 : StockTick( company == \"RHT\" ) over window:length( 1 )\n" + " f2 : StockTick( company == \"JBW\" ) over window:length( 1 )\n" + "then\n" + "end";
KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kconf.setOption(EventProcessingOption.STREAM);
KieBase kbase = loadKnowledgeBaseFromString(kconf, drl);
KieSession ksession = kbase.newKieSession();
AgendaEventListener ael = mock(AgendaEventListener.class);
ksession.addEventListener(ael);
StockTick st1 = new StockTick(1, "RHT", 10, 1000);
ksession.insert(st1);
StockTick st2 = new StockTick(2, "JBW", 10, 1000);
ksession.insert(st2);
ksession.fireAllRules();
ArgumentCaptor<org.kie.api.event.rule.AfterMatchFiredEvent> captor = ArgumentCaptor.forClass(org.kie.api.event.rule.AfterMatchFiredEvent.class);
verify(ael, times(1)).afterMatchFired(captor.capture());
AfterMatchFiredEvent aafe = captor.getValue();
assertThat((StockTick) aafe.getMatch().getDeclarationValue("f1"), is(st1));
assertThat((StockTick) aafe.getMatch().getDeclarationValue("f2"), is(st2));
}
use of org.kie.api.event.rule.AfterMatchFiredEvent in project drools by kiegroup.
the class RuleChainingTest method testRuleChainingWithLogicalInserts.
@Test
public void testRuleChainingWithLogicalInserts() throws Exception {
final KieBase kbase = loadKnowledgeBase("test_RuleChaining.drl");
final KieSession ksession = createKnowledgeSession(kbase);
// create working memory mock listener
final RuleRuntimeEventListener wml = Mockito.mock(RuleRuntimeEventListener.class);
final org.kie.api.event.rule.AgendaEventListener ael = Mockito.mock(org.kie.api.event.rule.AgendaEventListener.class);
ksession.addEventListener(wml);
ksession.addEventListener(ael);
final int fired = ksession.fireAllRules();
assertEquals(3, fired);
// capture the arguments and check that the rules fired in the proper sequence
final ArgumentCaptor<AfterMatchFiredEvent> actvs = ArgumentCaptor.forClass(org.kie.api.event.rule.AfterMatchFiredEvent.class);
verify(ael, times(3)).afterMatchFired(actvs.capture());
final List<AfterMatchFiredEvent> values = actvs.getAllValues();
assertThat(values.get(0).getMatch().getRule().getName(), is("init"));
assertThat(values.get(1).getMatch().getRule().getName(), is("r1"));
assertThat(values.get(2).getMatch().getRule().getName(), is("r2"));
verify(ael, never()).matchCancelled(any(org.kie.api.event.rule.MatchCancelledEvent.class));
verify(wml, times(2)).objectInserted(any(org.kie.api.event.rule.ObjectInsertedEvent.class));
verify(wml, never()).objectDeleted(any(ObjectDeletedEvent.class));
}
use of org.kie.api.event.rule.AfterMatchFiredEvent in project drools by kiegroup.
the class JBRULESTest method testJBRULES3111.
@Test
public void testJBRULES3111() {
final String str = "package org.drools.compiler\n" + "declare Bool123\n" + " bool1 : boolean\n" + " bool2 : boolean\n" + " bool3 : boolean\n" + "end\n" + "declare Thing\n" + " name : String\n" + " bool123 : Bool123\n" + "end\n" + "rule kickOff\n" + "when\n" + "then\n" + " insert( new Thing( \"one\", new Bool123( true, false, false ) ) );\n" + " insert( new Thing( \"two\", new Bool123( false, false, false ) ) );\n" + " insert( new Thing( \"three\", new Bool123( false, false, false ) ) );\n" + "end\n" + "rule r1\n" + "when\n" + " $t: Thing( bool123.bool1 == true )\n" + "then\n" + "end\n" + "rule r2\n" + "when\n" + " $t: Thing( bool123.bool2 == true )\n" + "then\n" + "end\n" + "rule r3\n" + "when\n" + " $t: Thing( bool123.bool3 == true )\n" + "then\n" + "end";
final KieBase kbase = loadKnowledgeBaseFromString(str);
final KieSession ksession = createKnowledgeSession(kbase);
final org.kie.api.event.rule.AgendaEventListener ael = mock(org.kie.api.event.rule.AgendaEventListener.class);
ksession.addEventListener(ael);
final int rulesFired = ksession.fireAllRules();
assertEquals(2, rulesFired);
final ArgumentCaptor<AfterMatchFiredEvent> captor = ArgumentCaptor.forClass(org.kie.api.event.rule.AfterMatchFiredEvent.class);
verify(ael, times(2)).afterMatchFired(captor.capture());
final List<org.kie.api.event.rule.AfterMatchFiredEvent> aafe = captor.getAllValues();
Assert.assertThat(aafe.get(0).getMatch().getRule().getName(), is("kickOff"));
Assert.assertThat(aafe.get(1).getMatch().getRule().getName(), is("r1"));
final Object value = aafe.get(1).getMatch().getDeclarationValue("$t");
final String name = (String) MVEL.eval("$t.name", Collections.singletonMap("$t", value));
Assert.assertThat(name, is("one"));
}
Aggregations