OpenASIP  2.0
Public Types | Public Member Functions | Static Public Attributes | Protected Types | Protected Attributes | Private Types | Private Attributes | Friends | List of all members
FiniteStateAutomaton Class Reference

#include <FiniteStateAutomaton.hh>

Inheritance diagram for FiniteStateAutomaton:
Inheritance graph
Collaboration diagram for FiniteStateAutomaton:
Collaboration graph

Public Types

typedef int FSAStateTransitionIndex
 Type used for indexing the transitions. More...
 
typedef int FSAStateIndex
 Type used for indexing the states. More...
 
typedef std::set< FSAStateIndexFSAStateIndexSet
 Type for a set of state indices. More...
 

Public Member Functions

 FiniteStateAutomaton (FSAStateTransitionIndex defaultState=ILLEGAL_STATE, int transitionCount=0)
 
virtual ~FiniteStateAutomaton ()
 
virtual const std::string & transitionName (FSAStateTransitionIndex transition) const
 
virtual std::string stateName (FSAStateIndex state) const
 
virtual FSAStateTransitionIndex transitionIndex (const std::string &transitionName) const
 
virtual FSAStateIndex destinationState (FSAStateIndex source, FSAStateTransitionIndex transition)
 
virtual bool isLegalTransition (FSAStateIndex source, FSAStateTransitionIndex transition)
 
virtual FSAStateIndex addState ()
 
virtual FSAStateTransitionIndex addTransitionType (const std::string &name)
 
virtual void setTransitionName (FSAStateTransitionIndex transition, const std::string &name)
 
virtual void setTransition (FSAStateIndex source, FSAStateIndex destination, FSAStateTransitionIndex transition)
 
virtual std::string toDotString () const
 
virtual FSAStateIndex startState () const
 

Static Public Attributes

static const FSAStateIndex ILLEGAL_STATE = -1
 A state id which denotes an illegal state. More...
 
static const FSAStateIndex UNKNOWN_STATE = -2
 A state id which denotes an unknown (unresolved) state. Used for lazy construction of states. More...
 

Protected Types

typedef std::vector< FSAStateIndexTransitionVector
 Vector which stores the target states of for each transition type. More...
 
typedef std::vector< TransitionVectorTransitionMap
 Type for storing the transitions of each state. The vector stores for each state (indexed from 0) a vector with elements as many there are transitions. The value of the element is the target state to which the transition leads to, or ILLEGAL_STATE_INDEX if there is no transition for that transition type from that state. Using this structure it's possible to find the target state using two table (vector) lookups. More...
 

Protected Attributes

TransitionMap transitions_
 The state transitions. In protected to allow fast access from derived classes. More...
 

Private Types

typedef std::map< std::string, FSAStateTransitionIndexTransitionNameIndex
 Type for finding the index for a transition using its name. More...
 

Private Attributes

int stateCount_
 Count of states in the automaton. More...
 
int transitionTypeCount_
 Count of different transition types in the automaton. More...
 
std::vector< std::string > transitionNames_
 Names of the state transitions types (indexed from 0). More...
 
TransitionNameIndex transitionIndices_
 Indices of the state transition types. More...
 
const FSAStateTransitionIndex defaultState_
 The default target state for state transitions. More...
 

Friends

class FSAFUResourceConflictDetector
 Friend due to highly optimized compiled simulation versions of the conflict detection functions. More...
 

Detailed Description

Generic finite state automaton (FSA).

This is a generic structure for FSA. The building and initialization of the states is the responsibility of derived classes.

Definition at line 49 of file FiniteStateAutomaton.hh.

Member Typedef Documentation

◆ FSAStateIndex

Type used for indexing the states.

Definition at line 57 of file FiniteStateAutomaton.hh.

◆ FSAStateIndexSet

Type for a set of state indices.

Definition at line 59 of file FiniteStateAutomaton.hh.

