Exemplo n.º 1
0
 public static void checkConstraintsAndPropagate(
     final LeftTupleSink sink,
     final LeftTuple leftTuple,
     final RightTuple rightTuple,
     final AlphaNodeFieldConstraint[] alphaConstraints,
     final BetaConstraints betaConstraints,
     final PropagationContext propagationContext,
     final InternalWorkingMemory wm,
     final FromMemory fm,
     final ContextEntry[] context,
     final boolean useLeftMemory,
     TupleSets<LeftTuple> trgLeftTuples,
     TupleSets<LeftTuple> stagedLeftTuples) {
   if (isAllowed(rightTuple.getFactHandle(), alphaConstraints, wm, fm)) {
     propagate(
         sink,
         leftTuple,
         rightTuple,
         betaConstraints,
         propagationContext,
         context,
         useLeftMemory,
         trgLeftTuples,
         stagedLeftTuples);
   }
 }
Exemplo n.º 2
0
  public static void propagate(
      LeftTupleSink sink,
      Tuple leftTuple,
      RightTuple rightTuple,
      BetaConstraints betaConstraints,
      PropagationContext propagationContext,
      ContextEntry[] context,
      boolean useLeftMemory,
      TupleSets<LeftTuple> trgLeftTuples,
      TupleSets<LeftTuple> stagedLeftTuples) {
    if (betaConstraints.isAllowedCachedLeft(context, rightTuple.getFactHandle())) {

      if (rightTuple.getFirstChild() == null) {
        // this is a new match, so propagate as assert
        LeftTuple childLeftTuple =
            sink.createLeftTuple(
                (LeftTuple) leftTuple, rightTuple, null, null, sink, useLeftMemory);
        childLeftTuple.setPropagationContext(propagationContext);
        trgLeftTuples.addInsert(childLeftTuple);
      } else {
        LeftTuple childLeftTuple = rightTuple.getFirstChild();
        childLeftTuple.setPropagationContext(propagationContext);
        updateChildLeftTuple(childLeftTuple, stagedLeftTuples, trgLeftTuples);
      }
    } else {
      deleteChildLeftTuple(
          propagationContext, trgLeftTuples, stagedLeftTuples, rightTuple.getFirstChild());
    }
  }
  public void updateSink(
      final ObjectSink sink,
      final PropagationContext context,
      final InternalWorkingMemory workingMemory) {
    BetaNode betaNode = (BetaNode) this.sink.getSinks()[0];

    Memory betaMemory = workingMemory.getNodeMemory(betaNode);
    BetaMemory bm;
    if (betaNode.getType() == NodeTypeEnums.AccumulateNode) {
      bm = ((AccumulateMemory) betaMemory).getBetaMemory();
    } else {
      bm = (BetaMemory) betaMemory;
    }

    // for RIA nodes, we need to store the ID of the created handles
    bm.getRightTupleMemory().iterator();
    if (bm.getRightTupleMemory().size() > 0) {
      final org.drools.core.util.Iterator it = bm.getRightTupleMemory().iterator();
      for (RightTuple entry = (RightTuple) it.next();
          entry != null;
          entry = (RightTuple) it.next()) {
        LeftTuple leftTuple = (LeftTuple) entry.getFactHandle().getObject();
        InternalFactHandle handle = (InternalFactHandle) leftTuple.getObject();
        sink.assertObject(handle, context, workingMemory);
      }
    }
  }
Exemplo n.º 4
0
  public static void findLeftTupleBlocker(
      BetaNode betaNode,
      TupleMemory rtm,
      ContextEntry[] contextEntry,
      BetaConstraints constraints,
      LeftTuple leftTuple,
      FastIterator it,
      boolean useLeftMemory) {
    // This method will also remove rightTuples that are from subnetwork where no leftmemory use
    // used

    for (RightTuple rightTuple = betaNode.getFirstRightTuple(leftTuple, rtm, null, it);
        rightTuple != null; ) {
      RightTuple nextRight = (RightTuple) it.next(rightTuple);
      if (constraints.isAllowedCachedLeft(contextEntry, rightTuple.getFactHandle())) {
        leftTuple.setBlocker(rightTuple);

        if (useLeftMemory) {
          rightTuple.addBlocked(leftTuple);
          break;
        } else if (betaNode.isRightInputIsRiaNode()) {
          // If we aren't using leftMemory and the right input is a RIAN, then we must iterate and
          // find all subetwork right tuples and remove them
          // so we don't break
          rtm.remove(rightTuple);
        } else {
          break;
        }
      }
      rightTuple = nextRight;
    }
  }
