use of org.kie.api.event.rule.AgendaEventListener in project drools by kiegroup.
the class FirstOrderLogicTest method testOrWithVariableResolution2.
// JBRULES-2526
@Test
public void testOrWithVariableResolution2() throws Exception {
// KieBase kbase = loadKnowledgeBase( "test_OrCEFollowedByMultipleEval.drl");
// KieSession workingMemory = createKnowledgeSession(kbase);
final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource("test_OrCEFollowedByMultipleEval2.drl", FirstOrderLogicTest.class), ResourceType.DRL);
assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
final InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
final KieSession ksession = createKnowledgeSession(kbase);
final AgendaEventListener al = mock(AgendaEventListener.class);
ksession.addEventListener(al);
ksession.insert(new FactA("a"));
ksession.insert(new FactB("b"));
ksession.insert(new FactC("c"));
ksession.fireAllRules();
verify(al, times(8)).afterMatchFired(any(AfterMatchFiredEvent.class));
}
use of org.kie.api.event.rule.AgendaEventListener in project drools by kiegroup.
the class DynamicRulesTest method testJBRULES_2206.
@Test(timeout = 10000)
public void testJBRULES_2206() {
KieBaseConfiguration config = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
((RuleBaseConfiguration) config).setRuleBaseUpdateHandler(null);
InternalKnowledgeBase kbase = (InternalKnowledgeBase) getKnowledgeBase(config);
KieSession session = createKnowledgeSession(kbase);
AgendaEventListener ael = mock(AgendaEventListener.class);
session.addEventListener(ael);
for (int i = 0; i < 5; i++) {
session.insert(new Cheese());
}
kbase.addPackages(loadKnowledgePackages("test_JBRULES_2206_1.drl"));
((InternalAgenda) session.getAgenda()).evaluateEagerList();
// two matching rules were added, so 2 activations should have been created
verify(ael, times(2)).matchCreated(any(MatchCreatedEvent.class));
int fireCount = session.fireAllRules();
// both should have fired
assertEquals(2, fireCount);
kbase.addPackages(loadKnowledgePackages("test_JBRULES_2206_2.drl"));
((InternalAgenda) session.getAgenda()).evaluateEagerList();
// one rule was overridden and should activate
verify(ael, times(3)).matchCreated(any(MatchCreatedEvent.class));
fireCount = session.fireAllRules();
// that rule should fire again
assertEquals(1, fireCount);
session.dispose();
}
use of org.kie.api.event.rule.AgendaEventListener in project drools by kiegroup.
the class ActivationIteratorTest method testFilteredEagerEvaluation.
@Test(timeout = 10000)
public void testFilteredEagerEvaluation() throws Exception {
String str = "package org.simple \n" + "rule xxx @Propagation(EAGER) \n" + "when \n" + " $s : String()\n" + "then \n" + "end \n" + "rule yyy @Propagation(EAGER) \n" + "when \n" + " $s : String()\n" + "then \n" + "end \n";
KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
conf.setOption(new ForceEagerActivationOption.FILTERED(new ForceEagerActivationFilter() {
@Override
public boolean accept(Rule rule) {
return rule.getName().equals("xxx");
}
}));
KieBase kbase = loadKnowledgeBaseFromString(str);
KieSession ksession = createKnowledgeSession(kbase, conf);
final List list = new ArrayList();
AgendaEventListener agendaEventListener = new DefaultAgendaEventListener() {
public void matchCreated(org.kie.api.event.rule.MatchCreatedEvent event) {
list.add("activated");
}
};
ksession.addEventListener(agendaEventListener);
ksession.insert("test");
((InternalWorkingMemory) ksession).flushPropagations();
assertEquals(1, list.size());
}
use of org.kie.api.event.rule.AgendaEventListener in project drools by kiegroup.
the class TraitTest method testIsAOperator.
@Test(timeout = 10000)
public void testIsAOperator() {
String source = "org/drools/compiler/factmodel/traits/testTraitIsA2.drl";
KieSession ksession = getSession(source);
TraitFactory.setMode(mode, ksession.getKieBase());
AgendaEventListener ael = mock(AgendaEventListener.class);
ksession.addEventListener(ael);
Person student = new Person("student", 18);
ksession.insert(student);
ksession.fireAllRules();
ArgumentCaptor<AfterMatchFiredEvent> cap = ArgumentCaptor.forClass(AfterMatchFiredEvent.class);
verify(ael, times(3)).afterMatchFired(cap.capture());
List<AfterMatchFiredEvent> values = cap.getAllValues();
assertThat(values.get(0).getMatch().getRule().getName(), is("create student"));
assertThat(values.get(1).getMatch().getRule().getName(), is("print student"));
assertThat(values.get(2).getMatch().getRule().getName(), is("print school"));
}
use of org.kie.api.event.rule.AgendaEventListener in project drools by kiegroup.
the class CepEspTest method testSalienceWithEventsPseudoClock.
@Test(timeout = 10000)
public void testSalienceWithEventsPseudoClock() throws IOException, ClassNotFoundException {
String str = "package org.drools.compiler\n" + "import " + StockTick.class.getName() + "\n" + "declare StockTick\n" + " @role ( event )\n" + "end\n" + "rule R1 salience 1000\n" + " when\n" + " $s1 : StockTick( company == 'RHT' )\n" + " $s2 : StockTick( company == 'ACME', this after[0s,1m] $s1 )\n" + " then\n" + "end\n" + "rule R2 salience 1000\n" + " when\n" + " $s1 : StockTick( company == 'RHT' )\n" + " not StockTick( company == 'ACME', this after[0s,1m] $s1 )\n" + " then\n" + "end\n" + "rule R3 salience 100\n" + " when\n" + " $s2 : StockTick( company == 'ACME' )\n" + " then\n" + "end\n";
KieBaseConfiguration config = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
config.setOption(EventProcessingOption.STREAM);
KieBase kbase = loadKnowledgeBaseFromString(config, str);
KieSessionConfiguration ksconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
ksconf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
KieSession ksession = kbase.newKieSession(ksconf, null);
AgendaEventListener ael = mock(AgendaEventListener.class);
ksession.addEventListener(ael);
SessionPseudoClock clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
clock.advanceTime(1000000, TimeUnit.MILLISECONDS);
ksession.insert(new StockTick(1, "RHT", 10, 1000));
clock.advanceTime(5, TimeUnit.SECONDS);
ksession.insert(new StockTick(2, "RHT", 10, 1000));
clock.advanceTime(5, TimeUnit.SECONDS);
ksession.insert(new StockTick(3, "RHT", 10, 1000));
clock.advanceTime(5, TimeUnit.SECONDS);
ksession.insert(new StockTick(4, "ACME", 10, 1000));
clock.advanceTime(5, TimeUnit.SECONDS);
int rulesFired = ksession.fireAllRules();
assertEquals(4, rulesFired);
ArgumentCaptor<AfterMatchFiredEvent> captor = ArgumentCaptor.forClass(AfterMatchFiredEvent.class);
verify(ael, times(4)).afterMatchFired(captor.capture());
List<AfterMatchFiredEvent> aafe = captor.getAllValues();
assertThat(aafe.get(0).getMatch().getRule().getName(), is("R1"));
assertThat(aafe.get(1).getMatch().getRule().getName(), is("R1"));
assertThat(aafe.get(2).getMatch().getRule().getName(), is("R1"));
assertThat(aafe.get(3).getMatch().getRule().getName(), is("R3"));
}
Aggregations