use of org.drools.compiler.util.debug.DebugList in project drools by kiegroup.
the class ParallelEvaluationTest method getMultipleParallelKieSessionsWithInsertionsCallable.
private Callable<Void> getMultipleParallelKieSessionsWithInsertionsCallable(KieBase kBase, int ruleNr) {
return new Callable<Void>() {
@Override
public Void call() {
KieSession ksession = kBase.newKieSession();
assertTrue(((InternalWorkingMemory) ksession).getAgenda().isParallelAgenda());
List<Integer> list = new DebugList<Integer>();
ksession.setGlobal("list", list);
insertFacts(ksession, 10);
ksession.fireAllRules();
assertEquals(ruleNr, list.size());
return null;
}
};
}
use of org.drools.compiler.util.debug.DebugList in project drools by kiegroup.
the class ParallelEvaluationTest method testFireUntilHaltWithAsyncInsert.
@Test(timeout = 10000L)
public void testFireUntilHaltWithAsyncInsert() {
StringBuilder sb = new StringBuilder(400);
sb.append("global java.util.List list;\n");
for (int i = 0; i < 10; i++) {
sb.append(getRule(i, ""));
}
KieSession ksession = new KieHelper().addContent(sb.toString(), ResourceType.DRL).build(MultithreadEvaluationOption.YES).newKieSession();
assertTrue(((InternalWorkingMemory) ksession).getAgenda().isParallelAgenda());
StatefulKnowledgeSessionImpl session = (StatefulKnowledgeSessionImpl) ksession;
CountDownLatch done = new CountDownLatch(1);
DebugList<Integer> list = new DebugList<Integer>();
list.onItemAdded = (l -> {
if (l.size() == 10) {
ksession.halt();
done.countDown();
}
});
ksession.setGlobal("list", list);
new Thread(() -> ksession.fireUntilHalt()).start();
for (int i = 0; i < 10; i++) {
session.insertAsync(i);
session.insertAsync("" + i);
}
try {
done.await();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
assertEquals(10, list.size());
}
use of org.drools.compiler.util.debug.DebugList in project drools by kiegroup.
the class ParallelEvaluationTest method testWithAsyncInsertions.
@Test(timeout = 10000L)
public void testWithAsyncInsertions() {
StringBuilder sb = new StringBuilder(4000);
sb.append("global java.util.List list;\n");
int ruleNr = 200;
for (int i = 0; i < ruleNr; i++) {
sb.append(getRule(i, "insertAsync( $i + 10 );\ninsertAsync( \"\" + ($i + 10) );\n"));
}
KieSession ksession = new KieHelper().addContent(sb.toString(), ResourceType.DRL).build(MultithreadEvaluationOption.YES).newKieSession();
assertTrue(((InternalWorkingMemory) ksession).getAgenda().isParallelAgenda());
StatefulKnowledgeSessionImpl session = (StatefulKnowledgeSessionImpl) ksession;
List<Integer> list = new DebugList<Integer>();
ksession.setGlobal("list", list);
for (int i = 0; i < 10; i++) {
session.insertAsync(i);
session.insertAsync("" + i);
}
ksession.fireAllRules();
assertEquals(ruleNr, list.size());
}
use of org.drools.compiler.util.debug.DebugList in project drools by kiegroup.
the class RuleUnitTest method testReactiveDataSourceWithRunUntilHalt.
@Test(timeout = 10000L)
public void testReactiveDataSourceWithRunUntilHalt() throws Exception {
String drl1 = "import " + Person.class.getCanonicalName() + "\n" + "import " + ReactiveAdultUnit.class.getCanonicalName() + "\n" + "rule Adult @Unit( ReactiveAdultUnit.class ) when\n" + " Person(age >= 18, $name : name) from persons\n" + "then\n" + " System.out.println($name + \" is adult\");" + " list.add($name);\n" + "end";
KieBase kbase = new KieHelper().addContent(drl1, ResourceType.DRL).build();
RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kbase);
DebugList<String> list = new DebugList<>();
executor.bindVariable("list", list);
DataSource<Person> persons = executor.newDataSource("persons", new Person("Mario", 42));
ReactiveAdultUnit adultUnit = new ReactiveAdultUnit(persons, list);
Semaphore ready = new Semaphore(0, true);
list.onItemAdded = (l -> ready.release());
new Thread(() -> executor.runUntilHalt(adultUnit)).start();
ready.acquire();
assertEquals(1, list.size());
assertEquals("Mario", list.get(0));
list.clear();
list.onItemAdded = (l -> ready.release());
persons.insert(new Person("Sofia", 4));
persons.insert(new Person("Marilena", 44));
ready.acquire();
assertEquals(1, list.size());
assertEquals("Marilena", list.get(0));
executor.halt();
}
Aggregations