use of org.drools.core.common.DefaultFactHandle in project drools by kiegroup.
the class AlphaNodeTest method testLiteralConstraintAssertObjectWithoutMemory.
@Test
public void testLiteralConstraintAssertObjectWithoutMemory() throws Exception {
InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
BuildContext buildContext = new BuildContext(kBase, Collections.emptyList());
buildContext.setRule(new RuleImpl("test"));
StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
final RuleImpl rule = new RuleImpl("test-rule");
PropagationContextFactory pctxFactory = RuntimeComponentFactory.get().getPropagationContextFactory();
final PropagationContext context = pctxFactory.createPropagationContext(0, PropagationContext.Type.INSERTION, null, null, null);
final MockObjectSource source = new MockObjectSource(buildContext.getNextNodeId());
AlphaNodeFieldConstraint constraint = ConstraintTestUtil.createCheeseTypeEqualsConstraint(store, "cheddar", useLambdaConstraint);
// With Memory
final AlphaNode alphaNode = new AlphaNode(buildContext.getNextNodeId(), constraint, source, // no memory
buildContext);
final MockObjectSink sink = new MockObjectSink();
alphaNode.addObjectSink(sink);
final Cheese cheddar = new Cheese("cheddar", 5);
final DefaultFactHandle f0 = (DefaultFactHandle) ksession.insert(cheddar);
// check sink is empty
assertLength(0, sink.getAsserted());
// object should assert as it passes text
alphaNode.assertObject(f0, context, ksession);
Assert.assertEquals(1, sink.getAsserted().size());
Object[] list = (Object[]) sink.getAsserted().get(0);
assertSame(cheddar, ksession.getObject((DefaultFactHandle) list[0]));
final Cheese stilton = new Cheese("stilton", 6);
final DefaultFactHandle f1 = new DefaultFactHandle(1, stilton);
// object should NOT assert as it does not pass test
alphaNode.assertObject(f1, context, ksession);
assertLength(1, sink.getAsserted());
list = (Object[]) sink.getAsserted().get(0);
assertSame(cheddar, ksession.getObject((DefaultFactHandle) list[0]));
}
use of org.drools.core.common.DefaultFactHandle 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 = KnowledgeBaseFactory.newKnowledgeBase();
BuildContext buildContext = new BuildContext(kBase, Collections.emptyList());
buildContext.setRule(new RuleImpl("test"));
StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
final RuleImpl rule = new RuleImpl("test-rule");
PropagationContextFactory pctxFactory = RuntimeComponentFactory.get().getPropagationContextFactory();
final PropagationContext context = pctxFactory.createPropagationContext(0, PropagationContext.Type.INSERTION, null, null, null);
final MockObjectSource source = new MockObjectSource(buildContext.getNextNodeId());
AlphaNodeFieldConstraint constraint = ConstraintTestUtil.createCheeseTypeEqualsConstraint(store, "cheddar", useLambdaConstraint);
final AlphaNode alphaNode = new AlphaNode(buildContext.getNextNodeId(), 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());
Assert.assertEquals(1, source.getUdated());
}
use of org.drools.core.common.DefaultFactHandle 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 = KnowledgeBaseFactory.newKnowledgeBase();
BuildContext buildContext = new BuildContext(kBase, Collections.emptyList());
buildContext.setRule(new RuleImpl("test"));
StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
final RuleImpl rule = new RuleImpl("test-rule");
PropagationContextFactory pctxFactory = RuntimeComponentFactory.get().getPropagationContextFactory();
final PropagationContext context = pctxFactory.createPropagationContext(0, PropagationContext.Type.INSERTION, null, null, null);
final MockObjectSource source = new MockObjectSource(buildContext.getNextNodeId());
AlphaNodeFieldConstraint constraint = ConstraintTestUtil.createCheeseTypeEqualsConstraint(store, "cheddar", useLambdaConstraint);
final AlphaNode alphaNode = new AlphaNode(buildContext.getNextNodeId(), 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.common.DefaultFactHandle 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());
}
use of org.drools.core.common.DefaultFactHandle in project drools by kiegroup.
the class ObjectHashMapTest method testIdentityWithResize.
@Test
public void testIdentityWithResize() {
KieBaseConfiguration kconf = RuleBaseFactory.newKnowledgeBaseConfiguration();
kconf.setOption(EqualityBehaviorOption.IDENTITY);
KieBase kbase = KnowledgeBaseFactory.newKnowledgeBase(RuleBaseFactory.newRuleBase(kconf));
KieSession ksession = kbase.newKieSession();
int length = 1 * 300;
List<FactHandle> handles = new ArrayList<FactHandle>(1000);
List<String> objects = new ArrayList<String>(1000);
for (int i = 0; i < length; i++) {
String s = getPropertyName(i);
FactHandle handle = ksession.insert(s);
objects.add(s);
handles.add(handle);
}
for (int i = 0; i < length; i++) {
String s = objects.get(i);
FactHandle handle = handles.get(i);
assertEquals(s, ksession.getObject(handle));
assertSame(handle, ksession.getFactHandle(s));
// now check with disconnected facthandle
handle = DefaultFactHandle.createFromExternalFormat(((DefaultFactHandle) handle).toExternalForm());
assertEquals(s, ksession.getObject(handle));
}
for (int i = 0; i < length; i++) {
FactHandle handle = handles.get(i);
// now retract with disconnected facthandle
handle = DefaultFactHandle.createFromExternalFormat(((DefaultFactHandle) handle).toExternalForm());
ksession.retract(handle);
assertEquals(length - i - 1, ksession.getObjects().size());
assertEquals(length - i - 1, ksession.getFactHandles().size());
}
assertEquals(0, ksession.getObjects().size());
assertEquals(0, ksession.getFactHandles().size());
}
Aggregations