Search in sources :

Example 61 with LeftInputAdapterNode

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

the class PropertySpecificTest method testRtnWithConstraintsNoWatches.

@Test
public void testRtnWithConstraintsNoWatches() {
    String rule1 = "A( a == 10 )";
    KieBase kbase = getKnowledgeBase(rule1);
    InternalWorkingMemory wm = ((InternalWorkingMemory) kbase.newKieSession());
    ObjectTypeNode otn = getObjectTypeNode(kbase, "A");
    assertNotNull(otn);
    Class classType = ((ClassObjectType) otn.getObjectType()).getClassType();
    List<String> sp = getSettableProperties(wm, otn);
    AlphaNode alphaNode = (AlphaNode) otn.getObjectSinkPropagator().getSinks()[0];
    assertEquals(calculatePositiveMask(classType, list("a"), sp), alphaNode.getDeclaredMask());
    assertEquals(calculatePositiveMask(classType, list("a"), sp), alphaNode.getInferredMask());
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) alphaNode.getObjectSinkPropagator().getSinks()[0];
    RuleTerminalNode rtNode = (RuleTerminalNode) liaNode.getSinkPropagator().getSinks()[0];
    // rtn declares nothing
    assertEquals(EmptyBitMask.get(), rtNode.getDeclaredMask());
    // rtn infers from alpha
    assertEquals(calculatePositiveMask(classType, list("a"), sp), rtNode.getInferredMask());
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) 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) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Test(org.junit.Test)

Example 62 with LeftInputAdapterNode

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

the class PropertySpecificTest method testRTNodeNoConstraintsNoPropertySpecific.

@Test
public void testRTNodeNoConstraintsNoPropertySpecific() {
    String rule = "package org.drools.compiler.integrationtests\n" + "import " + Person.class.getCanonicalName() + "\n" + "rule r1\n" + "when\n" + "   Person()\n" + "then\n" + "end\n";
    KieBase kbase = new KieHelper(PropertySpecificOption.ALLOWED).addContent(rule, ResourceType.DRL).build();
    ObjectTypeNode otn = getObjectTypeNode(kbase, "Person");
    assertNotNull(otn);
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) otn.getObjectSinkPropagator().getSinks()[0];
    RuleTerminalNode rtNode = (RuleTerminalNode) liaNode.getSinkPropagator().getSinks()[0];
    assertEquals(AllSetBitMask.get(), rtNode.getDeclaredMask());
    assertEquals(AllSetBitMask.get(), rtNode.getInferredMask());
}
Also used : KieBase(org.kie.api.KieBase) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) KieHelper(org.kie.internal.utils.KieHelper) Person(org.drools.compiler.Person) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Test(org.junit.Test)

Example 63 with LeftInputAdapterNode

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

the class PropertySpecificTest method testRtnNoConstraintsNoWatches.

@Test
public void testRtnNoConstraintsNoWatches() {
    String rule1 = "A()";
    KieBase kbase = getKnowledgeBase(rule1);
    InternalWorkingMemory wm = ((InternalWorkingMemory) kbase.newKieSession());
    ObjectTypeNode otn = getObjectTypeNode(kbase, "A");
    assertNotNull(otn);
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) otn.getObjectSinkPropagator().getSinks()[0];
    RuleTerminalNode rtNode = (RuleTerminalNode) liaNode.getSinkPropagator().getSinks()[0];
    assertEquals(EmptyBitMask.get(), rtNode.getDeclaredMask());
    assertEquals(EmptyBitMask.get(), rtNode.getInferredMask());
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) KieBase(org.kie.api.KieBase) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Test(org.junit.Test)

Example 64 with LeftInputAdapterNode

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

the class MemoryLeakTest method testStagedLeftTupleLeak.

