@SuppressWarnings("unchecked") private InternalFactHandle createFactHandle( final PropagationContext context, final InternalWorkingMemory workingMemory, final LeftTuple leftTuple) { InternalFactHandle handle = null; ProtobufMessages.FactHandle _handle = null; if (context.getReaderContext() != null) { Map<TupleKey, QueryElementContext> map = (Map<TupleKey, QueryElementContext>) context.getReaderContext().nodeMemories.get(getId()); if (map != null) { _handle = map.get(PersisterHelper.createTupleKey(leftTuple)).handle; } } if (_handle != null) { // create a handle with the given id handle = workingMemory .getFactHandleFactory() .newFactHandle( _handle.getId(), null, _handle.getRecency(), null, workingMemory, workingMemory); } else { handle = workingMemory .getFactHandleFactory() .newFactHandle(null, null, workingMemory, workingMemory); } return handle; }
@SuppressWarnings("unchecked") private QueryElementFactHandle createQueryResultHandle( PropagationContext context, InternalWorkingMemory workingMemory, Object[] objects) { QueryElementFactHandle handle = null; ProtobufMessages.FactHandle _handle = null; if (context.getReaderContext() != null) { Map<TupleKey, QueryElementContext> map = (Map<TupleKey, QueryElementContext>) context.getReaderContext().nodeMemories.get(node.getId()); if (map != null) { QueryElementContext _context = map.get(PersisterHelper.createTupleKey(leftTuple)); if (_context != null) { _handle = _context.results.removeFirst(); } } } if (_handle != null) { // create a handle with the given id handle = new QueryElementFactHandle(objects, _handle.getId(), _handle.getRecency()); } else { handle = new QueryElementFactHandle( objects, workingMemory.getFactHandleFactory().getAtomicId().incrementAndGet(), workingMemory.getFactHandleFactory().getAtomicRecency().incrementAndGet()); } return handle; }
/** * Takes the asserted <code>ReteTuple</code> received from the <code>TupleSource</code> and * adapts it into a FactHandleImpl * * @param tuple * The asserted <code>ReteTuple</code>. * @param context * The <code>PropagationContext</code> of the <code>WorkingMemory<code> action. * @param workingMemory * the <code>WorkingMemory</code> session. */ public void assertLeftTuple( final LeftTuple leftTuple, final PropagationContext context, final InternalWorkingMemory workingMemory) { // creating a dummy fact handle to wrap the tuple final InternalFactHandle handle = workingMemory .getFactHandleFactory() .newFactHandle( leftTuple, workingMemory .getObjectTypeConfigurationRegistry() .getObjectTypeConf(context.getEntryPoint(), leftTuple), workingMemory, null); boolean useLeftMemory = true; if (!this.tupleMemoryEnabled) { // This is a hack, to not add closed DroolsQuery objects Object object = ((InternalFactHandle) leftTuple.get(0)).getObject(); if (!(object instanceof DroolsQuery) || !((DroolsQuery) object).isOpen()) { useLeftMemory = false; } } if (useLeftMemory) { final ObjectHashMap memory = (ObjectHashMap) workingMemory.getNodeMemory(this); // add it to a memory mapping memory.put(leftTuple, handle); } // propagate it this.sink.propagateAssertObject(handle, context, workingMemory); }
@SuppressWarnings("unchecked") private InternalFactHandle createResultFactHandle( final PropagationContext context, final InternalWorkingMemory workingMemory, final LeftTuple leftTuple, final Object result) { InternalFactHandle handle = null; ProtobufMessages.FactHandle _handle = null; if (context.getReaderContext() != null) { Map<ProtobufInputMarshaller.TupleKey, ProtobufMessages.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, null); // so far, result is not an event } else { handle = workingMemory .getFactHandleFactory() .newFactHandle( result, workingMemory .getObjectTypeConfigurationRegistry() .getObjectTypeConf(context.getEntryPoint(), result), workingMemory, null); // so far, result is not an event } return handle; }
public void rowUpdated( final Rule rule, final LeftTuple resultLeftTuple, final PropagationContext context, final InternalWorkingMemory workingMemory) { RightTuple rightTuple = (RightTuple) resultLeftTuple.getObject(); if (rightTuple.getMemory() != null) { // Already sheduled as an insert return; } rightTuple.setLeftTuple(null); resultLeftTuple.setObject(null); // We need to recopy everything back again, as we don't know what has or hasn't changed QueryTerminalNode node = (QueryTerminalNode) resultLeftTuple.getLeftTupleSink(); Declaration[] decls = node.getDeclarations(); InternalFactHandle rootHandle = resultLeftTuple.get(0); DroolsQuery query = (DroolsQuery) rootHandle.getObject(); Object[] objects = new Object[query.getElements().length]; Declaration decl; for (int i = 0, length = this.variables.length; i < length; i++) { decl = decls[this.variables[i]]; objects[this.variables[i]] = decl.getValue(workingMemory, resultLeftTuple.get(decl).getObject()); } QueryElementFactHandle handle = (QueryElementFactHandle) rightTuple.getFactHandle(); handle.setRecency(workingMemory.getFactHandleFactory().getAtomicRecency().incrementAndGet()); handle.setObject(objects); if (query.isOpen()) { rightTuple.setLeftTuple(resultLeftTuple); resultLeftTuple.setObject(rightTuple); } // Don't need to recreate child links, as they will already be there form the first "add" RightTupleList rightTuples = query.getResultUpdateRightTupleList(); if (rightTuples == null) { rightTuples = new RightTupleList(); query.setResultUpdateRightTupleList(rightTuples); QueryResultUpdateAction updateAction = new QueryResultUpdateAction(context, this.factHandle, leftTuple, this.node); context.getQueue2().addFirst(updateAction); } rightTuples.add(rightTuple); }
public void propagateRetractLeftTupleDestroyRightTuple( final LeftTuple leftTuple, final PropagationContext context, final InternalWorkingMemory workingMemory) { LeftTuple child = leftTuple.getFirstChild(); InternalFactHandle rightParent = child.getRightParent().getFactHandle(); while (child != null) { LeftTuple temp = child.getLeftParentNext(); doPropagateRetractLeftTuple(context, workingMemory, child, child.getLeftTupleSink()); child.unlinkFromRightParent(); child.unlinkFromLeftParent(); child = temp; } workingMemory.getFactHandleFactory().destroyFactHandle(rightParent); }