Пример #1
0
  private static ProtobufMessages.FactHandle writeFactHandle(
      MarshallerWriteContext context,
      ObjectMarshallingStrategyStore objectMarshallingStrategyStore,
      InternalFactHandle handle)
      throws IOException {
    ProtobufMessages.FactHandle.Builder _handle = ProtobufMessages.FactHandle.newBuilder();

    _handle.setType(getHandleType(handle));
    _handle.setId(handle.getId());
    _handle.setRecency(handle.getRecency());

    if (_handle.getType() == ProtobufMessages.FactHandle.HandleType.EVENT) {
      // is event
      EventFactHandle efh = (EventFactHandle) handle;
      _handle.setTimestamp(efh.getStartTimestamp());
      _handle.setDuration(efh.getDuration());
      _handle.setIsExpired(efh.isExpired());
      _handle.setActivationsCount(efh.getActivationsCount());
    }

    if (handle.getEqualityKey() != null
        && handle.getEqualityKey().getStatus() == EqualityKey.JUSTIFIED) {
      _handle.setIsJustified(true);
    } else {
      _handle.setIsJustified(false);
    }

    Object object = handle.getObject();

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

      Integer index = context.getStrategyIndex(strategy);
      _handle.setStrategyIndex(index.intValue());
      _handle.setObject(
          ByteString.copyFrom(
              strategy.marshal(context.strategyContext.get(strategy), context, object)));
    }

    return _handle.build();
  }
Пример #2
0
  public static InternalFactHandle readFactHandle(
      MarshallerReaderContext context, SessionEntryPoint entryPoint, FactHandle _handle)
      throws IOException, ClassNotFoundException {
    Object object = null;
    ObjectMarshallingStrategy strategy = null;
    if (_handle.hasStrategyIndex()) {
      strategy = context.usedStrategies.get(_handle.getStrategyIndex());
      object =
          strategy.unmarshal(
              context.strategyContexts.get(strategy),
              context,
              _handle.getObject().toByteArray(),
              (context.ruleBase == null) ? null : context.ruleBase.getRootClassLoader());
    }

    InternalFactHandle handle = null;
    switch (_handle.getType()) {
      case FACT:
        {
          handle = new DefaultFactHandle(_handle.getId(), object, _handle.getRecency(), entryPoint);
          break;
        }
      case QUERY:
        {
          handle = new QueryElementFactHandle(object, _handle.getId(), _handle.getRecency());
          break;
        }
      case EVENT:
        {
          handle =
              new EventFactHandle(
                  _handle.getId(),
                  object,
                  _handle.getRecency(),
                  _handle.getTimestamp(),
                  _handle.getDuration(),
                  entryPoint);
          ((EventFactHandle) handle).setExpired(_handle.getIsExpired());
          // the event is re-propagated through the network, so the activations counter will be
          // recalculated
          // ((EventFactHandle) handle).setActivationsCount( _handle.getActivationsCount() );
          break;
        }
      default:
        {
          throw new IllegalStateException(
              "Unable to marshal FactHandle, as type does not exist:" + _handle.getType());
        }
    }
    return handle;
  }
Пример #3
0
    public void execute(InternalWorkingMemory workingMemory) {
      if (this.factHandle.isValid()) {
        // if the fact is still in the working memory (since it may have been previously retracted
        // already
        final PropagationContext context =
            new PropagationContextImpl(
                workingMemory.getNextPropagationIdCounter(),
                PropagationContext.EXPIRATION,
                null,
                null,
                this.factHandle);
        ((EventFactHandle) factHandle).setExpired(true);
        this.node.retractObject(factHandle, context, workingMemory);

        context.evaluateActionQueue(workingMemory);
        // if no activations for this expired event
        if (((EventFactHandle) factHandle).getActivationsCount() == 0) {
          // remove it from the object store and clean up resources
          ((EventFactHandle) factHandle).getEntryPoint().retract(factHandle);
        }
        context.evaluateActionQueue(workingMemory);
      }
    }
Пример #4
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();
    }
  }