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() );
}
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());
}
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());
}
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());
}
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());
}
Aggregations