Search in sources :

Example 41 with BetaMemory

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

the class PhreakAccumulateNode method doNode.

public void doNode(AccumulateNode accNode, LeftTupleSink sink, AccumulateMemory am, InternalWorkingMemory wm, TupleSets<LeftTuple> srcLeftTuples, TupleSets<LeftTuple> trgLeftTuples, TupleSets<LeftTuple> stagedLeftTuples) {
    BetaMemory bm = am.getBetaMemory();
    TupleSets<RightTuple> srcRightTuples = bm.getStagedRightTuples().takeAll();
    // order of left and right operations is to minimise wasted of innefficient joins.
    // We need to collect which leftTuple where updated, so that we can
    // add their result tuple to the real target tuples later
    TupleSets<LeftTuple> tempLeftTuples = new TupleSetsImpl<LeftTuple>();
    if (srcLeftTuples.getDeleteFirst() != null) {
        // use the real target here, as dealing direct with left tuples
        doLeftDeletes(accNode, am, wm, srcLeftTuples, trgLeftTuples, stagedLeftTuples);
    }
    if (srcRightTuples.getDeleteFirst() != null) {
        doRightDeletes(accNode, am, wm, srcRightTuples, tempLeftTuples);
    }
    if (srcRightTuples.getUpdateFirst() != null) {
        RuleNetworkEvaluator.doUpdatesReorderRightMemory(bm, srcRightTuples);
        doRightUpdates(accNode, am, wm, srcRightTuples, tempLeftTuples);
    }
    if (srcLeftTuples.getUpdateFirst() != null) {
        RuleNetworkEvaluator.doUpdatesReorderLeftMemory(bm, srcLeftTuples);
        doLeftUpdates(accNode, am, wm, srcLeftTuples, tempLeftTuples);
    }
    if (srcRightTuples.getInsertFirst() != null) {
        doRightInserts(accNode, am, wm, srcRightTuples, tempLeftTuples);
    }
    if (srcLeftTuples.getInsertFirst() != null) {
        doLeftInserts(accNode, am, wm, srcLeftTuples, tempLeftTuples);
    }
    Accumulate accumulate = accNode.getAccumulate();
    // LeftTuple retracts are already on the trgLeftTuples
    for (LeftTuple leftTuple = tempLeftTuples.getInsertFirst(); leftTuple != null; ) {
        LeftTuple next = leftTuple.getStagedNext();
        evaluateResultConstraints(accNode, sink, accumulate, leftTuple, leftTuple.getPropagationContext(), wm, am, (AccumulateContext) leftTuple.getContextObject(), trgLeftTuples, stagedLeftTuples);
        leftTuple.clearStaged();
        leftTuple = next;
    }
    for (LeftTuple leftTuple = tempLeftTuples.getUpdateFirst(); leftTuple != null; ) {
        LeftTuple next = leftTuple.getStagedNext();
        evaluateResultConstraints(accNode, sink, accumulate, leftTuple, leftTuple.getPropagationContext(), wm, am, (AccumulateContext) leftTuple.getContextObject(), trgLeftTuples, stagedLeftTuples);
        leftTuple.clearStaged();
        leftTuple = next;
    }
    srcRightTuples.resetAll();
    srcLeftTuples.resetAll();
}
Also used : TupleSetsImpl(org.drools.core.common.TupleSetsImpl) BetaMemory(org.drools.core.reteoo.BetaMemory) RightTuple(org.drools.core.reteoo.RightTuple) LeftTuple(org.drools.core.reteoo.LeftTuple) Accumulate(org.drools.core.rule.Accumulate)

Example 42 with BetaMemory

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

the class PhreakFromNode method doLeftInserts.