@Test
public void testStagedLeftTupleLeak() throws Exception {
    // BZ-1058874
    String str = "rule R1 when\n" + "    String( this == \"this\" )\n" + "    String( this == \"that\" )\n" + "then\n" + "end\n";
    KieBase kbase = new KieHelper().addContent(str, ResourceType.DRL).build();
    KieSession ksession = kbase.newKieSession();
    ksession.fireAllRules();
    for (int i = 0; i < 10; i++) {
        FactHandle fh = ksession.insert("this");
        ksession.fireAllRules();
        ksession.delete(fh);
        ksession.fireAllRules();
    }
    Rete rete = ((KnowledgeBaseImpl) kbase).getRete();
    LeftInputAdapterNode liaNode = null;
    for (ObjectTypeNode otn : rete.getObjectTypeNodes()) {
        if (String.class == otn.getObjectType().getValueType().getClassType()) {
            AlphaNode alphaNode = (AlphaNode) otn.getObjectSinkPropagator().getSinks()[0];
            liaNode = (LeftInputAdapterNode) alphaNode.getObjectSinkPropagator().getSinks()[0];
            break;
        }
    }
    assertNotNull(liaNode);
    InternalWorkingMemory wm = (InternalWorkingMemory) ksession;
    LeftInputAdapterNode.LiaNodeMemory memory = (LeftInputAdapterNode.LiaNodeMemory) wm.getNodeMemory(liaNode);
    TupleSets<LeftTuple> stagedLeftTuples = memory.getSegmentMemory().getStagedLeftTuples();
    assertNull(stagedLeftTuples.getDeleteFirst());
    assertNull(stagedLeftTuples.getInsertFirst());
}
Also used : Rete(org.drools.core.reteoo.Rete) FactHandle(org.kie.api.runtime.rule.FactHandle) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) KieHelper(org.kie.internal.utils.KieHelper) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) AlphaNode(org.drools.core.reteoo.AlphaNode) LeftTuple(org.drools.core.reteoo.LeftTuple) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Test(org.junit.Test)

Example 65 with LeftInputAdapterNode

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

the class LeftTupleIterator method getFirstLeftTuple.

