Search in sources :

Example 6 with StatefulKnowledgeSessionImpl

use of org.drools.kiesession.session.StatefulKnowledgeSessionImpl in project drools by kiegroup.

the class ParallelEvaluationTest method testFireUntilHaltWithAsyncInsert.

@Test(timeout = 40000L)
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, ""));
    }
    final KieModule kieModule = KieUtil.getKieModuleFromDrls("test", kieBaseTestConfiguration, sb.toString());
    final KieBase kbase = KieBaseUtil.newKieBaseFromKieModuleWithAdditionalOptions(kieModule, kieBaseTestConfiguration, MultithreadEvaluationOption.YES);
    KieSession ksession = kbase.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();
    try {
        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());
    } finally {
        ksession.halt();
        ksession.dispose();
    }
}
Also used : Arrays(java.util.Arrays) ClassObjectType(org.drools.core.base.ClassObjectType) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RunWith(org.junit.runner.RunWith) 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) StatefulKnowledgeSessionImpl(org.drools.kiesession.session.StatefulKnowledgeSessionImpl) KieBaseTestConfiguration(org.drools.testcoverage.common.util.KieBaseTestConfiguration) ArrayList(java.util.ArrayList) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Future(java.util.concurrent.Future) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RuleBaseFactory(org.drools.core.impl.RuleBaseFactory) CompositePartitionAwareObjectSinkAdapter(org.drools.core.reteoo.CompositePartitionAwareObjectSinkAdapter) EntryPointNode(org.drools.core.reteoo.EntryPointNode) TestParametersUtil(org.drools.testcoverage.common.util.TestParametersUtil) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) ExecutorService(java.util.concurrent.ExecutorService) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Parameterized(org.junit.runners.Parameterized) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) Collection(java.util.Collection) Assert.assertTrue(org.junit.Assert.assertTrue) 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) KieUtil(org.drools.testcoverage.common.util.KieUtil) KieModule(org.kie.api.builder.KieModule) DebugList(org.drools.mvel.compiler.util.debug.DebugList) ClockTypeOption(org.kie.api.runtime.conf.ClockTypeOption) Ignore(org.junit.Ignore) Assert.assertFalse(org.junit.Assert.assertFalse) KieBaseUtil(org.drools.testcoverage.common.util.KieBaseUtil) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) Assert.assertEquals(org.junit.Assert.assertEquals) CountDownLatch(java.util.concurrent.CountDownLatch) DebugList(org.drools.mvel.compiler.util.debug.DebugList) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) KieBase(org.kie.api.KieBase) StatefulKnowledgeSessionImpl(org.drools.kiesession.session.StatefulKnowledgeSessionImpl) KieSession(org.kie.api.runtime.KieSession) KieModule(org.kie.api.builder.KieModule) Test(org.junit.Test)

Example 7 with StatefulKnowledgeSessionImpl

use of org.drools.kiesession.session.StatefulKnowledgeSessionImpl in project drools by kiegroup.

the class ParallelEvaluationTest method testWithAsyncInsertions.

@Test(timeout = 40000L)
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"));
    }
    final KieModule kieModule = KieUtil.getKieModuleFromDrls("test", kieBaseTestConfiguration, sb.toString());
    final KieBase kbase = KieBaseUtil.newKieBaseFromKieModuleWithAdditionalOptions(kieModule, kieBaseTestConfiguration, MultithreadEvaluationOption.YES);
    KieSession ksession = kbase.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) KieBase(org.kie.api.KieBase) StatefulKnowledgeSessionImpl(org.drools.kiesession.session.StatefulKnowledgeSessionImpl) KieSession(org.kie.api.runtime.KieSession) DebugList(org.drools.mvel.compiler.util.debug.DebugList) KieModule(org.kie.api.builder.KieModule) Test(org.junit.Test)

Example 8 with StatefulKnowledgeSessionImpl

use of org.drools.kiesession.session.StatefulKnowledgeSessionImpl in project drools by kiegroup.

the class ReteEvaluatorTest method testPropertyReactivity.

@Test
public void testPropertyReactivity() {
    String str = "import " + Person.class.getCanonicalName() + ";" + "rule R when\n" + "  $s : String()\n" + "  $p : Person(name == $s)\n" + "then\n" + "  modify($p) { setAge($p.getAge()+1) }\n" + "end";
    ReteEvaluator reteEvaluator = new StatefulKnowledgeSessionImpl(1L, getKBase(str));
    Person me = new Person("Mario", 40);
    reteEvaluator.insert("Mario");
    reteEvaluator.insert(me);
    assertEquals(1, reteEvaluator.fireAllRules());
    assertEquals(41, me.getAge());
}
Also used : ReteEvaluator(org.drools.core.common.ReteEvaluator) StatefulKnowledgeSessionImpl(org.drools.kiesession.session.StatefulKnowledgeSessionImpl) Person(org.drools.mvel.compiler.Person) Test(org.junit.Test)

Example 9 with StatefulKnowledgeSessionImpl

use of org.drools.kiesession.session.StatefulKnowledgeSessionImpl in project drools by kiegroup.

the class FieldConstraintTest method testPrimitiveLiteralConstraint.

/**
 * <pre>
 *
 *                Cheese( price == 5 )
 *
 * </pre>
 */
