Search in sources :

Example 11 with JoinNode

use of org.drools.core.reteoo.JoinNode in project drools by kiegroup.

the class RemoveRuleTest method testPopulatedSharedToRtn.

@Test
public void testPopulatedSharedToRtn() throws Exception {
    InternalKnowledgeBase kbase1 = buildKnowledgeBase("r1", "   A() B() C() D() E()\n");
    InternalWorkingMemory wm = ((InternalWorkingMemory) kbase1.newKieSession());
    List list = new ArrayList();
    wm.setGlobal("list", list);
    wm.insert(new A(1));
    wm.insert(new A(2));
    wm.insert(new B(1));
    wm.insert(new C(1));
    wm.insert(new D(1));
    wm.insert(new E(1));
    wm.fireAllRules();
    assertEquals(2, list.size());
    assertEquals(7, countNodeMemories(wm.getNodeMemories()));
    kbase1.addPackages(buildKnowledgePackage("r2", "   A() B() C() D() E()\n"));
    wm.fireAllRules();
    assertEquals(8, countNodeMemories(wm.getNodeMemories()));
    assertEquals(4, list.size());
    RuleTerminalNode rtn1 = getRtn("org.kie.r1", kbase1);
    RuleTerminalNode rtn2 = getRtn("org.kie.r2", kbase1);
    PathMemory pmem1 = (PathMemory) wm.getNodeMemory(rtn1);
    PathMemory pmem2 = (PathMemory) wm.getNodeMemory(rtn2);
    SegmentMemory[] smems1 = pmem1.getSegmentMemories();
    SegmentMemory[] smems2 = pmem2.getSegmentMemories();
    assertEquals(2, smems1.length);
    assertEquals(2, smems2.length);
    assertSame(smems1[0], smems2[0]);
    assertNotSame(smems1[1], smems2[1]);
    SegmentMemory sm = smems1[0];
    assertEquals(smems1[1], sm.getFirst());
    JoinNode eNode1 = (JoinNode) rtn1.getLeftTupleSource();
    JoinNode eNode2 = (JoinNode) rtn2.getLeftTupleSource();
    assertSame(eNode1, eNode2);
    pmem1 = (PathMemory) wm.getNodeMemory(rtn1);
    kbase1.removeRule("org.kie", "r2");
    System.out.println("---");
    assertEquals(7, countNodeMemories(wm.getNodeMemories()));
    assertNull(sm.getFirst());
    pmem1 = (PathMemory) wm.getNodeMemory(rtn1);
    smems1 = pmem1.getSegmentMemories();
    assertEquals(1, smems1.length);
    assertSame(sm, smems1[0]);
    wm.insert(new A(1));
    wm.fireAllRules();
    assertEquals("r1", ((Match) list.get(4)).getRule().getName());
    // only one more added, as second rule as removed
    assertEquals(5, list.size());
}
Also used : SegmentMemory(org.drools.core.reteoo.SegmentMemory) JoinNode(org.drools.core.reteoo.JoinNode) ArrayList(java.util.ArrayList) Match(org.kie.api.runtime.rule.Match) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) ArrayList(java.util.ArrayList) List(java.util.List) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) PathMemory(org.drools.core.reteoo.PathMemory) Test(org.junit.Test)

Example 12 with JoinNode

use of org.drools.core.reteoo.JoinNode in project drools by kiegroup.

the class SegmentPropagationTest method test1.

