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 = (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 ClassFieldReader extractor = store.getReader(Cheese.class, "type");
final FieldValue field = FieldFactory.getInstance().getFieldValue("cheddar");
final MvelConstraint constraint = new MvelConstraintTestUtil("type == \"cheddar\"", field, extractor);
// With Memory
final AlphaNode alphaNode = new AlphaNode(buildContext.getNextId(), 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);
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 FactHandleTest method testEqualsObject.
/*
* Class under test for boolean equals(Object)
*/
@Test
public void testEqualsObject() {
final DefaultFactHandle f0 = new DefaultFactHandle(134, "cheese");
final DefaultFactHandle f1 = new DefaultFactHandle(96, "cheese");
final DefaultFactHandle f3 = new DefaultFactHandle(96, "cheese");
assertFalse("f0 should not equal f1", f0.equals(f1));
assertEquals(f1, f3);
assertNotSame(f1, f3);
}
use of org.drools.core.common.DefaultFactHandle in project drools by kiegroup.
the class FactHandleTest method testInvalidate.
@Test
public void testInvalidate() {
final DefaultFactHandle f0 = new DefaultFactHandle(134, "cheese");
assertEquals(134, f0.getId());
f0.invalidate();
// invalidate no longer sets the id to -1
assertEquals(134, f0.getId());
}
use of org.drools.core.common.DefaultFactHandle in project drools by kiegroup.
the class DefaultFactHandleFactoryTest method testNewFactHandle.
/*
* Class under test for FactHandle newFactHandle()
*/
@Test
public void testNewFactHandle() {
final ReteooFactHandleFactory factory = new ReteooFactHandleFactory();
DefaultFactHandle handle = (DefaultFactHandle) factory.newFactHandle("cheese", null, null, null);
assertEquals(1, handle.getId());
assertEquals(1, handle.getRecency());
// issue new handle
handle = (DefaultFactHandle) factory.newFactHandle("cheese", null, null, null);
assertEquals(2, handle.getId());
assertEquals(2, handle.getRecency());
// issue new handle, under a different reference so we can destroy later
final DefaultFactHandle handle2 = (DefaultFactHandle) factory.newFactHandle("cheese", null, null, null);
assertEquals(3, handle2.getId());
assertEquals(3, handle2.getRecency());
// Check recency increasion works
factory.increaseFactHandleRecency(handle);
assertEquals(4, handle.getRecency());
// issue new handle and make sure recency is still inline
handle = (DefaultFactHandle) factory.newFactHandle("cheese", null, null, null);
assertEquals(4, handle.getId());
assertEquals(5, handle.getRecency());
// destroy handle
factory.destroyFactHandle(handle2);
// @FIXME recycling is currently disabled
// // issue new fact handle and make sure it recycled the id=2
// handle = (DefaultFactHandle) factory.newFactHandle( "cheese", false, null );
// assertEquals( 2,
// handle.getId() );
// assertEquals( 5,
// handle.getRecency() );
//
// // issue new handle making sure it correctly resumes ids and recency
// handle = (DefaultFactHandle) factory.newFactHandle( "cheese", false, null );
// assertEquals( 4,
// handle.getId() );
// assertEquals( 6,
// handle.getRecency() );
}
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());
KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase(kconf);
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