Search in sources :

Example 11 with StatefulKnowledgeSessionImpl

use of org.drools.kiesession.session.StatefulKnowledgeSessionImpl in project drools by kiegroup.

the class LinkingTest method testForallNodes.

@Test
public void testForallNodes() throws Exception {
    String str = "";
    str += "package org.kie \n";
    str += "import " + A.class.getCanonicalName() + "\n";
    str += "import " + B.class.getCanonicalName() + "\n";
    str += "import " + C.class.getCanonicalName() + "\n";
    str += "import " + X.class.getCanonicalName() + "\n";
    str += "import " + E.class.getCanonicalName() + "\n";
    str += "import " + F.class.getCanonicalName() + "\n";
    str += "import " + G.class.getCanonicalName() + "\n";
    str += "global java.util.List list \n";
    str += "rule rule1 when \n";
    str += "   $a : A() \n";
    str += "   forall( B() )\n";
    str += "   $c : C() \n";
    str += "then \n";
    str += "  list.add( 'x' ); \n";
    str += "end \n";
    KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, str);
    ObjectTypeNode aotn = getObjectTypeNode(kbase, A.class);
    ObjectTypeNode botn = getObjectTypeNode(kbase, A.class);
    ObjectTypeNode cotn = getObjectTypeNode(kbase, A.class);
    KieSession wm = kbase.newKieSession();
    List list = new ArrayList();
    // wm.setGlobal( "list", list );
    // 
    // for ( int i = 0; i < 3; i++ ) {
    // wm.insert(  new A(i) );
    // }
    // 
    // wm.insert(  new B(2) );
    // 
    // for ( int i = 0; i < 3; i++ ) {
    // wm.insert(  new C(i) );
    // }
    // 
    // wm.fireAllRules();
    // assertEquals( 0, list.size() );
    wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
    list = new ArrayList();
    wm.setGlobal("list", list);
    for (int i = 0; i < 2; i++) {
        wm.insert(new A(i));
    }
    for (int i = 0; i < 27; i++) {
        wm.insert(new B(1));
    }
    for (int i = 0; i < 2; i++) {
        wm.insert(new C(i));
    }
    wm.fireAllRules();
    assertEquals(4, list.size());
// wm.retract( fh );
// wm.fireAllRules();
// assertEquals( 9, list.size() );
}
Also used : ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) ArrayList(java.util.ArrayList) KieBase(org.kie.api.KieBase) StatefulKnowledgeSessionImpl(org.drools.kiesession.session.StatefulKnowledgeSessionImpl) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 12 with StatefulKnowledgeSessionImpl

use of org.drools.kiesession.session.StatefulKnowledgeSessionImpl in project drools by kiegroup.

the class LinkingTest method testExistsNodes2.

@Test
public void testExistsNodes2() throws Exception {
    String str = "";
    str += "package org.kie \n";
    str += "import " + A.class.getCanonicalName() + "\n";
    str += "import " + B.class.getCanonicalName() + "\n";
    str += "import " + C.class.getCanonicalName() + "\n";
    str += "import " + X.class.getCanonicalName() + "\n";
    str += "import " + E.class.getCanonicalName() + "\n";
    str += "import " + F.class.getCanonicalName() + "\n";
    str += "import " + G.class.getCanonicalName() + "\n";
    str += "global java.util.List list \n";
    str += "rule rule1 when \n";
    str += "   $a : A() \n";
    str += "   exists B() \n";
    str += "   $c : C() \n";
    str += "then \n";
    str += "  list.add( 'x' ); \n";
    str += "end \n";
    KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, str);
    KieSession wm = kbase.newKieSession();
    List list = new ArrayList();
    wm.setGlobal("list", list);
    for (int i = 0; i < 3; i++) {
        wm.insert(new A(i));
    }
    for (int i = 0; i < 3; i++) {
        wm.insert(new C(i));
    }
    wm.fireAllRules();
    assertEquals(0, list.size());
    wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
    list = new ArrayList();
    wm.setGlobal("list", list);
    for (int i = 0; i < 3; i++) {
        wm.insert(new A(i));
    }
    FactHandle fh = wm.insert(new B(1));
    for (int i = 0; i < 3; i++) {
        wm.insert(new C(i));
    }
    wm.fireAllRules();
    assertEquals(9, list.size());
    wm.retract(fh);
    wm.fireAllRules();
    assertEquals(9, list.size());
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) ArrayList(java.util.ArrayList) KieBase(org.kie.api.KieBase) StatefulKnowledgeSessionImpl(org.drools.kiesession.session.StatefulKnowledgeSessionImpl) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 13 with StatefulKnowledgeSessionImpl

use of org.drools.kiesession.session.StatefulKnowledgeSessionImpl in project drools by kiegroup.

the class LinkingTest method testNotNodes1.

