OpenASIP  2.0
FiniteStateAutomaton.hh
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2009 Tampere University.
3 
4  This file is part of TTA-Based Codesign Environment (TCE).
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23  */
24 /**
25  * @file FiniteStateAutomaton.hh
26  *
27  * Declaration of FiniteStateAutomaton class.
28  *
29  * @author Pekka Jääskeläinen 2006 (pekka.jaaskelainen-no.spam-tut.fi)
30  * @note rating: red
31  */
32 
33 #ifndef TTA_FINITE_STATE_AUTOMATON_HH
34 #define TTA_FINITE_STATE_AUTOMATON_HH
35 
36 #include <string>
37 #include <set>
38 #include <map>
39 #include <vector>
40 
41 #include "Exception.hh"
42 
43 /**
44  * Generic finite state automaton (FSA).
45  *
46  * This is a generic structure for FSA. The building and initialization of
47  * the states is the responsibility of derived classes.
48  */
50 public:
51  /// Friend due to highly optimized compiled simulation versions of
52  /// the conflict detection functions.
54  /// Type used for indexing the transitions.
56  /// Type used for indexing the states.
57  typedef int FSAStateIndex;
58  /// Type for a set of state indices.
59  typedef std::set<FSAStateIndex> FSAStateIndexSet;
60  /// A state id which denotes an illegal state.
61  static const FSAStateIndex ILLEGAL_STATE = -1;
62  /// A state id which denotes an unknown (unresolved) state. Used for
63  /// lazy construction of states.
64  static const FSAStateIndex UNKNOWN_STATE = -2;
65 
68  int transitionCount = 0);
69  virtual ~FiniteStateAutomaton();
70 
71  virtual const std::string& transitionName(FSAStateTransitionIndex transition)
72  const;
73 
74  virtual std::string stateName(FSAStateIndex state) const;
75 
77  const std::string& transitionName) const;
78 
80  FSAStateIndex source,
81  FSAStateTransitionIndex transition);
82 
83  virtual bool isLegalTransition(
84  FSAStateIndex source,
85  FSAStateTransitionIndex transition);
86 
87  virtual FSAStateIndex addState();
88 
90  const std::string& name);
91 
92  virtual void setTransitionName(
93  FSAStateTransitionIndex transition, const std::string& name);
94 
95  virtual void setTransition(
96  FSAStateIndex source, FSAStateIndex destination,
97  FSAStateTransitionIndex transition);
98 
99  virtual std::string toDotString() const;
100 
101  virtual FSAStateIndex startState() const;
102 
103 
104 protected:
105 
106  /// Vector which stores the target states of for each transition type.
107  typedef std::vector<FSAStateIndex> TransitionVector;
108 
109  /// Type for storing the transitions of each state. The vector
110  /// stores for each state (indexed from 0) a vector with elements as
111  /// many there are transitions. The value of the element is the target
112  /// state to which the transition leads to, or ILLEGAL_STATE_INDEX if
113  /// there is no transition for that transition type from that state.
114  /// Using this structure it's possible to find the target state using two
115  /// table (vector) lookups.
116  typedef std::vector<TransitionVector> TransitionMap;
117 
118  /// The state transitions. In protected to allow fast access from
119  /// derived classes.
121 
122 private:
123 
124  /// Type for finding the index for a transition using its name.
125  typedef std::map<std::string, FSAStateTransitionIndex>
127  /// Count of states in the automaton.
129  /// Count of different transition types in the automaton.
131  /// Names of the state transitions types (indexed from 0).
132  std::vector<std::string> transitionNames_;
133  /// Indices of the state transition types.
135  /// The default target state for state transitions.
137 };
138 
139 #endif
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::FSAStateIndex
int FSAStateIndex
Type used for indexing the states.
Definition: FiniteStateAutomaton.hh:57
Exception.hh
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::startState
virtual FSAStateIndex startState() const
Definition: FiniteStateAutomaton.cc:282
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
FiniteStateAutomaton::FiniteStateAutomaton
FiniteStateAutomaton(FSAStateTransitionIndex defaultState=ILLEGAL_STATE, int transitionCount=0)
Definition: FiniteStateAutomaton.cc:55
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::TransitionNameIndex
std::map< std::string, FSAStateTransitionIndex > TransitionNameIndex
Type for finding the index for a transition using its name.
Definition: FiniteStateAutomaton.hh:126
FiniteStateAutomaton::setTransition
virtual void setTransition(FSAStateIndex source, FSAStateIndex destination, FSAStateTransitionIndex transition)
Definition: FiniteStateAutomaton.cc:219
FiniteStateAutomaton::defaultState_
const FSAStateTransitionIndex defaultState_
The default target state for state transitions.
Definition: FiniteStateAutomaton.hh:136
FiniteStateAutomaton::toDotString
virtual std::string toDotString() const
Definition: FiniteStateAutomaton.cc:235
FiniteStateAutomaton
Definition: FiniteStateAutomaton.hh:49
FiniteStateAutomaton::isLegalTransition
virtual bool isLegalTransition(FSAStateIndex source, FSAStateTransitionIndex transition)
Definition: FiniteStateAutomaton.cc:165
FiniteStateAutomaton::transitionIndex
virtual FSAStateTransitionIndex transitionIndex(const std::string &transitionName) const
Definition: FiniteStateAutomaton.cc:120
FiniteStateAutomaton::addState
virtual FSAStateIndex addState()
Definition: FiniteStateAutomaton.cc:178
FiniteStateAutomaton::FSAStateTransitionIndex
int FSAStateTransitionIndex
Type used for indexing the transitions.
Definition: FiniteStateAutomaton.hh:55
FiniteStateAutomaton::setTransitionName
virtual void setTransitionName(FSAStateTransitionIndex transition, const std::string &name)
Definition: FiniteStateAutomaton.cc:93
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
FiniteStateAutomaton::~FiniteStateAutomaton
virtual ~FiniteStateAutomaton()
Definition: FiniteStateAutomaton.cc:66
FSAFUResourceConflictDetector
Definition: FSAFUResourceConflictDetector.hh:49
FiniteStateAutomaton::addTransitionType
virtual FSAStateTransitionIndex addTransitionType(const std::string &name)
Definition: FiniteStateAutomaton.cc:194
FiniteStateAutomaton::FSAStateIndexSet
std::set< FSAStateIndex > FSAStateIndexSet
Type for a set of state indices.
Definition: FiniteStateAutomaton.hh:59
FiniteStateAutomaton::destinationState
virtual FSAStateIndex destinationState(FSAStateIndex source, FSAStateTransitionIndex transition)
Definition: FiniteStateAutomaton.cc:146
FiniteStateAutomaton::TransitionMap
std::vector< TransitionVector > TransitionMap
Type for storing the transitions of each state. The vector stores for each state (indexed from 0) a v...
Definition: FiniteStateAutomaton.hh:116