Example #1
0
  public static void dpUpdatesExistentialReorderLeftMemory(
      BetaMemory bm, LeftTupleSets srcLeftTuples) {
    LeftTupleMemory ltm = bm.getLeftTupleMemory();

    // sides must first be re-ordered, to ensure iteration integrity
    for (LeftTuple leftTuple = srcLeftTuples.getUpdateFirst(); leftTuple != null; ) {
      LeftTuple next = leftTuple.getStagedNext();
      if (leftTuple.getMemory() != null) {
        ltm.remove(leftTuple);
      }
      leftTuple = next;
    }

    for (LeftTuple leftTuple = srcLeftTuples.getUpdateFirst(); leftTuple != null; ) {
      LeftTuple next = leftTuple.getStagedNext();
      if (leftTuple.getBlocker() == null) {
        ltm.add(leftTuple);
        for (LeftTuple childLeftTuple = leftTuple.getFirstChild(); childLeftTuple != null; ) {
          LeftTuple childNext = childLeftTuple.getLeftParentNext();
          childLeftTuple.reAddRight();
          childLeftTuple = childNext;
        }
      }
      leftTuple = next;
    }
  }
Example #2
0
  public int evaluateNetwork(PathMemory pmem, InternalWorkingMemory wm, RuleExecutor executor) {
    SegmentMemory[] smems = pmem.getSegmentMemories();

    int smemIndex = 0;
    SegmentMemory smem = smems[smemIndex]; // 0
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) smem.getRootNode();

    NetworkNode node;
    Memory nodeMem;
    if (liaNode == smem.getTipNode()) {
      // segment only has liaNode in it
      // nothing is staged in the liaNode, so skip to next segment
      smem = smems[++smemIndex]; // 1
      node = smem.getRootNode();
      nodeMem = smem.getNodeMemories().getFirst();
    } else {
      // lia is in shared segment, so point to next node
      node = liaNode.getSinkPropagator().getFirstLeftTupleSink();
      nodeMem = smem.getNodeMemories().getFirst().getNext(); // skip the liaNode memory
    }

    LeftTupleSets srcTuples = smem.getStagedLeftTuples();

    if (log.isTraceEnabled()) {
      log.trace(
          "Rule[name={}] segments={} {}",
          ((TerminalNode) pmem.getNetworkNode()).getRule().getName(),
          smems.length,
          srcTuples.toStringSizes());
    }

    Set<String> visitedRules;
    if (((TerminalNode) pmem.getNetworkNode()).getType() == NodeTypeEnums.QueryTerminalNode) {
      visitedRules = new HashSet<String>();
    } else {
      visitedRules = Collections.<String>emptySet();
    }

    LinkedList<StackEntry> stack = new LinkedList<StackEntry>();
    eval1(
        liaNode,
        pmem,
        (LeftTupleSink) node,
        nodeMem,
        smems,
        smemIndex,
        srcTuples,
        wm,
        stack,
        visitedRules,
        true,
        executor);

    return 0;
  }
Example #3
0
  public void doNode(
      NotNode notNode,
      LeftTupleSink sink,
      BetaMemory bm,
      InternalWorkingMemory wm,
      LeftTupleSets srcLeftTuples,
      LeftTupleSets trgLeftTuples,
      LeftTupleSets stagedLeftTuples) {
    RightTupleSets srcRightTuples = bm.getStagedRightTuples().takeAll();

    if (srcLeftTuples.getDeleteFirst() != null) {
      // left deletes must come before right deletes. Otherwise right deletes could
      // stage an insertion, that is later deleted in the rightDelete, causing potential problems
      doLeftDeletes(bm, srcLeftTuples, trgLeftTuples, stagedLeftTuples);
    }

    if (srcLeftTuples.getUpdateFirst() != null) {
      // must happen before right inserts, so it can find left tuples to block.
      RuleNetworkEvaluator.doUpdatesExistentialReorderLeftMemory(bm, srcLeftTuples);
    }

    if (srcRightTuples.getUpdateFirst() != null) {
      RuleNetworkEvaluator.doUpdatesExistentialReorderRightMemory(
          bm, notNode, srcRightTuples); // this also preserves the next rightTuple
    }

    if (srcRightTuples.getInsertFirst() != null) {
      // must come before right updates and inserts, as they might cause insert propagation, while
      // this causes delete propagations, resulting in staging clash.
      doRightInserts(notNode, bm, wm, srcRightTuples, trgLeftTuples, stagedLeftTuples);
    }

    if (srcRightTuples.getUpdateFirst() != null) {
      // must come after rightInserts and before rightDeletes, to avoid staging clash
      doRightUpdates(notNode, sink, bm, wm, srcRightTuples, trgLeftTuples, stagedLeftTuples);
    }

    if (srcRightTuples.getDeleteFirst() != null) {
      // must come after rightUpdates, to avoid staging clash
      doRightDeletes(notNode, sink, bm, wm, srcRightTuples, trgLeftTuples);
    }

    if (srcLeftTuples.getUpdateFirst() != null) {
      doLeftUpdates(notNode, sink, bm, wm, srcLeftTuples, trgLeftTuples, stagedLeftTuples);
    }

    if (srcLeftTuples.getInsertFirst() != null) {
      doLeftInserts(notNode, sink, bm, wm, srcLeftTuples, trgLeftTuples);
    }

    srcRightTuples.resetAll();
    srcLeftTuples.resetAll();
  }
