Search in sources :

Example 41 with StatefulKnowledgeSessionImpl

use of org.drools.core.impl.StatefulKnowledgeSessionImpl in project drools by kiegroup.

the class RuleUnlinkingTest method testRuleSegmentsAllLinkedTestMasks.

@Test
public void testRuleSegmentsAllLinkedTestMasks() {
    setUp(JOIN_NODE);
    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase(kconf);
    StatefulKnowledgeSessionImpl wm = new StatefulKnowledgeSessionImpl(1L, kBase);
    PathMemory rs = (PathMemory) wm.getNodeMemory(rtn1);
    assertFalse(rs.isRuleLinked());
    assertEquals(1, rs.getAllLinkedMaskTest());
    rs = (PathMemory) wm.getNodeMemory(rtn2);
    assertFalse(rs.isRuleLinked());
    assertEquals(3, rs.getAllLinkedMaskTest());
    rs = (PathMemory) wm.getNodeMemory(rtn3);
    assertFalse(rs.isRuleLinked());
    assertEquals(7, rs.getAllLinkedMaskTest());
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 42 with StatefulKnowledgeSessionImpl

use of org.drools.core.impl.StatefulKnowledgeSessionImpl in project drools by kiegroup.

the class RuleUnlinkingTest 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");
    SegmentUtilities.createSegmentMemory(liaNode, wm);
    liaNode.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 43 with StatefulKnowledgeSessionImpl

use of org.drools.core.impl.StatefulKnowledgeSessionImpl in project drools by kiegroup.

the class RuleUnlinkingWithSegmentMemoryTest method testRuleSegmentsAllLinkedTestMasks.

@Test
public void testRuleSegmentsAllLinkedTestMasks() {
    setUp(JOIN_NODE);
    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase(kconf);
    StatefulKnowledgeSessionImpl wm = new StatefulKnowledgeSessionImpl(1L, kBase);
    PathMemory rs = (PathMemory) wm.getNodeMemory(rtn1);
    assertFalse(rs.isRuleLinked());
    assertEquals(1, rs.getAllLinkedMaskTest());
    rs = (PathMemory) wm.getNodeMemory(rtn2);
    assertFalse(rs.isRuleLinked());
    assertEquals(3, rs.getAllLinkedMaskTest());
    rs = (PathMemory) wm.getNodeMemory(rtn3);
    assertFalse(rs.isRuleLinked());
    assertEquals(7, rs.getAllLinkedMaskTest());
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 44 with StatefulKnowledgeSessionImpl

use of org.drools.core.impl.StatefulKnowledgeSessionImpl in project drools by kiegroup.

the class RuleUnlinkingWithSegmentMemoryTest method testSegmentNodeReferencesToSegments.

@Test
public void testSegmentNodeReferencesToSegments() {
    setUp(JOIN_NODE);
    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase(kconf);
    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);
    // lian
    SegmentUtilities.createSegmentMemory(lian, wm);
    LeftInputAdapterNode.LiaNodeMemory lmem = (LeftInputAdapterNode.LiaNodeMemory) 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 : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 45 with StatefulKnowledgeSessionImpl

use of org.drools.core.impl.StatefulKnowledgeSessionImpl in project drools by kiegroup.

the class FieldConstraintTest method testLiteralConstraint.

/**
 * <pre>
 *
 *                ( Cheese (type &quot;cheddar&quot;) )
 *
 * </pre>
 *
 * This is currently the same as using a ReturnValueConstraint just that it
 * doesn't need any requiredDeclarations
 */
@Test
public void testLiteralConstraint() {
    InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase();
    StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
    final ClassFieldReader extractor = store.getReader(Cheese.class, "type");
    final MvelConstraint constraint = new MvelConstraintTestUtil("type == \"cheddar\"", FieldFactory.getInstance().getFieldValue("cheddar"), extractor);
    final Cheese cheddar = new Cheese("cheddar", 5);
    final InternalFactHandle cheddarHandle = (InternalFactHandle) ksession.insert(cheddar);
    // check constraint
    assertTrue(constraint.isAllowed(cheddarHandle, ksession));
    final Cheese stilton = new Cheese("stilton", 5);
    final InternalFactHandle stiltonHandle = (InternalFactHandle) ksession.insert(stilton);
    // check constraint
    assertFalse(constraint.isAllowed(stiltonHandle, ksession));
}
Also used : ClassFieldReader(org.drools.core.base.ClassFieldReader) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) Cheese(org.drools.core.test.model.Cheese) InternalFactHandle(org.drools.core.common.InternalFactHandle) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Aggregations

StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)79 Test (org.junit.Test)63 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)41 ArrayList (java.util.ArrayList)22 InternalFactHandle (org.drools.core.common.InternalFactHandle)22 KieSession (org.kie.api.runtime.KieSession)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 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 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)6