public void doLeftInserts(FromNode fromNode, FromMemory fm, LeftTupleSink sink, InternalWorkingMemory wm, TupleSets<LeftTuple> srcLeftTuples, TupleSets<LeftTuple> trgLeftTuples) {
    BetaMemory bm = fm.getBetaMemory();
    ContextEntry[] context = bm.getContext();
    BetaConstraints betaConstraints = fromNode.getBetaConstraints();
    AlphaNodeFieldConstraint[] alphaConstraints = fromNode.getAlphaConstraints();
    DataProvider dataProvider = fromNode.getDataProvider();
    Class<?> resultClass = fromNode.getResultClass();
    for (LeftTuple leftTuple = srcLeftTuples.getInsertFirst(); leftTuple != null; ) {
        LeftTuple next = leftTuple.getStagedNext();
        PropagationContext propagationContext = leftTuple.getPropagationContext();
        Map<Object, RightTuple> matches = null;
        boolean useLeftMemory = RuleNetworkEvaluator.useLeftMemory(fromNode, leftTuple);
        if (useLeftMemory) {
            fm.getBetaMemory().getLeftTupleMemory().add(leftTuple);
            matches = new LinkedHashMap<Object, RightTuple>();
            leftTuple.setContextObject(matches);
        }
        betaConstraints.updateFromTuple(context, wm, leftTuple);
        for (final java.util.Iterator<?> it = dataProvider.getResults(leftTuple, wm, propagationContext, fm.providerContext); it.hasNext(); ) {
            final Object object = it.next();
            if ((object == null) || !resultClass.isAssignableFrom(object.getClass())) {
                // skip anything if it not assignable
                continue;
            }
            RightTuple rightTuple = fromNode.createRightTuple(leftTuple, propagationContext, wm, object);
            checkConstraintsAndPropagate(sink, leftTuple, rightTuple, alphaConstraints, betaConstraints, propagationContext, wm, fm, context, useLeftMemory, trgLeftTuples, null);
            if (useLeftMemory) {
                fromNode.addToCreatedHandlesMap(matches, rightTuple);
            }
        }
        leftTuple.clearStaged();
        leftTuple = next;
    }
    betaConstraints.resetTuple(context);
}
Also used : BetaConstraints(org.drools.core.common.BetaConstraints) PropagationContext(org.drools.core.spi.PropagationContext) BetaMemory(org.drools.core.reteoo.BetaMemory) LeftTuple(org.drools.core.reteoo.LeftTuple) PhreakJoinNode.updateChildLeftTuple(org.drools.core.phreak.PhreakJoinNode.updateChildLeftTuple) RightTuple(org.drools.core.reteoo.RightTuple) ContextEntry(org.drools.core.rule.ContextEntry) DataProvider(org.drools.core.spi.DataProvider) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint)

Example 43 with BetaMemory

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

the class BaseBetaConstraintsTest method checkBetaConstraints.

protected void checkBetaConstraints(BetaNodeFieldConstraint[] constraints, Class cls, short betaNodeType) {
    RuleBaseConfiguration config = new RuleBaseConfiguration();
    int depth = config.getCompositeKeyDepth();
    BetaConstraints betaConstraints;
    try {
        betaConstraints = (BetaConstraints) cls.getConstructor(new Class[] { BetaNodeFieldConstraint[].class, RuleBaseConfiguration.class }).newInstance(constraints, config);
    } catch (Exception e) {
        throw new RuntimeException("could not invoke constructor for " + cls.getName());
    }
    betaConstraints.initIndexes(depth, betaNodeType);
    // BetaConstraints betaConstraints = new DefaultBetaConstraints(constraints, config );
    constraints = betaConstraints.getConstraints();
    List<Integer> list = new ArrayList<Integer>();
    // get indexed positions
    for (int i = 0; i < constraints.length && list.size() < depth; i++) {
        if (((IndexableConstraint) constraints[i]).isIndexable(betaNodeType)) {
            list.add(i);
        }
    }
    // convert to array
    int[] indexedPositions = new int[list.size()];
    for (int i = 0; i < list.size(); i++) {
        indexedPositions[i] = i;
    }
    assertEquals((indexedPositions.length > 0), betaConstraints.isIndexed());
    assertEquals(indexedPositions.length, betaConstraints.getIndexCount());
    BetaMemory betaMemory = betaConstraints.createBetaMemory(config, NodeTypeEnums.JoinNode);
    if (indexedPositions.length > 0) {
        if (((IndexableConstraint) constraints[indexedPositions[0]]).getConstraintType() == ConstraintType.EQUAL) {
            TupleIndexHashTable tupleHashTable = (TupleIndexHashTable) betaMemory.getLeftTupleMemory();
            assertTrue(tupleHashTable.isIndexed());
            Index index = tupleHashTable.getIndex();
            for (int i = 0; i < indexedPositions.length; i++) {
                checkSameConstraintForIndex((IndexableConstraint) constraints[indexedPositions[i]], index.getFieldIndex(i));
            }
            TupleIndexHashTable factHashTable = (TupleIndexHashTable) betaMemory.getRightTupleMemory();
            assertTrue(factHashTable.isIndexed());
            index = factHashTable.getIndex();
            for (int i = 0; i < indexedPositions.length; i++) {
                checkSameConstraintForIndex((IndexableConstraint) constraints[indexedPositions[i]], index.getFieldIndex(i));
            }
        } else {
        }
    } else {
        TupleList tupleHashTable = (TupleList) betaMemory.getLeftTupleMemory();
        assertFalse(tupleHashTable.isIndexed());
        TupleList factHashTable = (TupleList) betaMemory.getRightTupleMemory();
        assertFalse(factHashTable.isIndexed());
    }
}
Also used : ArrayList(java.util.ArrayList) BetaMemory(org.drools.core.reteoo.BetaMemory) FieldIndex(org.drools.core.util.AbstractHashTable.FieldIndex) Index(org.drools.core.util.AbstractHashTable.Index) TupleIndexHashTable(org.drools.core.util.index.TupleIndexHashTable) BetaNodeFieldConstraint(org.drools.core.spi.BetaNodeFieldConstraint) IndexableConstraint(org.drools.core.rule.IndexableConstraint) RuleBaseConfiguration(org.drools.core.RuleBaseConfiguration) TupleList(org.drools.core.util.index.TupleList) IndexableConstraint(org.drools.core.rule.IndexableConstraint)