Example #4
0
 private static boolean processStreamTupleEntry(
     TupleEntryQueue tupleQueue, TupleEntry tupleEntry) {
   boolean isNonNormalizedDelete = false;
   if (log.isTraceEnabled()) {
     log.trace(
         "Stream removed entry {} {} size {}",
         System.identityHashCode(tupleQueue),
         tupleEntry,
         tupleQueue.size());
   }
   if (tupleEntry.getLeftTuple() != null) {
     SegmentMemory sm = tupleEntry.getNodeMemory().getSegmentMemory();
     LeftTupleSets tuples = sm.getStagedLeftTuples();
     tupleEntry.getLeftTuple().setPropagationContext(tupleEntry.getPropagationContext());
     switch (tupleEntry.getPropagationType()) {
       case PropagationContext.INSERTION:
       case PropagationContext.RULE_ADDITION:
         tuples.addInsert(tupleEntry.getLeftTuple());
         break;
       case PropagationContext.MODIFICATION:
         tuples.addUpdate(tupleEntry.getLeftTuple());
         break;
       case PropagationContext.DELETION:
       case PropagationContext.EXPIRATION:
       case PropagationContext.RULE_REMOVAL:
         isNonNormalizedDelete = tupleEntry.getLeftTuple().getStagedType() == LeftTuple.NONE;
         tuples.addDelete(tupleEntry.getLeftTuple());
         break;
     }
   } else {
     BetaMemory bm = (BetaMemory) tupleEntry.getNodeMemory();
     tupleEntry.getRightTuple().setPropagationContext(tupleEntry.getPropagationContext());
     switch (tupleEntry.getPropagationType()) {
       case PropagationContext.INSERTION:
       case PropagationContext.RULE_ADDITION:
         bm.getStagedRightTuples().addInsert(tupleEntry.getRightTuple());
         break;
       case PropagationContext.MODIFICATION:
         bm.getStagedRightTuples().addUpdate(tupleEntry.getRightTuple());
         break;
       case PropagationContext.DELETION:
       case PropagationContext.EXPIRATION:
       case PropagationContext.RULE_REMOVAL:
         isNonNormalizedDelete = tupleEntry.getRightTuple().getStagedType() == LeftTuple.NONE;
         bm.getStagedRightTuples().addDelete(tupleEntry.getRightTuple());
         break;
     }
   }
   return isNonNormalizedDelete;
 }
Example #5
0
  public void doLeftDeletes(
      BetaMemory bm,
      LeftTupleSets srcLeftTuples,
      LeftTupleSets trgLeftTuples,
      LeftTupleSets stagedLeftTuples) {
    LeftTupleMemory ltm = bm.getLeftTupleMemory();

    for (LeftTuple leftTuple = srcLeftTuples.getDeleteFirst(); leftTuple != null; ) {
      LeftTuple next = leftTuple.getStagedNext();
      RightTuple blocker = leftTuple.getBlocker();
      if (blocker == null) {
        if (leftTuple.getMemory() != null) {
          // it may have been staged and never actually added
          ltm.remove(leftTuple);
        }

        LeftTuple childLeftTuple = leftTuple.getFirstChild();

        if (childLeftTuple != null) { // NotNode only has one child
          childLeftTuple.setPropagationContext(leftTuple.getPropagationContext());
          RuleNetworkEvaluator.deleteLeftChild(
              childLeftTuple,
              trgLeftTuples,
              stagedLeftTuples); // no need to update pctx, as no right available, and pctx will
                                 // exist on a parent LeftTuple anyway
        }
      } else {
        blocker.removeBlocked(leftTuple);
      }
      leftTuple.clearStaged();
      leftTuple = next;
    }
  }
Example #6
0
  public void doLeftInserts(
      NotNode notNode,
      LeftTupleSink sink,
      BetaMemory bm,
      InternalWorkingMemory wm,
      LeftTupleSets srcLeftTuples,
      LeftTupleSets trgLeftTuples) {
    LeftTupleMemory ltm = bm.getLeftTupleMemory();
    RightTupleMemory rtm = bm.getRightTupleMemory();
    ContextEntry[] contextEntry = bm.getContext();
    BetaConstraints constraints = notNode.getRawConstraints();

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

      FastIterator it = notNode.getRightIterator(rtm);

      boolean useLeftMemory = RuleNetworkEvaluator.useLeftMemory(notNode, leftTuple);

      constraints.updateFromTuple(contextEntry, wm, leftTuple);

      // This method will also remove rightTuples that are from subnetwork where no leftmemory use
      // used
      RuleNetworkEvaluator.findLeftTupleBlocker(
          notNode, rtm, contextEntry, constraints, leftTuple, it, useLeftMemory);

      if (leftTuple.getBlocker() == null) {
        // tuple is not blocked, so add to memory so other fact handles can attempt to match
        if (useLeftMemory) {
          ltm.add(leftTuple);
        }

        trgLeftTuples.addInsert(
            sink.createLeftTuple(
                leftTuple,
                sink,
                leftTuple.getPropagationContext(),
                useLeftMemory)); // use leftTuple pctx here, as no right input caused the trigger
                                 // anway
      }
      leftTuple.clearStaged();
      leftTuple = next;
    }
    constraints.resetTuple(contextEntry);
  }
