Contents



Welcome!


Welcome to my Grass Roots Code Generation page. Here you will find papers and code that I have written on code generation. I intend to cover specific examples of code generation, as well as some information on "applied theory" (as opposed to theory that you can't apply...we've all seen a lot of that). I'll be adding new articles as I have time available.

Why "grass roots"? Well, I think code generation has developed a bad reputation over time. I've often heard that code generation is:
  • too complicated
  • requires expensive tools
  • is not maintainable
  • will never work in "my environment"
  • is unusable in a large scale (or small scale) production environment
  • doesn't take into account "my needs"
While these things can certainly be true, they don't have to be true. I call this page grass roots code generation to emphasize that this is a simple, basic approach that you can put to good use.

I hope you like what you see. If you have any questions, please write me at:


Thanks for reading! I have embedded a flash version of the code below. The Actionscript code was code generated with the code in this section.

Alternative content

Get Adobe Flash player



State Machine Code Generation in Python for C++


In this paper I'll show you how to perform code generation for finite state machines (state charts). I'll take you through a simplified code generation scenario, teach you the necessary technologies, and show you how to go from model to code.

When you have finished you’ll have answers to these questions:
  • What is code generation, really?
  • How do I model a state machine so it can be code generated?
  • How do I code generate a state machine?
  • What happens when I want to add my own custom code to the code generation?
  • What problems are best suited for code generation?
  • Am I going to lose my job to code generation?
For a practical example, I'll use a car alarm to examine code generation in C++ of a state machine. You should have a general understanding of Python, XML, and UML state charts, but you haven’t necessarily worked with code generation. You can also easily change from C++ to generate in your favourite language, whatever that may be.

State Machine Code Generation in Python (gnosis version)
State Machine Code Generation in Python (ElementTree version)

My paper uses the gnosis XML library. While this is a good library for learning, I don't think it scales well when you have to go beyond simple code generation.

As such, I highly recommend that you consider using the ElementTree library. It has several other nice features that I hope to describe in a future paper. The code in this download is implemented much the same as the gnosis version, except using ElementTree. All other aspects of the code generation are the same.


State Machine Code Generation in Python for Actionscript


Here I have ported the code generator so it creates a State Machine in Actionscript. All the same principles from my paper above still apply.

This is a preliminary release. It has the same features as the above code, but I may be expanding it to add more features.





Legalese


If you like this material, I hope you use it and pass the information around. As such, I have tried to make the legalese lightweight and let you do pretty much anything you like with the code.

Document LicenseCreative Commons License
The documents on this page are licensed under a Creative Commons Attribution-No Derivative Works 3.0 Unported License
Code LicenseThe code on this page is released under the BSD License.

Copyright (c) 2007-2008, 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.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* 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.