public void modifyLeftTuple( LeftTuple leftTuple, PropagationContext context, InternalWorkingMemory workingMemory) { // add it to a memory mapping InternalFactHandle handle = (InternalFactHandle) leftTuple.getObject(); // propagate it for (RightTuple rightTuple = handle.getFirstRightTuple(); rightTuple != null; rightTuple = rightTuple.getHandleNext()) { rightTuple.getRightTupleSink().modifyRightTuple(rightTuple, context, workingMemory); } }
public void modifyActivation( final InternalFactHandle factHandle, final PropagationContext context, final InternalWorkingMemory workingMemory) { if (activationNode == null) { this.activationNode = objectTypeNodes.get(ClassObjectType.Match_ObjectType); } if (activationNode != null) { ModifyPreviousTuples modifyPreviousTuples = new ModifyPreviousTuples( factHandle.getFirstLeftTuple(), factHandle.getFirstRightTuple(), unlinkingEnabled); factHandle.clearLeftTuples(); factHandle.clearRightTuples(); // There may be no queries defined this.activationNode.modifyObject(factHandle, modifyPreviousTuples, context, workingMemory); modifyPreviousTuples.retractTuples(context, workingMemory); } }
/** Retracts the corresponding tuple by retrieving and retracting the fact created for it */ public void retractLeftTuple( final LeftTuple tuple, final PropagationContext context, final InternalWorkingMemory workingMemory) { // retrieve handle from memory final InternalFactHandle factHandle = (InternalFactHandle) tuple.getObject(); for (RightTuple rightTuple = factHandle.getFirstRightTuple(); rightTuple != null; rightTuple = rightTuple.getHandleNext()) { rightTuple.getRightTupleSink().retractRightTuple(rightTuple, context, workingMemory); } factHandle.clearRightTuples(); for (LeftTuple leftTuple = factHandle.getLastLeftTuple(); leftTuple != null; leftTuple = leftTuple.getLeftParentNext()) { leftTuple.getLeftTupleSink().retractLeftTuple(leftTuple, context, workingMemory); } factHandle.clearLeftTuples(); }
public void modifyObject( final InternalFactHandle handle, final PropagationContext context, final ObjectTypeConf objectTypeConf, final InternalWorkingMemory wm) { if (log.isTraceEnabled()) { log.trace("Update {}", handle.toString()); } // checks if shadow is enabled if (objectTypeConf.isShadowEnabled()) { // the user has implemented the ShadowProxy interface, let their implementation // know it is safe to update the information the engine can see. ((ShadowProxy) handle.getObject()).updateProxy(); } ObjectTypeNode[] cachedNodes = objectTypeConf.getObjectTypeNodes(); // make a reference to the previous tuples, then null then on the handle ModifyPreviousTuples modifyPreviousTuples = new ModifyPreviousTuples( handle.getFirstLeftTuple(), handle.getFirstRightTuple(), unlinkingEnabled); handle.clearLeftTuples(); handle.clearRightTuples(); for (int i = 0, length = cachedNodes.length; i < length; i++) { cachedNodes[i].modifyObject(handle, modifyPreviousTuples, context, wm); // remove any right tuples that matches the current OTN before continue the modify on the next // OTN cache entry if (i < cachedNodes.length - 1) { RightTuple rightTuple = modifyPreviousTuples.peekRightTuple(); while (rightTuple != null && ((BetaNode) rightTuple.getRightTupleSink()).getObjectTypeNode() == cachedNodes[i]) { modifyPreviousTuples.removeRightTuple(); if (unlinkingEnabled) { BetaMemory bm = BetaNode.getBetaMemory((BetaNode) rightTuple.getRightTupleSink(), wm); BetaNode.doDeleteRightTuple(rightTuple, wm, bm); } else { ((BetaNode) rightTuple.getRightTupleSink()).retractRightTuple(rightTuple, context, wm); } rightTuple = modifyPreviousTuples.peekRightTuple(); } LeftTuple leftTuple; ObjectTypeNode otn; while (true) { leftTuple = modifyPreviousTuples.peekLeftTuple(); otn = null; if (leftTuple != null) { LeftTupleSink leftTupleSink = leftTuple.getLeftTupleSink(); if (leftTupleSink instanceof LeftTupleSource) { otn = ((LeftTupleSource) leftTupleSink).getLeftTupleSource().getObjectTypeNode(); } else if (leftTupleSink instanceof RuleTerminalNode) { otn = ((RuleTerminalNode) leftTupleSink).getObjectTypeNode(); } } if (otn == null || otn == cachedNodes[i + 1]) break; modifyPreviousTuples.removeLeftTuple(); if (unlinkingEnabled) { LeftInputAdapterNode liaNode = (LeftInputAdapterNode) leftTuple.getLeftTupleSink().getLeftTupleSource(); LiaNodeMemory lm = (LiaNodeMemory) wm.getNodeMemory(liaNode); LeftInputAdapterNode.doDeleteObject( leftTuple, context, lm.getSegmentMemory(), wm, liaNode, true, lm); } else { leftTuple.getLeftTupleSink().retractLeftTuple(leftTuple, context, wm); } } } } modifyPreviousTuples.retractTuples(context, wm); }
private void doRiaNode2( InternalWorkingMemory wm, TupleSets<LeftTuple> srcTuples, RightInputAdapterNode riaNode) { ObjectSink[] sinks = riaNode.getSinkPropagator().getSinks(); BetaNode betaNode = (BetaNode) sinks[0]; BetaMemory bm; Memory nodeMem = wm.getNodeMemory(betaNode); if (NodeTypeEnums.AccumulateNode == betaNode.getType()) { bm = ((AccumulateMemory) nodeMem).getBetaMemory(); } else { bm = (BetaMemory) nodeMem; } // Build up iteration array for other sinks BetaNode[] bns = null; BetaMemory[] bms = null; int length = sinks.length; if (length > 1) { bns = new BetaNode[sinks.length - 1]; bms = new BetaMemory[sinks.length - 1]; for (int i = 1; i < length; i++) { bns[i - 1] = (BetaNode) sinks[i]; Memory nodeMem2 = wm.getNodeMemory(bns[i - 1]); if (NodeTypeEnums.AccumulateNode == betaNode.getType()) { bms[i - 1] = ((AccumulateMemory) nodeMem2).getBetaMemory(); } else { bms[i - 1] = (BetaMemory) nodeMem2; } } } length--; // subtract one, as first is not in the array; for (LeftTuple leftTuple = srcTuples.getInsertFirst(); leftTuple != null; ) { LeftTuple next = leftTuple.getStagedNext(); PropagationContext pctx = leftTuple.getPropagationContext(); InternalFactHandle handle = riaNode.createFactHandle(leftTuple, pctx, wm); RightTuple rightTuple = new RightTupleImpl(handle, betaNode); leftTuple.setContextObject(handle); rightTuple.setPropagationContext(pctx); if (bm.getStagedRightTuples().isEmpty()) { bm.setNodeDirtyWithoutNotify(); } bm.getStagedRightTuples().addInsert(rightTuple); if (bns != null) { // Add peered RightTuples, they are attached to FH - unlink LeftTuples that has a peer ref for (int i = 0; i < length; i++) { rightTuple = new RightTupleImpl(handle, bns[i]); rightTuple.setPropagationContext(pctx); if (bms[i].getStagedRightTuples().isEmpty()) { bms[i].setNodeDirtyWithoutNotify(); } bms[i].getStagedRightTuples().addInsert(rightTuple); } } leftTuple.clearStaged(); leftTuple = next; } for (LeftTuple leftTuple = srcTuples.getDeleteFirst(); leftTuple != null; ) { LeftTuple next = leftTuple.getStagedNext(); InternalFactHandle handle = (InternalFactHandle) leftTuple.getContextObject(); RightTuple rightTuple = handle.getFirstRightTuple(); TupleSets<RightTuple> rightTuples = bm.getStagedRightTuples(); if (rightTuples.isEmpty()) { bm.setNodeDirtyWithoutNotify(); } rightTuples.addDelete(rightTuple); if (bns != null) { // Add peered RightTuples, they are attached to FH - unlink LeftTuples that has a peer ref for (int i = 0; i < length; i++) { rightTuple = rightTuple.getHandleNext(); rightTuples = bms[i].getStagedRightTuples(); if (rightTuples.isEmpty()) { bms[i].setNodeDirtyWithoutNotify(); } rightTuples.addDelete(rightTuple); } } leftTuple.clearStaged(); leftTuple = next; } for (LeftTuple leftTuple = srcTuples.getUpdateFirst(); leftTuple != null; ) { LeftTuple next = leftTuple.getStagedNext(); InternalFactHandle handle = (InternalFactHandle) leftTuple.getContextObject(); RightTuple rightTuple = handle.getFirstRightTuple(); TupleSets<RightTuple> rightTuples = bm.getStagedRightTuples(); if (rightTuples.isEmpty()) { bm.setNodeDirtyWithoutNotify(); } rightTuples.addUpdate(rightTuple); if (bns != null) { // Add peered RightTuples, they are attached to FH - unlink LeftTuples that has a peer ref for (int i = 0; i < length; i++) { rightTuple = rightTuple.getHandleNext(); rightTuples = bms[i].getStagedRightTuples(); if (rightTuples.isEmpty()) { bms[i].setNodeDirtyWithoutNotify(); } rightTuples.addUpdate(rightTuple); } } leftTuple.clearStaged(); leftTuple = next; } srcTuples.resetAll(); }