use of org.drools.kiesession.session.StatefulKnowledgeSessionImpl in project drools by kiegroup.
the class ReteTest method testIsShadowed.
@Test
public void testIsShadowed() {
StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
// Create a Rete network with ObjectTypeNodes for List, Collection and ArrayList
final Rete rete = kBase.getRete();
final ObjectTypeNode objectTypeNode = new ObjectTypeNode(1, this.entryPoint, new ClassObjectType(Cheese.class), buildContext);
objectTypeNode.attach(buildContext);
final MockObjectSink sink1 = new MockObjectSink();
objectTypeNode.addObjectSink(sink1);
// There are no String ObjectTypeNodes, make sure its not propagated
final Cheese cheese = new Cheese("brie", 15);
final DefaultFactHandle h1 = new DefaultFactHandle(1, cheese);
rete.assertObject(h1, pctxFactory.createPropagationContext(0, PropagationContext.Type.INSERTION, null, null, null), ksession);
ksession.fireAllRules();
final Object[] results = (Object[]) sink1.getAsserted().get(0);
}
use of org.drools.kiesession.session.StatefulKnowledgeSessionImpl in project drools by kiegroup.
the class RuleUnlinkingWithSegmentMemoryTest method testRuleSegmentLinking.
@Test
public void testRuleSegmentLinking() {
setUp(JOIN_NODE);
InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
StatefulKnowledgeSessionImpl wm = new StatefulKnowledgeSessionImpl(1L, kBase);
BetaMemory bm = null;
List<PathMemory> list;
PathMemory rtn1Rs = wm.getNodeMemory(rtn1);
PathMemory rtn2Rs = wm.getNodeMemory(rtn2);
PathMemory rtn3Rs = 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.kiesession.session.StatefulKnowledgeSessionImpl in project drools by kiegroup.
the class RuleUnlinkingWithSegmentMemoryTest method testSegmentNodeReferencesToSegments.
@Test
public void testSegmentNodeReferencesToSegments() {
setUp(JOIN_NODE);
InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
StatefulKnowledgeSessionImpl wm = new StatefulKnowledgeSessionImpl(1L, kBase);
BetaMemory bm = null;
List<PathMemory> list;
PathMemory rtn1Rs = wm.getNodeMemory(rtn1);
PathMemory rtn2Rs = wm.getNodeMemory(rtn2);
PathMemory rtn3Rs = wm.getNodeMemory(rtn3);
// lian
SegmentUtilities.createSegmentMemory(lian, wm);
LeftInputAdapterNode.LiaNodeMemory lmem = wm.getNodeMemory(lian);
assertEquals(1, lmem.getNodePosMaskBit());
// n1
SegmentUtilities.createSegmentMemory(n1, wm);
bm = (BetaMemory) wm.getNodeMemory(n1);
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 = (BetaMemory) wm.getNodeMemory(n2);
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 = (BetaMemory) wm.getNodeMemory(n3);
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
SegmentUtilities.createSegmentMemory(n4, wm);
bm = (BetaMemory) wm.getNodeMemory(n4);
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 = (BetaMemory) wm.getNodeMemory(n5);
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
SegmentUtilities.createSegmentMemory(n6, wm);
bm = (BetaMemory) wm.getNodeMemory(n6);
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 = (BetaMemory) wm.getNodeMemory(n7);
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 = (BetaMemory) wm.getNodeMemory(n8);
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.kiesession.session.StatefulKnowledgeSessionImpl in project drools by kiegroup.
the class MVELConsequenceBuilderTest method testSimpleExpression.
@Test
public void testSimpleExpression() throws Exception {
PackageDescr pkgDescr = new PackageDescr("pkg1");
KnowledgeBuilderImpl pkgBuilder = new KnowledgeBuilderImpl();
pkgBuilder.addPackage(pkgDescr);
InternalKnowledgePackage pkg = pkgBuilder.getPackageRegistry("pkg1").getPackage();
final RuleDescr ruleDescr = new RuleDescr("rule 1");
ruleDescr.setNamespace("pkg1");
ruleDescr.setConsequence("modify (cheese) {price = 5 };\nretract (cheese)");
DialectCompiletimeRegistry dialectRegistry = pkgBuilder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
MVELDialect mvelDialect = (MVELDialect) dialectRegistry.getDialect("mvel");
final RuleBuildContext context = new RuleBuildContext(pkgBuilder, ruleDescr, dialectRegistry, pkg, mvelDialect);
final InstrumentedDeclarationScopeResolver declarationResolver = new InstrumentedDeclarationScopeResolver();
final ObjectType cheeseObjeectType = new ClassObjectType(Cheese.class);
final Pattern pattern = new Pattern(0, cheeseObjeectType, "cheese");
final GroupElement subrule = new GroupElement(GroupElement.AND);
subrule.addChild(pattern);
final Map<String, Declaration> map = new HashMap<String, Declaration>();
map.put("cheese", pattern.getDeclaration());
declarationResolver.setDeclarations(map);
context.setDeclarationResolver(declarationResolver);
final MVELConsequenceBuilder builder = new MVELConsequenceBuilder();
builder.build(context, RuleImpl.DEFAULT_CONSEQUENCE_NAME);
InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
PropagationContextFactory pctxFactory = RuntimeComponentFactory.get().getPropagationContextFactory();
kBase.addPackage(pkg);
StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
BuildContext buildContext = new BuildContext(kBase, Collections.emptyList());
MockTupleSource source = new MockTupleSource(1, buildContext);
source.setObjectCount(1);
RuleTerminalNode rtn = new RuleTerminalNode(0, source, context.getRule(), subrule, 0, buildContext);
final Cheese cheddar = new Cheese("cheddar", 10);
final InternalFactHandle f0 = (InternalFactHandle) ksession.insert(cheddar);
final LeftTupleImpl tuple = new LeftTupleImpl(f0, rtn, true);
f0.removeLeftTuple(tuple);
final AgendaItem item = new AgendaItemImpl(0, tuple, 10, pctxFactory.createPropagationContext(1, PropagationContext.Type.DELETION, null, null, null), rtn, null);
final DefaultKnowledgeHelper kbHelper = new DefaultKnowledgeHelper(ksession);
kbHelper.setActivation(item);
((MVELConsequence) context.getRule().getConsequence()).compile((MVELDialectRuntimeData) pkgBuilder.getPackageRegistry(pkg.getName()).getDialectRuntimeRegistry().getDialectData("mvel"));
context.getRule().getConsequence().evaluate(kbHelper, ksession);
assertEquals(5, cheddar.getPrice());
}
use of org.drools.kiesession.session.StatefulKnowledgeSessionImpl in project drools by kiegroup.
the class MVELEvalBuilderTest method testSimpleExpression.
@Test
public void testSimpleExpression() {
InternalKnowledgePackage pkg = CoreComponentFactory.get().createKnowledgePackage("pkg1");
final RuleDescr ruleDescr = new RuleDescr("rule 1");
KnowledgeBuilderImpl pkgBuilder = new KnowledgeBuilderImpl(pkg);
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 InstrumentedDeclarationScopeResolver declarationResolver = new InstrumentedDeclarationScopeResolver();
final InternalReadAccessor extractor = store.getReader(Cheese.class, "price");
final Pattern pattern = new Pattern(0, new ClassObjectType(int.class));
final Declaration declaration = new Declaration("a", extractor, pattern);
final Map map = new HashMap();
map.put("a", declaration);
declarationResolver.setDeclarations(map);
context.setDeclarationResolver(declarationResolver);
final EvalDescr evalDescr = new EvalDescr();
evalDescr.setContent("a == 10");
final MVELEvalBuilder builder = new MVELEvalBuilder();
final EvalCondition eval = (EvalCondition) builder.build(context, evalDescr);
((MVELEvalExpression) eval.getEvalExpression()).compile((MVELDialectRuntimeData) pkgBuilder.getPackageRegistry(pkg.getName()).getDialectRuntimeRegistry().getDialectData("mvel"));
InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
BuildContext buildContext = new BuildContext(kBase, Collections.emptyList());
org.drools.core.reteoo.MockLeftTupleSink sink = new MockLeftTupleSink(buildContext);
MockTupleSource source = new MockTupleSource(1, buildContext);
source.setObjectCount(1);
sink.setLeftTupleSource(source);
final Cheese cheddar = new Cheese("cheddar", 10);
final InternalFactHandle f0 = (InternalFactHandle) ksession.insert(cheddar);
final LeftTupleImpl tuple = new LeftTupleImpl(f0, sink, true);
f0.removeLeftTuple(tuple);
Object evalContext = eval.createContext();
assertTrue(eval.isAllowed(tuple, ksession, evalContext));
cheddar.setPrice(9);
ksession.update(f0, cheddar);
assertFalse(eval.isAllowed(tuple, ksession, evalContext));
}
Aggregations