use of org.drools.core.impl.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");
KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase(kconf);
BuildContext buildContext = new BuildContext(kBase);
MockObjectSource mockObjectSource = new MockObjectSource(8);
MockTupleSource mockTupleSource = new MockTupleSource(9);
// 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.core.impl.StatefulKnowledgeSessionImpl in project drools by kiegroup.
the class PropertyChangeListenerTest method test1.
@Test
public void test1() {
StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
final ObjectTypeNode objectTypeNode = new ObjectTypeNode(1, this.entryPoint, new ClassObjectType(State.class), buildContext);
objectTypeNode.attach(buildContext);
final MockObjectSink sink = new MockObjectSink();
objectTypeNode.addObjectSink(sink);
final State a = new State("go");
ksession.insert(a, true);
ksession.fireAllRules();
assertEquals(1, sink.getAsserted().size());
a.setState("stop");
}
use of org.drools.core.impl.StatefulKnowledgeSessionImpl 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);
// LinkedList matches two ObjectTypeNodes
h1.setObject(new LinkedList());
rete.assertObject(h1, pctxFactory.createPropagationContext(0, PropagationContext.Type.INSERTION, null, null, null), ksession);
ClassObjectTypeConf conf = (ClassObjectTypeConf) ksession.getObjectTypeConfigurationRegistry().getObjectTypeConf(this.entryPoint.getEntryPoint(), new ArrayList());
assertLength(3, conf.getObjectTypeNodes());
conf = (ClassObjectTypeConf) ksession.getObjectTypeConfigurationRegistry().getObjectTypeConf(this.entryPoint.getEntryPoint(), new ArrayList());
assertLength(3, conf.getObjectTypeNodes());
}
use of org.drools.core.impl.StatefulKnowledgeSessionImpl 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());
}
use of org.drools.core.impl.StatefulKnowledgeSessionImpl 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);
}
Aggregations