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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations