Search in sources :

Example 6 with InternalFactHandle

use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.

the class AddRemoveRule method deleteLeftTuple.

private static void deleteLeftTuple(LeftTuple removingLt, LeftTuple removingLt2, LeftTuple prevLt) {
    // only the first LT in a peer chain is hooked into left and right parents or the FH.
    // If the first LT is being remove, those hooks need to be shifted to the next peer,
    // or nulled if there is no next peer.
    // When there is a subnetwork, it needs to shift to the peer of the next lt.
    // if it is not the first LT in the peer chain, leftParent and rightParent are null.
    // And the previous peer will need to point to the peer after removingLt, or removingLt2 if it exists.
    // is this the first LT in a peer chain chain
    boolean isFirstLt = prevLt == null;
    // if there is a subnetwork, skip to the peer after that
    LeftTuple nextPeerLt = (removingLt2 == null) ? removingLt.getPeer() : removingLt2.getPeer();
    if (!isFirstLt) {
        // This LT is not the first tuple in a peer chain. So just correct the peer chain linked list
        prevLt.setPeer(nextPeerLt);
    } else {
        if (nextPeerLt == null) {
            removingLt.unlinkFromLeftParent();
            removingLt.unlinkFromRightParent();
            return;
        }
        InternalFactHandle fh = removingLt.getFactHandle();
        // This is the first LT in a peer chain. Only this LT is hooked into the left and right parent LT and RT and
        // if it's the root (form the lian) it will be hooked itno the FH.
        LeftTuple leftPrevious = removingLt.getHandlePrevious();
        LeftTuple leftNext = removingLt.getHandleNext();
        LeftTuple rightPrevious = removingLt.getRightParentPrevious();
        LeftTuple rightNext = removingLt.getRightParentNext();
        LeftTuple leftParent = removingLt.getLeftParent();
        RightTuple rightParent = removingLt.getRightParent();
        // This tuple is the first peer and thus is linked into the left parent LT.
        nextPeerLt.setFactHandle(removingLt.getFactHandle());
        // correct the linked list
        if (leftPrevious != null) {
            nextPeerLt.setHandlePrevious(leftPrevious);
            leftPrevious.setHandleNext(nextPeerLt);
        }
        if (leftNext != null) {
            nextPeerLt.setHandleNext(leftNext);
            leftNext.setHandlePrevious(nextPeerLt);
        }
        // correct the linked list
        if (rightPrevious != null) {
            nextPeerLt.setRightParentPrevious(rightPrevious);
            rightPrevious.setRightParentNext(nextPeerLt);
        }
        if (rightNext != null) {
            nextPeerLt.setRightParentNext(rightNext);
            rightNext.setRightParentPrevious(nextPeerLt);
        }
        // correct the parent's first/last references
        if (leftParent != null) {
            nextPeerLt.setLeftParent(leftParent);
            if (leftParent.getFirstChild() == removingLt) {
                leftParent.setFirstChild(nextPeerLt);
            }
            if (leftParent.getLastChild() == removingLt) {
                leftParent.setLastChild(nextPeerLt);
            }
        } else {
            // is the LT for the LIAN, if so we need to process the FH too
            fh.removeLeftTuple(removingLt);
        }
        if (rightParent != null) {
            // This tuple is the first peer and thus is linked into the right parent RT.
            nextPeerLt.setRightParent(rightParent);
            // if nextLT is null, it's ok for parent's reference to be null
            if (rightParent.getFirstChild() == removingLt) {
                // if next peer exists, set it to this
                rightParent.setFirstChild(nextPeerLt);
            }
            if (rightParent.getLastChild() == removingLt) {
                rightParent.setLastChild(nextPeerLt);
            }
        }
    }
}
Also used : InternalFactHandle(org.drools.core.common.InternalFactHandle) LeftTuple(org.drools.core.reteoo.LeftTuple) RightTuple(org.drools.core.reteoo.RightTuple)

Example 7 with InternalFactHandle

use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.

the class PhreakAccumulateNode method reaccumulateForLeftTuple.

private static void reaccumulateForLeftTuple(final AccumulateNode accNode, final Accumulate accumulate, final LeftTuple leftTuple, final InternalWorkingMemory wm, final AccumulateMemory am, final AccumulateContext accctx) {
    accumulate.init(am.workingMemoryContext, accctx.context, leftTuple, wm);
    for (LeftTuple childMatch = leftTuple.getFirstChild(); childMatch != null; childMatch = childMatch.getHandleNext()) {
        RightTuple rightTuple = childMatch.getRightParent();
        InternalFactHandle childHandle = rightTuple.getFactHandle();
        LeftTuple tuple = leftTuple;
        if (accNode.isUnwrapRightObject()) {
            // if there is a subnetwork, handle must be unwrapped
            tuple = (LeftTuple) rightTuple;
            childHandle = rightTuple.getFactHandleForEvaluation();
        }
        accumulate.accumulate(am.workingMemoryContext, accctx.context, tuple, childHandle, wm);
    }
}
Also used : InternalFactHandle(org.drools.core.common.InternalFactHandle) LeftTuple(org.drools.core.reteoo.LeftTuple) RightTuple(org.drools.core.reteoo.RightTuple)

