use of org.drools.compiler.util.debug.DebugList in project drools by kiegroup.
the class ParallelEvaluationTest method test.
@Test(timeout = 10000L)
public void test() {
StringBuilder sb = new StringBuilder(400);
sb.append("global java.util.List list;\n");
for (int i = 0; i < 10; i++) {
sb.append(getRule(i, ""));
}
KieBase kbase = new KieHelper().addContent(sb.toString(), ResourceType.DRL).build(MultithreadEvaluationOption.YES);
EntryPointNode epn = ((InternalKnowledgeBase) kbase).getRete().getEntryPointNode(EntryPointId.DEFAULT);
ObjectTypeNode otn = epn.getObjectTypeNodes().get(new ClassObjectType(Integer.class));
assertTrue(((CompositePartitionAwareObjectSinkAdapter) otn.getObjectSinkPropagator()).isHashed());
KieSession ksession = kbase.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(10, list.size());
}
use of org.drools.compiler.util.debug.DebugList in project drools by kiegroup.
the class ParallelEvaluationTest method testEventsExpiration.
@Test(timeout = 10000L)
public void testEventsExpiration() {
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( 20ms ) @timestamp( timestamp ) end\n");
for (int i = 0; i < 10; i++) {
sb.append(getRuleWithEvent(i));
}
KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
sessionConfig.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
KieSession ksession = new KieHelper().addContent(sb.toString(), ResourceType.DRL).build(EventProcessingOption.STREAM, MultithreadEvaluationOption.YES).newKieSession(sessionConfig, null);
assertTrue(((InternalWorkingMemory) ksession).getAgenda().isParallelAgenda());
PseudoClockScheduler sessionClock = ksession.getSessionClock();
sessionClock.setStartupTime(0);
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());
assertEquals(10L, ksession.getFactCount());
sessionClock.advanceTime(29, TimeUnit.MILLISECONDS);
ksession.fireAllRules();
assertEquals(5L, ksession.getFactCount());
sessionClock.advanceTime(12, TimeUnit.MILLISECONDS);
ksession.fireAllRules();
assertEquals(0L, ksession.getFactCount());
}
use of org.drools.compiler.util.debug.DebugList in project drools by kiegroup.
the class ParallelEvaluationTest method testFireUntilHaltWithExpiration.
@Test(timeout = 10000L)
public void testFireUntilHaltWithExpiration() {
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( 20ms ) @timestamp( timestamp ) end\n");
for (int i = 0; i < 10; i++) {
sb.append(getRuleWithEventForExpiration(i));
}
KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
sessionConfig.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
KieSession ksession = new KieHelper().addContent(sb.toString(), ResourceType.DRL).build(EventProcessingOption.STREAM, MultithreadEvaluationOption.YES).newKieSession(sessionConfig, null);
assertTrue(((InternalWorkingMemory) ksession).getAgenda().isParallelAgenda());
PseudoClockScheduler sessionClock = ksession.getSessionClock();
sessionClock.setStartupTime(0);
DebugList<Integer> list = new DebugList<Integer>();
CountDownLatch done1 = new CountDownLatch(1);
list.onItemAdded = (l -> {
if (l.size() == 10) {
done1.countDown();
}
});
ksession.setGlobal("list", list);
for (int i = 0; i < 10; i++) {
ksession.insert(new MyEvent(i, i * 2L));
}
new Thread(() -> ksession.fireUntilHalt()).start();
try {
done1.await();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
assertEquals(10, list.size());
list.clear();
CountDownLatch done2 = new CountDownLatch(1);
list.onItemAdded = (l -> {
if (l.size() == 5) {
done2.countDown();
}
});
ksession.insert(1);
sessionClock.advanceTime(29, TimeUnit.MILLISECONDS);
try {
done2.await();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
assertEquals(5, list.size());
list.clear();
CountDownLatch done3 = new CountDownLatch(1);
list.onItemAdded = (l -> {
if (l.size() == 5) {
done3.countDown();
}
});
sessionClock.advanceTime(12, TimeUnit.MILLISECONDS);
try {
done3.await();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
assertEquals(5, list.size());
ksession.halt();
ksession.dispose();
}
use of org.drools.compiler.util.debug.DebugList in project drools by kiegroup.
the class ParallelEvaluationTest method testFireUntilHalt2.
@Test(timeout = 10000L)
public void testFireUntilHalt2() {
int rulesNr = 4;
int factsNr = 1;
int fireNr = rulesNr * factsNr;
String drl = "import " + A.class.getCanonicalName() + ";\n" + "import " + B.class.getCanonicalName() + ";\n" + "global java.util.concurrent.atomic.AtomicInteger counter\n" + "global java.util.concurrent.CountDownLatch done\n" + "global java.util.List list;\n";
for (int i = 0; i < rulesNr; i++) {
drl += getFireUntilHaltRule(fireNr, i);
}
KieBase kbase = new KieHelper().addContent(drl, ResourceType.DRL).build(MultithreadEvaluationOption.YES);
for (int loop = 0; loop < 10; loop++) {
System.out.println("Starting loop " + loop);
KieSession ksession = kbase.newKieSession();
assertTrue(((InternalWorkingMemory) ksession).getAgenda().isParallelAgenda());
CountDownLatch done = new CountDownLatch(1);
ksession.setGlobal("done", done);
AtomicInteger counter = new AtomicInteger(0);
ksession.setGlobal("counter", counter);
List<String> list = new DebugList<String>();
ksession.setGlobal("list", list);
new Thread(() -> {
ksession.fireUntilHalt();
}).start();
A a = new A(rulesNr + 1);
ksession.insert(a);
for (int i = 0; i < factsNr; i++) {
ksession.insert(new B(rulesNr + i + 3));
}
try {
done.await();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
assertEquals(fireNr, counter.get());
ksession.halt();
ksession.dispose();
System.out.println("Loop " + loop + " terminated");
}
}
use of org.drools.compiler.util.debug.DebugList in project drools by kiegroup.
the class ParallelEvaluationTest method testDisableParallelismWithSalience.
@Test(timeout = 10000L)
public void testDisableParallelismWithSalience() {
StringBuilder sb = new StringBuilder(400);
sb.append("global java.util.List list;\n");
for (int i = 0; i < 10; i++) {
sb.append(getRule(i, "", "salience " + i));
}
KieBase kbase = new KieHelper().addContent(sb.toString(), ResourceType.DRL).build(MultithreadEvaluationOption.YES);
KieSession ksession = kbase.newKieSession();
// multithread evaluation is not allowed when using salience
assertFalse(((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(10, list.size());
assertEquals(list, Arrays.asList(9, 8, 7, 6, 5, 4, 3, 2, 1, 0));
}
Aggregations