YOUR FEEDBACK
DataCore and Egenera Combination Delivers Next Generation Server and Storage Virtualization
Virtualization news for the channel community and you ! wrote: Trackback A...
SOA World Conference
Virtualization Conference
$50 Savings Expire May 23, 2008... – Register Today!


2007 West
GOLD SPONSORS:
Active Endpoints
Your SOA Needs BPEL for Orchestration
BEA
Virtualized SOA: Adaptive Infrastructure for Demanding Applications
Nexaweb
Overcoming Bandwidth Challenges with Nexaweb
TIBCO
What is Service Virtualization?
SILVER SPONSORS:
WSO2
Using Web Services Technologies and FOSS Solutions
Click For 2007 East
Event Webcasts

2008 East
PLATINUM SPONSORS:
Appcelerator
Think Fast: Accelerate AJAX Development with Appcelerator
GOLD SPONSORS:
DreamFace Interactive
The Ultimate Framework for Creating Personalized Web 2.0 Mashups
ICEsoft
AJAX and Social Computing for the Enterprise
Kaazing
Enterprise Comet: Real–Time, Real–Time, or Real–Time Web 2.0?
Nexaweb
Now Playing: Desktop Apps in the Browser!
Sun
jMaki as an AJAX Mashup Framework
POWER PANELS:
The Business Value
of RIAs
What Lies Beyond AJAX?
KEYNOTES:
Douglas Crockford
Can We Fix the Web?
Anthony Franco
2008: The Year of the RIA
Click For 2007 Event Webcasts
SYS-CON.TV
TOP COLDFUSION LINKS


Model-Driven Development with ColdFusion and UML
Generating Code from Class Diagrams

Digg This!

Page 2 of 2   « previous page

Populating the Model
Now we are ready to create a method that reads a UML diagram and populates a model of a CF application. Again, this is a simple matter of looping over all the classes we found in the XMI document and then drilling into each (Listing 2). From there, we will loop over the class properties, extracting what we find and pushing information into an instance of the Model class (Listing 3). Next, we loop over the functions and, for each of those, down into its arguments (Listing 4). When finished, our createModelFromFile function returns a Model object that represents the information in our diagram.

Generating Code from Templates
Having all of the diagram's information distilled into a struct makes it quite easy for us to generate out any kind of template code we want, adhering to whatever standard or framework fits our fancy. In the interest of time, we'll just show generation of the CFProperty section of a webservice-oriented CFC. As you'll see, it becomes simple to generate out all the code for that boring task, and generating out classes containing CFFunction 'stubs' for all of the documented functions is not much more difficult. While some code-generation frameworks encourage modeling your database first and then generating code from Entity Relationship model, I would suggest creating a class model and then generating your Data Definition Language scripts from your classes. You could go all the way and generate out stored procedures to provide supporting functions for generated stub code. I am writing such generators for my own use.

The approach used to generate code is straightforward. We create templates to generate ColdFusion or SQL code, using CFSaveContent to keep the generated content in a string buffer. To get ColdFusion to generate ColdFusion code, we use symbols to represent where in our code we want to write three special characters: <, >, and #. We want to keep the stuff we're generating fairly readable, so we substitute the double angle bracket characters for angle brackets ("") and dollar signs ($) for the pound signs. The double angle bracket character is not on your keyboard, so if you follow this method, you will have to map that character somehow. I have a macro that replaces my two typed angle brackets with one double angle bracket character. Once we have generated the whole file we want to save and have it in a string variable, we can clean up all of those placeholder symbols with the real thing with some simple string functions like replace (input,'"','>','all').

The idea is to loop over a model struct outputted from our Model.getModel() function. Below is an example of generating CFProperty code using our special templating characters. In our generation framework, this kind of code would be wrapped up in functions in the Generator class.

<cfset myModel= model.getModel() />
<cfloop collection="#myModel#" item="componentName" >
    <cfset currentCFC = myModel[componentName] />
    <cfloop collection="#currentcfc.properties#" item="propName" >
      <cfset currentProp = currentcfc.properties[propName] />
      <cfsavecontent variable="generatedProperties">
        <cfoutput>
          "cfproperty name="#propName#"
          required="#currentProp.required#"
          type="#currentProp.type#"
          hint="#currentProp.hint#"
          default="#currentProp.default#"
          displayname="#currentProp.displayname#"
        /"
        </cfoutput>
      </cfsavecontent>
    </cfloop>
