Search in sources :

Example 16 with DebugList

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;
        }
    };
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) KieSession(org.kie.api.runtime.KieSession) DebugList(org.drools.compiler.util.debug.DebugList) Callable(java.util.concurrent.Callable)

Example 17 with DebugList

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());
}
Also used : Arrays(java.util.Arrays) ClassObjectType(org.drools.core.base.ClassObjectType) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TimeoutException(java.util.concurrent.TimeoutException) EntryPointId(org.drools.core.rule.EntryPointId) MultithreadEvaluationOption(org.kie.internal.conf.MultithreadEvaluationOption) Callable(java.util.concurrent.Callable) ClockType(org.drools.core.ClockType) ResourceType(org.kie.api.io.ResourceType) ArrayList(java.util.ArrayList) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Future(java.util.concurrent.Future) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CompositePartitionAwareObjectSinkAdapter(org.drools.core.reteoo.CompositePartitionAwareObjectSinkAdapter) EntryPointNode(org.drools.core.reteoo.EntryPointNode) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) ExecutorService(java.util.concurrent.ExecutorService) KieHelper(org.kie.internal.utils.KieHelper) EventProcessingOption(org.kie.api.conf.EventProcessingOption) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) DebugList(org.drools.compiler.util.debug.DebugList) Test(org.junit.Test) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) Executors(java.util.concurrent.Executors) FactHandle(org.kie.api.runtime.rule.FactHandle) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) ClockTypeOption(org.kie.api.runtime.conf.ClockTypeOption) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) KnowledgeBaseFactory(org.drools.core.impl.KnowledgeBaseFactory) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) Assert(org.junit.Assert) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) KieHelper(org.kie.internal.utils.KieHelper) CountDownLatch(java.util.concurrent.CountDownLatch) DebugList(org.drools.compiler.util.debug.DebugList) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 18 with DebugList

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());
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) DebugList(org.drools.compiler.util.debug.DebugList) Test(org.junit.Test)

Example 19 with DebugList

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();
}
Also used : DataSource(org.kie.api.runtime.rule.DataSource) UnitVar(org.kie.api.definition.rule.UnitVar) ResourceType(org.kie.api.io.ResourceType) ArrayList(java.util.ArrayList) Person(org.drools.compiler.Person) Arrays.asList(java.util.Arrays.asList) LongAddress(org.drools.compiler.LongAddress) KieServices(org.kie.api.KieServices) RuleUnitFactory(org.drools.core.ruleunit.RuleUnitFactory) Assert.fail(org.junit.Assert.fail) KieBase(org.kie.api.KieBase) KieHelper(org.kie.internal.utils.KieHelper) KieFileSystem(org.kie.api.builder.KieFileSystem) InternalRuleUnitExecutor(org.drools.core.impl.InternalRuleUnitExecutor) Semaphore(java.util.concurrent.Semaphore) Results(org.kie.api.builder.Results) KieContainer(org.kie.api.runtime.KieContainer) Assert.assertTrue(org.junit.Assert.assertTrue) DebugList(org.drools.compiler.util.debug.DebugList) Test(org.junit.Test) RuleUnitUtil.getUnitName(org.drools.core.ruleunit.RuleUnitUtil.getUnitName) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) FactHandle(org.kie.api.runtime.rule.FactHandle) RuleUnit(org.kie.api.runtime.rule.RuleUnit) List(java.util.List) RuleUnitExecutor(org.kie.api.runtime.rule.RuleUnitExecutor) Assert.assertFalse(org.junit.Assert.assertFalse) PropertySpecificOption(org.kie.internal.builder.conf.PropertySpecificOption) ClassUtils.getCanonicalSimpleName(org.drools.core.util.ClassUtils.getCanonicalSimpleName) Assert.assertEquals(org.junit.Assert.assertEquals) InternalRuleUnitExecutor(org.drools.core.impl.InternalRuleUnitExecutor) RuleUnitExecutor(org.kie.api.runtime.rule.RuleUnitExecutor) KieBase(org.kie.api.KieBase) KieHelper(org.kie.internal.utils.KieHelper) Semaphore(java.util.concurrent.Semaphore) DebugList(org.drools.compiler.util.debug.DebugList) Person(org.drools.compiler.Person) Test(org.junit.Test)

Aggregations

DebugList (org.drools.compiler.util.debug.DebugList)19 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)17 KieSession (org.kie.api.runtime.KieSession)17 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)16 Test (org.junit.Test)15 KieHelper (org.kie.internal.utils.KieHelper)15 KieBase (org.kie.api.KieBase)9 FactHandle (org.kie.api.runtime.rule.FactHandle)7 Callable (java.util.concurrent.Callable)6 ArrayList (java.util.ArrayList)5 List (java.util.List)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)5 ResourceType (org.kie.api.io.ResourceType)5 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)5 ClassObjectType (org.drools.core.base.ClassObjectType)4 EntryPointNode (org.drools.core.reteoo.EntryPointNode)4 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)4 PseudoClockScheduler (org.drools.core.time.impl.PseudoClockScheduler)4 Arrays (java.util.Arrays)3