use of org.drools.core.impl.StatefulKnowledgeSessionImpl in project drools by kiegroup.
the class ReteTest method testAssertObjectWithNoMatchingObjectTypeNode.
@Test
public void testAssertObjectWithNoMatchingObjectTypeNode() {
StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
final Rete rete = kBase.getRete();
assertEquals(1, rete.getObjectTypeNodes().size());
List list = new ArrayList();
ksession.insert(list);
ksession.fireAllRules();
assertEquals(1, rete.getObjectTypeNodes().size());
}
use of org.drools.core.impl.StatefulKnowledgeSessionImpl in project drools by kiegroup.
the class ReteooWorkingMemoryTest method testGlobalResolver.
@Test
public void testGlobalResolver() {
final Map<String, Object> map = new HashMap<String, Object>();
map.put("global1", "value1");
map.put("global2", "value2");
final GlobalResolver resolver = new MapGlobalResolver(map);
InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase();
StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
ksession.setGlobalResolver(resolver);
assertEquals("value1", ksession.getGlobal("global1"));
assertEquals("value2", ksession.getGlobal("global2"));
}
use of org.drools.core.impl.StatefulKnowledgeSessionImpl in project drools by kiegroup.
the class RuleUnlinkingTest method testSegmentNodeReferencesToSegments.
@Test
public void testSegmentNodeReferencesToSegments() {
setUp(JOIN_NODE);
InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase();
StatefulKnowledgeSessionImpl wm = new StatefulKnowledgeSessionImpl(1L, kBase);
BetaMemory bm = null;
List<PathMemory> list;
PathMemory rtn1Rs = (PathMemory) wm.getNodeMemory(rtn1);
PathMemory rtn2Rs = (PathMemory) wm.getNodeMemory(rtn2);
PathMemory rtn3Rs = (PathMemory) wm.getNodeMemory(rtn3);
// n1
bm = createSegmentMemory(n1, wm);
assertEquals(2, bm.getNodePosMaskBit());
assertEquals(15, bm.getSegmentMemory().getAllLinkedMaskTest());
assertEquals(1, bm.getSegmentMemory().getSegmentPosMaskBit());
list = bm.getSegmentMemory().getPathMemories();
assertEquals(3, list.size());
assertTrue(list.contains(rtn1Rs));
assertTrue(list.contains(rtn2Rs));
assertTrue(list.contains(rtn3Rs));
// n2
bm = createSegmentMemory(n2, wm);
assertEquals(4, bm.getNodePosMaskBit());
assertEquals(15, bm.getSegmentMemory().getAllLinkedMaskTest());
assertEquals(1, bm.getSegmentMemory().getSegmentPosMaskBit());
list = bm.getSegmentMemory().getPathMemories();
assertEquals(3, list.size());
assertTrue(list.contains(rtn1Rs));
assertTrue(list.contains(rtn2Rs));
assertTrue(list.contains(rtn3Rs));
// n3
bm = createSegmentMemory(n3, wm);
assertEquals(8, bm.getNodePosMaskBit());
assertEquals(15, bm.getSegmentMemory().getAllLinkedMaskTest());
assertEquals(1, bm.getSegmentMemory().getSegmentPosMaskBit());
list = bm.getSegmentMemory().getPathMemories();
assertEquals(3, list.size());
assertTrue(list.contains(rtn1Rs));
assertTrue(list.contains(rtn2Rs));
assertTrue(list.contains(rtn3Rs));
// n4
bm = createSegmentMemory(n4, wm);
assertEquals(1, bm.getNodePosMaskBit());
assertEquals(3, bm.getSegmentMemory().getAllLinkedMaskTest());
assertEquals(2, bm.getSegmentMemory().getSegmentPosMaskBit());
list = bm.getSegmentMemory().getPathMemories();
assertEquals(2, list.size());
assertTrue(list.contains(rtn2Rs));
assertTrue(list.contains(rtn3Rs));
// n5
bm = createSegmentMemory(n5, wm);
assertEquals(2, bm.getNodePosMaskBit());
assertEquals(3, bm.getSegmentMemory().getAllLinkedMaskTest());
assertEquals(2, bm.getSegmentMemory().getSegmentPosMaskBit());
list = bm.getSegmentMemory().getPathMemories();
assertEquals(2, list.size());
assertTrue(list.contains(rtn2Rs));
assertTrue(list.contains(rtn3Rs));
// n6
bm = createSegmentMemory(n6, wm);
assertEquals(1, bm.getNodePosMaskBit());
assertEquals(7, bm.getSegmentMemory().getAllLinkedMaskTest());
assertEquals(4, bm.getSegmentMemory().getSegmentPosMaskBit());
list = bm.getSegmentMemory().getPathMemories();
assertEquals(1, list.size());
assertTrue(list.contains(rtn3Rs));
// n7
bm = createSegmentMemory(n7, wm);
assertEquals(2, bm.getNodePosMaskBit());
assertEquals(7, bm.getSegmentMemory().getAllLinkedMaskTest());
assertEquals(4, bm.getSegmentMemory().getSegmentPosMaskBit());
list = bm.getSegmentMemory().getPathMemories();
assertEquals(1, list.size());
assertTrue(list.contains(rtn3Rs));
// n8
bm = createSegmentMemory(n8, wm);
assertEquals(4, bm.getNodePosMaskBit());
assertEquals(7, bm.getSegmentMemory().getAllLinkedMaskTest());
assertEquals(4, bm.getSegmentMemory().getSegmentPosMaskBit());
list = bm.getSegmentMemory().getPathMemories();
assertEquals(1, list.size());
assertTrue(list.contains(rtn3Rs));
}
use of org.drools.core.impl.StatefulKnowledgeSessionImpl in project drools by kiegroup.
the class RuleUnlinkingWithSegmentMemoryTest method testRuleSegmentLinking.
@Test
public void testRuleSegmentLinking() {
setUp(JOIN_NODE);
InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase();
StatefulKnowledgeSessionImpl wm = new StatefulKnowledgeSessionImpl(1L, kBase);
BetaMemory bm = null;
List<PathMemory> list;
PathMemory rtn1Rs = (PathMemory) wm.getNodeMemory(rtn1);
PathMemory rtn2Rs = (PathMemory) wm.getNodeMemory(rtn2);
PathMemory rtn3Rs = (PathMemory) wm.getNodeMemory(rtn3);
DefaultFactHandle f1 = (DefaultFactHandle) wm.insert("test1");
lian.assertObject(f1, context, wm);
n1.assertObject(f1, context, wm);
n3.assertObject(f1, context, wm);
n4.assertObject(f1, context, wm);
n8.assertObject(f1, context, wm);
assertFalse(rtn1Rs.isRuleLinked());
assertFalse(rtn2Rs.isRuleLinked());
assertFalse(rtn3Rs.isRuleLinked());
// Link in Rule1
bm = (BetaMemory) wm.getNodeMemory(n2);
assertFalse(bm.getSegmentMemory().isSegmentLinked());
DefaultFactHandle f2 = (DefaultFactHandle) wm.insert("test2");
n2.assertObject(f2, context, wm);
assertTrue(bm.getSegmentMemory().isSegmentLinked());
assertTrue(rtn1Rs.isRuleLinked());
assertFalse(rtn2Rs.isRuleLinked());
assertFalse(rtn3Rs.isRuleLinked());
// Link in Rule2
bm = (BetaMemory) wm.getNodeMemory(n5);
assertFalse(bm.getSegmentMemory().isSegmentLinked());
n5.assertObject(f1, context, wm);
assertTrue(bm.getSegmentMemory().isSegmentLinked());
assertTrue(rtn1Rs.isRuleLinked());
assertTrue(rtn2Rs.isRuleLinked());
assertFalse(rtn3Rs.isRuleLinked());
// Link in Rule3
n6.assertObject(f1, context, wm);
n7.assertObject(f1, context, wm);
assertTrue(bm.getSegmentMemory().isSegmentLinked());
assertTrue(rtn1Rs.isRuleLinked());
assertTrue(rtn2Rs.isRuleLinked());
assertTrue(rtn3Rs.isRuleLinked());
// retract n2, should unlink all rules
n2.retractRightTuple(f2.getFirstRightTuple(), context, wm);
assertFalse(rtn1Rs.isRuleLinked());
assertFalse(rtn2Rs.isRuleLinked());
assertFalse(rtn3Rs.isRuleLinked());
}
use of org.drools.core.impl.StatefulKnowledgeSessionImpl in project drools by kiegroup.
the class MVELAccumulateBuilderTest method testSimpleExpression.
@Test
public void testSimpleExpression() {
KnowledgeBuilderImpl pkgBuilder = new KnowledgeBuilderImpl();
pkgBuilder.addPackage(new PackageDescr("pkg1"));
InternalKnowledgePackage pkg = pkgBuilder.getPackage("pkg1");
final RuleDescr ruleDescr = new RuleDescr("rule 1");
final KnowledgeBuilderConfigurationImpl conf = pkgBuilder.getBuilderConfiguration();
DialectCompiletimeRegistry dialectRegistry = pkgBuilder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
MVELDialect mvelDialect = (MVELDialect) dialectRegistry.getDialect("mvel");
final RuleBuildContext context = new RuleBuildContext(pkgBuilder, ruleDescr, dialectRegistry, pkg, mvelDialect);
final AccumulateDescr accDescr = new AccumulateDescr();
final PatternDescr inputPattern = new PatternDescr("org.drools.compiler.Cheese", "$cheese");
accDescr.setInputPattern(inputPattern);
accDescr.setInitCode("total = 0;");
accDescr.setActionCode("total += $cheese.price;");
accDescr.setReverseCode("total -= $cheese.price;");
accDescr.setResultCode("new Integer(total)");
final MVELAccumulateBuilder builder = new MVELAccumulateBuilder();
final Accumulate acc = (Accumulate) builder.build(context, accDescr);
((MVELCompileable) acc.getAccumulators()[0]).compile((MVELDialectRuntimeData) pkgBuilder.getPackageRegistry(pkg.getName()).getDialectRuntimeRegistry().getDialectData("mvel"));
InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
MockLeftTupleSink sink = new MockLeftTupleSink();
final Cheese cheddar1 = new Cheese("cheddar", 10);
final Cheese cheddar2 = new Cheese("cheddar", 8);
final InternalFactHandle f0 = (InternalFactHandle) ksession.insert(new InitialFactImpl());
final InternalFactHandle f1 = (InternalFactHandle) ksession.insert(cheddar1);
final InternalFactHandle f2 = (InternalFactHandle) ksession.insert(cheddar2);
final LeftTupleImpl tuple = new LeftTupleImpl(f0, sink, true);
Object wmContext = acc.createWorkingMemoryContext();
Object accContext = acc.createContext();
acc.init(wmContext, accContext, tuple, ksession);
acc.accumulate(wmContext, accContext, tuple, f1, ksession);
acc.accumulate(wmContext, accContext, tuple, f2, ksession);
assertEquals(new Integer(18), acc.getResult(wmContext, accContext, tuple, ksession));
acc.reverse(wmContext, accContext, tuple, f1, ksession);
assertEquals(new Integer(8), acc.getResult(wmContext, accContext, tuple, ksession));
}
Aggregations