Search in sources :

Example 76 with StatefulKnowledgeSessionImpl

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());
}
Also used : StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 77 with StatefulKnowledgeSessionImpl

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"));
}
Also used : HashMap(java.util.HashMap) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) GlobalResolver(org.drools.core.spi.GlobalResolver) MapGlobalResolver(org.drools.core.base.MapGlobalResolver) MapGlobalResolver(org.drools.core.base.MapGlobalResolver) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 78 with StatefulKnowledgeSessionImpl

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));
}
Also used : StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 79 with StatefulKnowledgeSessionImpl

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());
}
Also used : DefaultFactHandle(org.drools.core.common.DefaultFactHandle) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 80 with StatefulKnowledgeSessionImpl

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));
}
Also used : MVELCompileable(org.drools.core.base.mvel.MVELCompileable) RuleBuildContext(org.drools.compiler.rule.builder.RuleBuildContext) PatternDescr(org.drools.compiler.lang.descr.PatternDescr) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) DialectCompiletimeRegistry(org.drools.compiler.compiler.DialectCompiletimeRegistry) MockLeftTupleSink(org.drools.compiler.reteoo.MockLeftTupleSink) Cheese(org.drools.compiler.Cheese) AccumulateDescr(org.drools.compiler.lang.descr.AccumulateDescr) InitialFactImpl(org.drools.core.reteoo.InitialFactImpl) Accumulate(org.drools.core.rule.Accumulate) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) LeftTupleImpl(org.drools.core.reteoo.LeftTupleImpl) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) InternalFactHandle(org.drools.core.common.InternalFactHandle) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Aggregations

StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)84 Test (org.junit.Test)63 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)41 KieSession (org.kie.api.runtime.KieSession)24 ArrayList (java.util.ArrayList)22 InternalFactHandle (org.drools.core.common.InternalFactHandle)22 List (java.util.List)20 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)17 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)17 ClassObjectType (org.drools.core.base.ClassObjectType)14 FactHandle (org.kie.api.runtime.rule.FactHandle)14 KieBase (org.kie.api.KieBase)13 HashMap (java.util.HashMap)9 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)8 LeftTupleImpl (org.drools.core.reteoo.LeftTupleImpl)8 BuildContext (org.drools.core.reteoo.builder.BuildContext)8 Cheese (org.drools.core.test.model.Cheese)8 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)7 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)6 TruthMaintenanceSystem (org.drools.core.common.TruthMaintenanceSystem)6