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

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

Introduction

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

Prototype

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

Source Link

Document

Constructor.

Usage

From source file:it.polito.elite.dog.drivers.appliances.base.ApplianceDriverInstance.java

/**
 * Handles notifications coming from the meter associated to the device
 * handled by this driver. Currently on/*www  .j a  v  a2s.c o  m*/
 * {@link SinglePhaseActivePowerMeasurementNotification} are supported.
 * 
 * @param currentNotification
 *            The {@link Notification} to handle.
 */
public void handleNotification(Notification currentNotification) {
    //check if an executor is available
    if (this.executor != null) {
        // default behavior, only handle single phase active power
        // notifications, can be overridden
        if (currentNotification instanceof SinglePhaseActivePowerMeasurementNotification) {
            // get the notification name
            String name = SinglePhaseActivePowerMeasurementNotification.notificationName;

            // converts to a power measure
            @SuppressWarnings("unchecked")
            DecimalMeasure<Power> value = (DecimalMeasure<Power>) ((SinglePhaseActivePowerMeasurementNotification) currentNotification)
                    .getPowerValue();

            // build an event representing the new measure in Watt...
            // TODO check if it could be generalized to all notifications?
            // maybe using evaluators?
            TriggerEvent event = new TriggerEvent(name, TriggerEvent.SIGNAL_EVENT,
                    new Double(value.to(SI.WATT).getValue().doubleValue()));

            // trigger the event in the state machine
            try {
                this.executor.triggerEvent(event);
            } catch (ModelException e) {
                // log the error, and continue
                this.logger.log(LogService.LOG_WARNING,
                        "Error while triggering a new event on the device state machine for device: "
                                + device.getDeviceId(),
                        e);
            }

        }
    }

}

From source file:org.onehippo.repository.scxml.SCXMLWorkflowExecutor.java

/**
 * Invokes {@link SCXMLExecutor#triggerEvent(TriggerEvent)} with a {@link TriggerEvent#SIGNAL_EVENT}, the provided
 * action as event name and payload as event payload
 * <p>/*from   www .  ja  v  a  2s .  com*/
 * The action will first be validated against the provided custom actions map which must
 * have this action defined with value Boolean.TRUE, otherwise a WorkflowException is thrown before even invoking
 * the state machine.
 * </p>
 * @return {@link SCXMLWorkflowContext#getResult()} if there's no exception.
 */
public Object triggerAction(String action, Map<String, Boolean> actionsMap, Map<String, Object> payload)
        throws WorkflowException {
    if (!started) {
        throw new WorkflowException("Workflow " + scxmlId + " not started");
    }
    if (terminated) {
        throw new WorkflowException("Workflow " + scxmlId + " already terminated");
    }
    try {
        Boolean allowed = actionsMap.get(action);
        if (allowed == null || !allowed) {
            throw new WorkflowException("Cannot invoke workflow " + scxmlId + " action " + action
                    + ": action not allowed or undefined");
        }
        TriggerEvent event = new TriggerEvent(action, TriggerEvent.SIGNAL_EVENT, payload);
        if (payload == null) {
            log.debug("Invoking workflow {} action {}", scxmlId, action);
        } else {
            log.debug("Invoking workflow {} action {} with payload {}", scxmlId, action, payload.toString());
        }
        // reset result
        context.setResult(null);
        executor.triggerEvent(event);
        if (executor.getCurrentStatus().isFinal()) {
            terminated = true;
        }
    } catch (Exception e) {
        handleException(e);
    }
    // only reached when no exception
    return context.getResult();
}