Example #7
0
  public static LeftTuple deleteRightChild(
      LeftTuple childLeftTuple, LeftTupleSets trgLeftTuples, LeftTupleSets stagedLeftTuples) {
    switch (childLeftTuple.getStagedType()) {
        // handle clash with already staged entries
      case LeftTuple.INSERT:
        stagedLeftTuples.removeInsert(childLeftTuple);
        break;
      case LeftTuple.UPDATE:
        stagedLeftTuples.removeUpdate(childLeftTuple);
        break;
    }

    LeftTuple next = childLeftTuple.getRightParentNext();

    trgLeftTuples.addDelete(childLeftTuple);
    childLeftTuple.unlinkFromRightParent();
    childLeftTuple.unlinkFromLeftParent();

    return next;
  }
Example #8
0
  public void doRightDeletes(
      NotNode notNode,
      LeftTupleSink sink,
      BetaMemory bm,
      InternalWorkingMemory wm,
      RightTupleSets srcRightTuples,
      LeftTupleSets trgLeftTuples) {
    LeftTupleMemory ltm = bm.getLeftTupleMemory();
    RightTupleMemory rtm = bm.getRightTupleMemory();
    ContextEntry[] contextEntry = bm.getContext();
    BetaConstraints constraints = notNode.getRawConstraints();

    for (RightTuple rightTuple = srcRightTuples.getDeleteFirst(); rightTuple != null; ) {
      RightTuple next = rightTuple.getStagedNext();

      FastIterator it = notNode.getRightIterator(rtm);

      // assign now, so we can remove from memory before doing any possible propagations
      boolean useComparisonIndex = rtm.getIndexType().isComparison();
      RightTuple rootBlocker = useComparisonIndex ? null : (RightTuple) it.next(rightTuple);

      if (rightTuple.getMemory() != null) {
        // it may have been staged and never actually added
        rtm.remove(rightTuple);
      }

      if (rightTuple.getBlocked() != null) {
        for (LeftTuple leftTuple = rightTuple.getBlocked(); leftTuple != null; ) {
          LeftTuple temp = leftTuple.getBlockedNext();

          leftTuple.clearBlocker();

          if (leftTuple.getStagedType() == LeftTuple.UPDATE) {
            // ignore, as it will get processed via left iteration. Children cannot be processed
            // twice
            leftTuple = temp;
            continue;
          }

          constraints.updateFromTuple(contextEntry, wm, leftTuple);

          if (useComparisonIndex) {
            rootBlocker = rtm.getFirst(leftTuple, null, it);
          }

          // we know that older tuples have been checked so continue next
          for (RightTuple newBlocker = rootBlocker;
              newBlocker != null;
              newBlocker = (RightTuple) it.next(newBlocker)) {
            if (constraints.isAllowedCachedLeft(contextEntry, newBlocker.getFactHandle())) {
              leftTuple.setBlocker(newBlocker);
              newBlocker.addBlocked(leftTuple);

              break;
            }
          }

          if (leftTuple.getBlocker() == null) {
            // was previous blocked and not in memory, so add
            ltm.add(leftTuple);

            trgLeftTuples.addInsert(
                sink.createLeftTuple(leftTuple, sink, rightTuple.getPropagationContext(), true));
          }

          leftTuple = temp;
        }
      }

      rightTuple.nullBlocked();
      rightTuple.clearStaged();
      rightTuple = next;
    }

    constraints.resetTuple(contextEntry);
  }
