use of org.drools.compiler.util.debug.DebugList in project drools by kiegroup.
the class RuleUnitTest method testReactiveOnUnitCreatingDataSource.
@Test(timeout = 10000L)
public void testReactiveOnUnitCreatingDataSource() throws Exception {
// DROOLS-1647
String drl1 = "package org.drools.compiler.integrationtests\n" + "unit " + getCanonicalSimpleName(AdultUnitCreatingDataSource.class) + "\n" + "import " + Person.class.getCanonicalName() + "\n" + "rule Adult 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);
AdultUnitCreatingDataSource adultUnit = new AdultUnitCreatingDataSource(list);
adultUnit.insertPerson(new Person("Mario", 42));
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());
adultUnit.insertPerson(new Person("Sofia", 4));
adultUnit.insertPerson(new Person("Marilena", 44));
ready.acquire();
assertEquals(1, list.size());
assertEquals("Marilena", list.get(0));
executor.halt();
}
use of org.drools.compiler.util.debug.DebugList in project drools by kiegroup.
the class ParallelEvaluationTest method testWithInsertions.
@Test(timeout = 10000L)
public void testWithInsertions() {
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, "insert( $i + 10 );\ninsert( \"\" + ($i + 10) );\n"));
}
KieSession ksession = new KieHelper().addContent(sb.toString(), ResourceType.DRL).build(MultithreadEvaluationOption.YES).newKieSession();
assertTrue(((InternalWorkingMemory) ksession).getAgenda().isParallelAgenda());
List<Integer> list = new DebugList<Integer>();
ksession.setGlobal("list", list);
for (int i = 0; i < 10; i++) {
ksession.insert(i);
ksession.insert("" + i);
}
ksession.fireAllRules();
assertEquals(ruleNr, list.size());
}
use of org.drools.compiler.util.debug.DebugList in project drools by kiegroup.
the class ParallelEvaluationTest method getMultipleParallelKieSessionsFireUntilHaltCallable.
private Callable<Void> getMultipleParallelKieSessionsFireUntilHaltCallable(KieBase kBase, boolean asyncInsert) {
return () -> {
KieSession ksession = kBase.newKieSession();
assertThat(((InternalWorkingMemory) ksession).getAgenda().isParallelAgenda()).isTrue();
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();
if (asyncInsert) {
StatefulKnowledgeSessionImpl session = (StatefulKnowledgeSessionImpl) ksession;
for (int i = 0; i < 10; i++) {
session.insertAsync(i);
session.insertAsync("" + String.valueOf(i));
}
} else {
insertFacts(ksession, 10);
}
try {
done.await();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
assertThat(list.size()).isEqualTo(10);
return null;
};
}
use of org.drools.compiler.util.debug.DebugList in project drools by kiegroup.
the class ParallelEvaluationTest method getMultipleParallelKieSessionsWithUpdatesCallable.
private Callable<Void> getMultipleParallelKieSessionsWithUpdatesCallable(KieBase kBase) {
return new Callable<Void>() {
@Override
public Void call() {
KieSession ksession = kBase.newKieSession();
assertThat(((InternalWorkingMemory) ksession).getAgenda().isParallelAgenda()).as("Parallel agenda has to be enabled").isTrue();
List<Integer> list = new DebugList<Integer>();
ksession.setGlobal("list", list);
FactHandle[] fhs = new FactHandle[10];
fhs = insertFacts(ksession, 10);
ksession.fireAllRules();
assertThat(list.size()).isEqualTo(10);
list.clear();
for (int i = 0; i < 10; i++) {
ksession.update(fhs[i], i);
}
ksession.fireAllRules();
assertThat(list.size()).isEqualTo(10);
return null;
}
};
}
use of org.drools.compiler.util.debug.DebugList in project drools by kiegroup.
the class ParallelEvaluationTest method testImmediateEventsExpiration.
@Test(timeout = 10000L)
public void testImmediateEventsExpiration() {
StringBuilder sb = new StringBuilder(400);
sb.append("global java.util.List list;\n");
sb.append("import " + MyEvent.class.getCanonicalName() + ";\n");
sb.append("declare MyEvent @role( event ) @expires( 1ms ) @timestamp( timestamp ) end\n");
for (int i = 0; i < 10; i++) {
sb.append(getRuleWithEvent(i));
}
KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
KieSession ksession = new KieHelper().addContent(sb.toString(), ResourceType.DRL).build(EventProcessingOption.STREAM, MultithreadEvaluationOption.YES).newKieSession(sessionConfig, null);
assertTrue(((InternalWorkingMemory) ksession).getAgenda().isParallelAgenda());
List<Integer> list = new DebugList<Integer>();
ksession.setGlobal("list", list);
for (int i = 0; i < 10; i++) {
ksession.insert(new MyEvent(i, i * 2L));
}
ksession.fireAllRules();
assertEquals(10, list.size());
}
Aggregations