Exemplo n.º 5
0
  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.getObject();
      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.getLeftParentNext();
        }
      }
      _query.addContext(_context.build());
    }

    return _query.getContextCount() > 0
        ? ProtobufMessages.NodeMemory.newBuilder()
            .setNodeId(nodeId)
            .setNodeType(ProtobufMessages.NodeMemory.NodeType.QUERY_ELEMENT)
            .setQueryElement(_query.build())
            .build()
        : null;
  }
Exemplo n.º 6
0
  private static ProtobufMessages.NodeMemory writeRIANodeMemory(
      final int nodeId,
      final MarshallerWriteContext context,
      final BaseNode node,
      final NodeMemories memories,
      final Memory memory) {
    RightInputAdapterNode riaNode = (RightInputAdapterNode) node;

    ObjectSink[] sinks = riaNode.getSinkPropagator().getSinks();
    BetaNode betaNode = (BetaNode) sinks[0];

    Memory betaMemory = memories.peekNodeMemory(betaNode.getId());
    if (betaMemory == null) {
      return null;
    }
    BetaMemory bm;
    if (betaNode.getType() == NodeTypeEnums.AccumulateNode) {
      bm = ((AccumulateMemory) betaMemory).getBetaMemory();
    } else {
      bm = (BetaMemory) betaMemory;
    }

    // for RIA nodes, we need to store the ID of the created handles
    bm.getRightTupleMemory().iterator();
    if (bm.getRightTupleMemory().size() > 0) {
      ProtobufMessages.NodeMemory.RIANodeMemory.Builder _ria =
          ProtobufMessages.NodeMemory.RIANodeMemory.newBuilder();
      final org.drools.core.util.Iterator it = bm.getRightTupleMemory().iterator();

      // iterates over all propagated handles and assert them to the new sink
      for (RightTuple entry = (RightTuple) it.next();
          entry != null;
          entry = (RightTuple) it.next()) {
        LeftTuple leftTuple = (LeftTuple) entry.getFactHandle().getObject();
        InternalFactHandle handle = (InternalFactHandle) leftTuple.getObject();
        FactHandle _handle =
            ProtobufMessages.FactHandle.newBuilder()
                .setId(handle.getId())
                .setRecency(handle.getRecency())
                .build();
        _ria.addContext(
            ProtobufMessages.NodeMemory.RIANodeMemory.RIAContext.newBuilder()
                .setTuple(PersisterHelper.createTuple(leftTuple))
                .setResultHandle(_handle)
                .build());
      }

      return ProtobufMessages.NodeMemory.newBuilder()
          .setNodeId(nodeId)
          .setNodeType(ProtobufMessages.NodeMemory.NodeType.RIA)
          .setRia(_ria.build())
          .build();
    }
    return null;
  }
Exemplo n.º 7
0
  @SuppressWarnings("unchecked")
  private static ProtobufMessages.NodeMemory writeFromNodeMemory(
      final int nodeId, final Memory memory) {
    FromMemory fromMemory = (FromMemory) memory;

    if (fromMemory.betaMemory.getLeftTupleMemory().size() > 0) {
      ProtobufMessages.NodeMemory.FromNodeMemory.Builder _from =
          ProtobufMessages.NodeMemory.FromNodeMemory.newBuilder();

      final org.drools.core.util.Iterator tupleIter =
          fromMemory.betaMemory.getLeftTupleMemory().iterator();
      for (LeftTuple leftTuple = (LeftTuple) tupleIter.next();
          leftTuple != null;
          leftTuple = (LeftTuple) tupleIter.next()) {
        Map<Object, RightTuple> matches = (Map<Object, RightTuple>) leftTuple.getObject();
        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;
  }
  public void removeMemory(InternalWorkingMemory workingMemory) {
    BetaNode betaNode = (BetaNode) this.sink.getSinks()[0];

    Memory betaMemory = workingMemory.getNodeMemory(betaNode);
    BetaMemory bm;
    if (betaNode.getType() == NodeTypeEnums.AccumulateNode) {
      bm = ((AccumulateMemory) betaMemory).getBetaMemory();
    } else {
      bm = (BetaMemory) betaMemory;
    }

    if (bm.getRightTupleMemory().size() > 0) {
      final Iterator it = bm.getRightTupleMemory().iterator();
      for (RightTuple entry = (RightTuple) it.next();
          entry != null;
          entry = (RightTuple) it.next()) {
        LeftTuple leftTuple = (LeftTuple) entry.getFactHandle().getObject();
        leftTuple.unlinkFromLeftParent();
        leftTuple.unlinkFromRightParent();
      }
    }
    workingMemory.clearNodeMemory(this);
  }
 private Comparable getRightIndexedValue(RightTuple rightTuple) {
   return (Comparable)
       ascendingIndex.getExtractor().getValue(rightTuple.getFactHandle().getObject());
 }
Exemplo n.º 10
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);
  }
