use of org.kie.api.runtime.KieSessionConfiguration in project drools by kiegroup.
the class ActivateAndDeleteOnListenerTest method testSegMemInitializationWithForceEagerActivationAndAcc.
@Test(timeout = 10000L)
public void testSegMemInitializationWithForceEagerActivationAndAcc() throws InterruptedException {
// DROOLS-1247
String drl = "global java.util.List list\n" + "declare SimpleFact end\n" + "declare AnotherFact end\n" + "\n" + "rule Init when\n" + " not (SimpleFact())\n" + " not (AnotherFact())\n" + "then\n" + " insert(new SimpleFact());\n" + " insert(new AnotherFact());\n" + "end\n" + "\n" + "rule R1 no-loop when\n" + " $f : SimpleFact() \n" + " $h : AnotherFact() \n" + " $s : String() \n" + " accumulate($i: Integer(), $res : count($i))\n" + "then\n" + " list.add(\"1\");\n" + "end\n" + "\n" + "rule R2 no-loop when\n" + " $f : SimpleFact() \n" + " $h : AnotherFact() \n" + " $s : String() \n" + "then\n" + " list.add(\"2\");\n" + "end";
KieSessionConfiguration config = KieServices.Factory.get().newKieSessionConfiguration();
config.setOption(ForceEagerActivationOption.YES);
final KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession(config, null);
List<String> list = new ArrayList<String>();
ksession.setGlobal("list", list);
ksession.insert("test");
ksession.fireAllRules();
assertEquals(2, list.size());
assertTrue(list.containsAll(asList("1", "2")));
}
use of org.kie.api.runtime.KieSessionConfiguration 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.kie.api.runtime.KieSessionConfiguration in project drools by kiegroup.
the class ActivateAndDeleteOnListenerTest method testOneLinkedAndOneUnlinkedPath.
@Test
public void testOneLinkedAndOneUnlinkedPath() throws Exception {
String str = "package org.simple \n" + "rule xxx \n" + "when \n" + " String()\n" + " Integer()\n" + " Long()\n" + "then \n" + "end \n" + "rule yyy \n" + "when \n" + " String()\n" + " Integer()\n" + " Boolean()\n" + "then \n" + "end \n";
KieServices ks = KieServices.Factory.get();
KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
conf.setOption(ForceEagerActivationOption.YES);
KieSession ksession = new KieHelper().addContent(str, ResourceType.DRL).build().newKieSession(conf, null);
final List list = new ArrayList();
AgendaEventListener agendaEventListener = new org.kie.api.event.rule.DefaultAgendaEventListener() {
public void matchCreated(org.kie.api.event.rule.MatchCreatedEvent event) {
list.add(event.getMatch().getRule().getName());
}
};
ksession.addEventListener(agendaEventListener);
ksession.insert("test");
assertEquals(0, list.size());
ksession.insert(Boolean.TRUE);
assertEquals(0, list.size());
ksession.insert(1);
assertEquals(1, list.size());
assertEquals("yyy", list.get(0));
}
use of org.kie.api.runtime.KieSessionConfiguration 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.kie.api.runtime.KieSessionConfiguration in project drools by kiegroup.
the class ActivateAndDeleteOnListenerTest method testOneLazyAndOneImmediateSubPath.
@Test
public void testOneLazyAndOneImmediateSubPath() throws Exception {
String str = "package org.simple \n" + "rule xxx \n" + "when \n" + " $s : String()\n" + " exists( Integer() or Long() )\n" + "then \n" + "end \n" + "rule yyy \n" + "when \n" + " $s : String()\n" + " exists( Integer() or Long() )\n" + "then \n" + "end \n";
KieServices ks = KieServices.Factory.get();
KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
conf.setOption(new ForceEagerActivationOption.FILTERED(new ForceEagerActivationFilter() {
@Override
public boolean accept(Rule rule) {
return rule.getName().equals("yyy");
}
}));
KieSession ksession = new KieHelper().addContent(str, ResourceType.DRL).build().newKieSession(conf, null);
final List list = new ArrayList();
AgendaEventListener agendaEventListener = new org.kie.api.event.rule.DefaultAgendaEventListener() {
public void matchCreated(org.kie.api.event.rule.MatchCreatedEvent event) {
list.add(event.getMatch().getRule().getName());
}
};
ksession.addEventListener(agendaEventListener);
ksession.insert("test");
assertEquals(0, list.size());
ksession.insert(1);
assertEquals(1, list.size());
assertEquals("yyy", list.get(0));
}
Aggregations