Mindaugas Genutis


Senior developer at No Magic, Inc.

Jan 152014

MagicDraw allows the conversion of one model element type to another. To do this, the user selects an element and invokes the conversion action from the context menu. If the model is large and we need to convert many elements, it is more convenient to automate the element conversion task.

The Macro Engine allows the automation of tasks with one of five scripting languages (BeanShell, Groovy, JRuby, JavaScript, and Jython).
Let’s say we have a situation where we want to convert all classes found in a project to stereotypes *. Below is a sample JRuby script which performs such conversion.

The script can be easily modified to change the type of conversion source or target. For example, classes could be converted to interfaces or signals. Also, additional conditions could be checked to distinguish which elements should be converted.

# Author: Mindaugas Genutis, No Magic Inc.
# The purpose of this JRuby script is to demonstrate how model elements
# can be converted to model elements of another type: classes are converted to
# stereotypes.

require 'java'

# Import necessary MagicDraw Open API classes.
Application = com.nomagic.magicdraw.core.Application
Class = com.nomagic.uml2.ext.magicdraw.classes.mdkernel.Class
ConvertElementInfo = com.nomagic.magicdraw.uml.ConvertElementInfo
ElementFinder = com.nomagic.magicdraw.uml.ElementFinder
Refactoring = com.nomagic.magicdraw.uml.Refactoring
SessionManager = com.nomagic.magicdraw.openapi.uml.SessionManager
Stereotype = com.nomagic.uml2.ext.magicdraw.mdprofiles.Stereotype
StereotypesHelper = com.nomagic.uml2.ext.jmi.helpers.StereotypesHelper

# Gets all classes from the model.
def getClasses
    model = Application.getInstance().getProject().getModel()
    ElementFinder.getChildren(model, [Class.java_class].to_java(Java::java.lang.Class), true, false)

# Converts all classes in the model to stereotypes.
def migrateClasses
    info = ConvertElementInfo.new(Stereotype.java_class.to_java(Java::java.lang.Class))
    elementMetaclass = StereotypesHelper.getMetaClassByName(Application.getInstance().getProject(), "Element")
    for clazz in getClasses()
        if clazz.isEditable()
            stereotype = Refactoring::Converting.convert(clazz, info)
            StereotypesHelper.setBaseClass(stereotype, elementMetaclass, nil)

# Starts MagicDraw model editing session.
def startSession
    SessionManager.getInstance().createSession("Migrate classes to stereotypes.")

# Closes MagicDraw model editing session.
def closeSession

    # Starts editing session.

    # Performs migration.


    # Makes sure that in any case the MagicDraw editing session is closed.

* Class conversion to stereotype is a handy first step in automation when you are creating UML profile from a CMOF or EMOF metamodel. CMOF / EMOF metamodels are specified using classes, but for UML Profile the stereotypes with the tags associations and generalizations shall be used instead of classes. Note: conversion is only the first step in UML profile creation art, steps follow:
1. Metaclass selection for stereotype. This step allows choosing the metaclass, which the must correspond to metaelement type.
2. UML properties reuse and the metamodel properties type adjustment. This step allows specifying the best possible editors for properties.