@Test
public void test1() {
    setupJoinNode();
    JoinNode parentNode = joinNode;
    JoinNode child1Node = new JoinNode();
    JoinNode child2Node = new JoinNode();
    JoinNode child3Node = new JoinNode();
    parentNode.addTupleSink(child1Node);
    parentNode.addTupleSink(child2Node);
    parentNode.addTupleSink(child3Node);
    SegmentMemory smem = new SegmentMemory(parentNode);
    smem.setTipNode(parentNode);
    // @formatter:off
    test().left().insert(a0, a1).right().insert(b0, b1, b2).preStaged(smem0).insert().delete().update().postStaged(smem0).insert(t(a1, b2), t(a1, b0), t(a0, b2), t(a0, b1)).delete().update().postStaged(smem1).insert(t(a0, b1), t(a0, b2), t(a1, b0), t(a1, b2)).delete().update().postStaged(smem2).insert(t(a0, b1), t(a0, b2), t(a1, b0), t(a1, b2)).delete().update().run();
    test().left().update(a0).preStaged(smem0).insert(t(a1, b2), t(a1, b0)).delete().update().postStaged(smem0).insert(t(a0, b2), t(a0, b1), t(a1, b2), t(a1, b0)).delete().update().postStaged(smem1).insert(t(a1, b0), t(a1, b2), t(a0, b1), t(a0, b2)).delete().update().postStaged(smem2).insert(t(a1, b0), t(a1, b2), t(a0, b1), t(a0, b2)).delete().update().run();
    test().right().delete(b2).preStaged(smem0).insert(t(a0, b1), t(a1, b0)).delete().update().postStaged(smem0).insert(t(a0, b1), t(a1, b0)).delete().update().postStaged(smem1).insert(t(a1, b0), t(a0, b1)).postStaged(smem2).insert(t(a1, b0), t(a0, b1)).run();
// @formatter:on
}
Also used : SegmentMemory(org.drools.core.reteoo.SegmentMemory) JoinNode(org.drools.core.reteoo.JoinNode) PhreakJoinNode(org.drools.core.phreak.PhreakJoinNode) Test(org.junit.Test)

Example 13 with JoinNode

use of org.drools.core.reteoo.JoinNode in project drools by kiegroup.

the class IndexingTest method testIndexingOnQueryUnification.

@Test(timeout = 10000)
public void testIndexingOnQueryUnification() throws Exception {
    String str = "";
    str += "package org.drools.compiler.test  \n";
    str += "import org.drools.compiler.Person \n";
    str += "query peeps( String $name, String $likes, String $street) \n";
    str += "    $p : Person( $name := name, $likes := likes, $street := address.street ) \n";
    str += "end\n";
    KieBase kbase = loadKnowledgeBaseFromString(str);
    List<ObjectTypeNode> nodes = ((KnowledgeBaseImpl) kbase).getRete().getObjectTypeNodes();
    ObjectTypeNode node = null;
    for (ObjectTypeNode n : nodes) {
        if (((ClassObjectType) n.getObjectType()).getClassType() == DroolsQuery.class) {
            node = n;
            break;
        }
    }
    InternalWorkingMemory wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
    AlphaNode alphanode = (AlphaNode) node.getObjectSinkPropagator().getSinks()[0];
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) alphanode.getObjectSinkPropagator().getSinks()[0];
    // $p2
    JoinNode j = (JoinNode) liaNode.getSinkPropagator().getSinks()[0];
    TripleNonIndexSkipBetaConstraints c = (TripleNonIndexSkipBetaConstraints) j.getRawConstraints();
    // assertEquals( "$name", ((VariableConstraint)c.getConstraint()).getRequiredDeclarations()[0].getIdentifier() );
    assertTrue(c.isIndexed());
    BetaMemory bm = (BetaMemory) wm.getNodeMemory(j);
    assertTrue(bm.getLeftTupleMemory() instanceof TupleIndexHashTable);
    assertTrue(bm.getRightTupleMemory() instanceof TupleIndexHashTable);
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) TripleNonIndexSkipBetaConstraints(org.drools.core.common.TripleNonIndexSkipBetaConstraints) KieBase(org.kie.api.KieBase) JoinNode(org.drools.core.reteoo.JoinNode) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) BetaMemory(org.drools.core.reteoo.BetaMemory) TupleIndexHashTable(org.drools.core.util.index.TupleIndexHashTable) AlphaNode(org.drools.core.reteoo.AlphaNode) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Test(org.junit.Test)

