home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / xbmc-9.11.exe / system / python / spyce / modules / automaton.py next >
Encoding:
Python Source  |  2009-12-23  |  2.7 KB  |  80 lines

  1. ##################################################
  2. # SPYCE - Python-based HTML Scripting
  3. # Copyright (c) 2002 Rimon Barr.
  4. #
  5. # Refer to spyce.py
  6. # CVS: $Id: automaton.py 20864 2009-06-02 06:16:47Z ceros7 $
  7. ##################################################
  8.  
  9. from spyceModule import spyceModule
  10.  
  11. __doc__ = '''Automaton module allows Spyce users to create websites with
  12. state machine-based application flows. One can define an automaton
  13. programmatically using the start(), transition() and begin methods. The
  14. automaton is the executed (one step per request) using the step() method. This
  15. method accepts the current state, which should be managed by the user
  16. preferably via a session (keeping the information on the server), or possibly
  17. by get, post or cookie. The step() method then calls the recv() function on
  18. the given state, which returns an edge label. This edge points to the new
  19. state. The step() method then calls the send() method of the new state to
  20. generate the page content. The user should encode the new state in this
  21. content, or use on a subsequent request.'''
  22.  
  23. SEND = 0
  24. RECV = 1
  25. EDGES = 2
  26.  
  27. class automaton(spyceModule):
  28.   def start(self):
  29.     "Initialise an empty automaton"
  30.     self.clear()
  31.   def clear(self):
  32.     self._nodes = {}
  33.     self._edges = {}
  34.   # defining the automaton
  35.   def state(self, name, send, recv):
  36.     "Add a new automaton state"
  37.     self._nodes[name] = send, recv
  38.     self.transition(name, None, name)
  39.   def transition(self, state1, edge, state2):
  40.     "Add a new automaton transition"
  41.     if not self._nodes.has_key(state1):
  42.       raise 'state %s does not exist' % state1
  43.     if not self._nodes.has_key(state2):
  44.       raise 'state %s does not exist' % state2
  45.     self._edges[(state1, edge)] = state2
  46.   node=state
  47.   edge=transition
  48.   def begin(self, name):
  49.     if not self._nodes.has_key(name):
  50.       raise 'state %s does not exist' % name
  51.     self._begin = name
  52.   def define(self, sm, start):
  53.     self.clear()
  54.     for s1 in sm.keys():
  55.       self.node(s1, sm[s1][SEND], sm[s1][RECV])
  56.     for s1 in sm.keys():
  57.       for e in sm[s1][EDGES].keys():
  58.         self.edge(s1, e, sm[s1][EDGES][e])
  59.     self.begin(start)
  60.  
  61.   # running the automaton
  62.   def step(self, state=None):
  63.     """Run the automaton one step: recv (old state), transition, 
  64.     send (new state)"""
  65.     if state==None:
  66.       state = self._begin
  67.     else:
  68.       try: _, recv = self._nodes[state]
  69.       except: raise 'invalid state: %s' % state
  70.       edge = recv()
  71.       try: state = self._edges[(state, edge)]
  72.       except: raise 'invalid transition: %s,%s' % (state, edge)
  73.     try: send, _ = self._nodes[state]
  74.     except: raise 'invalid state: %s' % state
  75.     send()
  76.  
  77.  
  78. # rimtodo: cached state-machines
  79.  
  80.