Search in sources :

Example 26 with NotNode

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

the class LeftTupleIterator method getNextLeftTuple.

public LeftTuple getNextLeftTuple(LeftTupleSource source, LeftTupleSink sink, LeftTuple leftTuple, InternalWorkingMemory wm) {
    if (otnIterator != null) {
        LeftTuple leftParent = leftTuple.getLeftParent();
        while (leftTuple != null) {
            leftTuple = leftTuple.getHandleNext();
            for (; leftTuple != null; leftTuple = leftTuple.getHandleNext()) {
                // Iterate to find the next left tuple for this sink, skip tuples for other sinks due to sharing split
                if (leftTuple.getTupleSink() == sink) {
                    return leftTuple;
                }
            }
        }
        // We have a parent LeftTuple so try there next
        if (leftParent != null) {
            // we know it has to be evalNode query element node
            while (leftParent != null) {
                leftParent = getNextLeftTuple(source.getLeftTupleSource(), (LeftTupleSink) source, leftParent, wm);
                if (leftParent != null) {
                    for (leftTuple = leftParent.getFirstChild(); leftTuple != null; leftTuple = leftTuple.getHandleNext()) {
                        if (leftTuple.getTupleSink() == sink) {
                            return leftTuple;
                        }
                    }
                }
            }
            return null;
        }
        // We have exhausted the current FactHandle, now try the next
        while (otnIterator.hasNext()) {
            InternalFactHandle handle = otnIterator.next();
            leftTuple = handle.findFirstLeftTuple(lt -> lt.getTupleSink() == sink);
            if (leftTuple != null) {
                return leftTuple;
            }
        }
        // We've exhausted this OTN so set the iterator to null
        otnIterator = null;
    } else if (source instanceof AccumulateNode) {
        // when using phreak, accumulate result tuples will not link to leftParent, but to parent instead
        BetaMemory memory = ((AccumulateMemory) wm.getNodeMemory((MemoryFactory) source)).getBetaMemory();
        FastIterator localIt = memory.getLeftTupleMemory().fullFastIterator(leftTuple.getParent());
        LeftTuple childLeftTuple = leftTuple;
        leftTuple = childLeftTuple.getParent();
        while (leftTuple != null) {
            if (childLeftTuple == null) {
                childLeftTuple = leftTuple.getFirstChild();
            } else {
                childLeftTuple = childLeftTuple.getHandleNext();
            }
            for (; childLeftTuple != null; childLeftTuple = childLeftTuple.getHandleNext()) {
                if (childLeftTuple.getTupleSink() == sink) {
                    return childLeftTuple;
                }
            }
            leftTuple = (LeftTuple) localIt.next(leftTuple);
        }
    } else if (source instanceof JoinNode || source instanceof NotNode || source instanceof FromNode || source instanceof AccumulateNode) {
        BetaMemory memory;
        FastIterator localIt;
        if (source instanceof FromNode) {
            memory = ((FromMemory) wm.getNodeMemory((MemoryFactory) source)).getBetaMemory();
        } else if (source instanceof AccumulateNode) {
            memory = ((AccumulateMemory) wm.getNodeMemory((MemoryFactory) source)).getBetaMemory();
        } else {
            memory = (BetaMemory) wm.getNodeMemory((MemoryFactory) source);
        }
        localIt = memory.getLeftTupleMemory().fullFastIterator(leftTuple.getLeftParent());
        LeftTuple childLeftTuple = leftTuple;
        leftTuple = childLeftTuple.getLeftParent();
        while (leftTuple != null) {
            if (childLeftTuple == null) {
                childLeftTuple = leftTuple.getFirstChild();
            } else {
                childLeftTuple = childLeftTuple.getHandleNext();
            }
            for (; childLeftTuple != null; childLeftTuple = childLeftTuple.getHandleNext()) {
                if (childLeftTuple.getTupleSink() == sink) {
                    return childLeftTuple;
                }
            }
            leftTuple = (LeftTuple) localIt.next(leftTuple);
        }
    }
    if (source instanceof ExistsNode) {
        BetaMemory memory = (BetaMemory) wm.getNodeMemory((MemoryFactory) source);
        if (leftTuple != null) {
            RightTuple rightTuple = leftTuple.getLeftParent().getBlocker();
            FastIterator localIt = memory.getRightTupleMemory().fullFastIterator(rightTuple);
            for (LeftTuple childleftTuple = leftTuple.getHandleNext(); childleftTuple != null; childleftTuple = childleftTuple.getHandleNext()) {
                if (childleftTuple.getTupleSink() == sink) {
                    return childleftTuple;
                }
            }
            leftTuple = leftTuple.getLeftParent();
            // now move onto next RightTuple
            while (rightTuple != null) {
                if (rightTuple.getBlocked() != null) {
                    if (leftTuple != null) {
                        leftTuple = leftTuple.getBlockedNext();
                    } else {
                        leftTuple = rightTuple.getBlocked();
                    }
                    for (; leftTuple != null; leftTuple = leftTuple.getBlockedNext()) {
                        for (LeftTuple childleftTuple = leftTuple.getFirstChild(); childleftTuple != null; childleftTuple = childleftTuple.getHandleNext()) {
                            if (childleftTuple.getTupleSink() == sink) {
                                return childleftTuple;
                            }
                        }
                    }
                }
                rightTuple = (RightTuple) localIt.next(rightTuple);
            }
        }
    } else if (source instanceof EvalConditionNode || source instanceof QueryElementNode) {
        LeftTuple childLeftTuple = leftTuple;
        if (leftTuple != null) {
            leftTuple = leftTuple.getLeftParent();
            while (leftTuple != null) {
                if (childLeftTuple != null) {
                    childLeftTuple = childLeftTuple.getHandleNext();
                } else {
                    childLeftTuple = leftTuple.getFirstChild();
                }
                for (; childLeftTuple != null; childLeftTuple = childLeftTuple.getHandleNext()) {
                    if (childLeftTuple.getTupleSink() == sink) {
                        return childLeftTuple;
                    }
                }
                leftTuple = getNextLeftTuple(source.getLeftTupleSource(), (LeftTupleSink) source, leftTuple, wm);
            }
        }
    }
    return null;
}
Also used : NotNode(org.drools.core.reteoo.NotNode) ExistsNode(org.drools.core.reteoo.ExistsNode) InternalFactHandle(org.drools.core.common.InternalFactHandle) EvalConditionNode(org.drools.core.reteoo.EvalConditionNode) MemoryFactory(org.drools.core.common.MemoryFactory) AccumulateNode(org.drools.core.reteoo.AccumulateNode) BetaMemory(org.drools.core.reteoo.BetaMemory) FromNode(org.drools.core.reteoo.FromNode) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) QueryElementNode(org.drools.core.reteoo.QueryElementNode) BetaNode(org.drools.core.reteoo.BetaNode) FromMemory(org.drools.core.reteoo.FromNode.FromMemory) LeftTupleSink(org.drools.core.reteoo.LeftTupleSink) LeftTupleSource(org.drools.core.reteoo.LeftTupleSource) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) FastIterator(org.drools.core.util.FastIterator) AccumulateContext(org.drools.core.reteoo.AccumulateNode.AccumulateContext) JoinNode(org.drools.core.reteoo.JoinNode) LeftTuple(org.drools.core.reteoo.LeftTuple) RightTuple(org.drools.core.reteoo.RightTuple) Tuple(org.drools.core.spi.Tuple) AccumulateMemory(org.drools.core.reteoo.AccumulateNode.AccumulateMemory) ObjectSource(org.drools.core.reteoo.ObjectSource) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) Iterator(org.drools.core.util.Iterator) AccumulateMemory(org.drools.core.reteoo.AccumulateNode.AccumulateMemory) NotNode(org.drools.core.reteoo.NotNode) EvalConditionNode(org.drools.core.reteoo.EvalConditionNode) AccumulateNode(org.drools.core.reteoo.AccumulateNode) JoinNode(org.drools.core.reteoo.JoinNode) QueryElementNode(org.drools.core.reteoo.QueryElementNode) LeftTupleSink(org.drools.core.reteoo.LeftTupleSink) BetaMemory(org.drools.core.reteoo.BetaMemory) FromNode(org.drools.core.reteoo.FromNode) MemoryFactory(org.drools.core.common.MemoryFactory) LeftTuple(org.drools.core.reteoo.LeftTuple) ExistsNode(org.drools.core.reteoo.ExistsNode) RightTuple(org.drools.core.reteoo.RightTuple) FastIterator(org.drools.core.util.FastIterator) InternalFactHandle(org.drools.core.common.InternalFactHandle)

