sgdk::IndexTypeFSM<> Class Template Reference
[FSM Module]

#include <swiss_gd_knife/ai/fsm/base.hpp>

List of all members.


Detailed Description

template<>
class sgdk::IndexTypeFSM<>

Simple implementation of a dynamic finite state machine. It delegates initialization of its internal transition function and its start and end states to outside classes via the constructor and the initialize() function.

The example code shows how to construct an initializer class and to use an IndexTypeFSM.

// std::ostream and std::cout
#include <iostream>
// std::string
#include <string>
// std::vector
#include <vector>
// sgdk::IndexTypeFSM
#include <swiss_gd_knife/ai/fsm/base.hpp>
// sgdk::DenseMatrix
#include <swiss_gd_knife/zref/matrix.hpp>

/*
 * Initializer class template for the IndexTypeFSM in this example.
 */
template <typename Index>
class SampleFSMBuilder
{
 private:
    Index _state_count;
    Index _input_count;

 public:
    /*
     * Constructs a new SampleFSMBuilder that will keep track of the specified
     * number of states and inputs recognized by the example FSM.
     */
    SampleFSMBuilder(const Index state_count, const Index input_count)
      : _state_count(state_count)
      , _input_count(input_count)
    {
    }

    /*
     * Builds the example FSM's internal transition function.
     */
    template <typename TransitionFunction>
    void buildTransitionFunction(TransitionFunction& transition_function) const
    {
        transition_function.resize(_state_count, _input_count);

        // current state = S, input = 0 --> next state = A
        transition_function.setTransition(0, 0, 1);

        // current state = S, input = 1 --> next state = A
        transition_function.setTransition(0, 1, 1);

        // current state = S, input = 2 --> next state = B
        transition_function.setTransition(0, 2, 2);

        // current state = A, input = 1 --> next state = B
        transition_function.setTransition(1, 1, 2);

        // current state = A, input = 2 --> next state = S
        transition_function.setTransition(1, 2, 0);

        // current state = B, input = 3 --> next state = F
        transition_function.setTransition(2, 3, 3);
    }

    /*
     * Returns the example start state.
     */
    Index getSourceState() const
    {
        return 0;
    }

    /*
     * Returns the example end state.
     */
    Index getTargetState() const
    {
        return 3;
    }
};

/*
 * Runs the example.
 */
int main()
{
    typedef unsigned int
            Index;
    typedef sgdk::IndexTypeFSM<sgdk::DenseMatrix<Index> >
            FiniteStateMachine;

    std::vector<std::string> state;

    state.push_back("S");
    state.push_back("A");
    state.push_back("B");
    state.push_back("F");

    std::vector<Index> input;

    SampleFSMBuilder<Index> builder(state.size(), 4);
    FiniteStateMachine fsm(builder);

    std::cout << "FSM start state: " << state[fsm.getCurrentState()];  // S
    std::cout << std::endl;

    std::cout << "Processing input: 0" << std::endl;
    fsm.processInput(0);
    std::cout << "FSM current state: " << state[fsm.getCurrentState()];  // A
    std::cout << std::endl;

    std::cout << "Processing input: 2" << std::endl;
    fsm.processInput(2);
    std::cout << "FSM current state: " << state[fsm.getCurrentState()];  // S
    std::cout << std::endl;

    std::cout << "Processing input: 1" << std::endl;
    fsm.processInput(1);
    std::cout << "FSM current state: " << state[fsm.getCurrentState()];  // A
    std::cout << std::endl;

    std::cout << "Processing input: 3" << std::endl;
    fsm.processInput(3);
    std::cout << "FSM current state: " << state[fsm.getCurrentState()];  // A
    std::cout << std::endl;

    std::cout << "Resetting" << std::endl;
    fsm.reset();
    std::cout << "FSM current state: " << state[fsm.getCurrentState()];  // S
    std::cout << std::endl;

    while (!fsm.hasReachedTargetState())
    {
        fsm.makeNontrivialInputs(input);

        if (input.empty())
        {
            // Should not go here.
            break;
        }
        else
        {
            for (Index i = 0; i < input.size(); ++i)
            {
                std::cout << "If input " << input[i];
                std::cout << " is processed, the next state will be ";
                std::cout << state[fsm.getNextState(input[i])] << std::endl;
            }
        }

        // (input, next state) = (0, A), then (1, B), then (3, F)
        std::cout << "Processing input: " << input[0] << std::endl;
        fsm.processInput(input[0]);
        std::cout << "FSM current state: " << state[fsm.getCurrentState()];
        std::cout << std::endl;

        input.clear();
    }

    if (!fsm.hasReachedTargetState())
    {
        std::cout << "FSM target state unreachable." << std::endl;
    }

    return 0;
}


Template parameters

Parameter Description Default
StateInputMatrix The type of the underlying table used to implement the internal transition function.
Index The type of the states and inputs. unsigned int


Model of


Type requirements



Public Types

typedef implementation_defined TransitionFunction

Public Member Functions

 IndexTypeFSM ()
 The default constructor.
template<typename IndexTypeFSMBuilder>
 IndexTypeFSM (const IndexTypeFSMBuilder &fsm_builder)
 IndexTypeFSM (const IndexTypeFSM &copy)
 The copy constructor.
