Search in sources :

Example 71 with DefaultFactHandle

use of org.drools.core.common.DefaultFactHandle in project drools by kiegroup.

the class RuleUnlinkingTest 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 = (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.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 72 with DefaultFactHandle

use of org.drools.core.common.DefaultFactHandle in project drools by kiegroup.

the class NodeSegmentUnlinkingTest method testAllLinkedInWithExistsNodesOnly.

@Test
public void testAllLinkedInWithExistsNodesOnly() {
    setUp(EXISTS_NODE);
    // make sure it created ExistsNodes
    assertEquals(ExistsNode.class, n3.getClass());
    InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
    StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
    DefaultFactHandle f1 = (DefaultFactHandle) ksession.insert("test1");
    n3.assertObject(f1, context, ksession);
    BetaMemory bm = (BetaMemory) ksession.getNodeMemory(n3);
    assertFalse(bm.getSegmentMemory().isSegmentLinked());
    n4.assertObject(f1, context, ksession);
    assertFalse(bm.getSegmentMemory().isSegmentLinked());
    n5.assertObject(f1, context, ksession);
    assertFalse(bm.getSegmentMemory().isSegmentLinked());
    n6.assertObject(f1, context, ksession);
    // only after all 4 nodes are populated, is the segment linked in
    assertTrue(bm.getSegmentMemory().isSegmentLinked());
}
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) Test(org.junit.Test)

Example 73 with DefaultFactHandle

use of org.drools.core.common.DefaultFactHandle in project drools by kiegroup.

the class NodeSegmentUnlinkingTest method testAllLinkedInWithNotNodesOnly.

@Test
public void testAllLinkedInWithNotNodesOnly() {
    setUp(NOT_NODE);
    // make sure it created NotNodes
    assertEquals(NotNode.class, n3.getClass());
    InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
    StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
    BetaMemory bm = (BetaMemory) ksession.getNodeMemory(n3);
    createSegmentMemory(n3, ksession);
    // not nodes start off linked
    assertTrue(bm.getSegmentMemory().isSegmentLinked());
    // unlinked after first assertion
    DefaultFactHandle f1 = (DefaultFactHandle) ksession.insert("test1");
    n3.assertObject(f1, context, ksession);
    // this doesn't unlink on the assertObject, as the node's memory must be processed. So use the helper method the main network evaluator uses.
    PhreakNotNode.unlinkNotNodeOnRightInsert((NotNode) n3, bm, ksession);
    assertFalse(bm.getSegmentMemory().isSegmentLinked());
    n3.retractRightTuple(f1.getFirstRightTuple(), context, ksession);
    assertTrue(bm.getSegmentMemory().isSegmentLinked());
// assertFalse( bm.getSegmentMemory().isSigmentLinked() ); // check retraction unlinks again
}
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) Test(org.junit.Test)

Example 74 with DefaultFactHandle

use of org.drools.core.common.DefaultFactHandle in project drools by kiegroup.

the class ReteTest method testCache.

/**
 * Tests that interfaces and parent classes for an asserted  class are  cached, for  quick future iterations
 */