Example 27 with NotNode

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

the class IndexingTest method testIndexingOnQueryUnificationWithNot.

@Test(timeout = 10000)
public void testIndexingOnQueryUnificationWithNot() {
    final String drl = "package org.drools.compiler.test  \n" + "import " + Person.class.getCanonicalName() + "\n" + "query peeps( String $name, int $age ) \n" + "    not $p2 : Person( $name := name, age > $age ) \n" + "end\n";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("indexing-test", kieBaseTestConfiguration, drl);
    final StatefulKnowledgeSessionImpl wm = (StatefulKnowledgeSessionImpl) kbase.newKieSession();
    ReteDumper.dumpRete(wm);
    try {
        final List<ObjectTypeNode> nodes = ((RuleBase) kbase).getRete().getObjectTypeNodes();
        ObjectTypeNode node = null;
        for (final ObjectTypeNode n : nodes) {
            if (((ClassObjectType) n.getObjectType()).getClassType() == DroolsQuery.class) {
                node = n;
                break;
            }
        }
        assertNotNull(node);
        final AlphaNode alphanode = (AlphaNode) node.getObjectSinkPropagator().getSinks()[0];
        final LeftInputAdapterNode liaNode = (LeftInputAdapterNode) alphanode.getObjectSinkPropagator().getSinks()[0];
        final NotNode n = (NotNode) liaNode.getSinkPropagator().getSinks()[0];
        final DoubleNonIndexSkipBetaConstraints c = (DoubleNonIndexSkipBetaConstraints) n.getRawConstraints();
        assertTrue(c.isIndexed());
        final BetaMemory bm = (BetaMemory) wm.getNodeMemory(n);
        assertTrue(bm.getLeftTupleMemory() instanceof TupleIndexHashTable);
        assertTrue(bm.getRightTupleMemory() instanceof TupleIndexHashTable);
        final Map<String, Integer> map = new HashMap<>();
        map.put("inserted", 0);
        map.put("deleted", 0);
        map.put("updated", 0);
        wm.openLiveQuery("peeps", new Object[] { Variable.v, 99 }, new ViewChangedEventListener() {

            @Override
            public void rowInserted(final Row row) {
                final Integer integer = map.get("inserted");
                map.put("inserted", integer + 1);
            }

            @Override
            public void rowDeleted(final Row row) {
                final Integer integer = map.get("deleted");
                map.put("deleted", integer + 1);
            }

            @Override
            public void rowUpdated(final Row row) {
                final Integer integer = map.get("updated");
                map.put("updated", integer + 1);
            }
        });
        final Map<String, InternalFactHandle> peeps = new HashMap<>();
        Person p;
        InternalFactHandle fh;
        final int max = 3;
        // 1 matched, prior to any insertions
        assertInsertedUpdatedDeleted(map, 1, 0, 0);
        // x0 is the blocker
        for (int i = 0; i < max; i++) {
            p = new Person("x" + i, 100);
            fh = (InternalFactHandle) wm.insert(p);
            wm.fireAllRules();
            peeps.put(p.getName(), fh);
        }
        // insertions case 1 deletion
        assertInsertedUpdatedDeleted(map, 1, 0, 1);
        // each x is blocker in turn up to x99
        for (int i = 0; i < (max - 1); i++) {
            fh = peeps.get("x" + i);
            p = (Person) fh.getObject();
            p.setAge(90);
            wm.update(fh, p);
            wm.fireAllRules();
            // make sure this doesn't change
            assertEquals("i=" + i, 1, map.get("inserted").intValue());
        }
        // no change
        assertInsertedUpdatedDeleted(map, 1, 0, 1);
        // x99 is still the blocker, everything else is just added
        for (int i = 0; i < (max - 1); i++) {
            fh = peeps.get("x" + i);
            p = (Person) fh.getObject();
            p.setAge(102);
            wm.update(fh, p);
            wm.fireAllRules();
            // make sure this doesn't change
            assertEquals("i=" + i, 1, map.get("inserted").intValue());
        }
        // no change
        assertInsertedUpdatedDeleted(map, 1, 0, 1);
        // x99 is still the blocker
        for (int i = (max - 2); i >= 0; i--) {
            fh = peeps.get("x" + i);
            p = (Person) fh.getObject();
            p.setAge(90);
            wm.update(fh, p);
            wm.fireAllRules();
            // make sure this doesn't change
            assertEquals("i=" + i, 1, map.get("inserted").intValue());
        }
        // move x99, should no longer be a blocker, now it can increase
        fh = peeps.get("x" + (max - 1));
        p = (Person) fh.getObject();
        p.setAge(90);
        wm.update(fh, p);
        wm.fireAllRules();
        assertEquals(2, map.get("inserted").intValue());
    } finally {
        wm.dispose();
    }
}
Also used : NotNode(org.drools.core.reteoo.NotNode) HashMap(java.util.HashMap) 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) DoubleNonIndexSkipBetaConstraints(org.drools.core.common.DoubleNonIndexSkipBetaConstraints) ViewChangedEventListener(org.kie.api.runtime.rule.ViewChangedEventListener) KieBase(org.kie.api.KieBase) StatefulKnowledgeSessionImpl(org.drools.kiesession.session.StatefulKnowledgeSessionImpl) Row(org.kie.api.runtime.rule.Row) InternalFactHandle(org.drools.core.common.InternalFactHandle) Person(org.drools.testcoverage.common.model.Person) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Test(org.junit.Test)