@Test
public void testNotNodes1() throws Exception {
    String str = "";
    str += "package org.kie \n";
    str += "import " + A.class.getCanonicalName() + "\n";
    str += "import " + B.class.getCanonicalName() + "\n";
    str += "import " + C.class.getCanonicalName() + "\n";
    str += "import " + X.class.getCanonicalName() + "\n";
    str += "import " + E.class.getCanonicalName() + "\n";
    str += "import " + F.class.getCanonicalName() + "\n";
    str += "import " + G.class.getCanonicalName() + "\n";
    str += "global java.util.List list \n";
    str += "rule rule1 when \n";
    str += "   not A() \n";
    str += "then \n";
    str += "  list.add( 'x' ); \n";
    str += "end \n";
    KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, str);
    InternalWorkingMemory wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
    List list = new ArrayList();
    wm.setGlobal("list", list);
    wm.fireAllRules();
    assertEquals(1, list.size());
    wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
    list = new ArrayList();
    wm.setGlobal("list", list);
    FactHandle fh = wm.insert(new A(1));
    wm.fireAllRules();
    assertEquals(0, list.size());
    wm.retract(fh);
    wm.fireAllRules();
    assertEquals(1, list.size());
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) ArrayList(java.util.ArrayList) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) KieBase(org.kie.api.KieBase) StatefulKnowledgeSessionImpl(org.drools.kiesession.session.StatefulKnowledgeSessionImpl) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 14 with StatefulKnowledgeSessionImpl

use of org.drools.kiesession.session.StatefulKnowledgeSessionImpl in project drools by kiegroup.

the class NodeSegmentUnlinkingTest method testSingleNodeinSegment.

@Test
public void testSingleNodeinSegment() {
    rule1 = new RuleImpl("rule1");
    rule2 = new RuleImpl("rule2");
    rule3 = new RuleImpl("rule3");
    kBase = KnowledgeBaseFactory.newKnowledgeBase();
    BuildContext buildContext = new BuildContext(kBase, Collections.emptyList());
    MockObjectSource mockObjectSource = new MockObjectSource(8);
    MockTupleSource mockTupleSource = new MockTupleSource(9, buildContext);
    // n2 is only node in it's segment
    ObjectTypeNode otn = new ObjectTypeNode(2, null, new ClassObjectType(String.class), buildContext);
    BetaNode n1 = new JoinNode(10, new LeftInputAdapterNode(3, otn, buildContext), mockObjectSource, new EmptyBetaConstraints(), buildContext);
    BetaNode n2 = new JoinNode(11, n1, mockObjectSource, new EmptyBetaConstraints(), buildContext);
    BetaNode n3 = new JoinNode(12, n1, mockObjectSource, new EmptyBetaConstraints(), buildContext);
    BetaNode n4 = new JoinNode(13, n2, mockObjectSource, new EmptyBetaConstraints(), buildContext);
    BetaNode n5 = new JoinNode(14, n2, mockObjectSource, new EmptyBetaConstraints(), buildContext);
    n1.addAssociation(rule1);
    n1.addAssociation(rule2);
    n1.addAssociation(rule3);
    n2.addAssociation(rule2);
    n2.addAssociation(rule3);
    n3.addAssociation(rule1);
    n4.addAssociation(rule2);
    n5.addAssociation(rule3);
    mockObjectSource.attach(buildContext);
    mockTupleSource.attach(buildContext);
    n1.attach(buildContext);
    n2.attach(buildContext);
    n3.attach(buildContext);
    n4.attach(buildContext);
    n5.attach(buildContext);
    StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
    createSegmentMemory(n2, ksession);
    BetaMemory bm = (BetaMemory) ksession.getNodeMemory(n1);
    assertNull(bm.getSegmentMemory());
    bm = (BetaMemory) ksession.getNodeMemory(n3);
    assertNull(bm.getSegmentMemory());
    bm = (BetaMemory) ksession.getNodeMemory(n4);
    assertNull(bm.getSegmentMemory());
    bm = (BetaMemory) ksession.getNodeMemory(n2);
    assertEquals(1, bm.getNodePosMaskBit());
    assertEquals(1, bm.getSegmentMemory().getAllLinkedMaskTest());
}
Also used : BetaNode(org.drools.core.reteoo.BetaNode) ClassObjectType(org.drools.core.base.ClassObjectType) JoinNode(org.drools.core.reteoo.JoinNode) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) BetaMemory(org.drools.core.reteoo.BetaMemory) MockObjectSource(org.drools.core.reteoo.MockObjectSource) MockTupleSource(org.drools.core.reteoo.MockTupleSource) BuildContext(org.drools.core.reteoo.builder.BuildContext) EmptyBetaConstraints(org.drools.core.common.EmptyBetaConstraints) StatefulKnowledgeSessionImpl(org.drools.kiesession.session.StatefulKnowledgeSessionImpl) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Test(org.junit.Test)

Example 15 with StatefulKnowledgeSessionImpl

use of org.drools.kiesession.session.StatefulKnowledgeSessionImpl in project drools by kiegroup.

the class NodeSegmentUnlinkingTest method testAllLinkedInWithJoinNodesOnly.

@Test
public void testAllLinkedInWithJoinNodesOnly() {
    setUp(JOIN_NODE);
    // make sure it created JoinNodes
    assertEquals(JoinNode.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)

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