# stateExample.py - Utilities for Code Generation with the ElementTree # Library # Launch the code with the -v option to see the test output: # python stateExample.py -v # This code is released under the BSD License below. # # Copyright (c) 2008-2009, Jason Breti (http://breti.org/codegen) # # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # * Redistributions of source code must retain the above copyright notice, # this list of conditions and the following disclaimer. # * The name of the contributor may not be used to endorse or promote products # derived from this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. #------------------------------------------------------------------------------ import sys import xml.etree.ElementTree as et import codegenUtilitiesAutomatic as cgu #------------------------------------------------------------------------------ # # The following set of classes are intended to demonstrate how to create a # code generation system using an automatic factory method. The classes # used by the factory method are automatically determined based on the name # of the node. # In this example the codegen phase is the SampleCodegenPhaseAutomatic # class. # #------------------------------------------------------------------------------ class SMCodegen_scxml(cgu.CodegenHandler_HandleChildNodes): def process(self): print self.thisNode.tag, 'node. Initial state is:', self.thisNode.attrib['initialstate'] cgu.CodegenHandler_HandleChildNodes.process(self) #------------------------------------------------------------------------------ class SMCodegen_state(cgu.CodegenHandler_HandleChildNodes): def process(self): print ' ', self.thisNode.tag, 'node with id:', self.thisNode.attrib['id'] cgu.CodegenHandler_HandleChildNodes.process(self) #------------------------------------------------------------------------------ class SMCodegen_onentry(cgu.CodegenHandler_HandleChildNodes): def process(self): print ' ', self.thisNode.tag, 'node.' cgu.CodegenHandler_HandleChildNodes.process(self) #------------------------------------------------------------------------------ class SMCodegen_function(cgu.CodegenHandler_HandleChildNodes): def process(self): print ' ', self.thisNode.tag, 'node name is:', self.thisNode.attrib['name'] cgu.CodegenHandler_HandleChildNodes.process(self) #------------------------------------------------------------------------------ class SMCodegen_transition(cgu.CodegenHandler_HandleChildNodes): def process(self): print ' ', self.thisNode.tag, 'node event is:', self.thisNode.attrib['event'] cgu.CodegenHandler_HandleChildNodes.process(self) #------------------------------------------------------------------------------ class SMCodegen_target(cgu.CodegenHandler_HandleChildNodes): def process(self): print ' ', self.thisNode.tag, 'node next state is:', self.thisNode.attrib['next'] cgu.CodegenHandler_HandleChildNodes.process(self) #------------------------------------------------------------------------------ class SampleCodegenPhase(cgu.CodegenPhase): """ Example Usage: >>> root = et.Element("root") >>> root.append(et.parse("./car_alarm.xml").getroot()) >>> phase = SampleCodegenPhase(root, "SMCodegen_") >>> phase.process() scxml node. Initial state is: Initial state node with id: Initial transition node event is: BatteryOn target node next state is: Unarmed state node with id: Unarmed onentry node. function node name is: StopUnarmTimer transition node event is: RemoteArm target node next state is: Armed state node with id: Armed onentry node. function node name is: LockDoors transition node event is: RemoteUnarm target node next state is: WaitingToUnarm state node with id: WaitingToUnarm onentry node. function node name is: UnlockDoors function node name is: StartUnarmTimer transition node event is: DoorsOpen target node next state is: Unarmed transition node event is: UnarmTimerExpired target node next state is: Armed """ #------------------------------------------------------------------------------ def main(args): # You can run this module independently to test out the "Example Usage:" # listed at the top of each class. import doctest doctest.testmod() #------------------------------------------------------------------------------ if __name__ == '__main__': main(sys.argv[1:])