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