public LeftTuple getFirstLeftTuple(LeftTupleSource source, LeftTupleSink sink, InternalWorkingMemory wm) {
    if (source instanceof AccumulateNode) {
        AccumulateMemory accmem = (AccumulateMemory) wm.getNodeMemory((MemoryFactory) source);
        BetaMemory memory = accmem.getBetaMemory();
        FastIterator localIt = memory.getLeftTupleMemory().fullFastIterator();
        Tuple leftTuple = BetaNode.getFirstTuple(memory.getLeftTupleMemory(), localIt);
        if (leftTuple != null) {
            AccumulateContext accctx = (AccumulateContext) leftTuple.getContextObject();
            return accctx.getResultLeftTuple();
        }
        return null;
    }
    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();
        Tuple leftTuple = BetaNode.getFirstTuple(memory.getLeftTupleMemory(), localIt);
        while (leftTuple != null) {
            for (LeftTuple childleftTuple = leftTuple.getFirstChild(); 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);
        FastIterator localIt = memory.getRightTupleMemory().fullFastIterator();
        RightTuple rightTuple = (RightTuple) BetaNode.getFirstTuple(memory.getRightTupleMemory(), localIt);
        while (rightTuple != null) {
            if (rightTuple.getBlocked() != null) {
                for (LeftTuple leftTuple = rightTuple.getBlocked(); 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 LeftInputAdapterNode) {
        ObjectSource os = ((LeftInputAdapterNode) source).getParentObjectSource();
        while (!(os instanceof ObjectTypeNode)) {
            os = os.getParentObjectSource();
        }
        ObjectTypeNode otn = (ObjectTypeNode) os;
        otnIterator = wm.getNodeMemory(otn).iterator();
        while (otnIterator.hasNext()) {
            InternalFactHandle handle = otnIterator.next();
            LeftTuple leftTuple = handle.findFirstLeftTuple(lt -> lt.getTupleSink() == sink);
            if (leftTuple != null) {
                return leftTuple;
            }
        }
    } else if (source instanceof EvalConditionNode || source instanceof QueryElementNode) {
        LeftTuple parentLeftTuple = getFirstLeftTuple(source.getLeftTupleSource(), (LeftTupleSink) source, wm);
        while (parentLeftTuple != null) {
            for (LeftTuple leftTuple = parentLeftTuple.getFirstChild(); leftTuple != null; leftTuple = leftTuple.getHandleNext()) {
                if (leftTuple.getTupleSink() == sink) {
                    return leftTuple;
                }
            }
            parentLeftTuple = getNextLeftTuple(source.getLeftTupleSource(), (LeftTupleSink) source, parentLeftTuple, wm);
        }
    }
    return null;
}
Also used : NotNode(org.drools.core.reteoo.NotNode) ExistsNode(org.drools.core.reteoo.ExistsNode) EvalConditionNode(org.drools.core.reteoo.EvalConditionNode) FastIterator(org.drools.core.util.FastIterator) AccumulateNode(org.drools.core.reteoo.AccumulateNode) AccumulateContext(org.drools.core.reteoo.AccumulateNode.AccumulateContext) BetaMemory(org.drools.core.reteoo.BetaMemory) JoinNode(org.drools.core.reteoo.JoinNode) FromNode(org.drools.core.reteoo.FromNode) LeftTuple(org.drools.core.reteoo.LeftTuple) RightTuple(org.drools.core.reteoo.RightTuple) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) QueryElementNode(org.drools.core.reteoo.QueryElementNode) Tuple(org.drools.core.spi.Tuple) BetaNode(org.drools.core.reteoo.BetaNode) AccumulateMemory(org.drools.core.reteoo.AccumulateNode.AccumulateMemory) FromMemory(org.drools.core.reteoo.FromNode.FromMemory) ObjectSource(org.drools.core.reteoo.ObjectSource) LeftTupleSink(org.drools.core.reteoo.LeftTupleSink) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) Iterator(org.drools.core.util.Iterator) LeftTupleSource(org.drools.core.reteoo.LeftTupleSource) 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) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) LeftTupleSink(org.drools.core.reteoo.LeftTupleSink) BetaMemory(org.drools.core.reteoo.BetaMemory) FromNode(org.drools.core.reteoo.FromNode) LeftTuple(org.drools.core.reteoo.LeftTuple) ExistsNode(org.drools.core.reteoo.ExistsNode) RightTuple(org.drools.core.reteoo.RightTuple) ObjectSource(org.drools.core.reteoo.ObjectSource) FastIterator(org.drools.core.util.FastIterator) LeftTuple(org.drools.core.reteoo.LeftTuple) RightTuple(org.drools.core.reteoo.RightTuple) Tuple(org.drools.core.spi.Tuple) AccumulateContext(org.drools.core.reteoo.AccumulateNode.AccumulateContext) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode)

Aggregations

LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)72 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)60 Test (org.junit.Test)56 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)48 KieBase (org.kie.api.KieBase)35 JoinNode (org.drools.core.reteoo.JoinNode)28 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)28 SegmentMemory (org.drools.core.reteoo.SegmentMemory)28 LiaNodeMemory (org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory)27 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)25 BetaMemory (org.drools.core.reteoo.BetaMemory)25 ArrayList (java.util.ArrayList)17 PathMemory (org.drools.core.reteoo.PathMemory)15 List (java.util.List)12 FactHandle (org.kie.api.runtime.rule.FactHandle)12 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)12 ClassObjectType (org.drools.core.base.ClassObjectType)11 AlphaNode (org.drools.core.reteoo.AlphaNode)11 NotNode (org.drools.core.reteoo.NotNode)11 ExistsNode (org.drools.core.reteoo.ExistsNode)10