📄 scxmlexecutor.java
字号:
/**
* Get the root context for this execution.
*
* @return Context The root context.
*/
public Context getRootContext() {
return scInstance.getRootContext();
}
/**
* Get the state machine that is being executed.
* <b>NOTE:</b> This is the state machine definition or model used by this
* executor instance. It may be shared across multiple executor instances
* and as a best practice, should not be altered. Also note that
* manipulation of instance data for the executor should happen through
* its root context or state contexts only, never through the direct
* manipulation of any {@link Datamodel}s associated with this state
* machine definition.
*
* @return Returns the stateMachine.
*/
public SCXML getStateMachine() {
return stateMachine;
}
/**
* Set the state machine to be executed.
* <b>NOTE:</b> Should only be used before the executor is set in motion.
*
* @param stateMachine The stateMachine to set.
*/
public void setStateMachine(final SCXML stateMachine) {
// NormalizeStateMachine
SCXML sm = semantics.normalizeStateMachine(stateMachine,
errorReporter);
// StoreStateMachine
this.stateMachine = sm;
}
/**
* Initiate state machine execution.
*
* @throws ModelException in case there is a fatal SCXML object
* model problem.
*/
public void go() throws ModelException {
// same as reset
this.reset();
}
/**
* Get the environment specific error reporter.
*
* @return Returns the errorReporter.
*/
public ErrorReporter getErrorReporter() {
return errorReporter;
}
/**
* Set the environment specific error reporter.
*
* @param errorReporter The errorReporter to set.
*/
public void setErrorReporter(final ErrorReporter errorReporter) {
this.errorReporter = errorReporter;
}
/**
* Get the event dispatcher.
*
* @return Returns the eventdispatcher.
*/
public EventDispatcher getEventdispatcher() {
return eventdispatcher;
}
/**
* Set the event dispatcher.
*
* @param eventdispatcher The eventdispatcher to set.
*/
public void setEventdispatcher(final EventDispatcher eventdispatcher) {
this.eventdispatcher = eventdispatcher;
}
/**
* Use "super-step", default is <code>true</code>
* (that is, run-to-completion is default).
*
* @return Returns the superStep property.
* @see #setSuperStep(boolean)
*/
public boolean isSuperStep() {
return superStep;
}
/**
* Set the super step.
*
* @param superStep
* if true, the internal derived events are also processed
* (run-to-completion);
* if false, the internal derived events are stored in the
* CurrentStatus property and processed within the next
* triggerEvents() invocation, also the immediate (empty event) transitions
* are deferred until the next step
*/
public void setSuperStep(final boolean superStep) {
this.superStep = superStep;
}
/**
* Add a listener to the document root.
*
* @param scxml The document root to attach listener to.
* @param listener The SCXMLListener.
*/
public void addListener(final SCXML scxml, final SCXMLListener listener) {
Object observable = scxml;
scInstance.getNotificationRegistry().addListener(observable, listener);
}
/**
* Remove this listener from the document root.
*
* @param scxml The document root.
* @param listener The SCXMLListener to be removed.
*/
public void removeListener(final SCXML scxml,
final SCXMLListener listener) {
Object observable = scxml;
scInstance.getNotificationRegistry().removeListener(observable,
listener);
}
/**
* Add a listener to this transition target.
*
* @param transitionTarget The <code>TransitionTarget</code> to
* attach listener to.
* @param listener The SCXMLListener.
*/
public void addListener(final TransitionTarget transitionTarget,
final SCXMLListener listener) {
Object observable = transitionTarget;
scInstance.getNotificationRegistry().addListener(observable, listener);
}
/**
* Remove this listener for this transition target.
*
* @param transitionTarget The <code>TransitionTarget</code>.
* @param listener The SCXMLListener to be removed.
*/
public void removeListener(final TransitionTarget transitionTarget,
final SCXMLListener listener) {
Object observable = transitionTarget;
scInstance.getNotificationRegistry().removeListener(observable,
listener);
}
/**
* Add a listener to this transition.
*
* @param transition The <code>Transition</code> to attach listener to.
* @param listener The SCXMLListener.
*/
public void addListener(final Transition transition,
final SCXMLListener listener) {
Object observable = transition;
scInstance.getNotificationRegistry().addListener(observable, listener);
}
/**
* Remove this listener for this transition.
*
* @param transition The <code>Transition</code>.
* @param listener The SCXMLListener to be removed.
*/
public void removeListener(final Transition transition,
final SCXMLListener listener) {
Object observable = transition;
scInstance.getNotificationRegistry().removeListener(observable,
listener);
}
/**
* Register an <code>Invoker</code> for this target type.
*
* @param targettype The target type (specified by "targettype"
* attribute of <invoke> tag).
* @param invokerClass The <code>Invoker</code> <code>Class</code>.
*/
public void registerInvokerClass(final String targettype,
final Class invokerClass) {
scInstance.registerInvokerClass(targettype, invokerClass);
}
/**
* Remove the <code>Invoker</code> registered for this target
* type (if there is one registered).
*
* @param targettype The target type (specified by "targettype"
* attribute of <invoke> tag).
*/
public void unregisterInvokerClass(final String targettype) {
scInstance.unregisterInvokerClass(targettype);
}
/**
* Get the state chart instance for this executor.
*
* @return The SCInstance for this executor.
*/
SCInstance getSCInstance() {
return scInstance;
}
/**
* Log the current set of active states.
*/
private void logState() {
if (log.isDebugEnabled()) {
Iterator si = currentStatus.getStates().iterator();
StringBuffer sb = new StringBuffer("Current States: [");
while (si.hasNext()) {
State s = (State) si.next();
sb.append(s.getId());
if (si.hasNext()) {
sb.append(", ");
}
}
sb.append(']');
log.debug(sb.toString());
}
}
/**
* @param step The most recent Step
*/
private void updateStatus(final Step step) {
currentStatus = step.getAfterStatus();
scInstance.getRootContext().setLocal("_ALL_STATES",
SCXMLHelper.getAncestorClosure(currentStatus.getStates(), null));
setEventData((TriggerEvent[]) currentStatus.getEvents().
toArray(new TriggerEvent[0]));
}
/**
* @param evts The events being triggered.
* @return Object[] Previous values.
*/
private Object[] setEventData(final TriggerEvent[] evts) {
Context rootCtx = scInstance.getRootContext();
Object[] oldData = {rootCtx.get(EVENT_DATA),
rootCtx.get(EVENT_DATA_MAP)};
int len = evts.length;
if (len > 0) { // 0 has retry semantics (eg: see usage in reset())
Object eventData = null;
Map payloadMap = new HashMap();
for (int i = 0; i < len; i++) {
TriggerEvent te = evts[i];
payloadMap.put(te.getName(), te.getPayload());
}
if (len == 1) {
// we have only one event
eventData = evts[0].getPayload();
}
rootCtx.setLocal(EVENT_DATA, eventData);
rootCtx.setLocal(EVENT_DATA_MAP, payloadMap);
}
return oldData;
}
/**
* @param oldData The old values to restore to.
*/
private void restoreEventData(final Object[] oldData) {
scInstance.getRootContext().setLocal(EVENT_DATA, oldData[0]);
scInstance.getRootContext().setLocal(EVENT_DATA_MAP, oldData[1]);
}
/**
* The special variable for storing single event data / payload.
*/
private static final String EVENT_DATA = "_eventdata";
/**
* The special variable for storing event data / payload,
* when multiple events are triggered, keyed by event name.
*/
private static final String EVENT_DATA_MAP = "_eventdatamap";
/**
* SCXMLExecutor put into motion without setting a model (state machine).
*/
private static final String ERR_NO_STATE_MACHINE =
"SCXMLExecutor: State machine not set";
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -