Search in sources :

Example 26 with StatefulKnowledgeSessionImpl

use of org.drools.core.impl.StatefulKnowledgeSessionImpl 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());
}
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) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) FieldValue(org.drools.core.spi.FieldValue) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 27 with StatefulKnowledgeSessionImpl

use of org.drools.core.impl.StatefulKnowledgeSessionImpl 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());
}
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) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) FieldValue(org.drools.core.spi.FieldValue) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 28 with StatefulKnowledgeSessionImpl

use of org.drools.core.impl.StatefulKnowledgeSessionImpl in project drools by kiegroup.

the class NodeSegmentUnlinkingTest method tesMultiNodeSegmentDifferentInitialisationPoints.

@Test
public void tesMultiNodeSegmentDifferentInitialisationPoints() {
    setUp(JOIN_NODE);
    // Initialise from n3
    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase(kconf);
    StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
    createSegmentMemory(n3, ksession);
    BetaMemory bm = (BetaMemory) ksession.getNodeMemory(n1);
    assertNull(bm.getSegmentMemory());
    bm = (BetaMemory) ksession.getNodeMemory(n3);
    assertEquals(1, bm.getNodePosMaskBit());
    assertEquals(15, bm.getSegmentMemory().getAllLinkedMaskTest());
    bm = (BetaMemory) ksession.getNodeMemory(n4);
    assertEquals(2, bm.getNodePosMaskBit());
    assertEquals(15, bm.getSegmentMemory().getAllLinkedMaskTest());
    bm = (BetaMemory) ksession.getNodeMemory(n5);
    assertEquals(4, bm.getNodePosMaskBit());
    assertEquals(15, bm.getSegmentMemory().getAllLinkedMaskTest());
    bm = (BetaMemory) ksession.getNodeMemory(n6);
    assertEquals(8, bm.getNodePosMaskBit());
    assertEquals(15, bm.getSegmentMemory().getAllLinkedMaskTest());
    // Initialise from n4
    kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase(kconf);
    ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
    bm = createSegmentMemory(n4, ksession);
    bm = (BetaMemory) ksession.getNodeMemory(n1);
    assertNull(bm.getSegmentMemory());
    bm = (BetaMemory) ksession.getNodeMemory(n3);
    assertEquals(1, bm.getNodePosMaskBit());
    assertEquals(15, bm.getSegmentMemory().getAllLinkedMaskTest());
    bm = (BetaMemory) ksession.getNodeMemory(n4);
    assertEquals(2, bm.getNodePosMaskBit());
    assertEquals(15, bm.getSegmentMemory().getAllLinkedMaskTest());
    bm = (BetaMemory) ksession.getNodeMemory(n5);
    assertEquals(4, bm.getNodePosMaskBit());
    assertEquals(15, bm.getSegmentMemory().getAllLinkedMaskTest());
    bm = (BetaMemory) ksession.getNodeMemory(n6);
    assertEquals(8, bm.getNodePosMaskBit());
    assertEquals(15, bm.getSegmentMemory().getAllLinkedMaskTest());
    // Initialise from n5
    kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase(kconf);
    ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
    createSegmentMemory(n5, ksession);
    bm = (BetaMemory) ksession.getNodeMemory(n1);
    assertNull(bm.getSegmentMemory());
    bm = (BetaMemory) ksession.getNodeMemory(n3);
    assertEquals(1, bm.getNodePosMaskBit());
    assertEquals(15, bm.getSegmentMemory().getAllLinkedMaskTest());
    bm = (BetaMemory) ksession.getNodeMemory(n4);
    assertEquals(2, bm.getNodePosMaskBit());
    assertEquals(15, bm.getSegmentMemory().getAllLinkedMaskTest());
    bm = (BetaMemory) ksession.getNodeMemory(n5);
    assertEquals(4, bm.getNodePosMaskBit());
    assertEquals(15, bm.getSegmentMemory().getAllLinkedMaskTest());
    bm = (BetaMemory) ksession.getNodeMemory(n6);
    assertEquals(8, bm.getNodePosMaskBit());
    assertEquals(15, bm.getSegmentMemory().getAllLinkedMaskTest());
    // Initialise from n6
    kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase(kconf);
    ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
    createSegmentMemory(n6, ksession);
    bm = (BetaMemory) ksession.getNodeMemory(n1);
    assertNull(bm.getSegmentMemory());
    bm = (BetaMemory) ksession.getNodeMemory(n3);
    assertEquals(1, bm.getNodePosMaskBit());
    assertEquals(15, bm.getSegmentMemory().getAllLinkedMaskTest());
    bm = (BetaMemory) ksession.getNodeMemory(n4);
    assertEquals(2, bm.getNodePosMaskBit());
    assertEquals(15, bm.getSegmentMemory().getAllLinkedMaskTest());
    bm = (BetaMemory) ksession.getNodeMemory(n5);
    assertEquals(4, bm.getNodePosMaskBit());
    assertEquals(15, bm.getSegmentMemory().getAllLinkedMaskTest());
    bm = (BetaMemory) ksession.getNodeMemory(n6);
    assertEquals(8, bm.getNodePosMaskBit());
    assertEquals(15, bm.getSegmentMemory().getAllLinkedMaskTest());
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 29 with StatefulKnowledgeSessionImpl

use of org.drools.core.impl.StatefulKnowledgeSessionImpl in project drools by kiegroup.

the class NodeSegmentUnlinkingTest method testLiaNodeLinking.

@Test
public void testLiaNodeLinking() {
    setUp(JOIN_NODE);
    // Initialise from lian
    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase(kconf);
    StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
    SegmentUtilities.createSegmentMemory(liaNode, ksession);
    InternalFactHandle fh1 = (InternalFactHandle) ksession.insert("str1");
    n1.assertObject(fh1, context, ksession);
    LiaNodeMemory liaMem = (LiaNodeMemory) ksession.getNodeMemory(liaNode);
    assertEquals(1, liaMem.getNodePosMaskBit());
    assertEquals(3, liaMem.getSegmentMemory().getAllLinkedMaskTest());
    BetaMemory bm1 = (BetaMemory) ksession.getNodeMemory(n1);
    assertEquals(2, bm1.getNodePosMaskBit());
    assertEquals(3, bm1.getSegmentMemory().getAllLinkedMaskTest());
    // still unlinked
    assertFalse(liaMem.getSegmentMemory().isSegmentLinked());
    // now linked
    InternalFactHandle fh2 = (InternalFactHandle) ksession.insert("str2");
    liaNode.assertObject(fh2, context, ksession);
    assertTrue(liaMem.getSegmentMemory().isSegmentLinked());
    // test unlink after one retract
    liaNode.retractLeftTuple(fh2.getFirstLeftTuple(), context, ksession);
    assertFalse(liaMem.getSegmentMemory().isSegmentLinked());
    // check counter, after multiple asserts
    InternalFactHandle fh3 = (InternalFactHandle) ksession.insert("str3");
    InternalFactHandle fh4 = (InternalFactHandle) ksession.insert("str4");
    liaNode.assertObject(fh3, context, ksession);
    liaNode.assertObject(fh4, context, ksession);
    assertTrue(liaMem.getSegmentMemory().isSegmentLinked());
    liaNode.retractLeftTuple(fh3.getFirstLeftTuple(), context, ksession);
    assertTrue(liaMem.getSegmentMemory().isSegmentLinked());
    liaNode.retractLeftTuple(fh4.getFirstLeftTuple(), context, ksession);
    assertFalse(liaMem.getSegmentMemory().isSegmentLinked());
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) InternalFactHandle(org.drools.core.common.InternalFactHandle) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) LiaNodeMemory(org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory) Test(org.junit.Test)

