Example #1
0
  /**
   * Evaluate result constraints and propagate assert in case they are true
   *
   * @param leftTuple
   * @param context
   * @param workingMemory
   * @param memory
   * @param accresult
   * @param handle
   */
  public void evaluateResultConstraints(
      final ActivitySource source,
      final LeftTuple leftTuple,
      final PropagationContext context,
      final InternalWorkingMemory workingMemory,
      final AccumulateMemory memory,
      final AccumulateContext accctx,
      final boolean useLeftMemory) {

    // get the actual result
    final Object[] resultArray =
        this.accumulate.getResult(
            memory.workingMemoryContext, accctx.context, leftTuple, workingMemory);
    Object result = this.accumulate.isMultiFunction() ? resultArray : resultArray[0];
    if (result == null) {
      return;
    }

    if (accctx.result == null) {
      final InternalFactHandle handle =
          createResultFactHandle(context, workingMemory, leftTuple, result);

      accctx.result = createRightTuple(handle, this, context);
    } else {
      accctx.result.getFactHandle().setObject(result);
    }

    // First alpha node filters
    boolean isAllowed = result != null;
    for (int i = 0, length = this.resultConstraints.length; isAllowed && i < length; i++) {
      if (!this.resultConstraints[i].isAllowed(
          accctx.result.getFactHandle(), workingMemory, memory.alphaContexts[i])) {
        isAllowed = false;
      }
    }
    if (isAllowed) {
      this.resultBinder.updateFromTuple(memory.resultsContext, workingMemory, leftTuple);
      if (!this.resultBinder.isAllowedCachedLeft(
          memory.resultsContext, accctx.result.getFactHandle())) {
        isAllowed = false;
      }
      this.resultBinder.resetTuple(memory.resultsContext);
    }

    if (accctx.propagated == true) {
      // temporarily break the linked list to avoid wrong interactions
      LeftTuple[] matchings = splitList(leftTuple, accctx, false);
      if (isAllowed) {
        // modify
        if (ActivitySource.LEFT.equals(source)) {
          this.sink.propagateModifyChildLeftTuple(
              leftTuple.getFirstChild(), leftTuple, context, workingMemory, useLeftMemory);
        } else {
          this.sink.propagateModifyChildLeftTuple(
              leftTuple.getFirstChild(), accctx.result, context, workingMemory, useLeftMemory);
        }
      } else {
        // retract
        this.sink.propagateRetractLeftTuple(leftTuple, context, workingMemory);
        accctx.propagated = false;
      }
      // restore the matchings list
      restoreList(leftTuple, matchings);
    } else if (isAllowed) {
      // temporarily break the linked list to avoid wrong interactions
      LeftTuple[] matchings = splitList(leftTuple, accctx, false);
      // assert
      this.sink.propagateAssertLeftTuple(
          leftTuple, accctx.result, null, null, context, workingMemory, useLeftMemory);
      accctx.propagated = true;
      // restore the matchings list
      restoreList(leftTuple, matchings);
    }
  }