use of org.kie.api.KieBase in project drools by kiegroup.
the class MultithreadTest method testFireUntilHaltAndDispose.
@Test(timeout = 10000)
public void testFireUntilHaltAndDispose() throws InterruptedException {
// DROOLS-1103
final String drl = "rule R no-loop timer( int: 1s )\n" + "when\n" + " String()\n" + "then\n" + "end";
final KieHelper helper = new KieHelper();
helper.addContent(drl, ResourceType.DRL);
final KieBase kbase = helper.build(EventProcessingOption.STREAM);
final KieSessionConfiguration ksconf = KieServices.Factory.get().newKieSessionConfiguration();
ksconf.setOption(TimedRuleExecutionOption.YES);
final KieSession ksession = kbase.newKieSession(ksconf, null);
new Thread() {
@Override
public void run() {
ksession.fireUntilHalt();
}
}.start();
try {
Thread.sleep(100);
} catch (final InterruptedException e) {
// do nothing
}
ksession.insert("xxx");
try {
Thread.sleep(100);
} catch (final InterruptedException e) {
// do nothing
}
ksession.dispose();
}
use of org.kie.api.KieBase in project drools by kiegroup.
the class CommentTest method testCommentDelimiterInString.
@Test
public void testCommentDelimiterInString() throws Exception {
// JBRULES-3401
final String str = "rule x\n" + "dialect \"mvel\"\n" + "when\n" + "then\n" + "System.out.println( \"/*\" );\n" + "end\n";
final KieBase kbase = loadKnowledgeBaseFromString(str);
kbase.newKieSession();
}
use of org.kie.api.KieBase in project drools by kiegroup.
the class BindTest method testAutomaticBindings.
@Test
public void testAutomaticBindings() throws Exception {
final KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase("test_AutoBindings.drl"));
final KieSession ksession = createKnowledgeSession(kbase);
final List list = new ArrayList();
ksession.setGlobal("results", list);
final Person bob = new Person("bob", "stilton");
final Cheese stilton = new Cheese("stilton", 12);
ksession.insert(bob);
ksession.insert(stilton);
ksession.fireAllRules();
assertEquals(1, list.size());
assertEquals(bob, list.get(0));
}
use of org.kie.api.KieBase in project drools by kiegroup.
the class ConsequenceTest method testMVELConsequenceWithMapsAndArrays.
@Test
public void testMVELConsequenceWithMapsAndArrays() throws Exception {
String rule = "package org.drools.compiler.test;\n";
rule += "import java.util.ArrayList\n";
rule += "import java.util.HashMap\n";
rule += "global java.util.List list\n";
rule += "rule \"Test Rule\"\n";
rule += " dialect \"mvel\"";
rule += "when\n";
rule += "then\n";
rule += " m = new HashMap();\n";
rule += " l = new ArrayList();\n";
rule += " l.add(\"first\");\n";
rule += " m.put(\"content\", l);\n";
rule += " System.out.println(((ArrayList)m[\"content\"])[0]);\n";
rule += " list.add(((ArrayList)m[\"content\"])[0]);\n";
rule += "end";
final KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBaseFromString(rule));
KieSession session = createKnowledgeSession(kbase);
final List list = new ArrayList();
session.setGlobal("list", list);
session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, true);
session.fireAllRules();
assertEquals(1, ((List) session.getGlobal("list")).size());
assertEquals("first", ((List) session.getGlobal("list")).get(0));
}
use of org.kie.api.KieBase in project drools by kiegroup.
the class ConsequenceTest method testMVELConsequenceWithoutSemiColon1.
@Test
public void testMVELConsequenceWithoutSemiColon1() throws Exception {
String drl = "";
drl += "package test\n";
drl += "import org.drools.compiler.Person\n";
drl += "import org.drools.compiler.Pet\n";
drl += "rule test dialect 'mvel'\n";
drl += "when\n";
drl += " $person:Person()\n";
drl += " $pet:Pet()\n";
drl += "then\n";
drl += " delete($person) // some comment\n";
drl += " delete($pet) // another comment\n";
drl += "end\n";
final KieBase kbase = loadKnowledgeBaseFromString(drl);
final KieSession ksession = createKnowledgeSession(kbase);
// create working memory mock listener
final RuleRuntimeEventListener wml = Mockito.mock(RuleRuntimeEventListener.class);
ksession.addEventListener(wml);
final FactHandle personFH = ksession.insert(new Person("Toni"));
final FactHandle petFH = ksession.insert(new Pet("Toni"));
final int fired = ksession.fireAllRules();
assertEquals(1, fired);
// capture the arguments and check that the retracts happened
final ArgumentCaptor<ObjectDeletedEvent> retracts = ArgumentCaptor.forClass(ObjectDeletedEvent.class);
verify(wml, times(2)).objectDeleted(retracts.capture());
final List<ObjectDeletedEvent> values = retracts.getAllValues();
assertThat(values.get(0).getFactHandle(), is(personFH));
assertThat(values.get(1).getFactHandle(), is(petFH));
}
Aggregations