Search in sources :

Example 1 with FactHandle

use of org.drools.core.marshalling.impl.ProtobufMessages.FactHandle 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 2 with FactHandle

use of org.drools.core.marshalling.impl.ProtobufMessages.FactHandle 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 3 with FactHandle

use of org.drools.core.marshalling.impl.ProtobufMessages.FactHandle in project drools by kiegroup.

the class ProtobufOutputMarshaller method writeFromNodeMemory.

@SuppressWarnings("unchecked")
private static ProtobufMessages.NodeMemory writeFromNodeMemory(final int nodeId, final Memory memory) {
    FromMemory fromMemory = (FromMemory) memory;
    if (fromMemory.getBetaMemory().getLeftTupleMemory().size() > 0) {
        ProtobufMessages.NodeMemory.FromNodeMemory.Builder _from = ProtobufMessages.NodeMemory.FromNodeMemory.newBuilder();
        final org.drools.core.util.Iterator<LeftTuple> tupleIter = fromMemory.getBetaMemory().getLeftTupleMemory().iterator();
        for (LeftTuple leftTuple = tupleIter.next(); leftTuple != null; leftTuple = tupleIter.next()) {
            Map<Object, RightTuple> matches = (Map<Object, RightTuple>) leftTuple.getContextObject();
            ProtobufMessages.NodeMemory.FromNodeMemory.FromContext.Builder _context = ProtobufMessages.NodeMemory.FromNodeMemory.FromContext.newBuilder().setTuple(PersisterHelper.createTuple(leftTuple));
            for (RightTuple rightTuple : matches.values()) {
                FactHandle _handle = ProtobufMessages.FactHandle.newBuilder().setId(rightTuple.getFactHandle().getId()).setRecency(rightTuple.getFactHandle().getRecency()).build();
                _context.addHandle(_handle);
            }
            _from.addContext(_context.build());
        }
        return ProtobufMessages.NodeMemory.newBuilder().setNodeId(nodeId).setNodeType(ProtobufMessages.NodeMemory.NodeType.FROM).setFrom(_from.build()).build();
    }
    return null;
}
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) LeftTuple(org.drools.core.reteoo.LeftTuple) RightTuple(org.drools.core.reteoo.RightTuple) FromMemory(org.drools.core.reteoo.FromNode.FromMemory) QueryElementNodeMemory(org.drools.core.reteoo.QueryElementNode.QueryElementNodeMemory) ObjectTypeNodeMemory(org.drools.core.reteoo.ObjectTypeNode.ObjectTypeNodeMemory) Map(java.util.Map) ObjectHashMap(org.drools.core.util.ObjectHashMap)

Example 4 with FactHandle

use of org.drools.core.marshalling.impl.ProtobufMessages.FactHandle in project drools by kiegroup.

the class ProtobufOutputMarshaller method writeAccumulateNodeMemory.

private static ProtobufMessages.NodeMemory writeAccumulateNodeMemory(final int nodeId, final Memory memory) {
    // for accumulate nodes, we need to store the ID of created (result) handles
    AccumulateMemory accmem = (AccumulateMemory) memory;
    if (accmem.getBetaMemory().getLeftTupleMemory().size() > 0) {
        ProtobufMessages.NodeMemory.AccumulateNodeMemory.Builder _accumulate = ProtobufMessages.NodeMemory.AccumulateNodeMemory.newBuilder();
        final org.drools.core.util.Iterator<LeftTuple> tupleIter = accmem.getBetaMemory().getLeftTupleMemory().iterator();
        for (LeftTuple leftTuple = tupleIter.next(); leftTuple != null; leftTuple = tupleIter.next()) {
            AccumulateContext accctx = (AccumulateContext) leftTuple.getContextObject();
            if (accctx.getResultFactHandle() != null) {
                FactHandle _handle = ProtobufMessages.FactHandle.newBuilder().setId(accctx.getResultFactHandle().getId()).setRecency(accctx.getResultFactHandle().getRecency()).build();
                _accumulate.addContext(ProtobufMessages.NodeMemory.AccumulateNodeMemory.AccumulateContext.newBuilder().setTuple(PersisterHelper.createTuple(leftTuple)).setResultHandle(_handle).build());
            }
        }
        return ProtobufMessages.NodeMemory.newBuilder().setNodeId(nodeId).setNodeType(ProtobufMessages.NodeMemory.NodeType.ACCUMULATE).setAccumulate(_accumulate.build()).build();
    }
    return null;
}
Also used : AccumulateMemory(org.drools.core.reteoo.AccumulateNode.AccumulateMemory) 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) LeftTuple(org.drools.core.reteoo.LeftTuple) AccumulateContext(org.drools.core.reteoo.AccumulateNode.AccumulateContext)