Example 28 with NotNode

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

the class SegmentCreationTest method tesSubnetworkAfterShare.

@Test
public void tesSubnetworkAfterShare() throws Exception {
    KieBase kbase = buildKnowledgeBase("  X() A() \n", "   X() A()  not ( B() and C() ) \n");
    InternalWorkingMemory wm = ((InternalWorkingMemory) kbase.newKieSession());
    ObjectTypeNode aotn = getObjectTypeNode(kbase, LinkingTest.A.class);
    ObjectTypeNode dotn = getObjectTypeNode(kbase, LinkingTest.X.class);
    LeftInputAdapterNode lian = (LeftInputAdapterNode) dotn.getObjectSinkPropagator().getSinks()[0];
    JoinNode joinNode = (JoinNode) aotn.getObjectSinkPropagator().getSinks()[0];
    RuleTerminalNode rtn1 = (RuleTerminalNode) joinNode.getSinkPropagator().getSinks()[0];
    JoinNode bNode = (JoinNode) joinNode.getSinkPropagator().getSinks()[1];
    JoinNode cNode = (JoinNode) bNode.getSinkPropagator().getSinks()[0];
    RightInputAdapterNode riaNode = (RightInputAdapterNode) cNode.getSinkPropagator().getSinks()[0];
    NotNode notNode = (NotNode) joinNode.getSinkPropagator().getSinks()[2];
    RuleTerminalNode rtn2 = (RuleTerminalNode) notNode.getSinkPropagator().getSinks()[0];
    wm.insert(new LinkingTest.X());
    wm.insert(new LinkingTest.A());
    wm.insert(new LinkingTest.B());
    wm.insert(new LinkingTest.C());
    wm.fireAllRules();
    BetaMemory liaMem = (BetaMemory) wm.getNodeMemory(joinNode);
    SegmentMemory smem = liaMem.getSegmentMemory();
    assertEquals(lian, smem.getRootNode());
    assertEquals(joinNode, smem.getTipNode());
    SegmentMemory rtnSmem1 = smem.getFirst();
    assertEquals(rtn1, rtnSmem1.getRootNode());
    assertEquals(rtn1, rtnSmem1.getTipNode());
    SegmentMemory bSmem = rtnSmem1.getNext();
    assertEquals(bNode, bSmem.getRootNode());
    assertEquals(riaNode, bSmem.getTipNode());
    SegmentMemory notSmem = bSmem.getNext();
    assertEquals(notNode, notSmem.getRootNode());
    assertEquals(rtn2, notSmem.getTipNode());
    // child segment is not yet initialised, so null
    assertNull(bSmem.getFirst());
}
Also used : NotNode(org.drools.core.reteoo.NotNode) SegmentMemory(org.drools.core.reteoo.SegmentMemory) JoinNode(org.drools.core.reteoo.JoinNode) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) BetaMemory(org.drools.core.reteoo.BetaMemory) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) KieBase(org.kie.api.KieBase) RightInputAdapterNode(org.drools.core.reteoo.RightInputAdapterNode) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Test(org.junit.Test)