◆ FSAStateTransitionIndex

Type used for indexing the transitions.

Definition at line 55 of file FiniteStateAutomaton.hh.

◆ TransitionMap

typedef std::vector<TransitionVector> FiniteStateAutomaton::TransitionMap
protected

Type for storing the transitions of each state. The vector stores for each state (indexed from 0) a vector with elements as many there are transitions. The value of the element is the target state to which the transition leads to, or ILLEGAL_STATE_INDEX if there is no transition for that transition type from that state. Using this structure it's possible to find the target state using two table (vector) lookups.

Definition at line 116 of file FiniteStateAutomaton.hh.

◆ TransitionNameIndex

Type for finding the index for a transition using its name.

Definition at line 126 of file FiniteStateAutomaton.hh.

◆ TransitionVector

typedef std::vector<FSAStateIndex> FiniteStateAutomaton::TransitionVector
protected

Vector which stores the target states of for each transition type.

Definition at line 107 of file FiniteStateAutomaton.hh.

Constructor & Destructor Documentation

◆ FiniteStateAutomaton()

FiniteStateAutomaton::FiniteStateAutomaton ( FSAStateTransitionIndex  defaultState = ILLEGAL_STATE,
int  transitionCount = 0 
)

Constructor.

Parameters
defaultStateThe default state for state transitions (either ILLEGAL_STATE or UNKNOWN_STATE, as no other states are known at this point).
transitionCountThe initial count of transitions.

Definition at line 55 of file FiniteStateAutomaton.cc.

57  :
58  stateCount_(0),
59  transitionTypeCount_(transitionCount), transitionNames_(transitionCount),
60  defaultState_(defaultState) {
61 }

◆ ~FiniteStateAutomaton()

FiniteStateAutomaton::~FiniteStateAutomaton ( )
virtual

Destructor.

Definition at line 66 of file FiniteStateAutomaton.cc.

66  {
67 #if 0
68  Application::logStream() << stateCount_ << " states" << std::endl;
69 #endif
70 }

References Application::logStream(), and stateCount_.

Here is the call graph for this function:

Member Function Documentation

◆ addState()

FiniteStateAutomaton::FSAStateIndex FiniteStateAutomaton::addState ( )
virtual

Adds a new state to the automaton.

Returns
Returns the index of the new state. Indexing starts from 0.

Definition at line 178 of file FiniteStateAutomaton.cc.