</cfloop>

Saving the Code
Now that we have generated all this great stuff, we need to do something with it. Our Writer class will write out the generated content to files, but it would be nice if it would also help protect us from ourselves by, for example, moving existing files to a backup location to prevent overwrites and loss of work, and logging any problems encountered. Remember that, in many cases, the output is meant to be edited; it is just an outline for the real code. Down the road, maybe this class would be smart enough to use Subversion to try to merge changes from the model into your edited code. After cleaning up our generated code above and writing it to a file, we would see something like:

<cfproperty name="lastName"
    required="yes"
    type="string"
    hint="The User's family name"
    default=""
    displayname="Family Name"
/>

Conclusion
In this article, we have attempted to lay the foundation for a code-generation system that encourages developers to model before they code. When the model does more than fulfill documentation requirements, it becomes a more valuable tool for the developer. The blueprint for the application is generated at the start of the project, providing benefits across the development team. For organizations, model-driven development presents an opportunity to build-in standards and best practices. Senior developers and architects can focus on iteratively improving their generators to produce more code cleanly and handle mundane, repetitive tasks. Junior developers are provided a great environment for learning, and the structure they need to complete their projects rapidly. If this concept is of interest to you and your team, I encourage you to check out the OpenModel project on CFOpen and to contact me at openModel@anthrologik.net.

References

  • Author's project to develop this idea: http://cfopen.org/project/openModel
  • A great primer on XPath: http://w3schools.com/xpath/
  • The Subversion source control system: http://subversion.tigris.org/
  • Poseidon UML: http://gentlesoft.com
  • CFOpen: http://cfopen.org

  • Page 2 of 2   « previous page

    About Chip Temm
    Over the past decade, Chip Temm moved from North America to Europe and on to Africa where his company anthroLogik solutions provided analysis and development services to non-governmental organizations across seven timezones. He is currently back in Washington, DC where "remote development" means working from home and "wildlife" means raccoon.

    CFDJ LATEST STORIES . . .
    Red Hat Named "Platinum Sponsor" of Virtualization Conference & Expo
    Red Hat is a trusted open source provider. Red Hat offers enterprise customers a long-term plan for building infrastructures on the quality and innovation of open source. Combining open source operating system platform, Red Hat Enterprise Linux, together with applications, management
    3rd International Virtualization Conference & Expo: Themes & Topics
    From Application Virtualization to Xen, a round-up of the virtualization themes & topics being discussed in NYC June 23-24, 2008 by the world-class speaker faculty at the 3rd International Virtualization Conference & Expo being held by SYS-CON Events in The Roosevelt Hotel, in midtown
    AJAX and Enterprise RIA Tools - JSF, Flex, and JavaFX
    2008 is going to be an important year for Rich Internet Applications. Most organizations are delivering or planning to deliver Rich Internet Applications; however, at the same time, most IT managers are facing a dilemma: which Rich Internet Application technology and platform to use? T
    AJAX World - Adobe Flex 4 Is Shaping Up
    Adobe has published their first plan of what should be included in Flex 4 that is scheduled to release next year. Since Flex is an open source product, you have a say in this too. Obviously, there's a hope that upcoming Thermo release will bring together developers and designers. I'm c
    AJAX World - Cooking CRUD with Flex and BlazeDS
    In today's cooking class you'll add to your cookbook a delicious recipe. It's quick and won't cost you a dime. I'm sure you've been in one of these situations when you have unexpected guests arriving in 20 minutes and need to make a good impression. Let's create an application that
    Microsoft's Mike Neil To Keynote SYS-CON's 4th International Virtualization Conference & Expo
    Mike Neil is general manager for virtualization strategy in the Windows Server Division at Microsoft. Mike is focused on the delivery of the Windows virtualization technology, including Windows Server 2008 Hyper-V, Microsoft Hyper-V Server and Virtual PC 2007. Mike also directs the tec
    SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS
    SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
    Click to Add our RSS Feeds to the Service of Your Choice:
    Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
    myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
    Publish Your Article! Please send it to editorial(at)sys-con.com!

    Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021

    SYS-CON FEATURED WHITEPAPERS

    ADS BY GOOGLE