Search in sources :

Example 46 with StatefulKnowledgeSessionImpl

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);
}
Also used : Rete(org.drools.core.reteoo.Rete) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) ClassObjectType(org.drools.core.base.ClassObjectType) StatefulKnowledgeSessionImpl(org.drools.kiesession.session.StatefulKnowledgeSessionImpl) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) Cheese(org.drools.core.test.model.Cheese) MockObjectSink(org.drools.core.reteoo.MockObjectSink) Test(org.junit.Test)

Example 47 with StatefulKnowledgeSessionImpl

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());
}
Also used : DefaultFactHandle(org.drools.core.common.DefaultFactHandle) StatefulKnowledgeSessionImpl(org.drools.kiesession.session.StatefulKnowledgeSessionImpl) BetaMemory(org.drools.core.reteoo.BetaMemory) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) PathMemory(org.drools.core.reteoo.PathMemory) Test(org.junit.Test)

Example 48 with StatefulKnowledgeSessionImpl

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));
}
Also used : StatefulKnowledgeSessionImpl(org.drools.kiesession.session.StatefulKnowledgeSessionImpl) BetaMemory(org.drools.core.reteoo.BetaMemory) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) PathMemory(org.drools.core.reteoo.PathMemory) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Test(org.junit.Test)

Example 49 with StatefulKnowledgeSessionImpl

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());
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) HashMap(java.util.HashMap) GroupElement(org.drools.core.rule.GroupElement) Cheese(org.drools.mvel.compiler.Cheese) MVELConsequence(org.drools.mvel.expr.MVELConsequence) AgendaItem(org.drools.core.common.AgendaItem) ClassObjectType(org.drools.core.base.ClassObjectType) ObjectType(org.drools.core.spi.ObjectType) MockTupleSource(org.drools.core.reteoo.MockTupleSource) LeftTupleImpl(org.drools.core.reteoo.LeftTupleImpl) AgendaItemImpl(org.drools.core.common.AgendaItemImpl) Declaration(org.drools.core.rule.Declaration) ImportDeclaration(org.drools.core.rule.ImportDeclaration) PackageDescr(org.drools.drl.ast.descr.PackageDescr) InternalFactHandle(org.drools.core.common.InternalFactHandle) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Pattern(org.drools.core.rule.Pattern) PropagationContextFactory(org.drools.core.common.PropagationContextFactory) RuleBuildContext(org.drools.compiler.rule.builder.RuleBuildContext) DialectCompiletimeRegistry(org.drools.compiler.compiler.DialectCompiletimeRegistry) DefaultKnowledgeHelper(org.drools.kiesession.consequence.DefaultKnowledgeHelper) MVELDialect(org.drools.mvel.builder.MVELDialect) BuildContext(org.drools.core.reteoo.builder.BuildContext) RuleBuildContext(org.drools.compiler.rule.builder.RuleBuildContext) MVELConsequenceBuilder(org.drools.mvel.builder.MVELConsequenceBuilder) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) StatefulKnowledgeSessionImpl(org.drools.kiesession.session.StatefulKnowledgeSessionImpl) RuleDescr(org.drools.drl.ast.descr.RuleDescr) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Example 50 with StatefulKnowledgeSessionImpl

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));
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) HashMap(java.util.HashMap) MockLeftTupleSink(org.drools.core.reteoo.MockLeftTupleSink) Cheese(org.drools.mvel.compiler.Cheese) EvalCondition(org.drools.core.rule.EvalCondition) MockTupleSource(org.drools.core.reteoo.MockTupleSource) EvalDescr(org.drools.drl.ast.descr.EvalDescr) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) MVELEvalBuilder(org.drools.mvel.builder.MVELEvalBuilder) LeftTupleImpl(org.drools.core.reteoo.LeftTupleImpl) Declaration(org.drools.core.rule.Declaration) InternalFactHandle(org.drools.core.common.InternalFactHandle) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Pattern(org.drools.core.rule.Pattern) RuleBuildContext(org.drools.compiler.rule.builder.RuleBuildContext) DialectCompiletimeRegistry(org.drools.compiler.compiler.DialectCompiletimeRegistry) MVELDialect(org.drools.mvel.builder.MVELDialect) MVELEvalExpression(org.drools.mvel.expr.MVELEvalExpression) MockLeftTupleSink(org.drools.core.reteoo.MockLeftTupleSink) BuildContext(org.drools.core.reteoo.builder.BuildContext) RuleBuildContext(org.drools.compiler.rule.builder.RuleBuildContext) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) StatefulKnowledgeSessionImpl(org.drools.kiesession.session.StatefulKnowledgeSessionImpl) RuleDescr(org.drools.drl.ast.descr.RuleDescr) HashMap(java.util.HashMap) Map(java.util.Map) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) 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