Search in sources :

Example 41 with ObjectTypeNode

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

the class PropertySpecificTest method testComplexBetaSharedAlphaWithWatchesRemoveR1.

@Test
public void testComplexBetaSharedAlphaWithWatchesRemoveR1() {
    String rule1 = "$b : B( b == 15) @watch(i) A( a == 10, b == 15 ) @watch(c)";
    String rule2 = "$b : B( b == 15) @watch(j) A( a == 10, i == 20 ) @watch(s)";
    String rule3 = "$b : B( c == 15) @watch(k) A( a == 10, i == 20, b == 10 ) @watch(j)";
    KieBase kbase = getKnowledgeBase(rule1, rule2, rule3);
    InternalWorkingMemory wm = ((InternalWorkingMemory) kbase.newKieSession());
    kbase.removeRule("org.drools.compiler.integrationtests", "r0");
    ObjectTypeNode otn = getObjectTypeNode(kbase, "A");
    assertNotNull(otn);
    Class classType = ((ClassObjectType) otn.getObjectType()).getClassType();
    List<String> sp = getSettableProperties(wm, otn);
    AlphaNode alphaNode1 = (AlphaNode) otn.getObjectSinkPropagator().getSinks()[0];
    assertEquals(calculatePositiveMask(classType, list("a"), sp), alphaNode1.getDeclaredMask());
    assertEquals(calculatePositiveMask(classType, list("a", "i", "b", "s", "j"), sp), alphaNode1.getInferredMask());
    // first share
    AlphaNode alphaNode1_1 = (AlphaNode) alphaNode1.getObjectSinkPropagator().getSinks()[0];
    assertEquals(calculatePositiveMask(classType, list("i"), sp), alphaNode1_1.getDeclaredMask());
    assertEquals(calculatePositiveMask(classType, list("a", "i", "b", "s", "j"), sp), alphaNode1_1.getInferredMask());
    BetaNode betaNode1 = (BetaNode) alphaNode1_1.getObjectSinkPropagator().getSinks()[1];
    assertEquals(calculatePositiveMask(classType, list("s"), sp), betaNode1.getRightDeclaredMask());
    assertEquals(calculatePositiveMask(classType, list("a", "i", "s"), sp), betaNode1.getRightInferredMask());
    assertEquals(calculatePositiveMask(classType, list("j"), sp), betaNode1.getLeftDeclaredMask());
    assertEquals(calculatePositiveMask(classType, list("b", "j"), sp), betaNode1.getLeftInferredMask());
    // second split, third alpha
    AlphaNode alphaNode1_2 = (AlphaNode) alphaNode1_1.getObjectSinkPropagator().getSinks()[0];
    assertEquals(calculatePositiveMask(classType, list("b"), sp), alphaNode1_2.getDeclaredMask());
    assertEquals(calculatePositiveMask(classType, list("a", "b", "i", "j"), sp), alphaNode1_2.getInferredMask());
    BetaNode betaNode3 = (BetaNode) alphaNode1_2.getObjectSinkPropagator().getSinks()[0];
    assertEquals(calculatePositiveMask(classType, list("j"), sp), betaNode3.getRightDeclaredMask());
    assertEquals(calculatePositiveMask(classType, list("a", "i", "b", "j"), sp), betaNode3.getRightInferredMask());
    assertEquals(calculatePositiveMask(classType, list("k"), sp), betaNode3.getLeftDeclaredMask());
    assertEquals(calculatePositiveMask(classType, list("c", "k"), sp), betaNode3.getLeftInferredMask());
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) BetaNode(org.drools.core.reteoo.BetaNode) ClassObjectType(org.drools.core.base.ClassObjectType) KieBase(org.kie.api.KieBase) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) AlphaNode(org.drools.core.reteoo.AlphaNode) Test(org.junit.Test)

Example 42 with ObjectTypeNode

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

the class PropertySpecificTest method test2DifferentAlphaWatchBeforeSameBeta.