Example #9
0
  public void doRightUpdates(
      NotNode notNode,
      LeftTupleSink sink,
      BetaMemory bm,
      InternalWorkingMemory wm,
      RightTupleSets srcRightTuples,
      LeftTupleSets trgLeftTuples,
      LeftTupleSets stagedLeftTuples) {
    LeftTupleMemory ltm = bm.getLeftTupleMemory();
    RightTupleMemory rtm = bm.getRightTupleMemory();
    ContextEntry[] contextEntry = bm.getContext();
    BetaConstraints constraints = notNode.getRawConstraints();

    boolean iterateFromStart =
        notNode.isIndexedUnificationJoin() || rtm.getIndexType().isComparison();

    for (RightTuple rightTuple = srcRightTuples.getUpdateFirst(); rightTuple != null; ) {
      RightTuple next = rightTuple.getStagedNext();
      PropagationContext context = rightTuple.getPropagationContext();

      constraints.updateFromFactHandle(contextEntry, wm, rightTuple.getFactHandle());

      FastIterator leftIt = notNode.getLeftIterator(ltm);
      LeftTuple firstLeftTuple = notNode.getFirstLeftTuple(rightTuple, ltm, context, leftIt);

      LeftTuple firstBlocked = rightTuple.getTempBlocked();

      // first process non-blocked tuples, as we know only those ones are in the left memory.
      for (LeftTuple leftTuple = firstLeftTuple; leftTuple != null; ) {
        // preserve next now, in case we remove this leftTuple
        LeftTuple temp = (LeftTuple) leftIt.next(leftTuple);

        if (leftTuple.getStagedType() == LeftTuple.UPDATE) {
          // ignore, as it will get processed via left iteration. Children cannot be processed twice
          leftTuple = temp;
          continue;
        }

        // we know that only unblocked LeftTuples are  still in the memory
        if (constraints.isAllowedCachedRight(contextEntry, leftTuple)) {
          leftTuple.setBlocker(rightTuple);
          rightTuple.addBlocked(leftTuple);

          // this is now blocked so remove from memory
          ltm.remove(leftTuple);

          LeftTuple childLeftTuple = leftTuple.getFirstChild();
          if (childLeftTuple != null) {
            childLeftTuple.setPropagationContext(rightTuple.getPropagationContext());
            RuleNetworkEvaluator.deleteRightChild(childLeftTuple, trgLeftTuples, stagedLeftTuples);
          }
        }

        leftTuple = temp;
      }

      if (firstBlocked != null) {
        RightTuple rootBlocker = rightTuple.getTempNextRightTuple();
        if (rootBlocker == null) {
          iterateFromStart = true;
        }

        FastIterator rightIt = notNode.getRightIterator(rtm);

        // iterate all the existing previous blocked LeftTuples
        for (LeftTuple leftTuple = firstBlocked; leftTuple != null; ) {
          LeftTuple temp = leftTuple.getBlockedNext();

          leftTuple.clearBlocker();

          if (leftTuple.getStagedType() == LeftTuple.UPDATE) {
            // ignore, as it will get processed via left iteration. Children cannot be processed
            // twice
            // but need to add it back into list first
            leftTuple.setBlocker(rightTuple);
            rightTuple.addBlocked(leftTuple);

            leftTuple = temp;
            continue;
          }

          constraints.updateFromTuple(contextEntry, wm, leftTuple);

          if (iterateFromStart) {
            rootBlocker = notNode.getFirstRightTuple(leftTuple, rtm, null, rightIt);
          }

          // we know that older tuples have been checked so continue next
          for (RightTuple newBlocker = rootBlocker;
              newBlocker != null;
              newBlocker = (RightTuple) rightIt.next(newBlocker)) {
            // cannot select a RightTuple queued in the delete list
            // There may be UPDATE RightTuples too, but that's ok. They've already been re-added to
            // the correct bucket, safe to be reprocessed.
            if (leftTuple.getStagedType() != LeftTuple.DELETE
                && newBlocker.getStagedType() != LeftTuple.DELETE
                && constraints.isAllowedCachedLeft(contextEntry, newBlocker.getFactHandle())) {

              leftTuple.setBlocker(newBlocker);
              newBlocker.addBlocked(leftTuple);

              break;
            }
          }

          if (leftTuple.getBlocker() == null) {
            // was previous blocked and not in memory, so add
            ltm.add(leftTuple);

            // subclasses like ForallNotNode might override this propagation
            trgLeftTuples.addInsert(
                sink.createLeftTuple(leftTuple, sink, rightTuple.getPropagationContext(), true));
          }

          leftTuple = temp;
        }
      }
      rightTuple.clearStaged();
      rightTuple = next;
    }

    constraints.resetFactHandle(contextEntry);
    constraints.resetTuple(contextEntry);
  }
