use of org.drools.core.reteoo.builder.BuildContext in project drools by kiegroup.
the class SegmentPropagationTest method createContext.
public BuildContext createContext() {
RuleBaseConfiguration conf = new RuleBaseConfiguration();
KnowledgeBaseImpl rbase = new KnowledgeBaseImpl("ID", conf);
BuildContext buildContext = new BuildContext(rbase);
RuleImpl rule = new RuleImpl("rule1").setPackage("org.pkg1");
InternalKnowledgePackage pkg = new KnowledgePackageImpl("org.pkg1");
pkg.getDialectRuntimeRegistry().setDialectData("mvel", new MVELDialectRuntimeData());
pkg.addRule(rule);
buildContext.setRule(rule);
return buildContext;
}
use of org.drools.core.reteoo.builder.BuildContext in project drools by kiegroup.
the class AlphaNodeTest method testReturnValueConstraintAssertObject.
/*
* This just test AlphaNode With a different Constraint type.
*/
@Test
public void testReturnValueConstraintAssertObject() throws Exception {
InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase();
BuildContext buildContext = new BuildContext(kBase);
buildContext.setRule(new RuleImpl("test"));
StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
final RuleImpl rule = new RuleImpl("test-rule");
PropagationContextFactory pctxFactory = kBase.getConfiguration().getComponentFactory().getPropagationContextFactory();
final PropagationContext context = pctxFactory.createPropagationContext(0, PropagationContext.Type.INSERTION, null, null, null);
final MockObjectSource source = new MockObjectSource(buildContext.getNextId());
final InternalReadAccessor extractor = store.getReader(Cheese.class, "type");
final FieldValue field = FieldFactory.getInstance().getFieldValue("cheddar");
final MvelConstraint constraint = new MvelConstraintTestUtil("type == \"cheddar\"", field, extractor);
final AlphaNode alphaNode = new AlphaNode(buildContext.getNextId(), constraint, source, buildContext);
final MockObjectSink sink = new MockObjectSink();
alphaNode.addObjectSink(sink);
final Cheese cheddar = new Cheese("cheddar", 5);
final DefaultFactHandle f0 = (DefaultFactHandle) ksession.insert(cheddar);
assertLength(0, sink.getAsserted());
// object should assert as it passes text
alphaNode.assertObject(f0, context, ksession);
assertLength(1, sink.getAsserted());
final Object[] list = (Object[]) sink.getAsserted().get(0);
assertSame(cheddar, ksession.getObject((DefaultFactHandle) list[0]));
final Cheese stilton = new Cheese("stilton", 6);
f0.setObject(stilton);
sink.getAsserted().clear();
// object should not assert as it does not pass text
alphaNode.assertObject(f0, context, ksession);
assertLength(0, sink.getAsserted());
}
use of org.drools.core.reteoo.builder.BuildContext in project drools by kiegroup.
the class AlphaNodeTest method testUpdateSinkWithoutMemory.
@Test
public void testUpdateSinkWithoutMemory() {
// An AlphaNode should try and repropagate from its source
InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase();
BuildContext buildContext = new BuildContext(kBase);
buildContext.setRule(new RuleImpl("test"));
StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
final RuleImpl rule = new RuleImpl("test-rule");
PropagationContextFactory pctxFactory = kBase.getConfiguration().getComponentFactory().getPropagationContextFactory();
final PropagationContext context = pctxFactory.createPropagationContext(0, PropagationContext.Type.INSERTION, null, null, null);
final MockObjectSource source = new MockObjectSource(buildContext.getNextId());
final InternalReadAccessor extractor = store.getReader(Cheese.class, "type");
final FieldValue field = FieldFactory.getInstance().getFieldValue("cheddar");
final MvelConstraint constraint = new MvelConstraintTestUtil("type == \"cheddar\"", field, extractor);
final AlphaNode alphaNode = new AlphaNode(buildContext.getNextId(), constraint, source, // no memory
buildContext);
alphaNode.attach(buildContext);
final MockObjectSink sink1 = new MockObjectSink();
alphaNode.addObjectSink(sink1);
// Assert a single fact which should be in the AlphaNode memory and also
// propagated to the
// the tuple sink
final Cheese cheese = new Cheese("cheddar", 0);
final DefaultFactHandle handle1 = new DefaultFactHandle(1, cheese);
// adding handle to the mock source
source.addFact(handle1);
alphaNode.assertObject(handle1, context, ksession);
// Create a fact that should not be propagated, since the alpha node restriction will filter it out
final Cheese stilton = new Cheese("stilton", 10);
final DefaultFactHandle handle2 = new DefaultFactHandle(2, stilton);
// adding handle to the mock source
source.addFact(handle2);
alphaNode.assertObject(handle2, context, ksession);
assertLength(1, sink1.getAsserted());
// Attach a new tuple sink
final MockObjectSink sink2 = new MockObjectSink();
// Tell the alphanode to update the new node. Make sure the first sink1
// is not updated
// likewise the source should not do anything
alphaNode.updateSink(sink2, context, ksession);
assertLength(1, sink1.getAsserted());
assertLength(1, sink2.getAsserted());
assertEquals(1, source.getUdated());
}
use of org.drools.core.reteoo.builder.BuildContext in project drools by kiegroup.
the class NodeSegmentUnlinkingTest method setUp.
public void setUp(int... type) {
KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase(kconf);
buildContext = new BuildContext(kBase);
PropagationContextFactory pctxFactory = kBase.getConfiguration().getComponentFactory().getPropagationContextFactory();
context = pctxFactory.createPropagationContext(0, PropagationContext.Type.INSERTION, null, null, null);
MockTupleSource mockTupleSource = new MockTupleSource(9);
rule1 = new RuleImpl("rule1");
rule2 = new RuleImpl("rule2");
rule3 = new RuleImpl("rule3");
ObjectTypeNode otn = new ObjectTypeNode(3, null, new ClassObjectType(String.class), buildContext);
liaNode = new LeftInputAdapterNode(4, otn, buildContext);
// 3, 4, 5, 6 are in same shared segment
n1 = createBetaNode(10, type[0], liaNode);
n2 = createBetaNode(11, type[1], n1);
RuleTerminalNode rtn1 = new RuleTerminalNode(18, n2, rule1, rule1.getLhs(), 0, buildContext);
rtn1.attach(buildContext);
n3 = createBetaNode(12, type[2], n1);
n4 = createBetaNode(13, type[3], n3);
n5 = createBetaNode(14, type[4], n4);
n6 = createBetaNode(15, type[5], n5);
RuleTerminalNode rtn2 = new RuleTerminalNode(19, n6, rule2, rule2.getLhs(), 0, buildContext);
rtn2.attach(buildContext);
n7 = createBetaNode(16, type[6], n6);
n8 = createBetaNode(17, type[7], n7);
RuleTerminalNode rtn3 = new RuleTerminalNode(20, n8, rule3, rule3.getLhs(), 0, buildContext);
rtn3.attach(buildContext);
// n1 -> n2 -> r1
// \
// n3 -> n4 -> n5 -> n6 -> r2
// \
// n7 -> n8 -> r3
n1.addAssociation(rule1);
n1.addAssociation(rule2);
n1.addAssociation(rule3);
n2.addAssociation(rule1);
n2.addAssociation(rule2);
n2.addAssociation(rule3);
n3.addAssociation(rule2);
n3.addAssociation(rule3);
n4.addAssociation(rule2);
n4.addAssociation(rule3);
n5.addAssociation(rule2);
n5.addAssociation(rule3);
n6.addAssociation(rule2);
n6.addAssociation(rule3);
n7.addAssociation(rule3);
n8.addAssociation(rule3);
}
use of org.drools.core.reteoo.builder.BuildContext 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());
}
Aggregations