use of org.kie.api.runtime.StatelessKieSession in project drools by kiegroup.
the class MVELTest method testArrays.
@Test
public void testArrays() {
String text = "package test_mvel;\n";
text += "import " + TestObject.class.getCanonicalName() + ";\n";
text += "import function " + TestObject.class.getCanonicalName() + ".array;\n";
text += "no-loop true\n";
text += "dialect \"mvel\"\n";
text += "rule \"1\"\n";
text += "salience 1\n";
text += "when\n";
text += " $fact: TestObject()\n";
text += " eval($fact.checkHighestPriority(\"mvel\", 2))\n";
text += " eval($fact.stayHasDaysOfWeek(\"mvel\", false, new String[][]{{\"2008-04-01\", \"2008-04-10\"}}))\n";
text += "then\n";
text += " $fact.applyValueAddPromo(1,2,3,4,\"mvel\");\n";
text += "end";
final KieBase kieBase = loadKnowledgeBaseFromString(text.replaceAll("mvel", "java"), text);
final StatelessKieSession statelessKieSession = kieBase.newStatelessKieSession();
final List<String> list = new ArrayList<String>();
statelessKieSession.execute(new TestObject(list));
assertEquals(6, list.size());
assertTrue(list.containsAll(Arrays.asList("TestObject.checkHighestPriority: java|2", "TestObject.stayHasDaysOfWeek: java|false|[2008-04-01, 2008-04-10]", "TestObject.checkHighestPriority: mvel|2", "TestObject.stayHasDaysOfWeek: mvel|false|[2008-04-01, 2008-04-10]", "TestObject.applyValueAddPromo: 1|2|3|4|mvel", "TestObject.applyValueAddPromo: 1|2|3|4|java")));
}
use of org.kie.api.runtime.StatelessKieSession in project drools by kiegroup.
the class KieContainerDefaultsTest method testTwoKieSessionsOneStatelessDefault.
/**
* This test checks if default KieSessions behave as expected even if one of them is Stateless.
*/
@Test
public void testTwoKieSessionsOneStatelessDefault() {
final KieModuleModel kieModule = kieServices.newKieModuleModel();
kieModule.newKieBaseModel("firstKBase").setDefault(true).newKieSessionModel("firstKSession").setType(KieSessionModel.KieSessionType.STATELESS).setDefault(true);
kieModule.newKieBaseModel("secondKBase").setDefault(true).newKieSessionModel("secondKSession").setDefault(false);
buildKieModule(kieModule);
final KieContainer kieContainer = kieServices.newKieContainer(RELEASE_ID);
final StatelessKieSession firstKSession = kieContainer.newStatelessKieSession("firstKSession");
final KieSession secondKSession = kieContainer.newKieSession("secondKSession");
try {
Assertions.assertThat(firstKSession).isEqualTo(((InternalKieContainer) kieContainer).getStatelessKieSession());
Assertions.assertThat(secondKSession).isNotEqualTo(((InternalKieContainer) kieContainer).getStatelessKieSession());
} finally {
secondKSession.dispose();
}
}
use of org.kie.api.runtime.StatelessKieSession in project drools by kiegroup.
the class MultipleSheetsLoadingTest method test.
@Test
public void test() {
final KieBuilder kbuilder = this.buildResources();
final Collection<Message> results = kbuilder.getResults().getMessages(Level.ERROR, Level.WARNING);
if (results.size() > 0) {
LOGGER.error(results.toString());
}
Assertions.assertThat(results).as("Some errors/warnings found").isEmpty();
final KieBase kbase = KieBaseUtil.getDefaultKieBaseFromKieBuilder(kieBaseTestConfiguration, kbuilder);
final StatelessKieSession ksession = kbase.newStatelessKieSession();
final Set<String> resultSet = new HashSet<String>();
ksession.execute((Object) resultSet);
Assertions.assertThat(resultSet.size()).as("Wrong number of rules was fired").isEqualTo(2);
for (String ruleName : new String[] { "rule1", "rule2" }) {
Assertions.assertThat(resultSet.contains(ruleName)).as("Rule " + ruleName + " was not fired!").isTrue();
}
}
use of org.kie.api.runtime.StatelessKieSession in project drools by kiegroup.
the class Misc2Test method testListnersOnStatlessKieSession.
@Test
public void testListnersOnStatlessKieSession() {
// DROOLS-141
// BZ-999491
String str = "rule R when\n" + " String()\n" + "then\n" + "end\n";
KieBase kbase = loadKnowledgeBaseFromString(str);
StatelessKieSession ksession = kbase.newStatelessKieSession();
final List<String> firings = new ArrayList<String>();
AgendaEventListener agendaEventListener = new AgendaEventListener() {
public void matchCreated(org.kie.api.event.rule.MatchCreatedEvent event) {
}
public void matchCancelled(org.kie.api.event.rule.MatchCancelledEvent event) {
}
public void beforeMatchFired(org.kie.api.event.rule.BeforeMatchFiredEvent event) {
}
public void afterMatchFired(org.kie.api.event.rule.AfterMatchFiredEvent event) {
firings.add("Fired!");
}
public void agendaGroupPopped(org.kie.api.event.rule.AgendaGroupPoppedEvent event) {
}
public void agendaGroupPushed(org.kie.api.event.rule.AgendaGroupPushedEvent event) {
}
public void beforeRuleFlowGroupActivated(org.kie.api.event.rule.RuleFlowGroupActivatedEvent event) {
}
public void afterRuleFlowGroupActivated(org.kie.api.event.rule.RuleFlowGroupActivatedEvent event) {
}
public void beforeRuleFlowGroupDeactivated(org.kie.api.event.rule.RuleFlowGroupDeactivatedEvent event) {
}
public void afterRuleFlowGroupDeactivated(org.kie.api.event.rule.RuleFlowGroupDeactivatedEvent event) {
}
};
ksession.addEventListener(agendaEventListener);
ksession.execute("1");
ksession.execute("2");
assertEquals(2, firings.size());
ksession.removeEventListener(agendaEventListener);
ksession.execute("3");
assertEquals(2, firings.size());
}
use of org.kie.api.runtime.StatelessKieSession in project drools by kiegroup.
the class GlobalTest method testGlobalAccess.
@Test
public void testGlobalAccess() {
final String drl = "import org.drools.core.base.MapGlobalResolver;\n" + "global java.lang.String myGlobal;\n" + "global String unused; \n";
final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(drl.getBytes()), ResourceType.DRL);
final InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
final KieSession session1 = kbase.newKieSession();
final String sample = "default string";
// Testing 1.
System.out.println("Start testing 1.");
session1.setGlobal("myGlobal", "Testing 1");
session1.insert(sample);
session1.fireAllRules();
final Map.Entry[] entries1 = ((MapGlobalResolver) session1.getGlobals()).getGlobals();
assertEquals(1, entries1.length);
assertEquals(entries1[0].getValue(), "Testing 1");
assertEquals(1, session1.getGlobals().getGlobalKeys().size());
assertTrue(session1.getGlobals().getGlobalKeys().contains("myGlobal"));
session1.dispose();
// Testing 2.
System.out.println("Start testing 2.");
final StatelessKieSession session2 = session1.getKieBase().newStatelessKieSession();
session2.setGlobal("myGlobal", "Testing 2");
session2.execute(sample);
final Map.Entry[] entries2 = ((MapGlobalResolver) session2.getGlobals()).getGlobals();
assertEquals(1, entries2.length);
assertEquals(entries2[0].getValue(), "Testing 2");
assertEquals(1, session2.getGlobals().getGlobalKeys().size());
assertTrue(session2.getGlobals().getGlobalKeys().contains("myGlobal"));
// Testing 3.
System.out.println("Start testing 3.");
final StatefulKnowledgeSession session3 = ((StatelessKnowledgeSessionImpl) session2).newWorkingMemory();
session3.insert(sample);
session3.fireAllRules();
Map.Entry[] entries3 = ((MapGlobalResolver) session3.getGlobals()).getGlobals();
assertEquals(1, entries3.length);
assertEquals(entries3[0].getValue(), "Testing 2");
assertEquals(1, session3.getGlobals().getGlobalKeys().size());
assertTrue(session3.getGlobals().getGlobalKeys().contains("myGlobal"));
session3.setGlobal("myGlobal", "Testing 3 Over");
entries3 = ((MapGlobalResolver) session3.getGlobals()).getGlobals();
assertEquals(1, entries3.length);
assertEquals(entries3[0].getValue(), "Testing 3 Over");
assertEquals(1, session3.getGlobals().getGlobalKeys().size());
assertTrue(session3.getGlobals().getGlobalKeys().contains("myGlobal"));
session3.dispose();
// Testing 4.
System.out.println("Start testing 4.");
final StatefulKnowledgeSession session4 = ((StatelessKnowledgeSessionImpl) session2).newWorkingMemory();
session4.setGlobal("myGlobal", "Testing 4");
session4.insert(sample);
session4.fireAllRules();
final Map.Entry[] entries4 = ((MapGlobalResolver) session4.getGlobals()).getGlobals();
assertEquals(1, entries4.length);
assertEquals(entries4[0].getValue(), "Testing 4");
assertEquals(1, session4.getGlobals().getGlobalKeys().size());
assertTrue(session4.getGlobals().getGlobalKeys().contains("myGlobal"));
session4.dispose();
}
Aggregations