Example #10
0
  public void doLeftUpdates(
      NotNode notNode,
      LeftTupleSink sink,
      BetaMemory bm,
      InternalWorkingMemory wm,
      LeftTupleSets srcLeftTuples,
      LeftTupleSets trgLeftTuples,
      LeftTupleSets stagedLeftTuples) {
    LeftTupleMemory ltm = bm.getLeftTupleMemory();
    RightTupleMemory rtm = bm.getRightTupleMemory();
    ContextEntry[] contextEntry = bm.getContext();
    BetaConstraints constraints = notNode.getRawConstraints();
    boolean leftUpdateOptimizationAllowed = notNode.isLeftUpdateOptimizationAllowed();

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

      FastIterator rightIt = notNode.getRightIterator(rtm);
      RightTuple firstRightTuple = notNode.getFirstRightTuple(leftTuple, rtm, null, rightIt);

      // If in memory, remove it, because we'll need to add it anyway if it's not blocked, to ensure
      // iteration order
      RightTuple blocker = leftTuple.getBlocker();
      if (blocker == null) {
        if (leftTuple.getMemory()
            != null) { // memory can be null, if blocker was deleted in same do loop
          ltm.remove(leftTuple);
        }
      } else {
        // check if we changed bucket
        if (rtm.isIndexed() && !rightIt.isFullIterator()) {
          // if newRightTuple is null, we assume there was a bucket change and that bucket is empty
          if (firstRightTuple == null || firstRightTuple.getMemory() != blocker.getMemory()) {
            blocker.removeBlocked(leftTuple);
            blocker = null;
          }
        }
      }

      constraints.updateFromTuple(contextEntry, wm, leftTuple);

      if (!leftUpdateOptimizationAllowed && blocker != null) {
        blocker.removeBlocked(leftTuple);
        blocker = null;
      }

      // if we where not blocked before (or changed buckets), or the previous blocker no longer
      // blocks, then find the next blocker
      if (blocker == null
          || !constraints.isAllowedCachedLeft(contextEntry, blocker.getFactHandle())) {
        if (blocker != null) {
          // remove previous blocker if it exists, as we know it doesn't block any more
          blocker.removeBlocked(leftTuple);
        }

        // find first blocker, because it's a modify, we need to start from the beginning again
        for (RightTuple newBlocker = firstRightTuple;
            newBlocker != null;
            newBlocker = (RightTuple) rightIt.next(newBlocker)) {
          if (constraints.isAllowedCachedLeft(contextEntry, newBlocker.getFactHandle())) {
            leftTuple.setBlocker(newBlocker);
            newBlocker.addBlocked(leftTuple);

            break;
          }
        }

        LeftTuple childLeftTuple = leftTuple.getFirstChild();

        if (leftTuple.getBlocker() != null) {
          // blocked
          if (childLeftTuple != null) {
            // blocked, with previous children, so must have not been previously blocked, so retract
            // no need to remove, as we removed at the start
            // to be matched against, as it's now blocked
            childLeftTuple.setPropagationContext(
                leftTuple
                    .getBlocker()
                    .getPropagationContext()); // we have the righttuple, so use it for the pctx
            RuleNetworkEvaluator.deleteLeftChild(childLeftTuple, trgLeftTuples, stagedLeftTuples);
          } // else: it's blocked now and no children so blocked before, thus do nothing
        } else if (childLeftTuple == null) {
          // not blocked, with no children, must have been previously blocked so assert
          ltm.add(leftTuple); // add to memory so other fact handles can attempt to match
          trgLeftTuples.addInsert(
              sink.createLeftTuple(
                  leftTuple,
                  sink,
                  leftTuple.getPropagationContext(),
                  true)); // use leftTuple for the pctx here, as the right one is not available
          // this won't cause a problem, as the trigger tuple (to the left) will be more recent
          // anwyay
        } else {
          updateChildLeftTuple(childLeftTuple, stagedLeftTuples, trgLeftTuples);

          // not blocked, with children, so wasn't previous blocked and still isn't so modify
          ltm.add(leftTuple); // add to memory so other fact handles can attempt to match
          childLeftTuple.reAddLeft();
        }
      }
      leftTuple.clearStaged();
      leftTuple = next;
    }
    constraints.resetTuple(contextEntry);
  }
Example #11
0
  private void doRiaNode2(
      InternalWorkingMemory wm,
      LeftTupleSets srcTuples,
      RightInputAdapterNode riaNode,
      LinkedList<StackEntry> stack) {

    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 RightTuple(handle, betaNode);
      leftTuple.setObject(rightTuple);
      rightTuple.setPropagationContext(pctx);
      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 RightTuple(handle, bns[i]);
          rightTuple.setPropagationContext(pctx);
          bms[i].getStagedRightTuples().addInsert(rightTuple);
        }
      }

      leftTuple.clearStaged();
      leftTuple = next;
    }

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

      RightTuple rightTuple = (RightTuple) leftTuple.getObject();
      RightTupleSets rightTuples = bm.getStagedRightTuples();
      switch (rightTuple.getStagedType()) {
        case LeftTuple.INSERT:
          {
            rightTuples.removeInsert(rightTuple);
            break;
          }
        case LeftTuple.UPDATE:
          {
            rightTuples.removeUpdate(rightTuple);
            break;
          }
      }
      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();
          switch (rightTuple.getStagedType()) {
            case LeftTuple.INSERT:
              {
                rightTuples.removeInsert(rightTuple);
                break;
              }
            case LeftTuple.UPDATE:
              {
                rightTuples.removeUpdate(rightTuple);
                break;
              }
          }
          rightTuples.addDelete(rightTuple);
        }
      }

      leftTuple.clearStaged();
      leftTuple = next;
    }

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

      RightTuple rightTuple = (RightTuple) leftTuple.getObject();
      RightTupleSets rightTuples = bm.getStagedRightTuples();
      switch (rightTuple.getStagedType()) {
        case LeftTuple.INSERT:
          {
            rightTuples.removeInsert(rightTuple);
            break;
          }
        case LeftTuple.UPDATE:
          {
            rightTuples.removeUpdate(rightTuple);
            break;
          }
      }
      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();
          switch (rightTuple.getStagedType()) {
            case LeftTuple.INSERT:
              {
                rightTuples.removeInsert(rightTuple);
                break;
              }
            case LeftTuple.UPDATE:
              {
                rightTuples.removeUpdate(rightTuple);
                break;
              }
          }
          rightTuples.addUpdate(rightTuple);
        }
      }

      leftTuple.clearStaged();
      leftTuple = next;
    }

    srcTuples.resetAll();
  }
