Search in sources :

Example 1 with InternalFactHandle

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

the class PhreakQueryNode method doLeftUpdates.

public void doLeftUpdates(QueryElementNode queryNode, QueryElementNodeMemory qmem, InternalWorkingMemory wm, TupleSets<LeftTuple> srcLeftTuples) {
    for (LeftTuple leftTuple = srcLeftTuples.getUpdateFirst(); leftTuple != null; ) {
        LeftTuple next = leftTuple.getStagedNext();
        InternalFactHandle fh = (InternalFactHandle) leftTuple.getContextObject();
        DroolsQuery dquery = (DroolsQuery) fh.getObject();
        dquery.setParameters(queryNode.getActualArguments(leftTuple, wm));
        SegmentMemory qsmem = qmem.getQuerySegmentMemory();
        LeftInputAdapterNode lian = (LeftInputAdapterNode) qsmem.getRootNode();
        LiaNodeMemory lmem = (LiaNodeMemory) qsmem.getNodeMemories().getFirst();
        if (dquery.isOpen()) {
            // there is only one, all other LTs are peers
            LeftTuple childLeftTuple = fh.getFirstLeftTuple();
            LeftInputAdapterNode.doUpdateObject(childLeftTuple, childLeftTuple.getPropagationContext(), wm, lian, false, lmem, qmem.getQuerySegmentMemory());
        } else {
            if (fh.getFirstLeftTuple() != null) {
                // @TODO remove later (mdp)
                throw new RuntimeException("defensive programming while testing");
            }
            LiaNodeMemory lm = (LiaNodeMemory) qmem.getQuerySegmentMemory().getNodeMemories().get(0);
            LeftInputAdapterNode.doInsertObject(fh, leftTuple.getPropagationContext(), lian, wm, lm, false, dquery.isOpen());
        }
        leftTuple.clearStaged();
        leftTuple = next;
    }
}
Also used : SegmentMemory(org.drools.core.reteoo.SegmentMemory) InternalFactHandle(org.drools.core.common.InternalFactHandle) LeftTuple(org.drools.core.reteoo.LeftTuple) LiaNodeMemory(org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory) DroolsQuery(org.drools.core.base.DroolsQuery) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode)

Example 2 with InternalFactHandle

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

the class PhreakQueryNode method doLeftInserts.

public void doLeftInserts(QueryElementNode queryNode, QueryElementNodeMemory qmem, StackEntry stackEntry, InternalWorkingMemory wm, TupleSets<LeftTuple> srcLeftTuples) {
    for (LeftTuple leftTuple = srcLeftTuples.getInsertFirst(); leftTuple != null; ) {
        LeftTuple next = leftTuple.getStagedNext();
        PropagationContext pCtx = leftTuple.getPropagationContext();
        InternalFactHandle handle = queryNode.createFactHandle(pCtx, wm, leftTuple);
        DroolsQuery dquery = queryNode.createDroolsQuery(leftTuple, handle, stackEntry, qmem.getSegmentMemory().getPathMemories(), qmem, stackEntry.getSink(), wm);
        LeftInputAdapterNode lian = (LeftInputAdapterNode) qmem.getQuerySegmentMemory().getRootNode();
        LiaNodeMemory lm = (LiaNodeMemory) qmem.getQuerySegmentMemory().getNodeMemories().get(0);
        LeftInputAdapterNode.doInsertObject(handle, pCtx, lian, wm, lm, false, dquery.isOpen());
        leftTuple.clearStaged();
        leftTuple = next;
    }
}
Also used : PropagationContext(org.drools.core.spi.PropagationContext) InternalFactHandle(org.drools.core.common.InternalFactHandle) LeftTuple(org.drools.core.reteoo.LeftTuple) LiaNodeMemory(org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory) DroolsQuery(org.drools.core.base.DroolsQuery) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode)

Example 3 with InternalFactHandle

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

the class AccumulateNode method createResultFactHandle.

public InternalFactHandle createResultFactHandle(final PropagationContext context, final InternalWorkingMemory workingMemory, final LeftTuple leftTuple, final Object result) {
    InternalFactHandle handle;
    ProtobufMessages.FactHandle _handle = null;
    if (context.getReaderContext() != null) {
        Map<TupleKey, FactHandle> map = (Map<ProtobufInputMarshaller.TupleKey, ProtobufMessages.FactHandle>) context.getReaderContext().nodeMemories.get(getId());
        if (map != null) {
            _handle = map.get(PersisterHelper.createTupleKey(leftTuple));
        }
    }
    if (_handle != null) {
        // create a handle with the given id
        handle = workingMemory.getFactHandleFactory().newFactHandle(_handle.getId(), result, _handle.getRecency(), workingMemory.getObjectTypeConfigurationRegistry().getObjectTypeConf(context.getEntryPoint(), result), workingMemory, // so far, result is not an event
        null);
    } else {
        handle = workingMemory.getFactHandleFactory().newFactHandle(result, workingMemory.getObjectTypeConfigurationRegistry().getObjectTypeConf(context.getEntryPoint(), result), workingMemory, // so far, result is not an event
        null);
    }
    return handle;
}
Also used : ProtobufInputMarshaller(org.drools.core.marshalling.impl.ProtobufInputMarshaller) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.drools.core.marshalling.impl.ProtobufMessages.FactHandle) FactHandle(org.drools.core.marshalling.impl.ProtobufMessages.FactHandle) InternalFactHandle(org.drools.core.common.InternalFactHandle) ProtobufMessages(org.drools.core.marshalling.impl.ProtobufMessages) TupleKey(org.drools.core.marshalling.impl.ProtobufInputMarshaller.TupleKey) Map(java.util.Map)

