Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 4.0

The Eclipse UML2 project provides an implementation of the Unified Modeling Language (UML) 2.1 metamodel in Java. The fact that the underlying technology is the Eclipse Modeling Framework (EMF) makes it possible to use the EMFBuilder with the UML2 metamodel.

The introductory article by Ken Hussey explains how to create UML2 models with the Eclipse editor and how to create them programmatically with Java code. Here we will use Groovy and the EMFBuilder.

We use the UMLFactory for the EMFBuilder.

Code Block
def builder = new EMFBuilder(UMLFactory)

We create a model as the root node and then we create two primitive types and store them in Groovy variables because we have to reference them later on.

Code Block
def epo2Model = builder.Model(name: 'epo2') {
	packagedElement {
		def intPrimitiveType = PrimitiveType(name: 'int')
		def stringPrimitiveType = PrimitiveType(name: 'String')

We define an enumeration OrderStatus with three literals.

Code Block
def orderStatusEnumeration = Enumeration(name: 'OrderStatus') {
			ownedLiteral {
				EnumerationLiteral(name: 'Pending')
				EnumerationLiteral(name: 'Back Order')
				EnumerationLiteral(name: 'Complete')
			}
		}

The following code snippet shows the definition of the classes Address and USAddress. All the attributes are defined as a Property. The primitive types stringPrimitiveType and intPrimitiveType defined above are used. The class USAddress is a subclass of the abstract class Address. This is expressed with the Generalization object.

Code Block
def addressClass = Class(name: 'Address' ,isAbstract: true) {
			ownedAttribute {
				Property(name: 'name', type: stringPrimitiveType, lower: 0, upper: 1)
				Property(name: 'country', type: stringPrimitiveType, lower: 0, upper: 1)
			}
		}
		def usAddressClass = Class(name: 'USAddress') {
			generalization {
				Generalization(general: addressClass)
			}
			ownedAttribute {
				Property(name: 'street', type: stringPrimitiveType, lower: 0, upper: 1)
				Property(name: 'city', type: stringPrimitiveType, lower: 0, upper: 1)
				Property(name: 'state', type: stringPrimitiveType, lower: 0, upper: 1)
				Property(name: 'zip', type: intPrimitiveType, lower: 0, upper: 1)
			}
		}

Compare this code to the original code in the article! The code is much more concise and is a direct representation of the UML2 diagram. No auxiliary methods are needed.

See the homepage of the UML2 Builder for further details.