Example 14 with JoinNode

use of org.drools.core.reteoo.JoinNode in project drools by kiegroup.

the class IndexingTest method testBuildsIndexedMemory.

@Test(timeout = 10000)
public void testBuildsIndexedMemory() {
    // tests indexes are correctly built
    String drl = "";
    drl += "package org.drools.compiler.test\n";
    drl += "import org.drools.compiler.Person\n";
    drl += "global java.util.List list\n";
    drl += "rule test1\n";
    drl += "when\n";
    drl += "   $p1  : Person($name : name )\n";
    // indexed
    drl += "   $p2 : Person(name == $name)\n";
    // indexed
    drl += "   $p3 : Person(name == $p1.name)\n";
    // not indexed
    drl += "   $p4 : Person(address.street == $p1.address.street)\n";
    // indexed
    drl += "   $p5 : Person(address.street == $p1.name)\n";
    // drl += "   $p6 : Person( $name == name)\n"; // not indexed and won't compile
    // indexed
    drl += "   $p7 : Person(addresses[\"key\"].street == $p1.name)\n";
    // indexed
    drl += "   $p8 : Person(addresses[0].street == $p1.name)\n";
    // not indexed
    drl += "   $p9 : Person(name == $p1.address.street)\n";
    // indexed
    drl += "   $p10 : Person(addresses[0].street + 'xx' == $p1.name)\n";
    // not indexed
    drl += "   $p11 : Person(addresses[$p1].street == $p1.name)\n";
    drl += "then\n";
    drl += "end\n";
    KieBase kbase = loadKnowledgeBaseFromString(drl);
    ObjectTypeNode node = getObjectTypeNode(kbase, Person.class);
    InternalWorkingMemory wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) node.getObjectSinkPropagator().getSinks()[0];
    // $p2
    JoinNode j2 = (JoinNode) liaNode.getSinkPropagator().getSinks()[0];
    // $p3
    JoinNode j3 = (JoinNode) j2.getSinkPropagator().getSinks()[0];
    // $p4
    JoinNode j4 = (JoinNode) j3.getSinkPropagator().getSinks()[0];
    // $p5
    JoinNode j5 = (JoinNode) j4.getSinkPropagator().getSinks()[0];
    // JoinNode j6 = ( JoinNode ) j5.getSinkPropagator().getSinks()[0];  // $p6 // won't compile
    // $p7
    JoinNode j7 = (JoinNode) j5.getSinkPropagator().getSinks()[0];
    // $p8
    JoinNode j8 = (JoinNode) j7.getSinkPropagator().getSinks()[0];
    // $p9
    JoinNode j9 = (JoinNode) j8.getSinkPropagator().getSinks()[0];
    // $p10
    JoinNode j10 = (JoinNode) j9.getSinkPropagator().getSinks()[0];
    // $p11
    JoinNode j11 = (JoinNode) j10.getSinkPropagator().getSinks()[0];
    SingleBetaConstraints c = (SingleBetaConstraints) j2.getRawConstraints();
    assertEquals("$name", ((IndexableConstraint) c.getConstraint()).getFieldIndex().getDeclaration().getIdentifier());
    assertTrue(c.isIndexed());
    BetaMemory bm = (BetaMemory) wm.getNodeMemory(j2);
    assertTrue(bm.getLeftTupleMemory() instanceof TupleIndexHashTable);
    assertTrue(bm.getRightTupleMemory() instanceof TupleIndexHashTable);
    c = (SingleBetaConstraints) j3.getRawConstraints();
    assertEquals("name", ((IndexableConstraint) c.getConstraint()).getFieldIndex().getDeclaration().getIdentifier());
    assertTrue(c.isIndexed());
    bm = (BetaMemory) wm.getNodeMemory(j3);
    assertTrue(bm.getLeftTupleMemory() instanceof TupleIndexHashTable);
    assertTrue(bm.getRightTupleMemory() instanceof TupleIndexHashTable);
    c = (SingleBetaConstraints) j4.getRawConstraints();
    assertEquals("$p1", c.getConstraint().getRequiredDeclarations()[0].getIdentifier());
    assertFalse(c.isIndexed());
    bm = (BetaMemory) wm.getNodeMemory(j4);
    assertTrue(bm.getLeftTupleMemory() instanceof TupleList);
    assertTrue(bm.getRightTupleMemory() instanceof TupleList);
    c = (SingleBetaConstraints) j5.getRawConstraints();
    assertEquals("name", ((IndexableConstraint) c.getConstraint()).getFieldIndex().getDeclaration().getIdentifier());
    assertTrue(c.isIndexed());
    bm = (BetaMemory) wm.getNodeMemory(j5);
    assertTrue(bm.getLeftTupleMemory() instanceof TupleIndexHashTable);
    assertTrue(bm.getRightTupleMemory() instanceof TupleIndexHashTable);
    // won't compile
    // c = ( SingleBetaConstraints ) j6.getRawConstraints();
    // assertEquals( "name", ((VariableConstraint)c.getConstraint()).getRequiredDeclarations()[0].getIdentifier() );
    // assertFalse( c.isIndexed() );
    // bm = ( BetaMemory ) wm.getNodeMemory( j6 );
    // assertTrue( bm.getLeftTupleMemory() instanceof LeftTupleList );
    // assertTrue( bm.getRightTupleMemory() instanceof RightTupleList );
    c = (SingleBetaConstraints) j7.getRawConstraints();
    assertEquals("name", ((IndexableConstraint) c.getConstraint()).getFieldIndex().getDeclaration().getIdentifier());
    assertTrue(c.isIndexed());
    bm = (BetaMemory) wm.getNodeMemory(j7);
    assertTrue(bm.getLeftTupleMemory() instanceof TupleIndexHashTable);
    assertTrue(bm.getRightTupleMemory() instanceof TupleIndexHashTable);
    c = (SingleBetaConstraints) j8.getRawConstraints();
    assertEquals("name", ((IndexableConstraint) c.getConstraint()).getFieldIndex().getDeclaration().getIdentifier());
    assertTrue(c.isIndexed());
    bm = (BetaMemory) wm.getNodeMemory(j8);
    assertTrue(bm.getLeftTupleMemory() instanceof TupleIndexHashTable);
    assertTrue(bm.getRightTupleMemory() instanceof TupleIndexHashTable);
    c = (SingleBetaConstraints) j9.getRawConstraints();
    assertEquals("$p1", c.getConstraint().getRequiredDeclarations()[0].getIdentifier());
    assertFalse(c.isIndexed());
    bm = (BetaMemory) wm.getNodeMemory(j9);
    assertTrue(bm.getLeftTupleMemory() instanceof TupleList);
    assertTrue(bm.getRightTupleMemory() instanceof TupleList);
    c = (SingleBetaConstraints) j10.getRawConstraints();
    assertEquals("name", ((IndexableConstraint) c.getConstraint()).getFieldIndex().getDeclaration().getIdentifier());
    assertTrue(c.isIndexed());
    bm = (BetaMemory) wm.getNodeMemory(j10);
    assertTrue(bm.getLeftTupleMemory() instanceof TupleIndexHashTable);
    assertTrue(bm.getRightTupleMemory() instanceof TupleIndexHashTable);
    c = (SingleBetaConstraints) j11.getRawConstraints();
    assertEquals("$p1", c.getConstraint().getRequiredDeclarations()[0].getIdentifier());
    assertFalse(c.isIndexed());
    bm = (BetaMemory) wm.getNodeMemory(j11);
    assertTrue(bm.getLeftTupleMemory() instanceof TupleList);
    assertTrue(bm.getRightTupleMemory() instanceof TupleList);
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) TupleList(org.drools.core.util.index.TupleList) SingleBetaConstraints(org.drools.core.common.SingleBetaConstraints) KieBase(org.kie.api.KieBase) JoinNode(org.drools.core.reteoo.JoinNode) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) BetaMemory(org.drools.core.reteoo.BetaMemory) TupleIndexHashTable(org.drools.core.util.index.TupleIndexHashTable) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Test(org.junit.Test)