Example 29 with NotNode

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

the class SegmentCreationTest method tesShareInSubnetwork.

@Test
public void tesShareInSubnetwork() throws Exception {
    KieBase kbase = buildKnowledgeBase("  X() A() \n", "   X() A() B() C() \n", "   X() A()  not ( B() and C() ) \n");
    InternalWorkingMemory wm = ((InternalWorkingMemory) kbase.newKieSession());
    ObjectTypeNode aotn = getObjectTypeNode(kbase, LinkingTest.A.class);
    ObjectTypeNode dotn = getObjectTypeNode(kbase, LinkingTest.X.class);
    LeftInputAdapterNode lian = (LeftInputAdapterNode) dotn.getObjectSinkPropagator().getSinks()[0];
    JoinNode beta = (JoinNode) aotn.getObjectSinkPropagator().getSinks()[0];
    RuleTerminalNode rtn1 = (RuleTerminalNode) beta.getSinkPropagator().getSinks()[0];
    JoinNode bNode = (JoinNode) beta.getSinkPropagator().getSinks()[1];
    JoinNode cNode = (JoinNode) bNode.getSinkPropagator().getSinks()[0];
    RuleTerminalNode rtn2 = (RuleTerminalNode) cNode.getSinkPropagator().getSinks()[0];
    RightInputAdapterNode riaNode = (RightInputAdapterNode) cNode.getSinkPropagator().getSinks()[1];
    NotNode notNode = (NotNode) beta.getSinkPropagator().getSinks()[2];
    RuleTerminalNode rtn3 = (RuleTerminalNode) notNode.getSinkPropagator().getSinks()[0];
    wm.insert(new LinkingTest.X());
    wm.insert(new LinkingTest.A());
    wm.insert(new LinkingTest.B());
    wm.insert(new LinkingTest.C());
    wm.flushPropagations();
    // LiaNode  is in it's own segment
    LiaNodeMemory liaMem = (LiaNodeMemory) wm.getNodeMemory(lian);
    SegmentMemory smem = liaMem.getSegmentMemory();
    assertEquals(lian, smem.getRootNode());
    assertEquals(beta, smem.getTipNode());
    // segment is not initialized yet
    assertNull(smem.getFirst());
    wm.fireAllRules();
    SegmentMemory rtnSmem1 = smem.getFirst();
    assertEquals(rtn1, rtnSmem1.getRootNode());
    assertEquals(rtn1, rtnSmem1.getTipNode());
    SegmentMemory bSmem = rtnSmem1.getNext();
    assertEquals(bNode, bSmem.getRootNode());
    assertEquals(cNode, bSmem.getTipNode());
    SegmentMemory rtn2Smem = bSmem.getFirst();
    assertEquals(rtn2, rtn2Smem.getRootNode());
    assertEquals(rtn2, rtn2Smem.getTipNode());
    SegmentMemory riaSmem = rtn2Smem.getNext();
    assertEquals(riaNode, riaSmem.getRootNode());
    assertEquals(riaNode, riaSmem.getTipNode());
    SegmentMemory notSmem = bSmem.getNext();
    assertEquals(notNode, notSmem.getRootNode());
    assertEquals(rtn3, notSmem.getTipNode());
}
Also used : NotNode(org.drools.core.reteoo.NotNode) SegmentMemory(org.drools.core.reteoo.SegmentMemory) JoinNode(org.drools.core.reteoo.JoinNode) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) LiaNodeMemory(org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) KieBase(org.kie.api.KieBase) RightInputAdapterNode(org.drools.core.reteoo.RightInputAdapterNode) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Test(org.junit.Test)

Aggregations

NotNode (org.drools.core.reteoo.NotNode)29 JoinNode (org.drools.core.reteoo.JoinNode)24 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)24 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)23 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)17 Test (org.junit.Test)17 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)13 KieBase (org.kie.api.KieBase)13 SegmentMemory (org.drools.core.reteoo.SegmentMemory)12 BetaMemory (org.drools.core.reteoo.BetaMemory)10 ExistsNode (org.drools.core.reteoo.ExistsNode)10 LiaNodeMemory (org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory)9 RightInputAdapterNode (org.drools.core.reteoo.RightInputAdapterNode)8 BetaNode (org.drools.core.reteoo.BetaNode)7 ArrayList (java.util.ArrayList)6 AccumulateNode (org.drools.core.reteoo.AccumulateNode)6 LeftTuple (org.drools.core.reteoo.LeftTuple)6 LeftTupleSink (org.drools.core.reteoo.LeftTupleSink)6 LeftTupleSource (org.drools.core.reteoo.LeftTupleSource)6 ObjectSource (org.drools.core.reteoo.ObjectSource)6