Language reference

This section will not describe groovy language, as official Groovy Templates manual is already here for that. It will give you practical examples to start generating your own source code. It will be regularly updated to give you more and more help. Also, please read Anycode UML API in order to know more about methods you can use.

About Anycode UML API

Before 1.2.0_GA version, Anycode used to rely on Astah API and exposed it directly in the templates. Starting from 1.2.0_GA and above, Anycode will always rely on its own UML API The main objectives for wrapping Astah API are to ensure Anycode stability over Astah releases and to make the code generation easier. It is a very recent development and more functions will be added in the next releases. The examples below will show how to use it.

Class

// Display class name
${c.name}

// Class fully qualified name (separated by '.')
${c.getFullyQualifiedName(".")}

// Class package (separated by '.')
${c.owner.getFullyQualifiedName(".")}

// A php class source path (common use case for -name template) in target directory 
${targetDir}/${c.getFullyQualifiedName("/").php}

Attributes

// Iterating over attributes 
<% c.attributes.each { %>
	// 'it' variable is an IAttribute. This will print attribute name
	${it.name}
<% } %>

Operations

// Iterating over operations, that start with 'b' (using closures)
<% c.operations.findAll({it.name.startsWith("b")}).each { %>
	// 'it' variable is an IOperation. This will print operation name
	${it.name}
<% } %>

Relationship

Simple attributes and relations are both listed in the attributes of the owning class. However you can easily distinguish them thanks to Anycode API

// Iterating over simple attributes (i.e. : that are not part of a relationship)
<% c.attributes.findAll({!it.relation}).each { %>
	// This will use JavaDirective to render attribute in java language.
	${java.attribute(it)}
<% } %>

// Is my attribute 'att' part of a one to one relationship ?
<% if(att.relation && att.oneToOne) { %>
	// Let do something here...
<% } %>

Dependencies

<% c.clientDependencies.each { %>
	// 'it' is a IClassifier object c depends on.	
<% } %>

Inheritance

// Displays coma separated list of supertypes 
<% c.generalizations.each { %>
	${it.name} ${(it != c.generalizations.last()) ? "," : ""} 	
<% } %>

Stereotypes

<% if(c.hasStereotype("entity")) { %>
	// means that class has stereotype 'entity'
<% } %>