Example 5 with FactHandle

use of org.drools.core.marshalling.impl.ProtobufMessages.FactHandle in project drools by kiegroup.

the class ProtobufOutputMarshaller method writeQueryElementNodeMemory.

private static ProtobufMessages.NodeMemory writeQueryElementNodeMemory(final int nodeId, final Memory memory, final InternalWorkingMemory wm) {
    org.drools.core.util.Iterator<LeftTuple> it = LeftTupleIterator.iterator(wm, ((QueryElementNodeMemory) memory).getNode());
    ProtobufMessages.NodeMemory.QueryElementNodeMemory.Builder _query = ProtobufMessages.NodeMemory.QueryElementNodeMemory.newBuilder();
    for (LeftTuple leftTuple = it.next(); leftTuple != null; leftTuple = it.next()) {
        InternalFactHandle handle = (InternalFactHandle) leftTuple.getContextObject();
        FactHandle _handle = ProtobufMessages.FactHandle.newBuilder().setId(handle.getId()).setRecency(handle.getRecency()).build();
        ProtobufMessages.NodeMemory.QueryElementNodeMemory.QueryContext.Builder _context = ProtobufMessages.NodeMemory.QueryElementNodeMemory.QueryContext.newBuilder().setTuple(PersisterHelper.createTuple(leftTuple)).setHandle(_handle);
        LeftTuple childLeftTuple = leftTuple.getFirstChild();
        while (childLeftTuple != null) {
            RightTuple rightParent = childLeftTuple.getRightParent();
            _context.addResult(ProtobufMessages.FactHandle.newBuilder().setId(rightParent.getFactHandle().getId()).setRecency(rightParent.getFactHandle().getRecency()).build());
            while (childLeftTuple != null && childLeftTuple.getRightParent() == rightParent) {
                // skip to the next child that has a different right parent
                childLeftTuple = childLeftTuple.getHandleNext();
            }
        }
        _query.addContext(_context.build());
    }
    return _query.getContextCount() > 0 ? ProtobufMessages.NodeMemory.newBuilder().setNodeId(nodeId).setNodeType(ProtobufMessages.NodeMemory.NodeType.QUERY_ELEMENT).setQueryElement(_query.build()).build() : null;
}
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) QueryElementNodeMemory(org.drools.core.reteoo.QueryElementNode.QueryElementNodeMemory) LeftTuple(org.drools.core.reteoo.LeftTuple) RightTuple(org.drools.core.reteoo.RightTuple) InternalFactHandle(org.drools.core.common.InternalFactHandle) QueryElementNodeMemory(org.drools.core.reteoo.QueryElementNode.QueryElementNodeMemory) ObjectTypeNodeMemory(org.drools.core.reteoo.ObjectTypeNode.ObjectTypeNodeMemory)

Aggregations

InternalFactHandle (org.drools.core.common.InternalFactHandle)6 FactHandle (org.drools.core.marshalling.impl.ProtobufMessages.FactHandle)6 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)4 EventFactHandle (org.drools.core.common.EventFactHandle)4 QueryElementFactHandle (org.drools.core.common.QueryElementFactHandle)4 LeftTuple (org.drools.core.reteoo.LeftTuple)4 Map (java.util.Map)3 ObjectTypeNodeMemory (org.drools.core.reteoo.ObjectTypeNode.ObjectTypeNodeMemory)3 QueryElementNodeMemory (org.drools.core.reteoo.QueryElementNode.QueryElementNodeMemory)3 RightTuple (org.drools.core.reteoo.RightTuple)3 TupleKey (org.drools.core.marshalling.impl.ProtobufInputMarshaller.TupleKey)2 AccumulateMemory (org.drools.core.reteoo.AccumulateNode.AccumulateMemory)2 FromMemory (org.drools.core.reteoo.FromNode.FromMemory)2 List (java.util.List)1 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)1 Memory (org.drools.core.common.Memory)1 ProtobufInputMarshaller (org.drools.core.marshalling.impl.ProtobufInputMarshaller)1 ProtobufMessages (org.drools.core.marshalling.impl.ProtobufMessages)1 AccumulateContext (org.drools.core.reteoo.AccumulateNode.AccumulateContext)1 BetaMemory (org.drools.core.reteoo.BetaMemory)1