Example 44 with BetaMemory

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

the class SegmentUtilities method processBetaNode.

private static long processBetaNode(BetaNode betaNode, InternalWorkingMemory wm, SegmentMemory smem, long nodePosMask, long allLinkedTestMask, boolean updateNodeBit) {
    BetaMemory bm = NodeTypeEnums.AccumulateNode == betaNode.getType() ? ((AccumulateMemory) smem.createNodeMemory(betaNode, wm)).getBetaMemory() : (BetaMemory) smem.createNodeMemory(betaNode, wm);
    // this must be set first, to avoid recursion as sub networks can be initialised multiple ways
    // and bm.getSegmentMemory == null check can be used to avoid recursion.
    bm.setSegmentMemory(smem);
    if (betaNode.isRightInputIsRiaNode()) {
        RightInputAdapterNode riaNode = createRiaSegmentMemory(betaNode, wm);
        RiaNodeMemory riaMem = wm.getNodeMemory(riaNode);
        bm.setRiaRuleMemory(riaMem.getRiaPathMemory());
        if (updateNodeBit && canBeDisabled(betaNode) && riaMem.getRiaPathMemory().getAllLinkedMaskTest() > 0) {
            // only ria's with reactive subnetworks can be disabled and thus need checking
            allLinkedTestMask = allLinkedTestMask | nodePosMask;
        }
    } else if (updateNodeBit && canBeDisabled(betaNode)) {
        allLinkedTestMask = allLinkedTestMask | nodePosMask;
    }
    bm.setNodePosMaskBit(nodePosMask);
    if (NodeTypeEnums.NotNode == betaNode.getType()) {
        // not nodes start up linked in
        smem.linkNodeWithoutRuleNotify(bm.getNodePosMaskBit());
    }
    return allLinkedTestMask;
}
Also used : RiaNodeMemory(org.drools.core.reteoo.RightInputAdapterNode.RiaNodeMemory) BetaMemory(org.drools.core.reteoo.BetaMemory) RightInputAdapterNode(org.drools.core.reteoo.RightInputAdapterNode)

Example 45 with BetaMemory

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

the class AddRuleTest method testPopulatedRuleMidwayShare.

