Example usage for org.apache.commons.scxml TriggerEvent TriggerEvent

List of usage examples for org.apache.commons.scxml TriggerEvent TriggerEvent

Introduction

In this page you can find the example usage for org.apache.commons.scxml TriggerEvent TriggerEvent.

Prototype

public TriggerEvent(final String name, final int type, final Object payload) 

Source Link

Document

Constructor.

Usage

From source file:com.korwe.thecore.scxml.ScxmlMessageProcessor.java

@Override
public synchronized void processMessage(CoreMessage message) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Message = " + message);
        LOG.debug("State machine state = " + exec.getCurrentStatus().getStates());
    }//from  w w w  .  j av a  2 s. co  m

    try {
        switch (message.getMessageType()) {
        case ServiceRequest:
            if (LOG.isDebugEnabled()) {
                LOG.debug("Triggering request event");
            }

            ServiceRequest req = (ServiceRequest) message;
            exec.triggerEvent(
                    new TriggerEvent("ServiceRequest." + req.getFunction(), TriggerEvent.SIGNAL_EVENT, req));
            break;
        default:
            if (LOG.isDebugEnabled()) {
                LOG.debug("Triggering default event");
            }

            exec.triggerEvent(
                    new TriggerEvent(message.getMessageType().name(), TriggerEvent.SIGNAL_EVENT, message));
            break;
        }
    } catch (ModelException e) {
        LOG.error("Unable to process message - state machine error", e);
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Message processed");
    }

}

From source file:ch.shaktipat.saraswati.internal.common.AbstractStateMachine.java

/**
 * Fire an event on the SCXML engine./*from w ww. j  a  va  2 s  . co m*/
 *
 * @param event The event name.
 * @return Whether the state machine has reached a "final"
 *         configuration.
 */
public boolean fireEvent(final String event) {
    TriggerEvent[] evts = { new TriggerEvent(event, TriggerEvent.SIGNAL_EVENT, null) };
    try {
        engine.triggerEvents(evts);
    } catch (ModelException me) {
        logError(me);
    }
    return engine.getCurrentStatus().isFinal();
}

From source file:alma.acs.nc.sm.generic.AcsScxmlEngine.java

/**
 * Sends a signal (event) to the state machine.
 * /*from w  w w .j a  v  a2s  .co m*/
 * The call is synchronous and returns only when the state machine has gone through all transitions/actions, 
 * where of course a /do activity would still continue to run asynchronously.
 * Note that the underlying SCXML engine also supports asynchronous sending of multiple events
 * at a time, but we do not expose this feature in ACS.
 * <p>
 * TODO: How can the client find out whether the signal was applicable 
 *       for the current state or was ignored?
 *       
 * @return True - if all the states are final and there are not events
 *         pending from the last step. False - otherwise.
 */
public synchronized boolean fireSignal(S signal) {
    TriggerEvent evnt = new TriggerEvent(signal.name(), TriggerEvent.SIGNAL_EVENT, null);
    try {
        exec.triggerEvent(evnt);
    } catch (ModelException e) {
        logger.info(e.getMessage());
    }
    return exec.getCurrentStatus().isFinal();
}

From source file:alma.acs.nc.sm.generic.AcsScxmlEngine.java