Example #12
0
  private void doRiaNode(
      InternalWorkingMemory wm,
      LeftInputAdapterNode liaNode,
      PathMemory rmem,
      LeftTupleSets srcTuples,
      BetaNode betaNode,
      LeftTupleSinkNode sink,
      SegmentMemory[] smems,
      int smemIndex,
      Memory nodeMem,
      BetaMemory bm,
      LinkedList<StackEntry> stack,
      Set<String> visitedRules,
      RuleExecutor executor) {
    RiaPathMemory pathMem = bm.getRiaRuleMemory();
    SegmentMemory[] subnetworkSmems = pathMem.getSegmentMemories();
    SegmentMemory subSmem = null;
    for (int i = 0; subSmem == null; i++) {
      // segment positions outside of the subnetwork, in the parent chain, are null
      // so we must iterate to find the first non null segment memory
      subSmem = subnetworkSmems[i];
    }

    //        if (betaNode.getLeftTupleSource().getSinkPropagator().size() == 2) {
    //            // sub network is not part of  share split, so need to handle propagation
    //            // this ensures the first LeftTuple is actually the subnetwork node
    //            // and the main outer network now receives the peer, notice the swap at the end
    // "srcTuples == peerTuples"
    //            LeftTupleSets peerTuples = new LeftTupleSets();
    //            SegmentPropagator.processPeers(srcTuples, peerTuples, betaNode);
    //            // Make sure subnetwork Segment has tuples to process
    //            LeftTupleSets subnetworkStaged = subSmem.getStagedLeftTuples();
    //            subnetworkStaged.addAll(srcTuples);
    //
    //            srcTuples.resetAll();
    //
    //            srcTuples = peerTuples;
    //        }

    // Resume the node after the riaNode segment has been processed and the right input memory
    // populated
    StackEntry stackEntry =
        new StackEntry(
            liaNode,
            betaNode,
            sink,
            rmem,
            nodeMem,
            smems,
            smemIndex,
            srcTuples,
            visitedRules,
            false);
    stack.add(stackEntry);
    if (log.isTraceEnabled()) {
      int offset = getOffset(betaNode);
      log.trace(
          "{} RiaQueue {} {}", indent(offset), betaNode.toString(), srcTuples.toStringSizes());
    }

    //        RightInputAdapterNode riaNode = ( RightInputAdapterNode ) betaNode.getRightInput();
    // RiaNodeMemory riaNodeMemory = (RiaNodeMemory) wm.getNodeMemory((MemoryFactory)
    // betaNode.getRightInput());
    // LeftTupleSets riaStagedTuples =
    eval2(
        liaNode,
        pathMem,
        (LeftTupleSink) subSmem.getRootNode(),
        subSmem.getNodeMemories().getFirst(),
        subnetworkSmems,
        subSmem.getPos(),
        subSmem.getStagedLeftTuples(),
        wm,
        stack,
        visitedRules,
        true,
        executor);
  }
