Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/tutorius/tutorial.py
diff options
context:
space:
mode:
authorVincent Vinet <vince.vinet@gmail.com>2009-11-04 15:31:34 (GMT)
committer Vincent Vinet <vince.vinet@gmail.com>2009-11-04 19:10:06 (GMT)
commit2c28f3a79572c7a57fd00b1c5cc6424fb4fe54fb (patch)
treea62d6be48613ddb494be27cd80ce0d9ad808a2bd /tutorius/tutorial.py
parentd50c3cd98d6e99c7311286559136dc1a775a326d (diff)
Vault ADT Integration
Diffstat (limited to 'tutorius/tutorial.py')
-rw-r--r--tutorius/tutorial.py55
1 files changed, 39 insertions, 16 deletions
diff --git a/tutorius/tutorial.py b/tutorius/tutorial.py
index 9831a7b..b45363f 100644
--- a/tutorius/tutorial.py
+++ b/tutorius/tutorial.py
@@ -67,7 +67,8 @@ class Tutorial(object):
self.add_transition(Tutorial.INIT, \
(AutomaticTransitionEvent(), Tutorial.END))
else:
- raise NotImplementedError("Tutorial: Initilization from a dictionary is not supported yet")
+ self._state_dict = state_dict
+
# Minimally check for the presence of an INIT and an END
@@ -528,15 +529,20 @@ class Tutorial(object):
def _generate_unique_state_name(self):
name = "State" + str(self._state_name_nb)
- self._state_name_nb += 1
+ while name in self._state_dict:
+ self._state_name_nb += 1
+ name = "State" + str(self._state_name_nb)
return name
+ # Python Magic Methods
def __str__(self):
"""
Return a string representation of the tutorial
"""
return str(self._state_dict)
+ def __eq__(self, other):
+ return isinstance(other, type(self)) and self.get_state_dict() == other.get_state_dict()
class State(object):
"""
@@ -548,16 +554,20 @@ class State(object):
inputs, the validation should be done by the containing class.
"""
- def __init__(self, name, action_list=(), transition_list=()):
+ def __init__(self, name, actions={}, transitions={}):
"""
Initializes the content of the state, such as loading the actions
that are required and building the correct transitions.
- @param action_list The list of actions to execute when entering this
+ @param actions list or dict of actions to perform when entering the
state
- @param transition_list A list of tuples of the form
+ @param transitions list or dict of tuples of the form
(event, next_state_name), that explains the outgoing links for
this state
+
+ For actions and transitions, dictionaries allow specifying the name.
+ If lists are given, their contents will be added with add_action or
+ add_transition
"""
object.__init__(self)
@@ -567,13 +577,19 @@ class State(object):
self.action_name_nb = 0
self.transition_name_nb = 0
- self._actions = {}
- for action in action_list:
- self.add_action(action)
-
- self._transitions = {}
- for transition in transition_list:
- self.add_transition(transition)
+ if type(actions) is dict:
+ self._actions = dict(actions)
+ else:
+ self._actions = {}
+ for action in actions:
+ self.add_action(action)
+
+ if type(transitions) is dict:
+ self._transitions = dict(transitions)
+ else:
+ self._transitions = {}
+ for transition in transitions:
+ self.add_transition(transition)
# Action manipulations
@@ -741,7 +757,9 @@ class State(object):
# to make it easier to debug and know what we are
# manipulating
name = self.name + _NAME_SEPARATOR + "action" + str(self.action_name_nb)
- self.action_name_nb += 1
+ while name in self._actions:
+ self.action_name_nb += 1
+ name = self.name + _NAME_SEPARATOR + "action" + str(self.action_name_nb)
return name
def _generate_unique_transition_name(self, transition):
@@ -757,7 +775,9 @@ class State(object):
# generate a name to make it easier to debug and know
# what we are manipulating
name = self.name + _NAME_SEPARATOR + "transition" + str(self.transition_name_nb)
- self.transition_name_nb += 1
+ while name in self._transitions:
+ self.transition_name_nb += 1
+ name = self.name + _NAME_SEPARATOR + "transition" + str(self.transition_name_nb)
return name
def __eq__(self, otherState):
@@ -775,12 +795,15 @@ class State(object):
@param otherState The state that will be compared to this one
@return True if the states are the same, False otherwise
` """
- raise NotImplementedError
+ return isinstance(otherState, type(self)) and \
+ self.get_action_dict() == otherState.get_action_dict() and \
+ self.get_transition_dict() == otherState.get_transition_dict()
#TODO: Define the automatic transition in the same way as
# other events
class AutomaticTransitionEvent(TPropContainer):
- pass
+ def __repr__(self):
+ return str(self.__class__.__name__)
################## Error Handling and Exceptions ##############################