178  {
179  // add transition vector with no transitions for the new state
180  transitions_.push_back(
182  return stateCount_++;
183 }

References defaultState_, stateCount_, transitions_, and transitionTypeCount_.

Referenced by FUFiniteStateAutomaton::FUFiniteStateAutomaton(), and FUFiniteStateAutomaton::resolveState().

◆ addTransitionType()

FiniteStateAutomaton::FSAStateTransitionIndex FiniteStateAutomaton::addTransitionType ( const std::string &  name)
virtual

Adds a new transition type to the automaton.

Parameters
nameName of the transition type. Must be unique within all transition types.
Returns
Returns the index of the new transition type. Indexing starts from 0.

Definition at line 194 of file FiniteStateAutomaton.cc.

194  {
195 
196  transitionNames_.push_back(name.c_str());
197  transitionIndices_.insert(
198  TransitionNameIndex::value_type(name, transitionTypeCount_));
199 
200  // increment the size of each state's transition vectors to
201  // match the new count of transition types
202  for (TransitionMap::iterator i = transitions_.begin();
203  i != transitions_.end(); ++i) {
204  TransitionVector& vec = *i;
205  vec.resize(transitionTypeCount_ + 1, defaultState_);
206  }
207 
208  return transitionTypeCount_++;
209 }

References defaultState_, transitionIndices_, transitionNames_, transitions_, and transitionTypeCount_.

◆ destinationState()

FiniteStateAutomaton::FSAStateIndex FiniteStateAutomaton::destinationState ( FSAStateIndex  source,
FSAStateTransitionIndex  transition 
)
virtual

Returns the index of the state resulting from the given transition from the given source state.

This method is called often, thus should be as fast as possible. Therefore, all range checking etc. is disabled. Method is not const due to possibility of lazy initialization of states in derived classes.

Parameters
sourceThe source state.
transitionThe transition.
Returns
The index of the destination state.

Reimplemented in FUFiniteStateAutomaton.

Definition at line 146 of file FiniteStateAutomaton.cc.

147  {
148  return transitions_[source][transition];
149 }

References transitions_.

◆ isLegalTransition()

bool FiniteStateAutomaton::isLegalTransition ( FSAStateIndex  source,
FSAStateTransitionIndex  transition 
)
virtual

Returns true in case the given transition is legal (accepted by the state machine) from the given state.

This method is called often, thus should be as fast as possible. Therefore, all range checking etc. is disabled. Method is not const due to possibility of lazy initialization of states in derived classes.

Parameters
sourceThe source state.
transitionThe transition.
Returns
True in case the transition is legal.

Reimplemented in FUFiniteStateAutomaton.

Definition at line 165 of file FiniteStateAutomaton.cc.

167  {
168  return transitions_[source][transition] != ILLEGAL_STATE;
169 }

References ILLEGAL_STATE, and transitions_.

◆ setTransition()

void FiniteStateAutomaton::setTransition ( FSAStateIndex  source,
FSAStateIndex  destination,
FSAStateTransitionIndex  transition 
)
virtual

Set a transition between two states.

Parameters
sourceThe source state.
destinationThe destination state.
transitionThe type of transition.

Definition at line 219 of file FiniteStateAutomaton.cc.

222  {
223 
224  transitions_.at(source).at(transition) = destination;
225 }

References transitions_.

Referenced by FUFiniteStateAutomaton::resolveState().

◆ setTransitionName()

void FiniteStateAutomaton::setTransitionName ( FSAStateTransitionIndex  transition,
const std::string &  name 
)
virtual

Sets a name for the given transition.

Parameters
transitionThe index of the transition of which name to return.
nameThe name of the transition.

Definition at line 93 of file FiniteStateAutomaton.cc.

94  {
95  transitionNames_.at(transition) = name;
96  transitionIndices_[name] = transition;
97 }

References transitionIndices_, and transitionNames_.

Referenced by FUFiniteStateAutomaton::FUFiniteStateAutomaton().

◆ startState()

FiniteStateAutomaton::FSAStateIndex FiniteStateAutomaton::startState ( ) const
virtual

Returns the starting state of the automaton.

By default the starting state is assumed to be the state 0.

Returns
The starting state.

Definition at line 282 of file FiniteStateAutomaton.cc.

282  {
283  return 0;
284 }

Referenced by FSAFUResourceConflictDetector::isIdle(), and FSAFUResourceConflictDetector::reset().

◆ stateName()

std::string FiniteStateAutomaton::stateName ( FSAStateIndex  state) const
virtual

Returns the textual description of the state at the given index.

Parameters
stateThe index of the state of which name to return.
Returns
The name of the state.

Reimplemented in FUFiniteStateAutomaton.

Definition at line 106 of file FiniteStateAutomaton.cc.

106  {
107  return Conversion::toString(state);
108 }

References Conversion::toString().

Referenced by toDotString().

Here is the call graph for this function:

◆ toDotString()

std::string FiniteStateAutomaton::toDotString ( ) const
virtual

Creates a string that represents the FSA as a graph in GraphViz dot format.

This string can be rendered to a visual graph using the 'dot' tool.

Returns
The FSA in dot graph format.

Definition at line 235 of file FiniteStateAutomaton.cc.

235  {
236 
237  std::ostringstream s;
238  s << "digraph G {" << std::endl;
239 
240  // first print all the states as nodes
241  for (int i = 0; i < stateCount_; ++i) {
242  s << "\tS" << i << " [label=\"S" << i << "\\n" << stateName(i)
243  << "\"]; " << std::endl;
244  }
245 
246  // print transitions as edges
247  for (int i = 0; i < stateCount_; ++i) {
248  const TransitionVector& vec = transitions_.at(i);
249  for (int t = 0; t < transitionTypeCount_; ++t) {
250  std::string edgeLabel = transitionName(t);
251  if (edgeLabel == "")
252  edgeLabel = "[NOP]";
253  edgeLabel = edgeLabel + " (" + Conversion::toString(t) + ")";
254  switch (vec.at(t)) {
255  case ILLEGAL_STATE:
256  s << "\t/* No transition " << edgeLabel << " from S" << i
257  << " possible */" << std::endl;
258  break;
259  case UNKNOWN_STATE:
260  s << "\t/* Transition " << edgeLabel << " from S" << i
261  << " not initialized yet */" << std::endl;
262  break;
263  default:
264  s << "\tS" << i << " -> " << "S" << vec.at(t) << "[label=\""
265  << edgeLabel << "\"];" << std::endl;
266  break;
267  }
268  }
269  }
270  s << "}" << std::endl;
271  return s.str();
272 }

References ILLEGAL_STATE, stateCount_, stateName(), Conversion::toString(), transitionName(), transitions_, transitionTypeCount_, and UNKNOWN_STATE.

Referenced by FSAFUResourceConflictDetector::writeToDotFile().

Here is the call graph for this function:

◆ transitionIndex()

FiniteStateAutomaton::FSAStateTransitionIndex FiniteStateAutomaton::transitionIndex ( const std::string &  transitionName) const
virtual

Returns the index of the transition with given name.

Returns the transition index for an operation in case of an FU FSA.

Parameters
transitionNameThe name of the transition of index to return.
Returns
The index.
Exceptions
KeyNotFoundIn case no transition with given is found.

Definition at line 120 of file FiniteStateAutomaton.cc.

120  {
121  TransitionNameIndex::const_iterator i =
123 
124  if (i == transitionIndices_.end())
125  throw KeyNotFound(
126  __FILE__, __LINE__, __func__,
127  (boost::format("No transition '%s'") % transitionName).str());
128 
129  return (*i).second;
130 }

References __func__, transitionIndices_, and transitionName().

Referenced by FUFiniteStateAutomaton::operationCollisionMatrix(), and FSAFUResourceConflictDetector::operationID().

Here is the call graph for this function:

◆ transitionName()

const std::string & FiniteStateAutomaton::transitionName ( FSAStateTransitionIndex  transition) const
virtual

Returns the textual description of the transition at the given index.

For example, returns the name of the operation in case of an FU FSA.

Parameters
transitionThe index of the transition of which name to return.
Returns
The name of the transition.

Definition at line 81 of file FiniteStateAutomaton.cc.

82  {
83  return transitionNames_.at(transition);
84 }

References transitionNames_.

Referenced by FSAFUResourceConflictDetector::operationName(), toDotString(), and transitionIndex().

Friends And Related Function Documentation

◆ FSAFUResourceConflictDetector

friend class FSAFUResourceConflictDetector
friend

Friend due to highly optimized compiled simulation versions of the conflict detection functions.

Definition at line 53 of file FiniteStateAutomaton.hh.

Member Data Documentation

◆ defaultState_

const FSAStateTransitionIndex FiniteStateAutomaton::defaultState_
private

The default target state for state transitions.

Definition at line 136 of file FiniteStateAutomaton.hh.

Referenced by addState(), and addTransitionType().

◆ ILLEGAL_STATE

const FiniteStateAutomaton::FSAStateIndex FiniteStateAutomaton::ILLEGAL_STATE = -1
static

◆ stateCount_

int FiniteStateAutomaton::stateCount_
private

Count of states in the automaton.

Definition at line 128 of file FiniteStateAutomaton.hh.

Referenced by addState(), toDotString(), and ~FiniteStateAutomaton().

◆ transitionIndices_

TransitionNameIndex FiniteStateAutomaton::transitionIndices_
private

Indices of the state transition types.

Definition at line 134 of file FiniteStateAutomaton.hh.

Referenced by addTransitionType(), setTransitionName(), and transitionIndex().

◆ transitionNames_

std::vector<std::string> FiniteStateAutomaton::transitionNames_
private

Names of the state transitions types (indexed from 0).

Definition at line 132 of file FiniteStateAutomaton.hh.

Referenced by addTransitionType(), setTransitionName(), and transitionName().

◆ transitions_

TransitionMap FiniteStateAutomaton::transitions_
protected

◆ transitionTypeCount_

int FiniteStateAutomaton::transitionTypeCount_
private

Count of different transition types in the automaton.

Definition at line 130 of file FiniteStateAutomaton.hh.

Referenced by addState(), addTransitionType(), and toDotString().

◆ UNKNOWN_STATE

const FiniteStateAutomaton::FSAStateIndex FiniteStateAutomaton::UNKNOWN_STATE = -2
static

A state id which denotes an unknown (unresolved) state. Used for lazy construction of states.

Definition at line 64 of file FiniteStateAutomaton.hh.

Referenced by FSAFUResourceConflictDetector::advanceCycleLazyInline(), FUFiniteStateAutomaton::destinationState(), FSAFUResourceConflictDetector::issueOperationLazyInline(), and toDotString().


The documentation for this class was generated from the following files:
FiniteStateAutomaton::ILLEGAL_STATE
static const FSAStateIndex ILLEGAL_STATE
A state id which denotes an illegal state.
Definition: FiniteStateAutomaton.hh:61
FiniteStateAutomaton::stateCount_
int stateCount_
Count of states in the automaton.
Definition: FiniteStateAutomaton.hh:128
FiniteStateAutomaton::transitions_
TransitionMap transitions_
The state transitions. In protected to allow fast access from derived classes.
Definition: FiniteStateAutomaton.hh:120
FiniteStateAutomaton::stateName
virtual std::string stateName(FSAStateIndex state) const
Definition: FiniteStateAutomaton.cc:106
FiniteStateAutomaton::transitionName
virtual const std::string & transitionName(FSAStateTransitionIndex transition) const
Definition: FiniteStateAutomaton.cc:81
FiniteStateAutomaton::TransitionVector
std::vector< FSAStateIndex > TransitionVector
Vector which stores the target states of for each transition type.
Definition: FiniteStateAutomaton.hh:107
Application::logStream
static std::ostream & logStream()
Definition: Application.cc:155
Conversion::toString
static std::string toString(const T &source)
FiniteStateAutomaton::UNKNOWN_STATE
static const FSAStateIndex UNKNOWN_STATE
A state id which denotes an unknown (unresolved) state. Used for lazy construction of states.
Definition: FiniteStateAutomaton.hh:64
FiniteStateAutomaton::transitionIndices_
TransitionNameIndex transitionIndices_
Indices of the state transition types.
Definition: FiniteStateAutomaton.hh:134
FiniteStateAutomaton::defaultState_
const FSAStateTransitionIndex defaultState_
The default target state for state transitions.
Definition: FiniteStateAutomaton.hh:136
__func__
#define __func__
Definition: Application.hh:67
FiniteStateAutomaton::transitionTypeCount_
int transitionTypeCount_
Count of different transition types in the automaton.
Definition: FiniteStateAutomaton.hh:130
FiniteStateAutomaton::transitionNames_
std::vector< std::string > transitionNames_
Names of the state transitions types (indexed from 0).
Definition: FiniteStateAutomaton.hh:132
KeyNotFound
Definition: Exception.hh:285