Example #1
0
  public RuleAgendaItem createRuleAgendaItem(
      final int salience, final PathMemory rs, final TerminalNode rtn) {
    String agendaGroupName = rtn.getRule().getAgendaGroup();
    String ruleFlowGroupName = rtn.getRule().getRuleFlowGroup();

    RuleAgendaItem lazyAgendaItem;
    if (!StringUtils.isEmpty(ruleFlowGroupName)) {
      lazyAgendaItem =
          new RuleAgendaItem(
              activationCounter++,
              null,
              salience,
              null,
              rs,
              rtn,
              isDeclarativeAgenda(),
              (InternalAgendaGroup) getAgendaGroup(ruleFlowGroupName));
    } else {
      lazyAgendaItem =
          new RuleAgendaItem(
              activationCounter++,
              null,
              salience,
              null,
              rs,
              rtn,
              isDeclarativeAgenda(),
              (InternalAgendaGroup) getRuleFlowGroup(agendaGroupName));
    }

    return lazyAgendaItem;
  }
 public boolean accept(
     Activation activation,
     PropagationContext context,
     InternalWorkingMemory workingMemory,
     TerminalNode rtn) {
   if (activation.isRuleAgendaItem()) {
     ActivationKey key =
         PersisterHelper.createActivationKey(
             activation.getRule().getPackageName(),
             activation.getRule().getName(),
             activation.getTuple());
     if (!this.rneActivations.containsKey(key)) {
       rneaToFire.add((RuleAgendaItem) activation);
     }
     return true;
   } else {
     ActivationKey key =
         PersisterHelper.createActivationKey(
             rtn.getRule().getPackageName(), rtn.getRule().getName(), activation.getTuple());
     // add the tuple to the cache for correlation
     this.tuplesCache.put(key, activation.getTuple());
     // check if there was an active activation for it
     return !this.dormantActivations.containsKey(key);
   }
 }
Example #3
0
  public boolean createPostponedActivation(
      final LeftTuple tuple,
      final PropagationContext context,
      final InternalWorkingMemory workingMemory,
      final TerminalNode rtn) {

    final RuleImpl rule = rtn.getRule();
    AgendaItem item;
    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;
        }
      }
    }

    InternalAgendaGroup agendaGroup = (InternalAgendaGroup) getAgendaGroup(rule.getAgendaGroup());
    // do not add the activation if the rule is "lock-on-active" and the AgendaGroup is active
    if (rule.isLockOnActive()
        && agendaGroup.isActive()
        && agendaGroup.getAutoFocusActivator() != context) {
      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
  public void cancelActivation(
      final LeftTuple leftTuple,
      final PropagationContext context,
      final InternalWorkingMemory workingMemory,
      final Activation activation,
      final TerminalNode rtn) {
    AgendaItem item = (AgendaItem) activation;
    item.removeAllBlockersAndBlocked(this);

    if (isDeclarativeAgenda() && activation.getFactHandle() == null) {
      // This a control rule activation, nothing to do except update counters. As control rules are
      // not in agenda-groups etc.
      return;
    } else {
      // we are retracting an actual Activation, so also remove it and it's handle from the WM.
      removeActivation(item);
    }

    if (activation.isQueued()) {
      // on fact expiration, we don't remove the activation, but let it fire
      if (context.getType() == PropagationContext.EXPIRATION
          && context.getFactHandleOrigin() != null) {
      } else {
        activation.remove();

        if (activation.getActivationGroupNode() != null) {
          activation.getActivationGroupNode().getActivationGroup().removeActivation(activation);
        }
        leftTuple.decreaseActivationCountForEvents();

        ((EventSupport) workingMemory)
            .getAgendaEventSupport()
            .fireActivationCancelled(activation, workingMemory, MatchCancelledCause.WME_MODIFY);
      }
    }

    if (item.getActivationUnMatchListener() != null) {
      item.getActivationUnMatchListener().unMatch(workingMemory.getKnowledgeRuntime(), item);
    }

    TruthMaintenanceSystemHelper.removeLogicalDependencies(activation, context, rtn.getRule());
  }
Example #5
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;
  }