Search in sources :

Example 1 with DebugList

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();
}
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)

Example 2 with DebugList

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

Example 3 with DebugList

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

Example 4 with DebugList

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

Example 5 with DebugList

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