Search in sources :

Example 71 with StatefulKnowledgeSessionImpl

use of org.drools.core.impl.StatefulKnowledgeSessionImpl 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 72 with StatefulKnowledgeSessionImpl

use of org.drools.core.impl.StatefulKnowledgeSessionImpl 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)

Example 73 with StatefulKnowledgeSessionImpl

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

the class NodeSegmentUnlinkingTest method testAllLinkedInWithExistsNodesOnly.

@Test
public void testAllLinkedInWithExistsNodesOnly() {
    setUp(EXISTS_NODE);
    // make sure it created ExistsNodes
    assertEquals(ExistsNode.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)

Example 74 with StatefulKnowledgeSessionImpl

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

the class NodeSegmentUnlinkingTest method testAllLinkedInWithNotNodesOnly.

@Test
public void testAllLinkedInWithNotNodesOnly() {
    setUp(NOT_NODE);
    // make sure it created NotNodes
    assertEquals(NotNode.class, n3.getClass());
    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase(kconf);
    StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
    BetaMemory bm = (BetaMemory) ksession.getNodeMemory(n3);
    createSegmentMemory(n3, ksession);
    // not nodes start off linked
    assertTrue(bm.getSegmentMemory().isSegmentLinked());
    // unlinked after first assertion
    DefaultFactHandle f1 = (DefaultFactHandle) ksession.insert("test1");
    n3.assertObject(f1, context, ksession);
    // this doesn't unlink on the assertObject, as the node's memory must be processed. So use the helper method the main network evaluator uses.
    PhreakNotNode.unlinkNotNodeOnRightInsert((NotNode) n3, bm, ksession);
    assertFalse(bm.getSegmentMemory().isSegmentLinked());
    n3.retractRightTuple(f1.getFirstRightTuple(), context, ksession);
    assertTrue(bm.getSegmentMemory().isSegmentLinked());
// assertFalse( bm.getSegmentMemory().isSigmentLinked() ); // check retraction unlinks again
}
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)

Example 75 with StatefulKnowledgeSessionImpl

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

the class ReteTest method testAssertObject.

/**
 * Test asserts correctly propagate
 *
 * @throws Exception
 */
@Test
public void testAssertObject() throws Exception {
    StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
    // Create a Rete network with ObjectTypeNodes for List, Collection and ArrayList
    final Rete rete = kBase.getRete();
    final ObjectTypeNode objectTypeNode = new ObjectTypeNode(1, this.entryPoint, new ClassObjectType(List.class), buildContext);
    objectTypeNode.attach(buildContext);
    final MockObjectSink sink1 = new MockObjectSink();
    objectTypeNode.addObjectSink(sink1);
    // There are no String ObjectTypeNodes, make sure its not propagated
    final String string = "String";
    final DefaultFactHandle h1 = new DefaultFactHandle(1, string);
    rete.assertObject(h1, pctxFactory.createPropagationContext(0, PropagationContext.Type.INSERTION, null, null, null), ksession);
    assertLength(0, sink1.getAsserted());
    // There is a List ObjectTypeNode, make sure it was propagated
    final List list = new ArrayList();
    final DefaultFactHandle h2 = new DefaultFactHandle(1, list);
    rete.assertObject(h2, pctxFactory.createPropagationContext(0, PropagationContext.Type.INSERTION, null, null, null), ksession);
    ksession.fireAllRules();
    final List asserted = sink1.getAsserted();
    assertLength(1, asserted);
    final Object[] results = (Object[]) asserted.get(0);
    assertSame(list, ((DefaultFactHandle) results[0]).getObject());
}
Also used : DefaultFactHandle(org.drools.core.common.DefaultFactHandle) ClassObjectType(org.drools.core.base.ClassObjectType) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Aggregations

StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)84 Test (org.junit.Test)63 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)41 KieSession (org.kie.api.runtime.KieSession)24 ArrayList (java.util.ArrayList)22 InternalFactHandle (org.drools.core.common.InternalFactHandle)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 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)8 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