Search in sources :

Example 46 with DefaultFactHandle

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]));
}
Also used : PropagationContextFactory(org.drools.core.common.PropagationContextFactory) PropagationContext(org.drools.core.spi.PropagationContext) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Cheese(org.drools.core.test.model.Cheese) MvelConstraintTestUtil(org.drools.core.rule.MvelConstraintTestUtil) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) BuildContext(org.drools.core.reteoo.builder.BuildContext) ClassFieldReader(org.drools.core.base.ClassFieldReader) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) FieldValue(org.drools.core.spi.FieldValue) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 47 with DefaultFactHandle

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);
}
Also used : DefaultFactHandle(org.drools.core.common.DefaultFactHandle) Test(org.junit.Test)

Example 48 with DefaultFactHandle

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());
}
Also used : DefaultFactHandle(org.drools.core.common.DefaultFactHandle) Test(org.junit.Test)

Example 49 with DefaultFactHandle

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() );
}
Also used : DefaultFactHandle(org.drools.core.common.DefaultFactHandle) Test(org.junit.Test)

Example 50 with DefaultFactHandle

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());
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Aggregations

DefaultFactHandle (org.drools.core.common.DefaultFactHandle)55 Test (org.junit.Test)45 FactHandle (org.kie.api.runtime.rule.FactHandle)19 InternalFactHandle (org.drools.core.common.InternalFactHandle)17 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)14 Cheese (org.drools.core.test.model.Cheese)14 KieSession (org.kie.api.runtime.KieSession)13 ArrayList (java.util.ArrayList)12 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)12 ClassObjectType (org.drools.core.base.ClassObjectType)11 FieldIndex (org.drools.core.util.AbstractHashTable.FieldIndex)9 RightTupleImpl (org.drools.core.reteoo.RightTupleImpl)8 InternalReadAccessor (org.drools.core.spi.InternalReadAccessor)8 EventFactHandle (org.drools.core.common.EventFactHandle)7 RightTuple (org.drools.core.reteoo.RightTuple)7 Declaration (org.drools.core.rule.Declaration)6 Pattern (org.drools.core.rule.Pattern)6 Tuple (org.drools.core.spi.Tuple)6 TupleIndexHashTable (org.drools.core.util.index.TupleIndexHashTable)6 List (java.util.List)5