Exemplo n.º 1
0
  public void clear() {
    // reset focus stack
    this.focusStack.clear();
    this.focusStack.add(getMainAgendaGroup());

    // reset scheduled activations
    if (!this.scheduledActivations.isEmpty()) {
      for (ScheduledAgendaItem item = this.scheduledActivations.removeFirst();
          item != null;
          item = this.scheduledActivations.removeFirst()) {
        item.setEnqueued(false);
        Scheduler.removeAgendaItem(item, this);
      }
    }

    // reset staged activations
    getStageActivationsGroup().clear();

    // reset all agenda groups
    for (InternalAgendaGroup group : this.agendaGroups.values()) {
      group.reset();
    }

    // reset all activation groups.
    for (InternalActivationGroup group : this.activationGroups.values()) {
      group.setTriggeredForRecency(this.workingMemory.getFactHandleFactory().getRecency());
      group.reset();
    }
  }
Exemplo n.º 2
0
  /**
   * If the item belongs to an activation group, add it
   *
   * @param item
   */
  public void addItemToActivationGroup(final AgendaItem item) {
    String group = item.getRule().getActivationGroup();
    if (group != null && group.length() > 0) {
      InternalActivationGroup actgroup = getActivationGroup(group);

      actgroup.addActivation(item);
    }
  }
Exemplo n.º 3
0
  public void modifyActivation(final AgendaItem activation, boolean previouslyActive) {
    if (declarativeAgenda) {
      InternalFactHandle factHandle = activation.getFactHandle();
      workingMemory
          .getEntryPointNode()
          .modifyActivation(factHandle, activation.getPropagationContext(), workingMemory);

      if (previouslyActive) {
        // already activated
        return;
      }

      if (activation.isCanceled()
          || (activation.getBlockers() != null && activation.getBlockers().size() > 0)) {
        // it's blocked so do nothing
        return;
      }

      // All activations started off staged, they are unstaged if they are blocked or
      // allowed to move onto the actual agenda for firing.
      InternalActivationGroup activationGroup = getStageActivationsGroup();
      if (activation.getActivationGroupNode() != null
          && activation.getActivationGroupNode().getActivationGroup() == activationGroup) {
        // already staged, so return
        return;
      }

      activationGroup.addActivation(activation);
    } else {
      if (!previouslyActive) {
        addActivation(activation, true);
      } else {
        Timer timer = activation.getRule().getTimer();
        if (timer != null && timer instanceof ExpressionIntervalTimer) {
          ScheduledAgendaItem schItem = (ScheduledAgendaItem) activation;
          removeScheduleItem(schItem);

          scheduleItem(schItem, workingMemory);
        }
      }
    }
  }
Exemplo n.º 4
0
  /*
   * (non-Javadoc)
   *
   * @see org.kie.common.AgendaI#clearActivationGroup(org.kie.spi.ActivationGroup)
   */
  public void clearAndCancelActivationGroup(final InternalActivationGroup activationGroup) {
    final EventSupport eventsupport = (EventSupport) this.workingMemory;

    activationGroup.setTriggeredForRecency(this.workingMemory.getFactHandleFactory().getRecency());

    for (final Iterator it = activationGroup.iterator(); it.hasNext(); ) {
      final ActivationGroupNode node = (ActivationGroupNode) it.next();
      final Activation activation = node.getActivation();
      activation.setActivationGroupNode(null);

      if (activation.isQueued()) {
        activation.setQueued(false);
        activation.remove();

        eventsupport
            .getAgendaEventSupport()
            .fireActivationCancelled(activation, this.workingMemory, MatchCancelledCause.CLEAR);
      }
    }
    activationGroup.reset();
  }
Exemplo n.º 5
0
  /**
   * Fire this item.
   *
   * @param activation The activation to fire
   * @throws org.drools.core.spi.ConsequenceException If an error occurs while attempting to fire
   *     the consequence.
   */
  public synchronized void fireActivation(final Activation activation) throws ConsequenceException {
    // We do this first as if a node modifies a fact that causes a recursion
    // on an empty pattern
    // we need to make sure it re-activates
    this.workingMemory.startOperation();
    isFiringActivation = true;
    try {
      final EventSupport eventsupport = (EventSupport) this.workingMemory;

      eventsupport
          .getAgendaEventSupport()
          .fireBeforeActivationFired(activation, this.workingMemory);

      if (activation.getActivationGroupNode() != null) {
        // We know that this rule will cancel all other activations in the group
        // so lets remove the information now, before the consequence fires
        final InternalActivationGroup activationGroup =
            activation.getActivationGroupNode().getActivationGroup();
        activationGroup.removeActivation(activation);
        clearAndCancelActivationGroup(activationGroup);
      }
      activation.setQueued(false);

      try {

        this.knowledgeHelper.setActivation(activation);
        if (log.isTraceEnabled()) {
          log.trace("Fire \"{}\" \n{}", activation.getRule().getName(), activation.getTuple());
        }
        activation.getConsequence().evaluate(this.knowledgeHelper, this.workingMemory);
        this.knowledgeHelper.cancelRemainingPreviousLogicalDependencies();
        this.knowledgeHelper.reset();
      } catch (final Exception e) {
        if (this.legacyConsequenceExceptionHandler != null) {
          this.legacyConsequenceExceptionHandler.handleException(activation, this.workingMemory, e);
        } else if (this.consequenceExceptionHandler != null) {
          this.consequenceExceptionHandler.handleException(
              activation, this.workingMemory.getKnowledgeRuntime(), e);
        } else {
          throw new RuntimeException(e);
        }
      } finally {
        if (activation.getFactHandle() != null) {
          // update the Activation in the WM
          InternalFactHandle factHandle = activation.getFactHandle();
          workingMemory
              .getEntryPointNode()
              .modifyActivation(factHandle, activation.getPropagationContext(), workingMemory);
          activation.getPropagationContext().evaluateActionQueue(workingMemory);
        }
        // if the tuple contains expired events
        for (LeftTuple tuple = activation.getTuple(); tuple != null; tuple = tuple.getParent()) {
          if (tuple.getLastHandle() == null) {
            // can be null for eval, not and exists that have no right input
            continue;
          }
          if (tuple.getLastHandle().isEvent()) {
            EventFactHandle handle = (EventFactHandle) tuple.getLastHandle();
            // decrease the activation count for the event
            handle.decreaseActivationsCount();
            // handles "expire" only in stream mode.
            if (handle.isExpired()) {
              if (handle.getActivationsCount() <= 0) {
                // and if no more activations, retract the handle
                handle.getEntryPoint().retract(handle);
              }
            }
          }
        }
      }

      eventsupport.getAgendaEventSupport().fireAfterActivationFired(activation, this.workingMemory);

      unstageActivations();
    } finally {
      isFiringActivation = false;
      if (mustNotifyHalt) {
        mustNotifyHalt = false;
        notifyHalt();
      }
      this.workingMemory.endOperation();
    }
  }