Search in sources :

Example 21 with StatefulKnowledgeSessionImpl

use of org.drools.kiesession.session.StatefulKnowledgeSessionImpl in project drools by kiegroup.

the class PropertyChangeListenerTest method test1.

@Test
public void test1() {
    StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
    final ObjectTypeNode objectTypeNode = new ObjectTypeNode(1, this.entryPoint, new ClassObjectType(State.class), buildContext);
    objectTypeNode.attach(buildContext);
    final MockObjectSink sink = new MockObjectSink();
    objectTypeNode.addObjectSink(sink);
    final State a = new State("go");
    ksession.insert(a, true);
    ksession.fireAllRules();
    assertEquals(1, sink.getAsserted().size());
    a.setState("stop");
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) StatefulKnowledgeSessionImpl(org.drools.kiesession.session.StatefulKnowledgeSessionImpl) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) MockObjectSink(org.drools.core.reteoo.MockObjectSink) Test(org.junit.Test)

Example 22 with StatefulKnowledgeSessionImpl

use of org.drools.kiesession.session.StatefulKnowledgeSessionImpl in project drools by kiegroup.

the class ReteTest method testNotShadowed.

@Test
@Ignore
public void testNotShadowed() {
    Properties properties = new Properties();
    properties.setProperty("drools.shadowProxyExcludes", "org.drools.core.test.model.Cheese");
    RuleBaseConfiguration conf = new RuleBaseConfiguration(properties);
    InternalKnowledgeBase kBase = (InternalKnowledgeBase) RuleBaseFactory.newRuleBase(conf);
    buildContext = new BuildContext(kBase, Collections.emptyList());
    final StatefulKnowledgeSessionImpl ksession = new StatefulKnowledgeSessionImpl(1L, kBase);
    // Create a Rete network with ObjectTypeNodes for List, Collection and ArrayList
    final Rete rete = kBase.getRete();
    final EntryPointNode entryPoint = new EntryPointNode(0, rete, buildContext);
    entryPoint.attach(buildContext);
    final ObjectTypeNode objectTypeNode = new ObjectTypeNode(1, entryPoint, new ClassObjectType(Cheese.class), buildContext);
    objectTypeNode.attach(buildContext);
    final MockObjectSink sink1 = new MockObjectSink();
    objectTypeNode.addObjectSink(sink1);
    // There are no String ObjectTypeNodes, make sure its not propagated
    final Cheese cheese = new Cheese("brie", 15);
    final DefaultFactHandle h1 = new DefaultFactHandle(1, cheese);
    rete.assertObject(h1, pctxFactory.createPropagationContext(0, PropagationContext.Type.INSERTION, null, null, null), ksession);
    ksession.fireAllRules();
    final Object[] results = (Object[]) sink1.getAsserted().get(0);
}
Also used : Rete(org.drools.core.reteoo.Rete) ClassObjectType(org.drools.core.base.ClassObjectType) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) Cheese(org.drools.core.test.model.Cheese) MockObjectSink(org.drools.core.reteoo.MockObjectSink) Properties(java.util.Properties) RuleBaseConfiguration(org.drools.core.RuleBaseConfiguration) EntryPointNode(org.drools.core.reteoo.EntryPointNode) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) BuildContext(org.drools.core.reteoo.builder.BuildContext) StatefulKnowledgeSessionImpl(org.drools.kiesession.session.StatefulKnowledgeSessionImpl) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 23 with StatefulKnowledgeSessionImpl

use of org.drools.kiesession.session.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 : Rete(org.drools.core.reteoo.Rete) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) ClassObjectType(org.drools.core.base.ClassObjectType) StatefulKnowledgeSessionImpl(org.drools.kiesession.session.StatefulKnowledgeSessionImpl) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) MockObjectSink(org.drools.core.reteoo.MockObjectSink) Test(org.junit.Test)

Example 24 with StatefulKnowledgeSessionImpl

use of org.drools.kiesession.session.StatefulKnowledgeSessionImpl in project drools by kiegroup.

the class RuleUnlinkingTest method testRuleSegmentsAllLinkedTestMasks.

@Test
public void testRuleSegmentsAllLinkedTestMasks() {
    setUp(JOIN_NODE);
    InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
    StatefulKnowledgeSessionImpl wm = new StatefulKnowledgeSessionImpl(1L, kBase);
    PathMemory rs = wm.getNodeMemory(rtn1);
    assertFalse(rs.isRuleLinked());
    assertEquals(1, rs.getAllLinkedMaskTest());
    rs = wm.getNodeMemory(rtn2);
    assertFalse(rs.isRuleLinked());
    assertEquals(3, rs.getAllLinkedMaskTest());
    rs = wm.getNodeMemory(rtn3);
    assertFalse(rs.isRuleLinked());
    assertEquals(7, rs.getAllLinkedMaskTest());
}
Also used : StatefulKnowledgeSessionImpl(org.drools.kiesession.session.StatefulKnowledgeSessionImpl) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) PathMemory(org.drools.core.reteoo.PathMemory) Test(org.junit.Test)

Example 25 with StatefulKnowledgeSessionImpl

use of org.drools.kiesession.session.StatefulKnowledgeSessionImpl in project drools by kiegroup.

the class RuleUnlinkingTest method testSegmentNodeReferencesToSegments.