Example #13
0
  public void eval2(
      LeftInputAdapterNode liaNode,
      PathMemory rmem,
      NetworkNode node,
      Memory nodeMem,
      SegmentMemory[] smems,
      int smemIndex,
      LeftTupleSets trgTuples,
      InternalWorkingMemory wm,
      LinkedList<StackEntry> stack,
      Set<String> visitedRules,
      boolean processRian,
      RuleExecutor executor) {
    LeftTupleSets srcTuples;
    SegmentMemory smem = smems[smemIndex];
    while (true) {
      srcTuples = trgTuples; // previous target, is now the source
      if (log.isTraceEnabled()) {
        int offset = getOffset(node);
        log.trace(
            "{} {} {} {}", indent(offset), ++cycle, node.toString(), srcTuples.toStringSizes());
      }

      if (NodeTypeEnums.isTerminalNode(node)) {
        TerminalNode rtn = (TerminalNode) node;
        if (node.getType() == NodeTypeEnums.QueryTerminalNode) {
          pQtNode.doNode((QueryTerminalNode) rtn, wm, srcTuples, stack);
        } else {
          pRtNode.doNode(rtn, wm, srcTuples, executor);
        }
        return;
      } else if (NodeTypeEnums.RightInputAdaterNode == node.getType()) {
        doRiaNode2(wm, srcTuples, (RightInputAdapterNode) node, stack);
        return;
      }

      LeftTupleSets stagedLeftTuples;
      if (node == smem.getTipNode() && smem.getFirst() != null) {
        // we are about to process the segment tip, allow it to merge insert/update/delete clashes
        // Can happen if the next segments have not yet been initialized
        stagedLeftTuples = smem.getFirst().getStagedLeftTuples();
      } else {
        stagedLeftTuples = null;
      }

      LeftTupleSinkNode sink = ((LeftTupleSource) node).getSinkPropagator().getFirstLeftTupleSink();

      trgTuples = new LeftTupleSets();

      if (NodeTypeEnums.isBetaNode(node)) {
        BetaNode betaNode = (BetaNode) node;

        BetaMemory bm = null;
        AccumulateMemory am = null;
        if (NodeTypeEnums.AccumulateNode == node.getType()) {
          am = (AccumulateMemory) nodeMem;
          bm = am.getBetaMemory();
        } else {
          bm = (BetaMemory) nodeMem;
        }

        if (processRian && betaNode.isRightInputIsRiaNode()) {
          // if the subnetwork is nested in this segment, it will create srcTuples containing
          // peer LeftTuples, suitable for the node in the main path.
          doRiaNode(
              wm,
              liaNode,
              rmem,
              srcTuples,
              betaNode,
              sink,
              smems,
              smemIndex,
              nodeMem,
              bm,
              stack,
              visitedRules,
              executor);
          return; // return here is doRiaNode queues the evaluation on the stack, which is necessary
          // to handled nested query nodes
        }

        if (!bm.getDequeu().isEmpty()) {
          // If there are no staged RightTuples, then process the Dequeue, popping entries, until
          // another insert/expiration clash
          RightTupleSets rightTuples = bm.getStagedRightTuples();
          if (rightTuples.isEmpty()) {
            // nothing staged, so now process the Dequeu
            Deque<RightTuple> que = bm.getDequeu();
            while (!que.isEmpty()) {
              RightTuple rightTuple = que.peekFirst();
              if (rightTuple.getPropagationContext().getType() == PropagationContext.EXPIRATION
                  &&
                  // Cannot pop an expired fact, if the insert/update has not yet been evaluated.
                  rightTuple.getStagedType() != LeftTuple.NONE) {
                break;
              }

              switch (rightTuple.getPropagationContext().getType()) {
                case PropagationContext.INSERTION:
                case PropagationContext.RULE_ADDITION:
                  rightTuples.addInsert(rightTuple);
                  break;
                case PropagationContext.MODIFICATION:
                  rightTuples.addUpdate(rightTuple);
                  break;
                case PropagationContext.DELETION:
                case PropagationContext.EXPIRATION:
                case PropagationContext.RULE_REMOVAL:
                  rightTuples.addDelete(rightTuple);
                  break;
              }
              que.removeFirst();
            }
          }

          if (!bm.getDequeu().isEmpty()) {
            // The DeQue is not empty, add StackEntry for reprocessing.
            StackEntry stackEntry =
                new StackEntry(
                    liaNode,
                    node,
                    sink,
                    rmem,
                    nodeMem,
                    smems,
                    smemIndex,
                    trgTuples,
                    visitedRules,
                    false);
            stack.add(stackEntry);
          }
        }

        switch (node.getType()) {
          case NodeTypeEnums.JoinNode:
            {
              pJoinNode.doNode(
                  (JoinNode) node, sink, bm, wm, srcTuples, trgTuples, stagedLeftTuples);
              break;
            }
          case NodeTypeEnums.NotNode:
            {
              pNotNode.doNode((NotNode) node, sink, bm, wm, srcTuples, trgTuples, stagedLeftTuples);
              break;
            }
          case NodeTypeEnums.ExistsNode:
            {
              pExistsNode.doNode(
                  (ExistsNode) node, sink, bm, wm, srcTuples, trgTuples, stagedLeftTuples);
              break;
            }
          case NodeTypeEnums.AccumulateNode:
            {
              pAccNode.doNode(
                  (AccumulateNode) node, sink, am, wm, srcTuples, trgTuples, stagedLeftTuples);
              break;
            }
        }
      } else {
        switch (node.getType()) {
          case NodeTypeEnums.EvalConditionNode:
            {
              pEvalNode.doNode(
                  (EvalConditionNode) node,
                  (EvalMemory) nodeMem,
                  sink,
                  wm,
                  srcTuples,
                  trgTuples,
                  stagedLeftTuples);
              break;
            }
          case NodeTypeEnums.FromNode:
            {
              pFromNode.doNode(
                  (FromNode) node,
                  (FromMemory) nodeMem,
                  sink,
                  wm,
                  srcTuples,
                  trgTuples,
                  stagedLeftTuples);
              break;
            }
          case NodeTypeEnums.QueryElementNode:
            {
              QueryElementNodeMemory qmem = (QueryElementNodeMemory) nodeMem;

              if (srcTuples.isEmpty() && qmem.getResultLeftTuples().isEmpty()) {
                // no point in evaluating query element, and setting up stack, if there is nothing
                // to process
                break;
              }

              QueryElementNode qnode = (QueryElementNode) node;
              if (visitedRules == Collections.<String>emptySet()) {
                visitedRules = new HashSet<String>();
              }
              visitedRules.add(qnode.getQueryElement().getQueryName());

              // result tuples can happen when reactivity occurs inside of the query, prior to
              // evaluation
              // we will need special behaviour to add the results again, when this query result
              // resumes
              trgTuples.addAll(qmem.getResultLeftTuples());

              if (!srcTuples.isEmpty()) {
                // only process the Query Node if there are src tuples
                StackEntry stackEntry =
                    new StackEntry(
                        liaNode,
                        node,
                        sink,
                        rmem,
                        nodeMem,
                        smems,
                        smemIndex,
                        trgTuples,
                        visitedRules,
                        true);

                stack.add(stackEntry);

                pQueryNode.doNode(
                    qnode, (QueryElementNodeMemory) nodeMem, stackEntry, sink, wm, srcTuples);

                SegmentMemory qsmem = ((QueryElementNodeMemory) nodeMem).getQuerySegmentMemory();
                List<PathMemory> qrmems = qsmem.getPathMemories();

                // Build the evaluation information for each 'or' branch
                // Exception fo the last, place each entry on the stack, the last one evaluate now.
                for (int i = qrmems.size() - 1; i >= 0; i--) {
                  PathMemory qrmem = qrmems.get(i);

                  rmem = qrmem;
                  smems = qrmem.getSegmentMemories();
                  smemIndex = 0;
                  smem = smems[smemIndex]; // 0
                  liaNode = (LeftInputAdapterNode) smem.getRootNode();

                  if (liaNode == smem.getTipNode()) {
                    // segment only has liaNode in it
                    // nothing is staged in the liaNode, so skip to next segment
                    smem = smems[++smemIndex]; // 1
                    node = smem.getRootNode();
                    nodeMem = smem.getNodeMemories().getFirst();
                  } else {
                    // lia is in shared segment, so point to next node
                    node = liaNode.getSinkPropagator().getFirstLeftTupleSink();
                    nodeMem =
                        smem.getNodeMemories().getFirst().getNext(); // skip the liaNode memory
                  }

                  trgTuples = smem.getStagedLeftTuples();

                  if (i != 0 && !trgTuples.isEmpty()) {
                    // All entries except the last should be placed on the stack for evaluation
                    // later.
                    stackEntry =
                        new StackEntry(
                            liaNode,
                            node,
                            null,
                            rmem,
                            nodeMem,
                            smems,
                            smemIndex,
                            trgTuples,
                            visitedRules,
                            false);
                    if (log.isTraceEnabled()) {
                      int offset = getOffset(stackEntry.getNode());
                      log.trace(
                          "{} ORQueue branch={} {} {}",
                          indent(offset),
                          i,
                          stackEntry.getNode().toString(),
                          trgTuples.toStringSizes());
                    }
                    stack.add(stackEntry);
                  }
                }
                processRian = true; //  make sure it's reset, so ria nodes are processed
                continue;
              }
              break;
            }
          case NodeTypeEnums.ConditionalBranchNode:
            {
              pBranchNode.doNode(
                  (ConditionalBranchNode) node,
                  (ConditionalBranchMemory) nodeMem,
                  sink,
                  wm,
                  srcTuples,
                  trgTuples,
                  stagedLeftTuples,
                  executor);
              break;
            }
        }
      }

      if (node != smem.getTipNode()) {
        // get next node and node memory in the segment
        node = sink;
        nodeMem = nodeMem.getNext();
      } else {
        // Reached end of segment, start on new segment.
        SegmentPropagator.propagate(smem, trgTuples, wm);
        smem = smems[++smemIndex];
        trgTuples = smem.getStagedLeftTuples();
        if (log.isTraceEnabled()) {
          log.trace("Segment {}", smemIndex);
        }
        node = (LeftTupleSink) smem.getRootNode();
        nodeMem = smem.getNodeMemories().getFirst();
      }
      processRian = true; //  make sure it's reset, so ria nodes are processed
    }
  }