Example 8 with InternalFactHandle

use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.

the class PhreakAccumulateNode method removeMatch.

/**
 * Removes a match between left and right tuple
 */
private static void removeMatch(final AccumulateNode accNode, final Accumulate accumulate, final RightTuple rightTuple, final LeftTuple match, final InternalWorkingMemory wm, final AccumulateMemory am, final AccumulateContext accctx, final boolean reaccumulate) {
    // save the matching tuple
    LeftTuple leftTuple = match.getLeftParent();
    // removing link between left and right
    match.unlinkFromLeftParent();
    match.unlinkFromRightParent();
    // if there is a subnetwork, we need to unwrap the object from inside the tuple
    InternalFactHandle handle = rightTuple.getFactHandle();
    LeftTuple tuple = leftTuple;
    if (accNode.isUnwrapRightObject()) {
        tuple = (LeftTuple) rightTuple;
        handle = rightTuple.getFactHandleForEvaluation();
    }
    if (accumulate.supportsReverse()) {
        // just reverse this single match
        accumulate.reverse(am.workingMemoryContext, accctx.context, tuple, handle, wm);
    } else {
        // otherwise need to recalculate all matches for the given leftTuple
        if (reaccumulate) {
            reaccumulateForLeftTuple(accNode, accumulate, leftTuple, wm, am, accctx);
        }
    }
}
Also used : InternalFactHandle(org.drools.core.common.InternalFactHandle) LeftTuple(org.drools.core.reteoo.LeftTuple)

Example 9 with InternalFactHandle

use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.

the class InputMarshaller method readFactHandle.

public static InternalFactHandle readFactHandle(MarshallerReaderContext context) throws IOException, ClassNotFoundException {
    int type = context.stream.readInt();
    int id = context.stream.readInt();
    long recency = context.stream.readLong();
    long startTimeStamp = 0;
    long duration = 0;
    boolean expired = false;
    long activationsCount = 0;
    if (type == 2) {
        startTimeStamp = context.stream.readLong();
        duration = context.stream.readLong();
        expired = context.stream.readBoolean();
        activationsCount = context.stream.readLong();
    }
    int strategyIndex = context.stream.readInt();
    Object object = null;
    ObjectMarshallingStrategy strategy = null;
    // This is the old way of de/serializing strategy objects
    if (strategyIndex >= 0) {
        strategy = context.resolverStrategyFactory.getStrategy(strategyIndex);
    } else // This is the new way
    if (strategyIndex == -2) {
        String strategyClassName = context.stream.readUTF();
        if (!StringUtils.isEmpty(strategyClassName)) {
            strategy = context.resolverStrategyFactory.getStrategyObject(strategyClassName);
            if (strategy == null) {
                throw new IllegalStateException("No strategy of type " + strategyClassName + " available.");
            }
        }
    }
    // If either way retrieves a strategy, use it
    if (strategy != null) {
        object = strategy.read(context.stream);
    }
    EntryPoint entryPoint = null;
    if (context.readBoolean()) {
        String entryPointId = context.readUTF();
        if (entryPointId != null && !entryPointId.equals("")) {
            entryPoint = ((RuleRuntime) context.wm).getEntryPoint(entryPointId);
        }
    }
    EntryPointId confEP;
    if (entryPoint != null) {
        confEP = ((NamedEntryPoint) entryPoint).getEntryPoint();
    } else {
        confEP = context.wm.getEntryPoint();
    }
    ObjectTypeConf typeConf = context.wm.getObjectTypeConfigurationRegistry().getObjectTypeConf(confEP, object);
    InternalFactHandle handle = null;
    switch(type) {
        case 0:
            {
                handle = new DefaultFactHandle(id, object, recency, (WorkingMemoryEntryPoint) entryPoint, typeConf != null && typeConf.isTrait());
                break;
            }
        case 1:
            {
                handle = new QueryElementFactHandle(object, id, recency);
                break;
            }
        case 2:
            {
                handle = new EventFactHandle(id, object, recency, startTimeStamp, duration, (WorkingMemoryEntryPoint) entryPoint, typeConf != null && typeConf.isTrait());
                ((EventFactHandle) handle).setExpired(expired);
                ((EventFactHandle) handle).setActivationsCount(activationsCount);
                break;
            }
        default:
            {
                throw new IllegalStateException("Unable to marshal FactHandle, as type does not exist:" + type);
            }
    }
    return handle;
}
Also used : ObjectMarshallingStrategy(org.kie.api.marshalling.ObjectMarshallingStrategy) WorkingMemoryEntryPoint(org.drools.core.WorkingMemoryEntryPoint) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) WorkingMemoryEntryPoint(org.drools.core.WorkingMemoryEntryPoint) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) ObjectTypeConf(org.drools.core.reteoo.ObjectTypeConf) EntryPointId(org.drools.core.rule.EntryPointId) QueryElementFactHandle(org.drools.core.common.QueryElementFactHandle) EventFactHandle(org.drools.core.common.EventFactHandle) InternalFactHandle(org.drools.core.common.InternalFactHandle)