@Test
public void testPrimitiveLiteralConstraint() {
    InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
    ;
    StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
    final ClassFieldReader extractor = store.getReader(Cheese.class, "price");
    AlphaNodeFieldConstraint constraint = ConstraintTestUtil.createCheesePriceEqualsConstraint(extractor, 5, useLambdaConstraint);
    final Cheese cheddar = new Cheese("cheddar", 5);
    final InternalFactHandle cheddarHandle = (InternalFactHandle) ksession.insert(cheddar);
    // check constraint
    assertTrue(constraint.isAllowed(cheddarHandle, ksession));
    final Cheese stilton = new Cheese("stilton", 10);
    final InternalFactHandle stiltonHandle = (InternalFactHandle) ksession.insert(stilton);
    // check constraint
    assertFalse(constraint.isAllowed(stiltonHandle, ksession));
}
Also used : AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) ClassFieldReader(org.drools.mvel.accessors.ClassFieldReader) StatefulKnowledgeSessionImpl(org.drools.kiesession.session.StatefulKnowledgeSessionImpl) Cheese(org.drools.mvel.model.Cheese) InternalFactHandle(org.drools.core.common.InternalFactHandle) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Test(org.junit.Test)

Example 10 with StatefulKnowledgeSessionImpl

use of org.drools.kiesession.session.StatefulKnowledgeSessionImpl in project drools by kiegroup.

the class KnowledgeBuilderTest method testSerializable.

@Test
public void testSerializable() throws Exception {
    final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
    final PackageDescr packageDescr = new PackageDescr("p1");
    final RuleDescr ruleDescr = new RuleDescr("rule-1");
    packageDescr.addRule(ruleDescr);
    final AndDescr lhs = new AndDescr();
    ruleDescr.setLhs(lhs);
    packageDescr.addGlobal(new GlobalDescr("map", "java.util.Map"));
    ruleDescr.setConsequence("map.put(\"value\", new Integer(1) );");
    // check that packageDescr is serializable
    final PackageDescr back = (PackageDescr) SerializationHelper.serializeObject(packageDescr);
    assertNotNull(back);
    assertEquals("p1", back.getName());
    builder.addPackage(packageDescr);
    InternalKnowledgePackage pkg = builder.getPackage(packageDescr.getName());
    final RuleImpl rule = pkg.getRule("rule-1");
    assertLength(0, builder.getErrors().getErrors());
    InternalKnowledgePackage newPkg = SerializationHelper.serializeObject(pkg);
    final RuleImpl newRule = newPkg.getRule("rule-1");
    InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
    ;
    // It's been serialised so we have to simulate the re-wiring process
    newPkg.getDialectRuntimeRegistry().onAdd(kBase.getRootClassLoader());
    newPkg.getDialectRuntimeRegistry().onBeforeExecute();
    kBase.getGlobals().put("map", Map.class);
    final KieSession workingMemory = kBase.newKieSession();
    final HashMap map = new HashMap();
    workingMemory.setGlobal("map", map);
    final LeftTupleImpl tuple = new MockTuple(new HashMap());
    tuple.setLeftTupleSink(new RuleTerminalNode(1, new MockBetaNode(), newRule, newRule.getLhs(), 0, new BuildContext(kBase, Collections.emptyList())));
    final Activation activation = new MockActivation(newRule, 0, newRule.getLhs(), tuple);
    final DefaultKnowledgeHelper knowledgeHelper = new DefaultKnowledgeHelper(((StatefulKnowledgeSessionImpl) workingMemory));
    knowledgeHelper.setActivation(activation);
    newRule.getConsequence().evaluate(knowledgeHelper, ((StatefulKnowledgeSessionImpl) workingMemory));
    assertEquals(new Integer(1), map.get("value"));
}
Also used : GlobalDescr(org.drools.drl.ast.descr.GlobalDescr) HashMap(java.util.HashMap) AndDescr(org.drools.drl.ast.descr.AndDescr) DefaultKnowledgeHelper(org.drools.kiesession.consequence.DefaultKnowledgeHelper) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Activation(org.drools.core.spi.Activation) BuildContext(org.drools.core.reteoo.builder.BuildContext) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) MockBetaNode(org.drools.mvel.MockBetaNode) StatefulKnowledgeSessionImpl(org.drools.kiesession.session.StatefulKnowledgeSessionImpl) RuleDescr(org.drools.drl.ast.descr.RuleDescr) LeftTupleImpl(org.drools.core.reteoo.LeftTupleImpl) KieSession(org.kie.api.runtime.KieSession) PackageDescr(org.drools.drl.ast.descr.PackageDescr) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Test(org.junit.Test)

Aggregations

StatefulKnowledgeSessionImpl (org.drools.kiesession.session.StatefulKnowledgeSessionImpl)66 Test (org.junit.Test)57 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)34 ArrayList (java.util.ArrayList)15 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)15 KieBase (org.kie.api.KieBase)15 ClassObjectType (org.drools.core.base.ClassObjectType)14 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)14 BetaMemory (org.drools.core.reteoo.BetaMemory)14 List (java.util.List)13 InternalFactHandle (org.drools.core.common.InternalFactHandle)13 KieSession (org.kie.api.runtime.KieSession)12 FactHandle (org.kie.api.runtime.rule.FactHandle)11 MockObjectSink (org.drools.core.reteoo.MockObjectSink)10 BuildContext (org.drools.core.reteoo.builder.BuildContext)10 LeftTupleImpl (org.drools.core.reteoo.LeftTupleImpl)8 HashMap (java.util.HashMap)7 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)7 PathMemory (org.drools.core.reteoo.PathMemory)7 Rete (org.drools.core.reteoo.Rete)7