Example 15 with JoinNode

use of org.drools.core.reteoo.JoinNode in project drools by kiegroup.

the class LinkingTest method testNotNodeDoesNotUnlinksWithConstriants.

@Test
public void testNotNodeDoesNotUnlinksWithConstriants() {
    String str = "";
    str += "package org.kie \n";
    str += "import " + A.class.getCanonicalName() + "\n";
    str += "import " + B.class.getCanonicalName() + "\n";
    str += "import " + C.class.getCanonicalName() + "\n";
    str += "import " + D.class.getCanonicalName() + "\n";
    str += "import " + E.class.getCanonicalName() + "\n";
    str += "import " + F.class.getCanonicalName() + "\n";
    str += "import " + G.class.getCanonicalName() + "\n";
    str += "global java.util.List list \n";
    str += "rule rule1 when \n";
    str += "   $a : A() \n";
    str += "   not B( value == $a.value ) \n";
    str += "   $c : C() \n";
    str += "then \n";
    str += "  list.add( 'x' ); \n";
    str += "end \n";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    ObjectTypeNode aotn = getObjectTypeNode(kbase, A.class);
    InternalWorkingMemory wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
    List list = new ArrayList();
    wm.setGlobal("list", list);
    LeftInputAdapterNode aNode = (LeftInputAdapterNode) aotn.getObjectSinkPropagator().getSinks()[0];
    NotNode bNode = (NotNode) aNode.getSinkPropagator().getSinks()[0];
    JoinNode cNode = (JoinNode) bNode.getSinkPropagator().getSinks()[0];
    SegmentUtilities.createSegmentMemory(cNode, wm);
    LiaNodeMemory amem = (LiaNodeMemory) wm.getNodeMemory(aNode);
    // Only NotNode is linked in
    assertEquals(2, amem.getSegmentMemory().getLinkedNodeMask());
    FactHandle fha = wm.insert(new A());
    FactHandle fhb = wm.insert(new B(1));
    FactHandle fhc = wm.insert(new C());
    wm.flushPropagations();
    // All nodes are linked in
    assertEquals(7, amem.getSegmentMemory().getLinkedNodeMask());
    // NotNode does not unlink, due to variable constraint
    wm.retract(fhb);
    wm.flushPropagations();
    assertEquals(7, amem.getSegmentMemory().getLinkedNodeMask());
}
Also used : NotNode(org.drools.core.reteoo.NotNode) FactHandle(org.kie.api.runtime.rule.FactHandle) JoinNode(org.drools.core.reteoo.JoinNode) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) ArrayList(java.util.ArrayList) LiaNodeMemory(org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ArrayList(java.util.ArrayList) List(java.util.List) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Test(org.junit.Test)

Aggregations

JoinNode (org.drools.core.reteoo.JoinNode)35 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)32 Test (org.junit.Test)32 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)30 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)29 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)20 BetaMemory (org.drools.core.reteoo.BetaMemory)19 SegmentMemory (org.drools.core.reteoo.SegmentMemory)18 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)16 LiaNodeMemory (org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory)15 ArrayList (java.util.ArrayList)12 List (java.util.List)12 PathMemory (org.drools.core.reteoo.PathMemory)12 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)12 ExistsNode (org.drools.core.reteoo.ExistsNode)10 NotNode (org.drools.core.reteoo.NotNode)10 KieBase (org.kie.api.KieBase)10 FactHandle (org.kie.api.runtime.rule.FactHandle)9 RightInputAdapterNode (org.drools.core.reteoo.RightInputAdapterNode)8 Match (org.kie.api.runtime.rule.Match)8