Exemplo n.º 11
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);
  }
Exemplo n.º 12
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);
  }
Exemplo n.º 13
0
  public void doRightInserts(
      NotNode notNode,
      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();

    // this must be processed here, rather than initial insert, as we need to link the blocker
    unlinkNotNodeOnRightInsert(notNode, bm, wm);

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

      rtm.add(rightTuple);
      if (ltm == null || ltm.size() == 0) {
        // do nothing here, as no left memory
        rightTuple.clearStaged();
        rightTuple = next;
        continue;
      }

      FastIterator it = notNode.getLeftIterator(ltm);
      PropagationContext context = rightTuple.getPropagationContext();

      constraints.updateFromFactHandle(contextEntry, wm, rightTuple.getFactHandle());
      for (LeftTuple leftTuple = notNode.getFirstLeftTuple(rightTuple, ltm, context, it);
          leftTuple != null; ) {
        // preserve next now, in case we remove this leftTuple
        LeftTuple temp = (LeftTuple) it.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);

          // subclasses like ForallNotNode might override this propagation
          // ** @TODO (mdp) need to not break forall
          LeftTuple childLeftTuple = leftTuple.getFirstChild();

          if (childLeftTuple != null) { // NotNode only has one child
            childLeftTuple.setPropagationContext(rightTuple.getPropagationContext());
            RuleNetworkEvaluator.deleteLeftChild(childLeftTuple, trgLeftTuples, stagedLeftTuples);
          }
        }

        leftTuple = temp;
      }
      rightTuple.clearStaged();
      rightTuple = next;
    }
    constraints.resetFactHandle(contextEntry);
  }
Exemplo n.º 14
0
  @Test
  public void testRestract() {
    final PropagationContext context =
        pctxFactory.createPropagationContext(0, PropagationContext.INSERTION, null, null, null);
    final StatefulKnowledgeSessionImpl workingMemory =
        new StatefulKnowledgeSessionImpl(
            1L, (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase());
    final ClassFieldReader extractor = store.getReader(Cheese.class, "type");

    final MvelConstraint constraint =
        new MvelConstraintTestUtil(
            "type == \"stilton\"", FieldFactory.getInstance().getFieldValue("stilton"), extractor);

    final List list = new ArrayList();
    final Cheese cheese1 = new Cheese("stilton", 5);
    final Cheese cheese2 = new Cheese("stilton", 15);
    list.add(cheese1);
    list.add(cheese2);
    final MockDataProvider dataProvider = new MockDataProvider(list);

    final Pattern pattern = new Pattern(0, new ClassObjectType(Cheese.class));

    From fromCe = new From(dataProvider);
    fromCe.setResultPattern(pattern);

    final ReteFromNode from =
        new ReteFromNode(
            3,
            dataProvider,
            new MockTupleSource(30),
            new AlphaNodeFieldConstraint[] {constraint},
            null,
            true,
            buildContext,
            fromCe);
    final MockLeftTupleSink sink = new MockLeftTupleSink(5);
    from.addTupleSink(sink);

    final List asserted = sink.getAsserted();

    final Person person1 = new Person("xxx2", 30);
    final FactHandle person1Handle = workingMemory.insert(person1);
    final LeftTuple tuple = new LeftTupleImpl((DefaultFactHandle) person1Handle, from, true);
    from.assertLeftTuple(tuple, context, workingMemory);

    assertEquals(2, asserted.size());

    final FromMemory memory = (FromMemory) workingMemory.getNodeMemory(from);
    assertEquals(1, memory.getBetaMemory().getLeftTupleMemory().size());
    assertNull(memory.getBetaMemory().getRightTupleMemory());
    RightTuple rightTuple2 = tuple.getFirstChild().getRightParent();
    RightTuple rightTuple1 = tuple.getFirstChild().getHandleNext().getRightParent();
    assertFalse(rightTuple1.equals(rightTuple2));
    assertNull(tuple.getFirstChild().getHandleNext().getHandleNext());

    final InternalFactHandle handle2 = rightTuple2.getFactHandle();
    final InternalFactHandle handle1 = rightTuple1.getFactHandle();
    assertEquals(handle1.getObject(), cheese2);
    assertEquals(handle2.getObject(), cheese1);

    from.retractLeftTuple(tuple, context, workingMemory);
    assertEquals(0, memory.getBetaMemory().getLeftTupleMemory().size());
    assertNull(memory.getBetaMemory().getRightTupleMemory());
  }