Example 30 with StatefulKnowledgeSessionImpl

use of org.drools.core.impl.StatefulKnowledgeSessionImpl in project drools by kiegroup.

the class NodeSegmentUnlinkingTest method testLiaNodeInitialisation.

@Test
public void testLiaNodeInitialisation() {
    setUp(JOIN_NODE);
    // Initialise from lian
    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase(kconf);
    StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
    SegmentUtilities.createSegmentMemory(liaNode, ksession);
    liaNode.assertObject((InternalFactHandle) ksession.insert("str"), context, ksession);
    LiaNodeMemory liaMem = (LiaNodeMemory) ksession.getNodeMemory(liaNode);
    assertEquals(1, liaMem.getNodePosMaskBit());
    assertEquals(3, liaMem.getSegmentMemory().getAllLinkedMaskTest());
    BetaMemory bm1 = (BetaMemory) ksession.getNodeMemory(n1);
    assertEquals(2, bm1.getNodePosMaskBit());
    assertEquals(3, bm1.getSegmentMemory().getAllLinkedMaskTest());
    // Initialise from n1
    kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase(kconf);
    ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
    n1.assertObject((InternalFactHandle) ksession.insert("str"), context, ksession);
    liaMem = (LiaNodeMemory) ksession.getNodeMemory(liaNode);
    assertEquals(1, liaMem.getNodePosMaskBit());
    assertEquals(3, liaMem.getSegmentMemory().getAllLinkedMaskTest());
    bm1 = (BetaMemory) ksession.getNodeMemory(n1);
    assertEquals(2, bm1.getNodePosMaskBit());
    assertEquals(3, bm1.getSegmentMemory().getAllLinkedMaskTest());
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) LiaNodeMemory(org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory) Test(org.junit.Test)

Aggregations

StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)79 Test (org.junit.Test)63 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)41 ArrayList (java.util.ArrayList)22 InternalFactHandle (org.drools.core.common.InternalFactHandle)22 KieSession (org.kie.api.runtime.KieSession)22 List (java.util.List)20 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)17 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)17 ClassObjectType (org.drools.core.base.ClassObjectType)14 FactHandle (org.kie.api.runtime.rule.FactHandle)14 KieBase (org.kie.api.KieBase)13 HashMap (java.util.HashMap)9 LeftTupleImpl (org.drools.core.reteoo.LeftTupleImpl)8 BuildContext (org.drools.core.reteoo.builder.BuildContext)8 Cheese (org.drools.core.test.model.Cheese)8 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)7 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)6 TruthMaintenanceSystem (org.drools.core.common.TruthMaintenanceSystem)6 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)6