How to distinguish model elements

MagicDraw OpenAPI, scripting related questions and discussions

Moderator: Moderators

How to distinguish model elements

Postby Marco Wehrmeister » Tue Aug 14, 2007 8:58 am

Hi,

Is there any way to distinguish model elements according their type?
E.g. classes, class instances, lifelines, messages, and so on.

I tried to get a list of all classes of my model with the following code:

------------------------
import com.nomagic.uml2.omg.classes.kernel.Class;

...

protected void loadComponents(Element owner) {
if (owner instanceof Class) {
Class cl = (Class)owner;
m_Components.add(new ModelComponent(cl.getName(), (Class)owner));
}
else {
Collection ownedElements = owner.getOwnedElement();
if (ownedElements.size() > 0) {
for(Iterator it = ownedElements.iterator(); it.hasNext();) {
loadComponents((Element)it.next());
}
}
}
}
------------------------

The problem is that a lot of other unwanted elements are included in my
list, for example Stereotypes and Collaborations.

Any ideas?

Regards,
Marco

---
Ph.D. student at
Embedded Systems Lab - Informatics Institute
Federal University of Rio Grande do Sul - Brazil
Marco Wehrmeister
 
Posts Rating:

Re: How to distinguish model elements

Postby Martynas Lelevicius » Thu Aug 16, 2007 4:13 am

Hi,

You should not use interfaces from com.nomagic.uml2.omg.* package when
working with UML model.
Please use interfaces from com.nomagic.uml2.ext.magicdraw.* (cast if
needed).

To get element type use:
// element type
java.lang.Class classType = element.getClassType();
// type name
String shortName = ClassTypes.getShortName(classType);
// or check instanceof

Regards,
--
Martynas Lelevicius
Programmer
No Magic Lithuanian Development Center
Savanoriu pr. 363, LT-49425, Kaunas
P.O. box 2166, LT-44011, Kaunas
Phone: +370 37 324032 Fax: +370 37 320670
E-mail : marte
WWW: http://www.magicdraw.com

Martynas Lelevicius
 
Posts Rating:

Re: How to distinguish model elements

Postby Marco Wehrmeister » Thu Aug 16, 2007 8:45 am

Hi Martynas,

Thanks, this works fine.

Best regards,
Marco




Martynas Lelevicius wrote:
> Hi,
>
> You should not use interfaces from com.nomagic.uml2.omg.* package when
> working with UML model.
> Please use interfaces from com.nomagic.uml2.ext.magicdraw.* (cast if
> needed).
>
> To get element type use:
> // element type
> java.lang.Class classType = element.getClassType();
> // type name
> String shortName = ClassTypes.getShortName(classType);
> // or check instanceof
>
> Regards,
> --
> Martynas Lelevicius
> Programmer
> No Magic Lithuanian Development Center
> Savanoriu pr. 363, LT-49425, Kaunas
> P.O. box 2166, LT-44011, Kaunas
> Phone: +370 37 324032 Fax: +370 37 320670
> E-mail : marte
> WWW: http://www.magicdraw.com
Marco Wehrmeister
 
Posts Rating:


Return to Programmatic Extendibility

Who is online

Users browsing this forum: No registered users and 0 guests