For the original article, see the official website of andromda.
System OverviewAndromda is a code generation tool that inputs a UML model to generate source code. By using a series of template files (which can be customized), andromda can generate the UML model to the source code of any programming language. The default template file generates Java code (J2EE code ). In the andromda system, two main components are used: The andromda code generation engine. Apache Project Construction and Management System Maven andromda code generator is actually a common code generation engine. This engine is a platform that contains code modules called cartridge. These code modules are responsible for generating specific code. The andromda module is a collection of Java help classes (called metafacade) in the form of source code template files and jar files. Now there are some modules. You can put these modules under the engine to generate code. These modules can generate Java code for different popular open-source frameworks (such as spring, hibernate, and J2EE ). Of course, the engine also supports other modules. You can write a module by yourself (at least theoretically) to generate various code from the UML model. Maven is actually an optional component. The andromda engine can be called directly from the command line, IDE, or build scripts (such as ant. However, the existence of the maven plug-in greatly simplifies the process of using andromda. If you do not try your best to integrate andromda into an existing project, we strongly recommend that you use Maven to call andromda. This is a normal way to obtain the andromda service.
Project development cycleThe following is a typical andromda project development process (Java ). 1. run the maven andromdapp: Generate command to create a new project directory (this command calls the maven app generator plug-in); 2. configure this project to meet your needs. 3. use magicdraw to edit <projectdir>/MDA/src/UML/<projectname> model. XMI file. This file is the model of our application. 4. in the root directory of your project, call any of the following commands to generate code: Maven MDA generates code from the model (re-) Maven install generates code from the model (re, include the schema file of the database, and then build all the source module Maven create-schema. If you are using andromda to generate your persistent layer (such as hibernate or spring ), this command will install SQL DDL in the database. This command assumes that you have created a DDL file (using Maven install) 5 in the <projectdir>/CORE/src/Java directory and processed the code that needs to be compiled manually; 6. Compile and test your code. 7. If your project changes, redefine the UML Model and start from step 3. Create a new project and create a project, first, switch the command line directory to the parent directory of the project to be created, and execute the command Maven andromdapp: Generate. When asked to use a name (without any space) as the Project ID, the name you entered will be the name of the project. For example, if you create a project under the/myprojects directory whose ID is mydemo, the root directory of your project will be/myprojects/mydemo
Project directory structureThe project directory structure generated by the maven app generation plug-in is as follows: <projectdir>/MDA/src stores the UML model file. The <projectdir>/MDA/conf directory stores the andromda engine configuration file. <Projectdir>/CORE/target is the place where the code generator stores most of the code, and the SQL schema file is also stored here. Java source files are stored in the src/subdirectory. You can learn these codes, but you cannot modify them because these files will be overwritten when the code is re-generated next time. <Projectdir>/CORE/src stores the code that needs to be manually modified. files in this directory will not be overwritten in future code generation.
Andromda ConfigurationAndromda is finally driven by configuration information in andromda. xml. Assume that the project generated by the maven app generator plug-in is in the <projectdir>/MDA/CONF/directory. This file contains the name of the UML model file and the configuration of different andromda modules. (These configurations are isolated from each other in namespaces ). If you use the project generated by the maven app generator plug-in, many configuration values in the andromda. xml file are actually represented by the maven build attribute. <Projectdir> project. properties contains Maven attributes that will be passed to andromda. XML, including JDBC configuration information and database dialect. <Projectdir> build. properties contains Maven attributes.
Create your UML ModelIn theory, andromda can use the UML files generated by any UML modeling tool as the starting point for generating code. These UML models should comply with the XMI file format. To save your workload, magicdraw is strongly recommended. Andromda code generation is driven by the UML version. When the andromda engine parses your UML model, it first looks for classes based on the version type. if it finds that it can recognize the version type, appropriate modules will be called to generate code for this class, which type of template can be identified depends on the module used by andormda. In MDA, a major concept is PiM (platform independent model ). Many UML tools are designed to generate Java code, so they use the Java data type as the default type when defining the model, which breaks through the concept of Pim. Umlmda expects the UML model to contain common data types (especially UML data types ). To simplify modeling (to ensure that appropriate UML data types and versions can be used), The andromda UML profile has been developed and can be used as part of your model. This profile file is named andromda-profile-XXX.xml.zip (XXX is the version number you use andromda ). This profile contains all UML data types, versions, and andromda modules. Any new UML model should first import the file to your project (if magicdraw is used, you can use the file_use profile/module menu to import the file ). Assume that your project is generated using the maven app generation plug-in. The pre-generated project model file <projectdir>/MDA/src/UML/<projectname> model. XMI already contains this profile .. This profile file can be found in the subdirectory of andromda <sourcedir>/etc/profile/target. Generally, you can use magicdraw to open a UML file, which allows you to specify the location of the file. Copy this file from the original directory of andromda to the magicdraw's profile directory (/program files/magic draw UML/profiles on a Windows XP installation ), magicdraw can find this profile file locally.
How does andromda generate code?Next we will introduce the process of generating code through the andromda engine. All modules required by the project are imported by the engine. Next, the engine parses the XMI file of the UML Model and creates an object tree of the model. This object tree will be used by the engine and module. In addition to the object tree, helper classes (called metafacades) are created to simplify the work of modules. The engine converts the object tree to search for classes that can recognize the template type. For each class, the corresponding module is used to generate code. Each class in the model may generate multiple source code files: Multiple templates in each class module may be used to generate code, and multiple modules in each class may be used to generate code. The default template in the module may be used, or the Custom template may be used, depending on your configuration. For each class in the model, step 4 is repeated. Loading the cartridges to use when the andromda engine starts to work, it first imports and activates the modules it will use. For each module to be used, the JAR file of the module must be activated by referencing its namespace in the andromda. xml configuration file in the Java classpath of the andromda engine. If you use the maven plug-in, place the module in the classpath of the engine, modify the <projectdir>/MDA/Project. xml file, and add the <dependency> of the module in the <dependencies> section. If the maven plug-in is used, the andromda. xml configuration file is in the <projectdir>/MDA/conf directory. This file contains one or more <namespace> tags to activate the module.
Import UML ModelJust as there is a standard Object Model to parse the XML file (DOM) and Java implementation of this dom api (for example, Apache's xerces ), there is also a standard Object Model to parse the metadata stored in the UML model. This standard parsing UML metadata object model is called Meta Object Facility (MOF ). one Java implementation of this API is netbeans 'mcm (metadata repository ). the andromda engine uses netbeans MCM to parse the UML model. The act of generating code from the UML model requires some computation not easily handled by the simple scripting capabilities found in the template engine used by andromda. things such as traversing the MCM looking for relationships among classes, testing for certain conditions in the model, formulating identifier names and package names based on attributes found in the UML model-These all require computation handled easier in Java code. to simplify the source code templates, the facade design pattern is used to create helper classes that shield the complexities of the MCM from the template. these helper classes are called metafacades (for meta data facades ). each cartridge used by the andromda engine usually contains its own set of metafacades to aid in platform specific code generation. it is the engine's responsibility to instantiate these metafacades for use by the source code templates.
Matching stereotypes to cartridgesAfter initialization, The andromda engine parses the UML Model and looks for classes of specific versions. when it finds a pattern that it can recognize (or, more accurately, a pattern that cartridge can recognize), the appropriate cartridge (s) will be called to generate code. cartridge is actually a jar file. in this jar file there is a file/META-INF/andromda/cartridge. XML. this file is called cartridge descriptor. in cartridge descriptor, there are <template> tags that specify the code generation templates available from the cartridge. inside each <template> tag is a <modelelements> tag. this <modelelements> node will contain one or more <modelelement> nodes. each <modelelement> is used to specify (among other things) the stereotype the template maps. this specification is done in one of two ways: Use the stereotype attribute of the <modelelement> element directly. the value of the stereotype attribute specifies the stereotype the template maps. indirectly use the <type> element. the <type> node specifies the name of the metafacade class the template uses. the metafacade's definition, in turn, contains a definition of the stereotype the metafacade maps. the metafacade's definition is found in the metafacade descriptor-a file in the cartridge. JAR file named/META-INF/andromda-metafacades.xml. the metafacade descriptor contains <metafacade> tags for each metafacade used by the cartridge. each <metafacade> tag in turn contains a <stereotype> tag which defines the name of the stereotype the metafacade maps.
Generating code from templatesOnce the andromda engine identifies a class from the UML Model and matches the corresponding cartridge, appropriate templates will be called to generate code. by default, andromda uses the velocity template engine to generate code. of course, you can also use other template engines, but in fact, velocity has done a lot of work. to see all of the templates available from a participant cartridge, examine the contents of the cartridge's cartridge descriptor file (/META-INF/andromda/cartridge. XML ). this file can be found in one of two places: inside of the cartridge. JAR file itself if it is part of the default andromda source distribution, in the/src/META-INF/andromda sub-directory of the cartridge's source directory (<andromdasourcedir>/cartridges/<nameofcartridge>) each Template available for use in the cartridge is defined in a <template> node inside the cartridge descriptor. the <template> node contains several pieces of important information: The Path attribute specifies the template file in cartridge. the full path in the jar file the outlet attribute specifies (indirectly) The sub-directory where the source file will be written. an outlet is a conceptual name assigned to a sub-directory. it is actually the name of a property found in the cartridge's namespace in the andromda. xml configuration file. for example, an outlet named config-files wocould mean there is a namespace property named config-files in andromda. XML that specifies the sub-directory where all configuration files are to be written. the overwrite attribute specifies whether the template overwrites the previously generated code During code generation. generally, the property of the Code to be manually modified is set to false and will have a different outlet than code that does not require hand modification. <modelelements> the tag defines the metafacade used by the template. the variable attribute of the <modelelements> tag defines the name of the instance variable to be used by the template. that Template variable will be an instance of the specified metafacade. the <modelelements> tag of A <template> definition contains a <modelelement> tag, which may in turn contain a <type> tag. the <type> tag specifies the metafacade class that the template will use. the <type> tag may optionally contain <property> tags. these <property> tags are used for conditional code generation. if a value is specified for a property, that property must match that value for template code to be saved to disk. if the <property> tag does not contain a value, then that property simply must be defined. the property might be a namespace property defined in the andromda. XML config file (to allow for the code generation to be defined at configuration time ). the property may instead be a property set by the template after it has run. this allows the template itself to determine if code shoshould be generated at run-time. for more information on how cartridges work in general, see the cartridge index page
Custom Code GenerationThere are two methods to customize the code generation process. override a template from a pre-existing cartridge with a custom version customize the entire cartridge, re-compiling. JAR file write your own cartridge from scratch. overriding a cartridge's default template (s) the simplest way to customize code generation is to slightly modify or simply re-write one or two templates in an existing cartridge. assuming a participant cartridge does mostly what you are looking for, but you need to change or extend its functionality, you can instruct the andromda engine to use your template rather than the one from the cartridge's. JAR file. locate the original template file you wish to modify. these files can be found in the/templates sub-directory of the cartridge's. JAR file. if you have the andromda source distribution installed on your machine, you can also find them in the/src/templates sub-directory of the cartridge's source directory (<andromdasourcedir>/cartridges/<nameofcartridge> ). copy the desired files to your project directory. if your project conforms to the standard project structure using the maven plug-ins, the best place to put your copy is in the/MDA/src directory of your project (such as <projectdir>/MDA/src/customtemplates /). since you may override templates from more than one cartridge, it is also a good idea to further segregate each cartridge into its own sub-directory. finally, it is important you honor the template structure found in the cartridge. so, to override the hibernate. HBM. XML. VSL template for version 3, place a copy of that file in the sub-directory <projectdir> MDA/src/customtemplates/andromda-hibernate/templates/hibernate3. in the cartridge's <namespace> entry in the andromda. xml configuration file, specify a mergelocation property. for our example hibernate template overwrite, We wocould make the following addition in the andromda. XML file: <namespace name = "hibernate">... <Properties>... <property name = "mergelocation" >$ {Maven. SRC. dir}/customtemplates/andromda-hibernate </property>... </Properties>... </namespace> when you specify a mergelocation property, the andromda engine will first look in your mergelocation directory when looking for a particle template file. if it does not find it, it will fall back on the cartridge's. JAR file. see overriding cartridge resources at overriding cartridge resources for more details.
Customize existing cartridgesAndromda standard cartridges can be modified and expanded if you need more control than a simple template override can provide. the source directory for a cartridge comes with the andromda source distribution. the cartridge's root directory is <andromdasourcedir>/cartridges/<nameofcartridge>. all the source files are located under that directory. from the cartridge root directory, the command Maven install will re-build and re-package the cartridge.
Create cartridges from scratchThe complete cartridges can also be developed. the most difficult aspect of cartridge development is that you may need to customize metafacades. it is gratifying that andromda can be used to develop metafacades. you can use magicdraw to model your metafacade. Then, let andromda generate the metafacade code E.