All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.squirrelframework.foundation.fsm.StateMachine Maven / Gradle / Ivy

Go to download

foundation module of squirrel framework which provided event driven infrastructure and a finite state machine implementation.

There is a newer version: 0.3.10
Show newest version
package org.squirrelframework.foundation.fsm;

import java.util.List;

import org.squirrelframework.foundation.component.Observable;
import org.squirrelframework.foundation.event.SquirrelEvent;
import org.squirrelframework.foundation.fsm.ActionExecutor.ExecActionLisenter;

/**
 * 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);
    
    /**
     * @return current state id of state machine
     */
    S getCurrentState();
    
    /**
     * @return last active state id of state machine
     */
    S getLastState();
    
    /**
     * @param stateId the identify of state 
     * @return raw state of the same state identify
     */
    ImmutableState getRawStateFrom(S stateId);
    
    /**
     * @return current raw state of state machine
     */
    ImmutableState getCurrentRawState();
    
    /**
     * @return last active raw state of state machine
     */
    ImmutableState getLastRawState();
    
    /**
     * @param parentStateId id of parent state
     * @return last active child state of the parent state
     */
    S getLastActiveChildStateOf(S parentStateId);
    
    List getSubStatesOn(S parentState);
    
    /**
     * @return id of state machine initial state
     */
    S getInitialState();
    
    /**
     * 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();
    
    T getThis();
    
    interface StateMachineListener, S, E, C> {
        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> {
        void started(StartEvent event);
    }
    interface StartEvent , S, E, C> extends StateMachineEvent {}
    void addStartListener(StartListener listener);
    void removeStartListener(StartListener listener);
    
    interface TerminateListener, S, E, C> {
        void terminated(TerminateEvent event);
    }
    interface TerminateEvent , S, E, C> extends StateMachineEvent {}
    void addTerminateListener(TerminateListener listener);
    void removeTerminateListener(TerminateListener listener);
    
    interface StateMachineExceptionListener, S, E, C> {
        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> {
        void transitionBegin(TransitionBeginEvent event);
    }
    interface TransitionBeginEvent, S, E, C> extends TransitionEvent {}
    void addTransitionBeginListener(TransitionBeginListener listener);
    void removeTransitionBeginListener(TransitionBeginListener listener);
    
    interface TransitionCompleteListener, S, E, C> {
        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> {
        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> {
        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);
}