@Test
public void testSegmentNodeReferencesToSegments() {
    setUp(JOIN_NODE);
    InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
    StatefulKnowledgeSessionImpl wm = new StatefulKnowledgeSessionImpl(1L, kBase);
    BetaMemory bm = null;
    List<PathMemory> list;
    PathMemory rtn1Rs = wm.getNodeMemory(rtn1);
    PathMemory rtn2Rs = wm.getNodeMemory(rtn2);
    PathMemory rtn3Rs = wm.getNodeMemory(rtn3);
    // n1
    bm = createSegmentMemory(n1, wm);
    assertEquals(2, bm.getNodePosMaskBit());
    assertEquals(15, bm.getSegmentMemory().getAllLinkedMaskTest());
    assertEquals(1, bm.getSegmentMemory().getSegmentPosMaskBit());
    list = bm.getSegmentMemory().getPathMemories();
    assertEquals(3, list.size());
    assertTrue(list.contains(rtn1Rs));
    assertTrue(list.contains(rtn2Rs));
    assertTrue(list.contains(rtn3Rs));
    // n2
    bm = createSegmentMemory(n2, wm);
    assertEquals(4, bm.getNodePosMaskBit());
    assertEquals(15, bm.getSegmentMemory().getAllLinkedMaskTest());
    assertEquals(1, bm.getSegmentMemory().getSegmentPosMaskBit());
    list = bm.getSegmentMemory().getPathMemories();
    assertEquals(3, list.size());
    assertTrue(list.contains(rtn1Rs));
    assertTrue(list.contains(rtn2Rs));
    assertTrue(list.contains(rtn3Rs));
    // n3
    bm = createSegmentMemory(n3, wm);
    assertEquals(8, bm.getNodePosMaskBit());
    assertEquals(15, bm.getSegmentMemory().getAllLinkedMaskTest());
    assertEquals(1, bm.getSegmentMemory().getSegmentPosMaskBit());
    list = bm.getSegmentMemory().getPathMemories();
    assertEquals(3, list.size());
    assertTrue(list.contains(rtn1Rs));
    assertTrue(list.contains(rtn2Rs));
    assertTrue(list.contains(rtn3Rs));
    // n4
    bm = createSegmentMemory(n4, wm);
    assertEquals(1, bm.getNodePosMaskBit());
    assertEquals(3, bm.getSegmentMemory().getAllLinkedMaskTest());
    assertEquals(2, bm.getSegmentMemory().getSegmentPosMaskBit());
    list = bm.getSegmentMemory().getPathMemories();
    assertEquals(2, list.size());
    assertTrue(list.contains(rtn2Rs));
    assertTrue(list.contains(rtn3Rs));
    // n5
    bm = createSegmentMemory(n5, wm);
    assertEquals(2, bm.getNodePosMaskBit());
    assertEquals(3, bm.getSegmentMemory().getAllLinkedMaskTest());
    assertEquals(2, bm.getSegmentMemory().getSegmentPosMaskBit());
    list = bm.getSegmentMemory().getPathMemories();
    assertEquals(2, list.size());
    assertTrue(list.contains(rtn2Rs));
    assertTrue(list.contains(rtn3Rs));
    // n6
    bm = createSegmentMemory(n6, wm);
    assertEquals(1, bm.getNodePosMaskBit());
    assertEquals(7, bm.getSegmentMemory().getAllLinkedMaskTest());
    assertEquals(4, bm.getSegmentMemory().getSegmentPosMaskBit());
    list = bm.getSegmentMemory().getPathMemories();
    assertEquals(1, list.size());
    assertTrue(list.contains(rtn3Rs));
    // n7
    bm = createSegmentMemory(n7, wm);
    assertEquals(2, bm.getNodePosMaskBit());
    assertEquals(7, bm.getSegmentMemory().getAllLinkedMaskTest());
    assertEquals(4, bm.getSegmentMemory().getSegmentPosMaskBit());
    list = bm.getSegmentMemory().getPathMemories();
    assertEquals(1, list.size());
    assertTrue(list.contains(rtn3Rs));
    // n8
    bm = createSegmentMemory(n8, wm);
    assertEquals(4, bm.getNodePosMaskBit());
    assertEquals(7, bm.getSegmentMemory().getAllLinkedMaskTest());
    assertEquals(4, bm.getSegmentMemory().getSegmentPosMaskBit());
    list = bm.getSegmentMemory().getPathMemories();
    assertEquals(1, list.size());
    assertTrue(list.contains(rtn3Rs));
}
Also used : StatefulKnowledgeSessionImpl(org.drools.kiesession.session.StatefulKnowledgeSessionImpl) BetaMemory(org.drools.core.reteoo.BetaMemory) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) PathMemory(org.drools.core.reteoo.PathMemory) Test(org.junit.Test)

Aggregations

StatefulKnowledgeSessionImpl (org.drools.kiesession.session.StatefulKnowledgeSessionImpl)66 Test (org.junit.Test)57 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)34 ArrayList (java.util.ArrayList)15 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)15 KieBase (org.kie.api.KieBase)15 ClassObjectType (org.drools.core.base.ClassObjectType)14 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)14 BetaMemory (org.drools.core.reteoo.BetaMemory)14 List (java.util.List)13 InternalFactHandle (org.drools.core.common.InternalFactHandle)13 KieSession (org.kie.api.runtime.KieSession)12 FactHandle (org.kie.api.runtime.rule.FactHandle)11 MockObjectSink (org.drools.core.reteoo.MockObjectSink)10 BuildContext (org.drools.core.reteoo.builder.BuildContext)10 LeftTupleImpl (org.drools.core.reteoo.LeftTupleImpl)8 HashMap (java.util.HashMap)7 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)7 PathMemory (org.drools.core.reteoo.PathMemory)7 Rete (org.drools.core.reteoo.Rete)7