Пример #1
0
 public static void unlinkCreatedHandles(final LeftTuple leftTuple) {
   Map<Object, RightTuple> matches = (Map<Object, RightTuple>) leftTuple.getContextObject();
   FastIterator rightIt = LinkedList.fastIterator;
   for (RightTuple rightTuple : matches.values()) {
     for (RightTuple current = rightTuple; current != null; ) {
       RightTuple next = (RightTuple) rightIt.next(current);
       current.unlinkFromRightParent();
       current = next;
     }
   }
 }
Пример #2
0
  public void doLeftDeletes(
      FromMemory fm,
      TupleSets<LeftTuple> srcLeftTuples,
      TupleSets<LeftTuple> trgLeftTuples,
      TupleSets<LeftTuple> stagedLeftTuples) {
    BetaMemory bm = fm.getBetaMemory();
    TupleMemory ltm = bm.getLeftTupleMemory();

    for (LeftTuple leftTuple = srcLeftTuples.getDeleteFirst(); leftTuple != null; ) {
      LeftTuple next = leftTuple.getStagedNext();

      ltm.remove(leftTuple);

      Map<Object, RightTuple> matches = (Map<Object, RightTuple>) leftTuple.getContextObject();

      if (leftTuple.getFirstChild() != null) {
        LeftTuple childLeftTuple = leftTuple.getFirstChild();

        while (childLeftTuple != null) {
          childLeftTuple.setPropagationContext(leftTuple.getPropagationContext());
          LeftTuple nextChild = childLeftTuple.getLeftParentNext();
          RuleNetworkEvaluator.unlinkAndDeleteChildLeftTuple(
              childLeftTuple, trgLeftTuples, stagedLeftTuples);
          childLeftTuple = nextChild;
        }
      }

      // if matches == null, the deletion might be happening before the fact was even propagated.
      // See BZ-1019473 for details.
      if (matches != null) {

        // @TODO (mdp) is this really necessary? won't the entire FH and RightTuple chaines just et
        // GC'd?
        unlinkCreatedHandles(leftTuple);
      }

      leftTuple.clearStaged();
      leftTuple = next;
    }
  }
Пример #3
0
  public void doLeftUpdates(
      FromNode fromNode,
      FromMemory fm,
      LeftTupleSink sink,
      InternalWorkingMemory wm,
      TupleSets<LeftTuple> srcLeftTuples,
      TupleSets<LeftTuple> trgLeftTuples,
      TupleSets<LeftTuple> stagedLeftTuples) {
    BetaMemory bm = fm.getBetaMemory();
    ContextEntry[] context = bm.getContext();
    BetaConstraints betaConstraints = fromNode.getBetaConstraints();
    AlphaNodeFieldConstraint[] alphaConstraints = fromNode.getAlphaConstraints();
    DataProvider dataProvider = fromNode.getDataProvider();
    Class<?> resultClass = fromNode.getResultClass();

    for (LeftTuple leftTuple = srcLeftTuples.getUpdateFirst(); leftTuple != null; ) {
      LeftTuple next = leftTuple.getStagedNext();

      PropagationContext propagationContext = leftTuple.getPropagationContext();

      final Map<Object, RightTuple> previousMatches =
          (Map<Object, RightTuple>) leftTuple.getContextObject();
      final Map<Object, RightTuple> newMatches = new HashMap<Object, RightTuple>();
      leftTuple.setContextObject(newMatches);

      betaConstraints.updateFromTuple(context, wm, leftTuple);

      FastIterator rightIt = LinkedList.fastIterator;
      for (final java.util.Iterator<?> it =
              dataProvider.getResults(leftTuple, wm, propagationContext, fm.providerContext);
          it.hasNext(); ) {
        final Object object = it.next();
        if ((object == null) || !resultClass.isAssignableFrom(object.getClass())) {
          continue; // skip anything if it not assignable
        }

        RightTuple rightTuple = previousMatches.remove(object);

        if (rightTuple == null) {
          // new match, propagate assert
          rightTuple = fromNode.createRightTuple(leftTuple, propagationContext, wm, object);
        } else {
          // previous match, so reevaluate and propagate modify
          if (rightIt.next(rightTuple) != null) {
            // handle the odd case where more than one object has the same hashcode/equals value
            previousMatches.put(object, (RightTuple) rightIt.next(rightTuple));
            rightTuple.setNext(null);
          }
        }

        checkConstraintsAndPropagate(
            sink,
            leftTuple,
            rightTuple,
            alphaConstraints,
            betaConstraints,
            propagationContext,
            wm,
            fm,
            context,
            true,
            trgLeftTuples,
            null);

        fromNode.addToCreatedHandlesMap(newMatches, rightTuple);
      }

      for (RightTuple rightTuple : previousMatches.values()) {
        for (RightTuple current = rightTuple;
            current != null;
            current = (RightTuple) rightIt.next(current)) {
          deleteChildLeftTuple(
              propagationContext, trgLeftTuples, stagedLeftTuples, current.getFirstChild());
        }
      }

      leftTuple.clearStaged();
      leftTuple = next;
    }
    betaConstraints.resetTuple(context);
  }
Пример #4
0
  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();
  }