@Test
public void test2DifferentAlphaWatchBeforeSameBeta() {
    String rule1 = "B(a == 15) @watch(b) C()";
    String rule2 = "B(a == 15) @watch(c) C()";
    KieBase kbase = getKnowledgeBase(rule1, rule2);
    InternalWorkingMemory wm = ((InternalWorkingMemory) kbase.newKieSession());
    ObjectTypeNode otnB = getObjectTypeNode(kbase, "B");
    List<String> sp = getSettableProperties(wm, otnB);
    Class classType = ((ClassObjectType) otnB.getObjectType()).getClassType();
    AlphaNode alphaNode = (AlphaNode) otnB.getObjectSinkPropagator().getSinks()[0];
    assertEquals(calculatePositiveMask(classType, list("a"), sp), alphaNode.getDeclaredMask());
    assertEquals(calculatePositiveMask(classType, list("a", "b", "c"), sp), alphaNode.getInferredMask());
    ObjectTypeNode otnC = getObjectTypeNode(kbase, "C");
    BetaNode betaNodeC1 = (BetaNode) otnC.getObjectSinkPropagator().getSinks()[0];
    BetaNode betaNodeC2 = (BetaNode) otnC.getObjectSinkPropagator().getSinks()[1];
    LeftInputAdapterNode lia1 = (LeftInputAdapterNode) alphaNode.getObjectSinkPropagator().getSinks()[0];
    assertSame(betaNodeC1, lia1.getSinkPropagator().getSinks()[0]);
    LeftInputAdapterNode lia2 = (LeftInputAdapterNode) alphaNode.getObjectSinkPropagator().getSinks()[1];
    assertSame(betaNodeC2, lia2.getSinkPropagator().getSinks()[0]);
    assertEquals(EmptyBitMask.get(), betaNodeC1.getRightDeclaredMask());
    assertEquals(EmptyBitMask.get(), betaNodeC1.getRightInferredMask());
    assertEquals(calculatePositiveMask(classType, list("b"), sp), betaNodeC1.getLeftDeclaredMask());
    assertEquals(calculatePositiveMask(classType, list("a", "b"), sp), betaNodeC1.getLeftInferredMask());
    assertEquals(EmptyBitMask.get(), betaNodeC2.getRightDeclaredMask());
    assertEquals(EmptyBitMask.get(), betaNodeC2.getRightInferredMask());
    assertEquals(calculatePositiveMask(classType, list("c"), sp), betaNodeC2.getLeftDeclaredMask());
    assertEquals(calculatePositiveMask(classType, list("a", "c"), sp), betaNodeC2.getLeftInferredMask());
    kbase.removeRule("org.drools.compiler.integrationtests", "r0");
    assertEquals(calculatePositiveMask(classType, list("a"), sp), alphaNode.getDeclaredMask());
    assertEquals(calculatePositiveMask(classType, list("a", "c"), sp), alphaNode.getInferredMask());
    assertEquals(1, lia2.getSinkPropagator().getSinks().length);
    BetaNode betaNodeC = (BetaNode) lia2.getSinkPropagator().getSinks()[0];
    assertEquals(EmptyBitMask.get(), betaNodeC2.getRightDeclaredMask());
    assertEquals(EmptyBitMask.get(), betaNodeC2.getRightInferredMask());
    assertEquals(calculatePositiveMask(classType, list("c"), sp), betaNodeC2.getLeftDeclaredMask());
    assertEquals(calculatePositiveMask(classType, list("a", "c"), sp), betaNodeC2.getLeftInferredMask());
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) BetaNode(org.drools.core.reteoo.BetaNode) ClassObjectType(org.drools.core.base.ClassObjectType) KieBase(org.kie.api.KieBase) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) AlphaNode(org.drools.core.reteoo.AlphaNode) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Test(org.junit.Test)

Example 43 with ObjectTypeNode

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

the class IndexingTest method testFullFastIteratorResume.

@Test(timeout = 10000)
public void testFullFastIteratorResume() throws Exception {
    String str = "";
    str += "package org.drools.compiler.test  \n";
    str += "import org.drools.compiler.Person \n";
    str += "query peeps( String $name, int $age ) \n";
    str += "    not $p2 : Person( $name := name, age > $age ) \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;
        }
    }
    StatefulKnowledgeSessionImpl wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
    AlphaNode alphanode = (AlphaNode) node.getObjectSinkPropagator().getSinks()[0];
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) alphanode.getObjectSinkPropagator().getSinks()[0];
    NotNode n = (NotNode) liaNode.getSinkPropagator().getSinks()[0];
    DoubleNonIndexSkipBetaConstraints c = (DoubleNonIndexSkipBetaConstraints) n.getRawConstraints();
    // assertEquals( "$name", ((VariableConstraint)c.getConstraint()).getRequiredDeclarations()[0].getIdentifier() );
    assertTrue(c.isIndexed());
    BetaMemory bm = (BetaMemory) wm.getNodeMemory(n);
    System.out.println(bm.getLeftTupleMemory().getClass());
    System.out.println(bm.getRightTupleMemory().getClass());
    assertTrue(bm.getLeftTupleMemory() instanceof TupleIndexHashTable);
    assertTrue(bm.getRightTupleMemory() instanceof TupleIndexHashTable);
    final Map<String, Integer> map = new HashMap<String, Integer>();
    map.put("inserted", new Integer(0));
    map.put("deleted", new Integer(0));
    map.put("updated", new Integer(0));
    wm.openLiveQuery("peeps", new Object[] { Variable.v, 99 }, new ViewChangedEventListener() {

        @Override
        public void rowInserted(Row row) {
        }

        @Override
        public void rowDeleted(Row row) {
        }

        @Override
        public void rowUpdated(Row row) {
        }
    });
    Map<String, InternalFactHandle> peeps = new HashMap<String, InternalFactHandle>();
    Person p = new Person("x0", 100);
    InternalFactHandle fh = (InternalFactHandle) wm.insert(p);
    peeps.put(p.getName(), fh);
    for (int i = 1; i < 100; i++) {
        p = new Person("x" + i, 101);
        fh = (InternalFactHandle) wm.insert(p);
        wm.fireAllRules();
        peeps.put(p.getName(), fh);
    }
    List<RightTuple> list = new ArrayList<RightTuple>(100);
    FastIterator it = n.getRightIterator(bm.getRightTupleMemory());
    for (RightTuple rt = n.getFirstRightTuple(null, bm.getRightTupleMemory(), null, it); rt != null; rt = (RightTuple) it.next(rt)) {
        list.add(rt);
    }
    assertEquals(100, list.size());
    // check we can resume from each entry in the list above.
    for (int i = 0; i < 100; i++) {
        RightTuple rightTuple = list.get(i);
        // resumes from the current rightTuple
        it = n.getRightIterator(bm.getRightTupleMemory(), rightTuple);
        int j = i + 1;
        for (RightTuple rt = (RightTuple) it.next(rightTuple); rt != null; rt = (RightTuple) it.next(rt)) {
            assertSame(list.get(j), rt);
            j++;
        }
    }
}
Also used : NotNode(org.drools.core.reteoo.NotNode) HashMap(java.util.HashMap) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) ArrayList(java.util.ArrayList) DoubleNonIndexSkipBetaConstraints(org.drools.core.common.DoubleNonIndexSkipBetaConstraints) KieBase(org.kie.api.KieBase) FastIterator(org.drools.core.util.FastIterator) InternalFactHandle(org.drools.core.common.InternalFactHandle) BetaMemory(org.drools.core.reteoo.BetaMemory) TupleIndexHashTable(org.drools.core.util.index.TupleIndexHashTable) AlphaNode(org.drools.core.reteoo.AlphaNode) RightTuple(org.drools.core.reteoo.RightTuple) IndexableConstraint(org.drools.core.rule.IndexableConstraint) ViewChangedEventListener(org.kie.api.runtime.rule.ViewChangedEventListener) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) Row(org.kie.api.runtime.rule.Row) Person(org.drools.compiler.Person) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Test(org.junit.Test)

