Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » UML2 » Get classes from a model with the API
Get classes from a model with the API [message #1754556] Mon, 20 February 2017 08:38 Go to next message
Pablo AG is currently offline Pablo AGFriend
Messages: 7
Registered: February 2017
Junior Member
Hi,

I am using the UML2 API to load a model that I have created previously with Papyrus. I want to parse this model to find and get all the classes. This is the code that I am using by now:

EList<Type> list4 = model.getOwnedTypes();
for (Type element : list4)
{
out(element.getName());
}


With this code I am able to get a lot of elements of the model, including classes, associations, etc. However, I just want to get the classes of a UML diagram. How can I achieve this? How can I check the type of the elements?

This must be quite simple to do but I am just browsing through the API and I can't find anything. Could you refer me to some examples?

Regards,
Pablo
Re: Get classes from a model with the API [message #1754715 is a reply to message #1754556] Tue, 21 February 2017 18:22 Go to previous messageGo to next message
German Vega is currently offline German VegaFriend
Messages: 44
Registered: December 2015
Member
Hello

Just test if the element is an instance of org.eclipse.uml2.uml.Class and cast it

for (Type element : model.getOwnedTypes()) { 
    if (element instanceof org.eclipse.uml2.uml.Class ) {
       org.eclipse.uml2.uml.Class  theClass = (org.eclipse.uml2.uml.Class ) element;
    }
}

Re: Get classes from a model with the API [message #1754778 is a reply to message #1754556] Wed, 22 February 2017 10:07 Go to previous messageGo to next message
Pablo AG is currently offline Pablo AGFriend
Messages: 7
Registered: February 2017
Junior Member
Thanks, I am new to Java and I obviated that.

Another approach could be this one:

if(element.getClass().getName().equals("org.eclipse.uml2.uml.internal.impl.ClassImpl"))
Re: Get classes from a model with the API [message #1754782 is a reply to message #1754778] Wed, 22 February 2017 10:30 Go to previous message
Ed Willink is currently offline Ed WillinkFriend
Messages: 5580
Registered: July 2009
Senior Member
Hi

No. Never compare by exact name, you ensure that all extensibility with inheritance goes wrong and break the encapsulation whereby users should only use the interface.

If you really want to compare the name, at least use eClass().getName() to hide the EMF details.

Regards

Ed Willink
Previous Topic:Get and create SysML subprofiles (SysML 1.4 Memtamodel)
Next Topic:[CDO/UML2/MARTE] Marte profile issue
Goto Forum:
  


Current Time: Fri Jan 19 06:04:59 GMT 2018

Powered by FUDForum. Page generated in 0.01451 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software