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);
   }
 }
 public int compare(org.drools.core.spi.Activation o1, org.drools.core.spi.Activation o2) {
   int result = o1.getRule().getName().compareTo(o2.getRule().getName());
   if (result == 0) {
     LeftTuple t1 = o1.getTuple();
     LeftTuple t2 = o2.getTuple();
     while (result == 0 && t1 != null && t2 != null) {
       if (t1.getLastHandle() != null && t2.getLastHandle() != null) {
         // can be null for eval, not and exists that have no right input
         result = t1.getLastHandle().getId() - t2.getLastHandle().getId();
       }
       t1 = t1.getParent();
       t2 = t2.getParent();
     }
   }
   return result;
 }
  private static void writeBeliefSet(
      MarshallerWriteContext context,
      BeliefSet beliefSet,
      org.drools.core.marshalling.impl.ProtobufMessages.EqualityKey.Builder _key)
      throws IOException {

    ProtobufMessages.BeliefSet.Builder _beliefSet = ProtobufMessages.BeliefSet.newBuilder();
    _beliefSet.setHandleId(beliefSet.getFactHandle().getId());

    ObjectMarshallingStrategyStore objectMarshallingStrategyStore =
        context.objectMarshallingStrategyStore;

    // for ( LinkedListEntry node = (LinkedListEntry) beliefSet.getFirst(); node != null; node =
    // (LinkedListEntry) node.getNext() ) {
    FastIterator it = beliefSet.iterator();
    for (LinkedListEntry node = (LinkedListEntry) beliefSet.getFirst();
        node != null;
        node = (LinkedListEntry) it.next(node)) {
      LogicalDependency belief = (LogicalDependency) node.getObject();
      ProtobufMessages.LogicalDependency.Builder _logicalDependency =
          ProtobufMessages.LogicalDependency.newBuilder();
      // _belief.setActivation( value )

      LogicalDependency dependency = (LogicalDependency) node.getObject();
      org.drools.core.spi.Activation activation = dependency.getJustifier();
      ProtobufMessages.Activation _activation =
          ProtobufMessages.Activation.newBuilder()
              .setPackageName(activation.getRule().getPackage())
              .setRuleName(activation.getRule().getName())
              .setTuple(PersisterHelper.createTuple(activation.getTuple()))
              .build();
      _logicalDependency.setActivation(_activation);

      if (belief.getObject() != null) {
        ObjectMarshallingStrategy strategy =
            objectMarshallingStrategyStore.getStrategyObject(belief.getObject());

        Integer index = context.getStrategyIndex(strategy);
        _logicalDependency.setObjectStrategyIndex(index.intValue());
        _logicalDependency.setObject(
            ByteString.copyFrom(
                strategy.marshal(
                    context.strategyContext.get(strategy), context, belief.getObject())));
      }

      if (belief.getValue() != null) {
        ObjectMarshallingStrategy strategy =
            objectMarshallingStrategyStore.getStrategyObject(belief.getValue());

        Integer index = context.getStrategyIndex(strategy);
        _logicalDependency.setValueStrategyIndex(index.intValue());
        _logicalDependency.setValue(
            ByteString.copyFrom(
                strategy.marshal(
                    context.strategyContext.get(strategy), context, belief.getValue())));
      }
      _beliefSet.addLogicalDependency(_logicalDependency.build());
    }
    _key.setBeliefSet(_beliefSet);
  }
Esempio n. 4
0
 private boolean checkProcessInstance(Activation activation, long processInstanceId) {
   final Map<String, Declaration> declarations = activation.getSubRule().getOuterDeclarations();
   for (Declaration declaration : declarations.values()) {
     if ("processInstance".equals(declaration.getIdentifier())) {
       Object value =
           declaration.getValue(workingMemory, activation.getTuple().get(declaration).getObject());
       if (value instanceof ProcessInstance) {
         return ((ProcessInstance) value).getId() == processInstanceId;
       }
     }
   }
   return true;
 }
Esempio n. 5
0
  public Trigger createTrigger(Activation item, InternalWorkingMemory wm) {
    long timestamp = wm.getTimerService().getCurrentTime();
    String[] calendarNames = item.getRule().getCalendars();
    Calendars calendars = wm.getCalendars();

    Declaration[][] timerDeclrs = ((AgendaItem) item).getTerminalNode().getTimerDeclarations();

    ScheduledAgendaItem schItem = (ScheduledAgendaItem) item;
    DefaultJobHandle jh = null;
    if (schItem.getJobHandle() != null) {
      jh = (DefaultJobHandle) schItem.getJobHandle();
    }

    return createTrigger(timestamp, item.getTuple(), jh, calendarNames, calendars, timerDeclrs, wm);
  }
Esempio n. 6
0
 protected boolean checkProcessInstance(Activation activation) {
   final Map<?, ?> declarations = activation.getSubRule().getOuterDeclarations();
   for (Iterator<?> it = declarations.values().iterator(); it.hasNext(); ) {
     Declaration declaration = (Declaration) it.next();
     if ("processInstance".equals(declaration.getIdentifier())
         || "org.kie.api.runtime.process.WorkflowProcessInstance"
             .equals(declaration.getTypeName())) {
       Object value =
           declaration.getValue(
               ((StatefulKnowledgeSessionImpl) getProcessInstance().getKnowledgeRuntime())
                   .getInternalWorkingMemory(),
               ((InternalFactHandle) activation.getTuple().get(declaration)).getObject());
       if (value instanceof ProcessInstance) {
         return ((ProcessInstance) value).getId() == getProcessInstance().getId();
       }
     }
   }
   return true;
 }
Esempio n. 7
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();
    }
  }