use of org.kie.api.runtime.StatelessKieSession in project drools by kiegroup.
the class SequentialTest method testSalience.
@Test
public void testSalience() throws Exception {
KieBase kbase = loadKnowledgeBase(kconf, "simpleSalience.drl");
StatelessKieSession ksession = createStatelessKnowledgeSession(kbase);
final List list = new ArrayList();
ksession.setGlobal("list", list);
ksession.execute(new Person("pob"));
assertEquals(3, list.size());
assertEquals("rule 3", list.get(0));
assertEquals("rule 2", list.get(1));
assertEquals("rule 1", list.get(2));
}
use of org.kie.api.runtime.StatelessKieSession 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.runtime.StatelessKieSession in project drools by kiegroup.
the class SequentialTest method testSharedSegment.
@Test
public void testSharedSegment() throws Exception {
// BZ-1228313
String str = "package org.drools.compiler.test\n" + "import \n" + Message.class.getCanonicalName() + ";" + "rule R1 when\n" + " $s : String()\n" + " $m : Message()\n" + " $i : Integer( this < $s.length )\n" + "then\n" + " modify($m) { setMessage($s) };\n" + "end\n" + "\n" + "rule R2 when\n" + " $s : String()\n" + " $m : Message()\n" + " $i : Integer( this > $s.length )\n" + "then\n" + "end\n";
StatelessKieSession ksession = new KieHelper().addContent(str, ResourceType.DRL).build(SequentialOption.YES).newStatelessKieSession();
ksession.execute(CommandFactory.newInsertElements(Arrays.asList("test", new Message(), 3, 5)));
}
use of org.kie.api.runtime.StatelessKieSession in project drools by kiegroup.
the class SequentialTest method testSequentialPlusPhreakRevisitOriginallyEmptyGroup.
@Test
public void testSequentialPlusPhreakRevisitOriginallyEmptyGroup() throws Exception {
String str = "";
str += "package org.drools.compiler.test\n";
str += "import " + A.class.getCanonicalName() + "\n";
str += "global " + List.class.getCanonicalName() + " list\n";
// Focus is done as g1, g2, g1 to demonstrate that groups will not re-activate
str += "rule r0 when\n";
str += "then\n";
str += " drools.getKnowledgeRuntime().getAgenda().getAgendaGroup( 'g1' ).setFocus();\n";
str += " drools.getKnowledgeRuntime().getAgenda().getAgendaGroup( 'g2' ).setFocus();\n";
str += " drools.getKnowledgeRuntime().getAgenda().getAgendaGroup( 'g1' ).setFocus();\n";
str += "end\n";
// r1_x is here to show they do not react when g2.r9 changes A o=2,
// i.e. checking that re-activating g1 won't cause it to pick up previous non evaluated rules.
// this is mostly checking that the none linking doesn't interfere with the expected results.
// additional checks this works if g1 never had any Matches on the first visit
str += "rule r1_x agenda-group 'g1' when\n";
str += " a : A( object == 2 )\n";
str += "then\n";
str += " list.add( drools.getRule().getName() );\n";
str += "end\n";
// This changes A o=2 to check if g1.r1_x incorrect reacts when g1 is re-entered
str += "rule r9 agenda-group 'g2' when\n";
str += " a : A(object >= 5 )\n";
str += "then\n";
str += " modify(a) { setObject( 2 ) };\n";
str += " list.add( drools.getRule().getName() );\n";
str += "end\n";
KieBase kbase = loadKnowledgeBaseFromString(kconf, str);
StatelessKieSession ksession = createStatelessKnowledgeSession(kbase);
final List list = new ArrayList();
ksession.setGlobal("list", list);
ksession.execute(CommandFactory.newInsertElements(Arrays.asList(new Object[] { new A(5) })));
assertEquals(1, list.size());
assertEquals("r9", list.get(0));
}
use of org.kie.api.runtime.StatelessKieSession in project drools by kiegroup.
the class StatelessSessionTest method testNotInStatelessSession.
@Test
public void testNotInStatelessSession() throws Exception {
final KieBaseConfiguration kbc = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kbc.setOption(SequentialOption.YES);
final KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase(kbc, "test_NotInStatelessSession.drl"));
final StatelessKieSession session = kbase.newStatelessKieSession();
final List list = new ArrayList();
session.setGlobal("list", list);
session.execute("not integer");
assertEquals("not integer", list.get(0));
}
Aggregations