Example 44 with ObjectTypeNode

use of org.drools.core.reteoo.ObjectTypeNode 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 45 with ObjectTypeNode

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

the class IndexingTest method testBuildsIndexedAlphaNodes.

@Test(timeout = 10000)
public void testBuildsIndexedAlphaNodes() {
    String drl = "";
    drl += "package org.drools.compiler.test\n";
    drl += "import org.drools.compiler.Person\n";
    drl += "rule test1\n";
    drl += "when\n";
    drl += "   Person(name == \"Mark\", age == 37)\n";
    drl += "   Person(name == \"Mark\", happy == true)\n";
    drl += "then\n";
    drl += "end\n";
    KieBase kbase = loadKnowledgeBaseFromString(drl);
    ObjectTypeNode otn = getObjectTypeNode(kbase, Person.class);
    InternalWorkingMemory wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
    AlphaNode alphaNode1 = (AlphaNode) otn.getObjectSinkPropagator().getSinks()[0];
    CompositeObjectSinkAdapter sinkAdapter = (CompositeObjectSinkAdapter) alphaNode1.getObjectSinkPropagator();
    ObjectSinkNodeList hashableSinks = sinkAdapter.getHashableSinks();
    assertNotNull(hashableSinks);
    assertEquals(2, hashableSinks.size());
    AlphaNode alphaNode2 = (AlphaNode) alphaNode1.getObjectSinkPropagator().getSinks()[0];
    assertSame(hashableSinks.getFirst(), alphaNode2);
    AlphaNode alphaNode3 = (AlphaNode) alphaNode1.getObjectSinkPropagator().getSinks()[1];
    assertSame(hashableSinks.getLast(), alphaNode3);
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) KieBase(org.kie.api.KieBase) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) CompositeObjectSinkAdapter(org.drools.core.reteoo.CompositeObjectSinkAdapter) AlphaNode(org.drools.core.reteoo.AlphaNode) ObjectSinkNodeList(org.drools.core.reteoo.ObjectSinkNodeList) Test(org.junit.Test)

Aggregations

ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)116 Test (org.junit.Test)100 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)69 KieBase (org.kie.api.KieBase)64 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)60 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)40 ClassObjectType (org.drools.core.base.ClassObjectType)34 JoinNode (org.drools.core.reteoo.JoinNode)31 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)30 AlphaNode (org.drools.core.reteoo.AlphaNode)29 ArrayList (java.util.ArrayList)28 BetaNode (org.drools.core.reteoo.BetaNode)27 BetaMemory (org.drools.core.reteoo.BetaMemory)24 List (java.util.List)23 SegmentMemory (org.drools.core.reteoo.SegmentMemory)23 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)23 LiaNodeMemory (org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory)22 KieSession (org.kie.api.runtime.KieSession)20 Person (org.drools.compiler.Person)17 EntryPointNode (org.drools.core.reteoo.EntryPointNode)16