Example #14
0
  public void eval1(
      LeftInputAdapterNode liaNode,
      PathMemory rmem,
      NetworkNode node,
      Memory nodeMem,
      SegmentMemory[] smems,
      int smemIndex,
      LeftTupleSets trgTuples,
      InternalWorkingMemory wm,
      LinkedList<StackEntry> stack,
      Set<String> visitedRules,
      boolean processRian,
      RuleExecutor executor) {
    while (true) {
      eval2(
          liaNode,
          rmem,
          node,
          nodeMem,
          smems,
          smemIndex,
          trgTuples,
          wm,
          stack,
          visitedRules,
          processRian,
          executor);

      // eval
      if (!stack.isEmpty()) {
        StackEntry entry = stack.removeLast();

        node = entry.getNode();
        nodeMem = entry.getNodeMem();
        trgTuples = entry.getTrgTuples();
        if (node.getType() == NodeTypeEnums.QueryElementNode) {
          // copy across the results, if any from the query node memory
          trgTuples.addAll(((QueryElementNodeMemory) nodeMem).getResultLeftTuples());
        }

        LeftTupleSinkNode sink = entry.getSink();
        rmem = entry.getRmem();

        smems = entry.getSmems();
        smemIndex = entry.getSmemIndex();
        visitedRules = entry.getVisitedRules();
        if (NodeTypeEnums.isBetaNode(node)) {
          // queued beta nodes do not want their ria node evaluated, otherwise there is recursion
          processRian = false;
        } else {
          processRian = true;
        }

        if (entry.isResumeFromNextNode()) {
          SegmentMemory smem = smems[smemIndex];
          if (node != smem.getTipNode()) {
            // get next node and node memory in the segment
            LeftTupleSink nextSink = sink.getNextLeftTupleSinkNode();
            if (nextSink == null) {
              node = sink;
            } else {
              // there is a nested subnetwork, take out path
              node = nextSink;
            }

            nodeMem = nodeMem.getNext();
          } else {
            // Reached end of segment, start on new segment.
            SegmentPropagator.propagate(smem, trgTuples, wm);
            smem = smems[++smemIndex];
            trgTuples = smem.getStagedLeftTuples();
            node = (LeftTupleSink) smem.getRootNode();
            nodeMem = smem.getNodeMemories().getFirst();
          }
        }

        if (log.isTraceEnabled()) {
          int offset = getOffset(node);
          log.trace("{} Resume {} {}", indent(offset), node.toString(), trgTuples.toStringSizes());
        }
      } else {
        return; // stack is empty return;
      }
    }
  }