org.squirrelframework.foundation.fsm.StateMachine Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of squirrel-foundation Show documentation
Show all versions of squirrel-foundation Show documentation
foundation module of squirrel framework which provided event driven infrastructure and a finite state machine implementation.
package org.squirrelframework.foundation.fsm;
import java.lang.reflect.Method;
import java.util.List;
import org.squirrelframework.foundation.component.Observable;
import org.squirrelframework.foundation.event.SquirrelEvent;
import org.squirrelframework.foundation.fsm.ActionExecutor.ExecActionLisenter;
import org.squirrelframework.foundation.util.ReflectUtils;
/**
* Interface for finite state machine.
*
* @author Henry.He
*
* @param type of State Machine
* @param type of State
* @param type of Event
* @param type of Context
*/
public interface StateMachine, S, E, C> extends Visitable, Observable {
/**
* Fires the specified event
* @param event the event
* @param context external context
*/
void fire(E event, C context);
/**
* Test transition result under circumstance
* @param event test event
* @param context text context
* @return test transition result
*/
S test(E event, C context);
/**
* Start state machine under external context
* @param context external context
*/
void start(C context);
/**
* Terminate state machine under external context
* @param context external context
*/
void terminate(C context);
/**
* @return current status of state machine
*/
StateMachineStatus getStatus();
/**
* @return type-safe state machine instance
*/
T getThis();
/**
* @return current state id of state machine
*/
S getCurrentState();
/**
* @return last active state id of state machine
*/
S getLastState();
/**
* @return id of state machine initial state
*/
S getInitialState();
/**
* @param parentStateId id of parent state
* @return last active child state of the parent state
*/
S getLastActiveChildStateOf(S parentStateId);
/**
* @param parentStateId
* @return sub state of parallel state
*/
List getSubStatesOn(S parentStateId);
/**
* @return current raw state of state machine
*/
ImmutableState getCurrentRawState();
/**
* @return last active raw state of state machine
*/
ImmutableState getLastRawState();
/**
* @return initial raw state of state machine
*/
ImmutableState getInitialRawState();
@Deprecated
ImmutableState getRawStateFrom(S stateId);
/**
* Dump current state machine data. This operation can only be done when state machine status is
* {@link StateMachineStatus#IDLE}, otherwise null will be returned.
*
* @return dumped state machine data reader
*/
StateMachineData.Reader dumpSavedData();
/**
* Load saved data for current state machine. The operation can only be done when state machine
* status is {@link StateMachineStatus#INITIALIZED} or {@link StateMachineStatus#TERMINATED}.
*
* @param savedData provided saved data
* @return true if load saved data success otherwise false
*/
boolean loadSavedData(StateMachineData.Reader savedData);
/**
* @return whether state machine is context sensitive
*/
boolean isContextSensitive();
interface StateMachineListener, S, E, C> {
// leverage bridge method to call the method of actual listener
public static final Method STATEMACHINE_EVENT_METHOD = ReflectUtils.getMethod(
StateMachineListener.class, "stateMachineEvent", new Class>[]{StateMachineEvent.class});
void stateMachineEvent(StateMachineEvent event);
}
interface StateMachineEvent, S, E, C> extends SquirrelEvent {
T getStateMachine();
}
void addStateMachineListener(StateMachineListener listener);
void removeStateMachineListener(StateMachineListener listener);
interface StartListener, S, E, C> {
public static final Method START_EVENT_METHOD = ReflectUtils.getMethod(
StartListener.class, "started", new Class>[]{StartEvent.class});
void started(StartEvent event);
}
interface StartEvent , S, E, C> extends StateMachineEvent {}
void addStartListener(StartListener listener);
void removeStartListener(StartListener listener);
interface TerminateListener, S, E, C> {
public static final Method TERMINATE_EVENT_METHOD = ReflectUtils.getMethod(
TerminateListener.class, "terminated", new Class>[]{TerminateEvent.class});
void terminated(TerminateEvent event);
}
interface TerminateEvent , S, E, C> extends StateMachineEvent {}
void addTerminateListener(TerminateListener listener);
void removeTerminateListener(TerminateListener listener);
interface StateMachineExceptionListener, S, E, C> {
public static final Method STATEMACHINE_EXCEPTION_EVENT_METHOD = ReflectUtils.getMethod(
StateMachineExceptionListener.class, "stateMachineException", new Class>[]{StateMachineExceptionEvent.class});
void stateMachineException(StateMachineExceptionEvent event);
}
interface StateMachineExceptionEvent, S, E, C> extends StateMachineEvent {
Exception getException();
}
void addStateMachineExceptionListener(StateMachineExceptionListener listener);
void removeStateMachineExceptionListener(StateMachineExceptionListener listener);
interface TransitionEvent, S, E, C> extends StateMachineEvent {
S getSourceState();
E getCause();
C getContext();
}
interface TransitionBeginListener, S, E, C> {
public static final Method TRANSITION_BEGIN_EVENT_METHOD = ReflectUtils.getMethod(
TransitionBeginListener.class, "transitionBegin", new Class>[]{TransitionBeginEvent.class});
void transitionBegin(TransitionBeginEvent event);
}
interface TransitionBeginEvent, S, E, C> extends TransitionEvent {}
void addTransitionBeginListener(TransitionBeginListener listener);
void removeTransitionBeginListener(TransitionBeginListener listener);
interface TransitionCompleteListener, S, E, C> {
public static final Method TRANSITION_COMPLETE_EVENT_METHOD = ReflectUtils.getMethod(
TransitionCompleteListener.class, "transitionComplete", new Class>[]{TransitionCompleteEvent.class});
void transitionComplete(TransitionCompleteEvent event);
}
interface TransitionCompleteEvent, S, E, C> extends TransitionEvent {
S getTargetState();
}
void addTransitionCompleteListener(TransitionCompleteListener listener);
void removeTransitionCompleteListener(TransitionCompleteListener listener);
interface TransitionExceptionListener, S, E, C> {
public static final Method TRANSITION_EXCEPTION_EVENT_METHOD = ReflectUtils.getMethod(
TransitionExceptionListener.class, "transitionException", new Class>[]{TransitionExceptionEvent.class});
void transitionException(TransitionExceptionEvent event);
}
interface TransitionExceptionEvent, S, E, C> extends TransitionEvent {
S getTargetState();
Exception getException();
}
void addTransitionExceptionListener(TransitionExceptionListener listener);
void removeTransitionExceptionListener(TransitionExceptionListener listener);
interface TransitionDeclinedListener, S, E, C> {
public static final Method TRANSITION_DECLINED_EVENT_METHOD = ReflectUtils.getMethod(
TransitionDeclinedListener.class, "transitionDeclined", new Class>[]{TransitionDeclinedEvent.class});
void transitionDeclined(TransitionDeclinedEvent event);
}
interface TransitionDeclinedEvent, S, E, C> extends TransitionEvent {}
void addTransitionDeclinedListener(TransitionDeclinedListener listener);
void removeTransitionDecleindListener(TransitionDeclinedListener listener);
void addExecActionListener(ExecActionLisenter listener);
void removeExecActionListener(ExecActionLisenter listener);
}