use of org.drools.kiesession.session.StatefulKnowledgeSessionImpl in project drools by kiegroup.
the class MVELSalienceBuilderTest method testSimpleExpression.
@Test
public void testSimpleExpression() {
StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
final Person p = new Person("mark", "", 31);
final InternalFactHandle f0 = (InternalFactHandle) ksession.insert(p);
MockLeftTupleSink sink = new MockLeftTupleSink(buildContext);
MockTupleSource source = new MockTupleSource(1, buildContext);
source.setObjectCount(1);
sink.setLeftTupleSource(source);
final LeftTupleImpl tuple = new LeftTupleImpl(f0, sink, true);
RuleTerminalNode rtn = new RuleTerminalNode();
rtn.setSalienceDeclarations(context.getDeclarationResolver().getDeclarations(context.getRule()).values().toArray(new Declaration[1]));
AgendaItem item = new AgendaItemImpl(0, tuple, 0, null, rtn, null);
assertEquals(25, context.getRule().getSalience().getValue(item, context.getRule(), ksession));
}
use of org.drools.kiesession.session.StatefulKnowledgeSessionImpl 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;
};
}
use of org.drools.kiesession.session.StatefulKnowledgeSessionImpl in project drools by kiegroup.
the class KnowledgeBuilderTest method testReload.
@Test
public void testReload() 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) );");
builder.addPackage(packageDescr);
InternalKnowledgePackage pkg = builder.getPackage(packageDescr.getName());
RuleImpl rule = pkg.getRule("rule-1");
assertLength(0, builder.getErrors().getErrors());
InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
kBase.addGlobal("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(), rule, rule.getLhs(), 0, new BuildContext(kBase, Collections.emptyList())));
final Activation activation = new MockActivation(rule, 0, rule.getLhs(), tuple);
DefaultKnowledgeHelper knowledgeHelper = new DefaultKnowledgeHelper(((StatefulKnowledgeSessionImpl) workingMemory));
knowledgeHelper.setActivation(activation);
rule.getConsequence().evaluate(knowledgeHelper, ((StatefulKnowledgeSessionImpl) workingMemory));
assertEquals(new Integer(1), map.get("value"));
ruleDescr.setConsequence("map.put(\"value\", new Integer(2) );");
pkg.removeRule(rule);
// Make sure the compiled classes are also removed
assertEquals(0, ((JavaDialectRuntimeData) pkg.getDialectRuntimeRegistry().getDialectData("java")).getStore().size());
builder.addPackage(packageDescr);
pkg = builder.getPackage(packageDescr.getName());
rule = pkg.getRule("rule-1");
knowledgeHelper = new DefaultKnowledgeHelper(((StatefulKnowledgeSessionImpl) workingMemory));
knowledgeHelper.setActivation(activation);
rule.getConsequence().evaluate(knowledgeHelper, ((StatefulKnowledgeSessionImpl) workingMemory));
assertEquals(new Integer(2), map.get("value"));
}
use of org.drools.kiesession.session.StatefulKnowledgeSessionImpl in project drools by kiegroup.
the class LinkingTest method testAccumulateNodes2.
@Test
public void testAccumulateNodes2() throws Exception {
String str = "";
str += "package org.kie \n";
str += "import " + A.class.getCanonicalName() + "\n";
str += "import " + B.class.getCanonicalName() + "\n";
str += "import " + C.class.getCanonicalName() + "\n";
str += "import " + X.class.getCanonicalName() + "\n";
str += "import " + E.class.getCanonicalName() + "\n";
str += "import " + F.class.getCanonicalName() + "\n";
str += "import " + G.class.getCanonicalName() + "\n";
str += "global java.util.List list \n";
str += "rule rule1 when \n";
str += " A() \n";
str += " accumulate( $a : B(); $l : collectList( $a ) ) \n";
str += " C() \n";
str += "then \n";
str += " list.add( $l.size() ); \n";
str += "end \n";
KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, str);
KieSession wm = kbase.newKieSession();
List list = new ArrayList();
wm.setGlobal("list", list);
wm.fireAllRules();
assertEquals(0, list.size());
wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
list = new ArrayList();
wm.setGlobal("list", list);
FactHandle fh1 = wm.insert(new B(1));
FactHandle fh2 = wm.insert(new B(2));
FactHandle fh3 = wm.insert(new B(3));
FactHandle fh4 = wm.insert(new B(4));
FactHandle fha = wm.insert(new A(1));
FactHandle fhc = wm.insert(new C(1));
wm.fireAllRules();
assertEquals(4, list.get(0));
}
use of org.drools.kiesession.session.StatefulKnowledgeSessionImpl in project drools by kiegroup.
the class LinkingTest method testNotNodes2.
@Test
public void testNotNodes2() throws Exception {
String str = "";
str += "package org.kie \n";
str += "import " + A.class.getCanonicalName() + "\n";
str += "import " + B.class.getCanonicalName() + "\n";
str += "import " + C.class.getCanonicalName() + "\n";
str += "import " + X.class.getCanonicalName() + "\n";
str += "import " + E.class.getCanonicalName() + "\n";
str += "import " + F.class.getCanonicalName() + "\n";
str += "import " + G.class.getCanonicalName() + "\n";
str += "global java.util.List list \n";
str += "rule rule1 when \n";
str += " $a : A() \n";
str += " not B() \n";
str += " $c : C() \n";
str += "then \n";
str += " list.add( 'x' ); \n";
str += "end \n";
KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, str);
ObjectTypeNode aotn = getObjectTypeNode(kbase, A.class);
ObjectTypeNode botn = getObjectTypeNode(kbase, A.class);
ObjectTypeNode cotn = getObjectTypeNode(kbase, A.class);
InternalWorkingMemory wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
List list = new ArrayList();
wm.setGlobal("list", list);
for (int i = 0; i < 3; i++) {
wm.insert(new A(i));
}
for (int i = 0; i < 3; i++) {
wm.insert(new C(i));
}
wm.fireAllRules();
assertEquals(9, list.size());
wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
list = new ArrayList();
wm.setGlobal("list", list);
for (int i = 0; i < 3; i++) {
wm.insert(new A(i));
}
FactHandle fh = wm.insert(new B(1));
for (int i = 0; i < 3; i++) {
wm.insert(new C(i));
}
wm.fireAllRules();
assertEquals(0, list.size());
wm.retract(fh);
wm.fireAllRules();
assertEquals(9, list.size());
}
Aggregations