@Test
public void testPopulatedRuleMidwayShare() throws Exception {
    InternalKnowledgeBase kbase1 = buildKnowledgeBase("r1", "   a : A() B() C(1;) 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 A(3));
    wm.insert(new B(1));
    wm.insert(new C(1));
    wm.insert(new C(2));
    wm.insert(new D(1));
    wm.insert(new E(1));
    wm.fireAllRules();
    assertEquals(3, list.size());
    kbase1.addPackages(buildKnowledgePackage("r2", "   a : A() B() C(2;) D() E()\n"));
    ObjectTypeNode aotn = getObjectTypeNode(kbase1, A.class);
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) aotn.getObjectSinkPropagator().getSinks()[0];
    JoinNode bNode = (JoinNode) liaNode.getSinkPropagator().getFirstLeftTupleSink();
    JoinNode c1Node = (JoinNode) bNode.getSinkPropagator().getFirstLeftTupleSink();
    JoinNode c2Node = (JoinNode) bNode.getSinkPropagator().getLastLeftTupleSink();
    LiaNodeMemory lm = (LiaNodeMemory) wm.getNodeMemory(liaNode);
    SegmentMemory sm = lm.getSegmentMemory();
    BetaMemory c1Mem = (BetaMemory) wm.getNodeMemory(c1Node);
    assertSame(sm.getFirst(), c1Mem.getSegmentMemory());
    assertEquals(3, c1Mem.getLeftTupleMemory().size());
    assertEquals(1, c1Mem.getRightTupleMemory().size());
    BetaMemory c2Mem = (BetaMemory) wm.getNodeMemory(c2Node);
    SegmentMemory c2Smem = sm.getFirst().getNext();
    assertSame(c2Smem, c2Mem.getSegmentMemory());
    assertEquals(0, c2Mem.getLeftTupleMemory().size());
    assertEquals(0, c2Mem.getRightTupleMemory().size());
    assertNotNull(c2Smem.getStagedLeftTuples().getInsertFirst());
    assertNotNull(c2Smem.getStagedLeftTuples().getInsertFirst().getStagedNext());
    assertNotNull(c2Smem.getStagedLeftTuples().getInsertFirst().getStagedNext().getStagedNext());
    assertNull(c2Smem.getStagedLeftTuples().getInsertFirst().getStagedNext().getStagedNext().getStagedNext());
    wm.fireAllRules();
    assertEquals(3, c2Mem.getLeftTupleMemory().size());
    assertEquals(1, c2Mem.getRightTupleMemory().size());
    assertNull(c2Smem.getStagedLeftTuples().getInsertFirst());
    assertEquals(6, list.size());
    assertEquals("r1", ((Match) list.get(0)).getRule().getName());
    assertEquals("r1", ((Match) list.get(1)).getRule().getName());
    assertEquals("r1", ((Match) list.get(2)).getRule().getName());
    assertEquals("r2", ((Match) list.get(3)).getRule().getName());
    assertEquals(3, ((A) ((Match) list.get(3)).getDeclarationValue("a")).getObject());
    assertEquals("r2", ((Match) list.get(4)).getRule().getName());
    assertEquals(2, ((A) ((Match) list.get(4)).getDeclarationValue("a")).getObject());
    assertEquals("r2", ((Match) list.get(5)).getRule().getName());
    assertEquals(1, ((A) ((Match) list.get(5)).getDeclarationValue("a")).getObject());
}
Also used : SegmentMemory(org.drools.core.reteoo.SegmentMemory) JoinNode(org.drools.core.reteoo.JoinNode) ArrayList(java.util.ArrayList) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) BetaMemory(org.drools.core.reteoo.BetaMemory) LiaNodeMemory(org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory) Match(org.kie.api.runtime.rule.Match) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Test(org.junit.Test)

Aggregations

BetaMemory (org.drools.core.reteoo.BetaMemory)49 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)25 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)24 Test (org.junit.Test)24 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)23 RightTuple (org.drools.core.reteoo.RightTuple)20 JoinNode (org.drools.core.reteoo.JoinNode)19 LeftTuple (org.drools.core.reteoo.LeftTuple)19 ArrayList (java.util.ArrayList)16 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)14 SegmentMemory (org.drools.core.reteoo.SegmentMemory)13 TupleMemory (org.drools.core.reteoo.TupleMemory)13 BetaNode (org.drools.core.reteoo.BetaNode)12 List (java.util.List)11 FastIterator (org.drools.core.util.FastIterator)11 LiaNodeMemory (org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory)10 KieBase (org.kie.api.KieBase)10 InternalFactHandle (org.drools.core.common.InternalFactHandle)9 AccumulateMemory (org.drools.core.reteoo.AccumulateNode.AccumulateMemory)9 BetaConstraints (org.drools.core.common.BetaConstraints)8