Example 4 with InternalFactHandle

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

the class FromNode method createFactHandle.

public InternalFactHandle createFactHandle(Tuple leftTuple, PropagationContext context, InternalWorkingMemory workingMemory, Object object) {
    FactHandle _handle = null;
    if (objectTypeConf == null) {
        // use default entry point and object class. Notice that at this point object is assignable to resultClass
        objectTypeConf = new ClassObjectTypeConf(workingMemory.getEntryPoint(), resultClass, workingMemory.getKnowledgeBase());
    }
    if (context.getReaderContext() != null) {
        Map<TupleKey, List<FactHandle>> map = (Map<TupleKey, List<FactHandle>>) context.getReaderContext().nodeMemories.get(getId());
        if (map != null) {
            TupleKey key = PersisterHelper.createTupleKey(leftTuple);
            List<FactHandle> list = map.get(key);
            if (list != null && !list.isEmpty()) {
                // it is a linked list, so the operation is fairly efficient
                _handle = ((java.util.LinkedList<FactHandle>) list).removeFirst();
                if (list.isEmpty()) {
                    map.remove(key);
                }
            }
        }
    }
    InternalFactHandle handle;
    if (_handle != null) {
        // create a handle with the given id
        handle = workingMemory.getFactHandleFactory().newFactHandle(_handle.getId(), object, _handle.getRecency(), objectTypeConf, workingMemory, null);
    } else {
        handle = workingMemory.getFactHandleFactory().newFactHandle(object, objectTypeConf, workingMemory, null);
    }
    return handle;
}
Also used : InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.drools.core.marshalling.impl.ProtobufMessages.FactHandle) TupleList(org.drools.core.util.index.TupleList) List(java.util.List) TupleKey(org.drools.core.marshalling.impl.ProtobufInputMarshaller.TupleKey) InternalFactHandle(org.drools.core.common.InternalFactHandle) Map(java.util.Map)

Example 5 with InternalFactHandle

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

the class ProtobufOutputMarshaller method writeFactHandle.

private static ProtobufMessages.FactHandle writeFactHandle(MarshallerWriteContext context, ObjectMarshallingStrategyStore objectMarshallingStrategyStore, InternalFactHandle handle) throws IOException {
    ProtobufMessages.FactHandle.Builder _handle = ProtobufMessages.FactHandle.newBuilder();
    _handle.setType(getHandleType(handle));
    _handle.setId(handle.getId());
    _handle.setRecency(handle.getRecency());
    if (_handle.getType() == ProtobufMessages.FactHandle.HandleType.EVENT) {
        // is event
        EventFactHandle efh = (EventFactHandle) handle;
        _handle.setTimestamp(efh.getStartTimestamp());
        _handle.setDuration(efh.getDuration());
        _handle.setIsExpired(efh.isExpired());
        _handle.setActivationsCount(efh.getActivationsCount());
        _handle.setOtnCount(efh.getOtnCount());
    }
    if (handle.getEqualityKey() != null && handle.getEqualityKey().getStatus() == EqualityKey.JUSTIFIED) {
        _handle.setIsJustified(true);
    } else {
        _handle.setIsJustified(false);
    }
    Object object = handle.getObject();
    if (object != null) {
        ObjectMarshallingStrategy strategy = objectMarshallingStrategyStore.getStrategyObject(object);
        Integer index = context.getStrategyIndex(strategy);
        _handle.setStrategyIndex(index);
        _handle.setObject(ByteString.copyFrom(strategy.marshal(context.strategyContext.get(strategy), context, object)));
    }
    return _handle.build();
}
Also used : InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.drools.core.marshalling.impl.ProtobufMessages.FactHandle) QueryElementFactHandle(org.drools.core.common.QueryElementFactHandle) EventFactHandle(org.drools.core.common.EventFactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) ObjectMarshallingStrategy(org.kie.api.marshalling.ObjectMarshallingStrategy) EventFactHandle(org.drools.core.common.EventFactHandle)

Aggregations

InternalFactHandle (org.drools.core.common.InternalFactHandle)336 Test (org.junit.Test)138 KieSession (org.kie.api.runtime.KieSession)93 ArrayList (java.util.ArrayList)92 LeftTuple (org.drools.core.reteoo.LeftTuple)53 List (java.util.List)47 KieBase (org.kie.api.KieBase)44 Declaration (org.drools.core.rule.Declaration)41 RightTuple (org.drools.core.reteoo.RightTuple)38 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)35 FactHandle (org.kie.api.runtime.rule.FactHandle)33 LeftTupleImpl (org.drools.core.reteoo.LeftTupleImpl)30 RightTupleImpl (org.drools.core.reteoo.RightTupleImpl)30 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)27 Tuple (org.drools.core.spi.Tuple)25 ClassObjectType (org.drools.core.base.ClassObjectType)24 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)24 ObjectTypeConf (org.drools.core.reteoo.ObjectTypeConf)23 Cheese (org.drools.core.test.model.Cheese)23 WorkingMemoryEntryPoint (org.drools.core.WorkingMemoryEntryPoint)21