statemap.py
Go to the documentation of this file.
1 #
2 # The contents of this file are subject to the Mozilla Public
3 # License Version 1.1 (the "License"); you may not use this file
4 # except in compliance with the License. You may obtain a copy of
5 # the License at http://www.mozilla.org/MPL/
6 #
7 # Software distributed under the License is distributed on an "AS
8 # IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9 # implied. See the License for the specific language governing
10 # rights and limitations under the License.
11 #
12 # The Original Code is State Machine Compiler (SMC).
13 #
14 # The Initial Developer of the Original Code is Charles W. Rapp.
15 # Portions created by Charles W. Rapp are
16 # Copyright (C) 2005. Charles W. Rapp.
17 # All Rights Reserved.
18 #
19 # Port to Python by Francois Perrad, francois.perrad@gadz.org
20 # Copyright 2004, Francois Perrad.
21 # All Rights Reserved.
22 #
23 # Contributor(s):
24 #
25 # RCS ID
26 # $Id: statemap.py,v 1.7 2009/11/24 20:42:39 cwrapp Exp $
27 #
28 # See: http://smc.sourceforge.net/
29 #
30 
31 import sys
32 
33 
34 class StateUndefinedException(Exception):
35  """A StateUndefinedException is thrown by
36  an SMC-generated state machine whenever a transition is taken
37  and there is no state currently set. This occurs when a
38  transition is issued from within a transition action."""
39  pass
40 
41 
43  """A TransitionUndefinedException is thrown by
44  an SMC-generated state machine whenever a transition is taken
45  which:
46  - Is not explicitly defined in the current state.
47  - Is not explicitly defined in the current FSM's default state.
48  - There is no Default transition in the current state."""
49  pass
50 
51 
52 class State(object):
53  """base State class"""
54 
55  def __init__(self, name, id):
56  self._name = name
57  self._id = id
58 
59  def getName(self):
60  """Returns the state's printable name."""
61  return self._name
62 
63  def getId(self):
64  """Returns the state's unique identifier."""
65  return self._id
66 
67 
68 class FSMContext(object):
69  """The user can derive FSM contexts from this class and interface
70  to them with the methods of this class.
71 
72  The finite state machine needs to be initialized to the starting
73  state of the FSM. This must be done manually in the constructor
74  of the derived class.
75  """
76 
77  def __init__(self, state):
78  self._state = state
79  self._previous_state = None
80  self._state_stack = []
81  self._transition = None
82  self._debug_flag = False
83  self._debug_stream = sys.stderr
84 
85  def getDebugFlag(self):
86  """Returns the debug flag's current setting."""
87  return self._debug_flag
88 
89  def setDebugFlag(self, flag):
90  """Sets the debug flag.
91  A true value means debugging is on and false means off."""
92  self._debug_flag = flag
93 
94  def getDebugStream(self):
95  """Returns the stream to which debug output is written."""
96  return self._debug_stream
97 
98  def setDebugStream(self, stream):
99  """Sets the debug output stream."""
100  self._debug_stream = stream
101 
102  def getState(self):
103  """Returns the current state."""
104  if self._state is None:
105  raise StateUndefinedException
106  return self._state
107 
108  def isInTransition(self):
109  """Is this state machine already inside a transition?
110  True if state is undefined."""
111  if self._state is None:
112  return True
113  else:
114  return False
115 
116  def getTransition(self):
117  """Returns the current transition's name.
118  Used only for debugging purposes."""
119  return self._transition
120 
121  def clearState(self):
122  """Clears the current state."""
123  self._previous_state = self._state
124  self._state = None
125 
126  def getPreviousState(self):
127  """Returns the state which a transition left.
128  May be None"""
129  return self._previous_state
130 
131  def setState(self, state):
132  """Sets the current state to the specified state."""
133  if not isinstance(state, State):
134  raise ValueError("state should be a statemap.State")
135  self._state = state
136  if self._debug_flag:
137  self._debug_stream.write("NEW STATE : %s\n" % self._state.getName())
138 
139  def isStateStackEmpty(self):
140  """Returns True if the state stack is empty and False otherwise."""
141  return len(self._state_stack) == 0
142 
144  """Returns the state stack's depth."""
145  return len(self._state_stack)
146 
147  def pushState(self, state):
148  """Push the current state on top of the state stack
149  and make the specified state the current state."""
150  if not isinstance(state, State):
151  raise ValueError("state should be a statemap.State")
152  if self._state is not None:
153  self._state_stack.append(self._state)
154  self._state = state
155  if self._debug_flag:
156  self._debug_stream.write("PUSH TO STATE : %s\n" % self._state.getName())
157 
158  def popState(self):
159  """Make the state on top of the state stack the current state."""
160  if len(self._state_stack) == 0:
161  if self._debug_flag:
162  self._debug_stream.write("POPPING ON EMPTY STATE STACK.\n")
163  raise ValueError("empty state stack")
164  else:
165  self._state = self._state_stack.pop()
166  if self._debug_flag:
167  self._debug_stream.write("POP TO STATE : %s\n" % self._state.getName())
168 
169  def emptyStateStack(self):
170  """Remove all states from the state stack."""
171  self._state_stack = []
def getName(self)
Definition: statemap.py:59
def __init__(self, name, id)
Definition: statemap.py:55
def pushState(self, state)
Definition: statemap.py:147
def getStateStackDepth(self)
Definition: statemap.py:143
def setDebugStream(self, stream)
Definition: statemap.py:98
def setState(self, state)
Definition: statemap.py:131
def __init__(self, state)
Definition: statemap.py:77
def setDebugFlag(self, flag)
Definition: statemap.py:89


smclib
Author(s): Various
autogenerated on Wed Sep 2 2020 03:40:43