public void assertLeftTuple(
      final LeftTuple leftTuple,
      final PropagationContext context,
      final InternalWorkingMemory workingMemory) {
    // while we don't do anything with this, it's needed to serialization has a hook point
    // It will still keep the LT's in the serialization, but sucked form the child
    workingMemory.getNodeMemory(this);

    // creating a dummy fact handle to wrap the tuple
    final InternalFactHandle handle = createFactHandle(leftTuple, context, workingMemory);
    boolean useLeftMemory = true;
    if (!isLeftTupleMemoryEnabled()) {
      // This is a hack, to not add closed DroolsQuery objects
      Object object = leftTuple.get(0).getObject();
      if (!(object instanceof DroolsQuery) || !((DroolsQuery) object).isOpen()) {
        useLeftMemory = false;
      }
    }

    if (useLeftMemory) {
      leftTuple.setObject(handle);
    }

    // propagate it
    this.sink.propagateAssertObject(handle, context, workingMemory);

    //        if ( useLeftMemory) {
    //            leftTuple.setObject( handle.getFirstRightTuple() );
    //        }

  }
Example #2
0
 public ScheduledAgendaItem createScheduledAgendaItem(
     final LeftTuple tuple,
     final PropagationContext context,
     final TerminalNode rtn,
     InternalAgendaGroup agendaGroup) {
   RuleTerminalNodeLeftTuple rtnLeftTuple = (RuleTerminalNodeLeftTuple) tuple;
   rtnLeftTuple.init(activationCounter++, 0, context, null, agendaGroup);
   ScheduledAgendaItem item = new ScheduledAgendaItem(rtnLeftTuple, this);
   tuple.setObject(item);
   return item;
 }
Example #3
0
  public boolean createActivation(
      final LeftTuple tuple,
      final PropagationContext context,
      final InternalWorkingMemory workingMemory,
      final TerminalNode rtn) {
    // First process control rules
    // Control rules do increase ActivationCountForEvent and agenda ActivateActivations, they do not
    // currently fire events
    // ControlRules for now re-use the same PropagationContext
    if (rtn.isFireDirect()) {
      // Fire RunLevel == 0 straight away. agenda-groups, rule-flow groups, salience are ignored
      AgendaItem item = createAgendaItem(tuple, 0, context, rtn, null, null);
      tuple.setObject(item);
      if (activationsFilter != null && !activationsFilter.accept(item, workingMemory, rtn)) {
        return false;
      }

      item.setQueued(true);
      tuple.increaseActivationCountForEvents();
      fireActivation(item); // Control rules fire straight away.
      return true;
    }

    final RuleImpl rule = rtn.getRule();
    AgendaItem item;
    final Timer timer = rule.getTimer();
    InternalAgendaGroup agendaGroup = (InternalAgendaGroup) getAgendaGroup(rule.getAgendaGroup());
    if (timer != null) {
      item = createScheduledAgendaItem(tuple, context, rtn, agendaGroup);
    } else {
      if (rule.getCalendars() != null) {
        // for normal activations check for Calendar inclusion here, scheduled activations check on
        // each trigger point
        long timestamp = workingMemory.getSessionClock().getCurrentTime();
        for (String cal : rule.getCalendars()) {
          if (!workingMemory.getCalendars().get(cal).isTimeIncluded(timestamp)) {
            return false;
          }
        }
      }

      if (rule.isLockOnActive()
          && agendaGroup.isActive()
          && agendaGroup.getAutoFocusActivator() != context) {
        // do not add the activation if the rule is "lock-on-active" and the AgendaGroup is active
        if (tuple.getObject() == null) {
          tuple.setObject(
              Boolean
                  .TRUE); // this is so we can do a check with a bit more intent than a null check
                          // on modify
        }
        return false;
      }

      item = createAgendaItem(tuple, 0, context, rtn, null, agendaGroup);
      item.setSalience(
          rule.getSalience()
              .getValue(new DefaultKnowledgeHelper(item, workingMemory), rule, workingMemory));
    }

    if (activationsFilter != null && !activationsFilter.accept(item, workingMemory, rtn)) {
      return false;
    }
    item.setQueued(true);
    tuple.increaseActivationCountForEvents();

    ((EventSupport) workingMemory)
        .getAgendaEventSupport()
        .fireActivationCreated(item, workingMemory);
    return true;
  }
Example #4
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();
  }