1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
class Tutorial(object):
""" This class replaces the previous Tutorial class and
allows manipulation of the abstract representation
of a tutorial as a state machine
"""
def __init__(self):
pass
def add_state(self, state_name):
pass
def add_action(self, state_name, action):
pass
def add_transition(self, state_name, transition):
pass
def update_action(self, state_name, action_name, properties):
pass
def update_transition(self, state_name, transition_name, properties):
pass
def replace_state(self, state_name, state):
pass
def delete_action(self, state_name, action_name):
pass
def delete_transition(self, state_name, transition_name):
pass
def delete_state(self, state_name):
pass
def _validate_action(self, action):
"""
Validate that an action conforms to what we expect,
throws an exception otherwise.
@param action The action to validate
@throw InvalidAction if the action fails to conform to what we expect
"""
pass
def _validate_transition(self, transition):
"""
Validate that a transition conforms to what we expect,
throws an exception otherwise.
@param transition The transition to validate
@throw InvalidTransition if the transition fails to conform to what we expect
"""
pass
class State(object):
"""
This is a step in a tutorial. The state represents a collection of actions
to undertake when entering the state, and a series of transitions to lead
to next states.
This class is not meant to be used explicitly as no validation is done on
inputs, the validation should be done by the containing class.
"""
def __init__(self, name="", action_list=[], transition_list=[]):
"""
Initializes the content of the state, like loading the actions
that are required and building the correct tests.
@param action_list The list of actions to execute when entering this
state
@param transition_list A list of tuples of the form
(event, next_state_name), that explains the outgoing links for
this state
"""
object.__init__(self)
self.name = name
self._actions = {}
for action in action_list:
self._actions[self._generate_unique_action_name(action)] = action
self._transitions = {}
for transition in transition_list:
self._transitions[self._generate_unique_transition_name(transition)] = transition
# Action manipulations
def add_action(self, new_action):
"""
Adds an action to the state (only if it wasn't added before)
@param new_action The new action to execute when in this state
@return a unique name for this action
"""
if new_action not in self._actions:
self._actions.append(new_action)
return True
return False
def delete_action(self, action_name):
"""
Delete the action with the name action_name returned when the
action was added or when they are listed
@param action_name The name of the action to delete
@return True if the action existed of False if no action had this name
"""
pass
def update_action(self, action_name, action):
"""
Update the action with the name action_name with the properties from
action
@param action_name The name of the action to update
@param action The action whose properties will be copied over
@return True if action_name existed and the properties were valid, False otherwise
"""
pass
def get_action_dict(self):
"""
@return A dictionary of actions that the state will execute
"""
return self._actions
def delete_actions(self):
"""
Removes all the action associated with this state. A cleared state will
not do anything when entered or exited.
"""
self._actions = {}
# Transition manipulations
def add_transition(self, transition):
"""
Adds a transition from this state to another state.
The same transition may not be added twice.
@param transition The new transition.
@return A unique name for the transition could be added, False otherwise
"""
return False
def update_transition(self, transition_name, transition):
"""
Update the transition with the name transition_name with the properties from
transition
@param transition_name The name of the transition to update
@param transition The transition whose properties will be copied over
@return True if transition_name existed and the properties were valid, False otherwise
"""
pass
def delete_transition(self, transition_name):
"""
Delete the transition with the name transition_name
@param transition_name The name of the transition to delete
@return True if transition_name existed, False otherwise
"""
pass
def get_transition_dict(self):
"""
@return The dictionary of transitions associated with this state.
"""
return self._transitions
def delete_transitions(self):
"""
Delete all the transitions associated with this state.
"""
self._transitions = {}
def _generate_unique_action_name(self, action):
"""
Returns a unique name for the action in this state
@param action The action to generate a name for
@return A name garanteed to be unique within this state
"""
return None
def _generate_unique_transition_name(self, transition):
"""
Returns a unique name for the transition in this state
@param transition The transition to generate a name for
@return A name garanteed to be unique within this state
"""
return None
|