IndexTypeFSMoperator= (const IndexTypeFSM &copy)
 The assignment operator.
template<typename IndexTypeFSMBuilder>
void initialize (const IndexTypeFSMBuilder &fsm_builder)
const TransitionFunctiongetTransitionFunction () const
void reset ()
Index getCurrentState () const
template<typename BackInsertionSequence>
void makeNontrivialInputs (BackInsertionSequence &sequence) const
Index getNextState (const Index input) const
void processInput (const Index input)
bool hasReachedTargetState () const


Member Typedef Documentation

typedef implementation_defined sgdk::IndexTypeFSM<>::TransitionFunction
 

The type of the internal transition function of this IndexTypeFSM.


Constructor & Destructor Documentation

sgdk::IndexTypeFSM<>::IndexTypeFSM  )  [inline]
 

Constructs a new IndexTypeFSM, to be initialized later.

template<typename IndexTypeFSMBuilder>
sgdk::IndexTypeFSM<>::IndexTypeFSM const IndexTypeFSMBuilder &  fsm_builder  )  [inline, explicit]
 

Constructs a new IndexTypeFSM.

Initializes the internal transition function of this IndexTypeFSM; sets its start and end states to the source and target states, respectively, as defined by the specified initializer; and sets its current state to its start state.

Parameters:
fsm_builder the object to which initialization of this IndexTypeFSM is delegated.
Precondition:
IndexTypeFSMBuilder must implement buildTransitionFunction() with IndexTypeFSM::TransitionFunction as the argument type.

IndexTypeFSMBuilder must implement getSourceState(), and the function's return type must be convertible to IndexType.

IndexTypeFSMBuilder must implement getTargetState(), and the function's return type must be convertible to IndexType.

All of these functions must be const qualified.

sgdk::IndexTypeFSM<>::IndexTypeFSM const IndexTypeFSM<> &  copy  )  [inline]
 

Constructs a new IndexTypeFSM that will be internally equivalent to the copy at this point in time.

Parameters:
copy the IndexTypeFSM being copied.


Member Function Documentation

IndexTypeFSM& sgdk::IndexTypeFSM<>::operator= const IndexTypeFSM<> &  copy  )  [inline]
 

Sets this IndexTypeFSM to be internally equivalent to the copy.

Parameters:
copy the IndexTypeFSM being copied.
Returns:
a reference to this IndexTypeFSM.

template<typename IndexTypeFSMBuilder>
void sgdk::IndexTypeFSM<>::initialize const IndexTypeFSMBuilder &  fsm_builder  ) 
 

Initializes the internal transition function of this IndexTypeFSM; sets its start and end states to the source and target states, respectively, as defined by the specified initializer; and sets its current state to its start state.

Parameters:
fsm_builder the object to which initialization of this IndexTypeFSM is delegated.
Precondition:
IndexTypeFSMBuilder must implement buildTransitionFunction() with IndexTypeFSM::TransitionFunction as the argument type.

IndexTypeFSMBuilder must implement getSourceState(), and the function's return type must be convertible to IndexType.

IndexTypeFSMBuilder must implement getTargetState(), and the function's return type must be convertible to IndexType.

All of these functions must be const qualified.

const TransitionFunction& sgdk::IndexTypeFSM<>::getTransitionFunction  )  const [inline]
 

Returns a const reference to the transition function by which this IndexTypeFSM operates.

Returns:
a const reference to the internal transition function of this IndexTypeFSM.

void sgdk::IndexTypeFSM<>::reset  )  [inline]
 

Sets the current state of this IndexTypeFSM to its start state.

Index sgdk::IndexTypeFSM<>::getCurrentState  )  const [inline]
 

Returns the current state of this IndexTypeFSM. After a call to initialize() or reset(), an immediate call to this function returns the start state.

Returns:
the current state of this IndexTypeFSM.

template<typename BackInsertionSequence>
void sgdk::IndexTypeFSM<>::makeNontrivialInputs BackInsertionSequence &  sequence  )  const
 

Outputs the set of inputs that will change the current state of this IndexTypeFSM if it processes any of them via its processInput() function.

Parameters:
sequence the output set.
Precondition:
BackInsertionSequence models the Back Insertion Sequence concept.

Index sgdk::IndexTypeFSM<>::getNextState const Index  input  )  const [inline]
 

Returns the state that will be returned by a subsequent call to getCurrentState() if the specified input is passed to processInput().

Parameters:
input the specified input.
Precondition:
input is a valid input according to the internal transition function.
Returns:
the state to which this IndexTypeFSM will transition upon processing the specified input.

void sgdk::IndexTypeFSM<>::processInput const Index  input  )  [inline]
 

Processes the specified input. If it is one of the inputs passed out from a call to makeNontrivialInputs() while in the current state, then the current state will change according to the internal transition function of this IndexTypeFSM.

Parameters:
input the input to be processed.
Precondition:
input is a valid input according to the internal transition function.

bool sgdk::IndexTypeFSM<>::hasReachedTargetState  )  const [inline]
 

Returns true if this IndexTypeFSM will not process any more inputs, false otherwise.

Returns:
true if the processInput() function will have no effect regardless of the parameter value, false otherwise.


Swiss GD Knife is hosted by SourceForge.net.