Example 10 with InternalFactHandle

use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.

the class ProtobufInputMarshaller method readFactHandle.

public static InternalFactHandle readFactHandle(MarshallerReaderContext context, EntryPoint entryPoint, FactHandle _handle) throws IOException, ClassNotFoundException {
    Object object = null;
    ObjectMarshallingStrategy strategy = null;
    if (_handle.hasStrategyIndex()) {
        strategy = context.usedStrategies.get(_handle.getStrategyIndex());
        object = strategy.unmarshal(context.strategyContexts.get(strategy), context, _handle.getObject().toByteArray(), (context.kBase == null) ? null : context.kBase.getRootClassLoader());
    }
    EntryPointId confEP;
    if (entryPoint != null) {
        confEP = ((NamedEntryPoint) entryPoint).getEntryPoint();
    } else {
        confEP = context.wm.getEntryPoint();
    }
    ObjectTypeConf typeConf = context.wm.getObjectTypeConfigurationRegistry().getObjectTypeConf(confEP, object);
    InternalFactHandle handle = null;
    switch(_handle.getType()) {
        case FACT:
            {
                handle = new DefaultFactHandle(_handle.getId(), object, _handle.getRecency(), (WorkingMemoryEntryPoint) entryPoint, typeConf != null && typeConf.isTrait());
                break;
            }
        case QUERY:
            {
                handle = new QueryElementFactHandle(object, _handle.getId(), _handle.getRecency());
                break;
            }
        case EVENT:
            {
                handle = new EventFactHandle(_handle.getId(), object, _handle.getRecency(), _handle.getTimestamp(), _handle.getDuration(), (WorkingMemoryEntryPoint) entryPoint, typeConf != null && typeConf.isTrait());
                ((EventFactHandle) handle).setExpired(_handle.getIsExpired());
                ((EventFactHandle) handle).setOtnCount(_handle.getOtnCount());
                // ((EventFactHandle) handle).setActivationsCount( _handle.getActivationsCount() );
                break;
            }
        default:
            {
                throw new IllegalStateException("Unable to marshal FactHandle, as type does not exist:" + _handle.getType());
            }
    }
    return handle;
}
Also used : DefaultFactHandle(org.drools.core.common.DefaultFactHandle) ObjectTypeConf(org.drools.core.reteoo.ObjectTypeConf) EntryPointId(org.drools.core.rule.EntryPointId) ObjectMarshallingStrategy(org.kie.api.marshalling.ObjectMarshallingStrategy) QueryElementFactHandle(org.drools.core.common.QueryElementFactHandle) EventFactHandle(org.drools.core.common.EventFactHandle) InternalFactHandle(org.drools.core.common.InternalFactHandle)

Aggregations

InternalFactHandle (org.drools.core.common.InternalFactHandle)182 Test (org.junit.Test)89 KieSession (org.kie.api.runtime.KieSession)66 ArrayList (java.util.ArrayList)63 List (java.util.List)41 KieBase (org.kie.api.KieBase)31 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)29 LeftTuple (org.drools.core.reteoo.LeftTuple)27 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)21 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)21 FactHandle (org.kie.api.runtime.rule.FactHandle)21 RightTuple (org.drools.core.reteoo.RightTuple)20 Declaration (org.drools.core.rule.Declaration)20 LeftTupleImpl (org.drools.core.reteoo.LeftTupleImpl)16 StockTickInterface (org.drools.compiler.StockTickInterface)15 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)15 RightTupleImpl (org.drools.core.reteoo.RightTupleImpl)15 StockTick (org.drools.compiler.StockTick)14 Cheese (org.drools.core.test.model.Cheese)14 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)14