/**
 * Synchronous event handling as in {@link #fireSignal(Enum)}, 
 * but possibly with exceptions for the following cases:
 * <ul>/*from   w  ww .ja v a2  s  .c om*/
 *   <li> The <code>signal</code> gets checked if it can be handled by the current state(s);
 *        an <code>AcsJIllegalStateEventEx</code> exception is thrown if not.
 *   <li> If an executed action throws a AcsJStateMachineActionEx exception, that exception gets thrown here.
 *        Depending on the concrete state machine, an additional response to the error may be 
 *        that the SM goes to an error state, due to an internal event triggered by the action.
 *   <li> <code>ModelException</code>, as thrown by {@link SCXMLExecutor#triggerEvent}, unlikely
 *        with our static use of the SCXML engine.
 * </ul>
 * @param signal
 * @return True - if all the states are final and there are not events
 *         pending from the last step. False - otherwise.
 * @throws AcsJIllegalStateEventEx
 * @throws AcsJStateMachineActionEx
 * @throws ModelException
 */
public synchronized boolean fireSignalWithErrorFeedback(S signal)
        throws AcsJIllegalStateEventEx, AcsJStateMachineActionEx, ModelException {

    // check if signal is OK, throw exception if not.
    Set<S> applicableSignals = getApplicableSignals();
    if (!applicableSignals.contains(signal)) {
        AcsJIllegalStateEventEx ex = new AcsJIllegalStateEventEx();
        ex.setEvent(signal.name());
        ex.setState(getCurrentState());
        throw ex;
    }

    // Register error callback with action dispatcher.
    // This is only thread safe because this method is synchronized and we 
    // execute only one event at a time.
    MyActionExceptionHandler handler = new MyActionExceptionHandler();
    actionDispatcher.setActionExceptionHandler(handler);
    try {
        TriggerEvent evnt = new TriggerEvent(signal.name(), TriggerEvent.SIGNAL_EVENT, null);
        exec.triggerEvent(evnt);

        if (handler.theEx != null) {
            throw handler.theEx;
        } else {
            // either there was no action associated with the event,
            // or all actions executed without exception.
            return exec.getCurrentStatus().isFinal();
        }
    } finally {
        actionDispatcher.setActionExceptionHandler(null);
    }
}

From source file:de.dfki.iui.mmds.scxml.engine.impl.SCXMLEngineImpl.java

/**
 * Fire an event on the SCXML engine./*www.ja  v  a  2 s . c om*/
 * 
 * @param event
 *            The event name.
 * @return Whether the state machine has reached a &quot;final&quot;
 *         configuration.
 */
@Override
public boolean fireEvent(final String event, final Object payload) {

    TriggerEvent evts = new TriggerEvent(event, TriggerEvent.SIGNAL_EVENT, payload);

    Set<TransitionTarget> lastLeafs = engine.getCurrentStatus().getStates();

    try {
        engine.triggerEvent(evts);
    } catch (ModelException me) {
        logError(me);
    }
    SCXMLEngineActivator.sendActiveStates(id, this.getActiveStates(), this.getAllActiveStates());

    // logInfo( "Send 'Scxml state'" );
    SCXMLEngineActivator.sendScxmlState(id, State.IDLE, getAvailableEventsStates());
    List<Set<String>> cfgHistory = new ArrayList<Set<String>>(configHistory);
    updateConfigHistory(lastLeafs);
    SCXMLEngineActivator.sendScxmlConfigHistory(id, cfgHistory);

    // if (cb!=null) {
    //
    // Set<TransitionTarget> targets =
    // engine.getCurrentStatus().getAllStates();
    // List<Set<String>> allEvents = new LinkedList<Set<String>>();
    // List<String> states = new ArrayList<String>(targets.size());
    // Set<String> events = null;
    //
    // for (TransitionTarget state : targets) {
    // states.add(state.getId());
    // events = new HashSet<String>();
    // for (Transition t : state.getTransitionsList()) {
    // if (t.getEvent() != null)
    // events.add(t.getEvent());
    // }
    // allEvents.add(events);
    //
    // }
    // cb.notifyEvents(this.getAllActiveStates(), allEvents);
    // }

    // SCXMLEngineComponent.sendActiveStates(scxmlDocument,
    // engine.getCurrentStatus().getAllStates());
    return engine.getCurrentStatus().isFinal();
}

From source file:org.dishevelled.piccolo.identify.StateMachineSupport.java

/**
 * Fire an event with the specified event name on the state machine for
 * this state machine support class (fails silently).
 *
 * @param eventName event name, must not be null
 *//*from www. ja va 2 s  . co  m*/
void fireStateMachineEvent(final String eventName) {
    if (eventName == null) {
        throw new IllegalArgumentException("eventName must not be null");
    }
    try {
        TriggerEvent event = new TriggerEvent(eventName, TriggerEvent.SIGNAL_EVENT, null);
        executor.triggerEvents(new TriggerEvent[] { event });
    } catch (ModelException e) {
        // ignore
    }
}