@Test
public void testCache() {
    StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
    // Create a Rete network with ObjectTypeNodes for List, Collection and ArrayList
    final Rete rete = kBase.getRete();
    ObjectTypeNode objectTypeNode = new ObjectTypeNode(1, this.entryPoint, new ClassObjectType(List.class), buildContext);
    objectTypeNode.attach(buildContext);
    MockObjectSink sink = new MockObjectSink();
    objectTypeNode.addObjectSink(sink);
    objectTypeNode = new ObjectTypeNode(1, this.entryPoint, new ClassObjectType(Collection.class), buildContext);
    objectTypeNode.attach(buildContext);
    sink = new MockObjectSink();
    objectTypeNode.addObjectSink(sink);
    objectTypeNode = new ObjectTypeNode(1, this.entryPoint, new ClassObjectType(ArrayList.class), buildContext);
    objectTypeNode.attach(buildContext);
    sink = new MockObjectSink();
    objectTypeNode.addObjectSink(sink);
    // ArrayList matches all three ObjectTypeNodes
    final DefaultFactHandle h1 = new DefaultFactHandle(1, new ArrayList());
    rete.assertObject(h1, pctxFactory.createPropagationContext(0, PropagationContext.Type.INSERTION, null, null, null), ksession);
    // ArrayList matches two ObjectTypeNodes
    h1.setObject(new ArrayList());
    rete.assertObject(h1, pctxFactory.createPropagationContext(0, PropagationContext.Type.INSERTION, null, null, null), ksession);
    ClassObjectTypeConf conf = (ClassObjectTypeConf) ksession.getObjectTypeConfigurationRegistry().getOrCreateObjectTypeConf(this.entryPoint.getEntryPoint(), new ArrayList());
    assertLength(3, conf.getObjectTypeNodes());
    conf = (ClassObjectTypeConf) ksession.getObjectTypeConfigurationRegistry().getOrCreateObjectTypeConf(this.entryPoint.getEntryPoint(), new ArrayList());
    assertLength(3, conf.getObjectTypeNodes());
}
Also used : Rete(org.drools.core.reteoo.Rete) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) ClassObjectTypeConf(org.drools.core.reteoo.ClassObjectTypeConf) ClassObjectType(org.drools.core.base.ClassObjectType) StatefulKnowledgeSessionImpl(org.drools.kiesession.session.StatefulKnowledgeSessionImpl) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) MockObjectSink(org.drools.core.reteoo.MockObjectSink) Test(org.junit.Test)

Example 75 with DefaultFactHandle

use of org.drools.core.common.DefaultFactHandle in project drools by kiegroup.

the class ReteTest method testRetractObject.

/**
 * All objects deleted from a RootNode must be propagated to all children
 * ObjectTypeNodes.
 */
@Test
public void testRetractObject() throws Exception {
    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(List.class), buildContext);
    objectTypeNode.attach(buildContext);
    final MockObjectSink sink1 = new MockObjectSink();
    objectTypeNode.addObjectSink(sink1);
    // There are no String ObjectTypeNodes, make sure its not propagated
    final String string = "String";
    final DefaultFactHandle h1 = new DefaultFactHandle(1, string);
    rete.assertObject(h1, pctxFactory.createPropagationContext(0, PropagationContext.Type.INSERTION, null, null, null), ksession);
    assertLength(0, sink1.getAsserted());
    assertLength(0, sink1.getRetracted());
    // There is a List ObjectTypeNode, make sure it was propagated
    final List list = new ArrayList();
    final DefaultFactHandle h2 = new DefaultFactHandle(1, list);
    // need  to assert first, to force it to build  up the cache
    rete.assertObject(h2, pctxFactory.createPropagationContext(0, PropagationContext.Type.INSERTION, null, null, null), ksession);
    rete.retractObject(h2, pctxFactory.createPropagationContext(0, PropagationContext.Type.INSERTION, null, null, null), ksession);
    ksession.fireAllRules();
    final List retracted = sink1.getRetracted();
    assertLength(1, retracted);
    final Object[] results = (Object[]) retracted.get(0);
    assertSame(list, ((DefaultFactHandle) results[0]).getObject());
}
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) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) MockObjectSink(org.drools.core.reteoo.MockObjectSink) Test(org.junit.Test)

Aggregations

DefaultFactHandle (org.drools.core.common.DefaultFactHandle)92 Test (org.junit.Test)78 InternalFactHandle (org.drools.core.common.InternalFactHandle)30 FactHandle (org.kie.api.runtime.rule.FactHandle)29 Cheese (org.drools.core.test.model.Cheese)24 ArrayList (java.util.ArrayList)23 ClassObjectType (org.drools.core.base.ClassObjectType)22 KieSession (org.kie.api.runtime.KieSession)19 FieldIndex (org.drools.core.util.AbstractHashTable.FieldIndex)18 RightTupleImpl (org.drools.core.reteoo.RightTupleImpl)16 Declaration (org.drools.core.rule.Declaration)15 RightTuple (org.drools.core.reteoo.RightTuple)14 InternalReadAccessor (org.drools.core.spi.InternalReadAccessor)14 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)13 StatefulKnowledgeSessionImpl (org.drools.kiesession.session.StatefulKnowledgeSessionImpl)13 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)12 Pattern (org.drools.core.rule.Pattern)12 Tuple (org.drools.core.spi.Tuple)12 TupleIndexHashTable (org.drools.core.util.index.TupleIndexHashTable)12 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)11