Search in sources :

Example 76 with DefaultFactHandle

use of org.drools.core.common.DefaultFactHandle 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 77 with DefaultFactHandle

use of org.drools.core.common.DefaultFactHandle 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 78 with DefaultFactHandle

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

the class Misc2Test method testDynamicAddRule.

@Test
public void testDynamicAddRule() {
    // DROOLS-17
    String str = "import " + Misc2Test.A.class.getCanonicalName() + "\n" + "rule r1 when\n" + "    $a : A( f1 == 1 )\n" + "then\n" + "end\n" + "\n" + "rule r2 when\n" + "    $a : A( f2 == 1 )\n" + "then\n" + "end\n" + "\n" + "rule r3 when\n" + "    $a : A( f3 == 1 )" + "then\n" + "end";
    String str2 = "import " + Misc2Test.A.class.getCanonicalName() + "\n" + "rule r4 when\n" + "    $a : A( f2 == 1, f4 == 1 )" + "then\n" + "end";
    InternalKnowledgeBase kbase = (InternalKnowledgeBase) KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, str);
    KieSession ksession = kbase.newKieSession();
    FactHandle fh = ksession.insert(new A(1, 1, 1, 1));
    ksession.fireAllRules();
    Collection<KiePackage> kpkgs = KieBaseUtil.getKieBaseFromKieModuleFromDrl("tmp", kieBaseTestConfiguration, str2).getKiePackages();
    kbase.addPackages(kpkgs);
    ksession.fireAllRules();
    // this second insert forces the regeneration of the otnIds
    ksession.insert(new A(2, 2, 2, 2));
    LeftTuple leftTuple = ((DefaultFactHandle) fh).getFirstLeftTuple();
    ObjectTypeNode.Id letTupleOtnId = leftTuple.getInputOtnId();
    leftTuple = leftTuple.getHandleNext();
    while (leftTuple != null) {
        assertTrue(letTupleOtnId.before(leftTuple.getInputOtnId()));
        letTupleOtnId = leftTuple.getInputOtnId();
        leftTuple = leftTuple.getHandleNext();
    }
}
Also used : DefaultFactHandle(org.drools.core.common.DefaultFactHandle) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) KiePackage(org.kie.api.definition.KiePackage) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) FactWithString(org.drools.mvel.integrationtests.facts.FactWithString) LeftTuple(org.drools.core.reteoo.LeftTuple) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Test(org.junit.Test)

Example 79 with DefaultFactHandle

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

the class EventFactHandleDeserializationTest method testDisconnectedEventFactHandle.

@Test
public void testDisconnectedEventFactHandle() {
    // DROOLS-924
    final String drl = "declare String \n" + "  @role(event)\n" + "end\n";
    final KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
    final DefaultFactHandle helloHandle = (DefaultFactHandle) ksession.insert("hello");
    final DefaultFactHandle goodbyeHandle = (DefaultFactHandle) ksession.insert("goodbye");
    final SoftAssertions softly = new SoftAssertions();
    FactHandle key = DefaultFactHandle.createFromExternalFormat(helloHandle.toExternalForm());
    softly.assertThat(key).isInstanceOf(EventFactHandle.class);
    softly.assertThat(ksession.getObject(key)).isEqualTo("hello");
    key = DefaultFactHandle.createFromExternalFormat(goodbyeHandle.toExternalForm());
    softly.assertThat(key).isInstanceOf(EventFactHandle.class);
    softly.assertThat(ksession.getObject(key)).isEqualTo("goodbye");
    softly.assertAll();
}
Also used : DefaultFactHandle(org.drools.core.common.DefaultFactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) EventFactHandle(org.drools.core.common.EventFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) SoftAssertions(org.assertj.core.api.SoftAssertions) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 80 with DefaultFactHandle

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

the class InsertObjectCommand method execute.

public FactHandle execute(Context context) {
    KieSession ksession = ((RegistryContext) context).lookup(KieSession.class);
    FactHandle factHandle;
    if (StringUtils.isEmpty(this.entryPoint)) {
        factHandle = ksession.insert(object);
    } else {
        factHandle = ksession.getEntryPoint(this.entryPoint).insert(object);
    }
    if (outIdentifier != null) {
        if (this.returnObject) {
            ((RegistryContext) context).lookup(ExecutionResultImpl.class).setResult(this.outIdentifier, object);
        }
        ((RegistryContext) context).lookup(ExecutionResultImpl.class).getFactHandles().put(this.outIdentifier, factHandle);
    }
    if (disconnected) {
        DefaultFactHandle disconnectedHandle = ((DefaultFactHandle) factHandle).clone();
        disconnectedHandle.disconnect();
        return disconnectedHandle;
    }
    return factHandle;
}
Also used : DefaultFactHandle(org.drools.core.common.DefaultFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) ExecutionResultImpl(org.drools.core.runtime.impl.ExecutionResultImpl) KieSession(org.kie.api.runtime.KieSession) RegistryContext(org.kie.internal.command.RegistryContext)

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