#include <swiss_gd_knife/ai/fsm/base.hpp>
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; }
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 |
Index
must be an unsigned integral type. StateInputMatrix::operator()
must be convertible to const Index&
.
Public Types | |
typedef implementation_defined | TransitionFunction |
Public Member Functions | |
IndexTypeFSM () | |
The default constructor. | |
template<typename IndexTypeFSMBuilder> | |
IndexTypeFSM (const IndexTypeFSMBuilder &fsm_builder) | |
IndexTypeFSM (const IndexTypeFSM ©) | |
The copy constructor. | |
IndexTypeFSM & | operator= (const IndexTypeFSM ©) |
The assignment operator. | |
template<typename IndexTypeFSMBuilder> | |
void | initialize (const IndexTypeFSMBuilder &fsm_builder) |
const TransitionFunction & | getTransitionFunction () 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 |
|
The type of the internal transition function of this |
|
Constructs a new |
|
Constructs a new
Initializes the internal transition function of this
|
|
Constructs a new
|
|
Sets this
|
|
Initializes the internal transition function of this
|
|
Returns a const reference to the transition function by which this
|
|
Sets the current state of this |
|
Returns the current state of this
|
|
Outputs the set of inputs that will change the current state of this
|
|
Returns the state that will be returned by a subsequent call to
|
|
Processes the specified input. If it is one of the inputs passed out from a call to
|
|
Returns
|
Swiss GD Knife is hosted by .