Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » UML2 » how to find out the type of element in a package
how to find out the type of element in a package [message #476921] Sun, 10 February 2008 10:38 Go to next message
kevin is currently offline kevinFriend
Messages: 22
Registered: July 2009
Junior Member
Hi,

Supposed a UML2 package has been loaded. I want to traverse this package
and find out the type of each element like class, interface, or
component programmatically. How could I do it?

Thanks for any solutions.

Zengyu
Re: how to find out the type of element in a package [message #476922 is a reply to message #476921] Mon, 11 February 2008 02:42 Go to previous messageGo to next message
Rafael Chaves is currently offline Rafael ChavesFriend
Messages: 362
Registered: July 2009
Senior Member
Zengyu,

Like with any ECore based metamodel, you can retrieve the metaclass of
the model element (or EObject) invoking the eClass() operation.

You can then compare the result with the metaclass literal obtained via
<metamodel>Package.Literals.<metaclass constant>. For example,
UMLPackage.Literals.CLASS, UMLPackage.Literals.INTERFACE,
UMLPackage.Literals.COMPONENT etc.

HTH,

Rafael

Zengyu Lu wrote:
> Hi,
>
> Supposed a UML2 package has been loaded. I want to traverse this package
> and find out the type of each element like class, interface, or
> component programmatically. How could I do it?
>
> Thanks for any solutions.
>
> Zengyu
Re: how to find out the type of element in a package [message #476923 is a reply to message #476922] Mon, 11 February 2008 06:40 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: antti.evesti.vtt.fi

I have made it as follows in my code.
EList<Element> el = package.getOwnedElements();
for(Object o : el) {
if(o instanceof StateMachine) {
stateMachine((StateMachine) o);
} else if (o instanceof Activity){
activity((Activity) o);
} else if(o instanceof Collaboration) {
sequenceDiagram((Collaboration) o);
} else if(o instanceof Component) {
componentDiagram((Component) o);
} else {
//System.out.println("Unknown diagram type");
}

I don't know is this the best way, but it looks working.

BR,
Antti

"Rafael Chaves" <rafael@no.spam.abstratt.com> wrote in message
news:foocm6$517$1@build.eclipse.org...
> Zengyu,
>
> Like with any ECore based metamodel, you can retrieve the metaclass of
> the model element (or EObject) invoking the eClass() operation.
>
> You can then compare the result with the metaclass literal obtained via
> <metamodel>Package.Literals.<metaclass constant>. For example,
> UMLPackage.Literals.CLASS, UMLPackage.Literals.INTERFACE,
> UMLPackage.Literals.COMPONENT etc.
>
> HTH,
>
> Rafael
>
> Zengyu Lu wrote:
>> Hi,
>>
>> Supposed a UML2 package has been loaded. I want to traverse this package
>> and find out the type of each element like class, interface, or
>> component programmatically. How could I do it?
>>
>> Thanks for any solutions.
>>
>> Zengyu
Re: how to find out the type of element in a package [message #476925 is a reply to message #476921] Mon, 11 February 2008 11:48 Go to previous messageGo to next message
Tas Frangoullides is currently offline Tas FrangoullidesFriend
Messages: 195
Registered: July 2009
Senior Member
Zengyu,

A flexible (if somewhat more complicated) approach is to use UMLSwitch.
Switch classes are a feature of EMF's code generation and can be used to
programatically process models based on their type. Below is an example
which picks out the metaclasses you mention. Note that the casePackage
method allows recursive processing of package contents.

If you are unfamilar with Switch classes they can take a little getting
used to but they worth the initial effort.

UMLSwitch umlSwitch = new UMLSwitch() {
public Object casePackage(Package object) {
// process the contents of the package through the switch
List<PackageableElement> packageContents =
object.getPackagedElements();
for (PackageableElement element : packageContents) {
doSwitch(element);
}
return super.casePackage(object);
}

public Object caseClass(Class object) {
// do something with class
return super.caseClass(object);
}

public Object caseInterface(Interface object) {
// do something with interface
return super.caseInterface(object);
}

public Object caseComponent(Component object) {
// do somewith with a component
return super.caseComponent(object);
}

};



Model myUmlModel = ... // load model from resource here;

umlSwitch.doSwitch(myUmlModel);
Re: how to find out the type of element in a package [message #476933 is a reply to message #476923] Wed, 13 February 2008 19:49 Go to previous message
kevin is currently offline kevinFriend
Messages: 22
Registered: July 2009
Junior Member
Antti,

Actually I had a solution that is very similar like yours. Just use
if(obj instanceof internal.ClassImpl) and other implementations to find
out the type of each element in package. Well, it works. But I do not
think it is a good idea to access internal things directly. Neither does
Eclipse. :)

But thanks anyway!

Zengyu


Antti Evesti wrote:
> I have made it as follows in my code.
> EList<Element> el = package.getOwnedElements();
> for(Object o : el) {
> if(o instanceof StateMachine) {
> stateMachine((StateMachine) o);
> } else if (o instanceof Activity){
> activity((Activity) o);
> } else if(o instanceof Collaboration) {
> sequenceDiagram((Collaboration) o);
> } else if(o instanceof Component) {
> componentDiagram((Component) o);
> } else {
> //System.out.println("Unknown diagram type");
> }
>
> I don't know is this the best way, but it looks working.
>
> BR,
> Antti
>
> "Rafael Chaves" <rafael@no.spam.abstratt.com> wrote in message
> news:foocm6$517$1@build.eclipse.org...
>> Zengyu,
>>
>> Like with any ECore based metamodel, you can retrieve the metaclass of
>> the model element (or EObject) invoking the eClass() operation.
>>
>> You can then compare the result with the metaclass literal obtained via
>> <metamodel>Package.Literals.<metaclass constant>. For example,
>> UMLPackage.Literals.CLASS, UMLPackage.Literals.INTERFACE,
>> UMLPackage.Literals.COMPONENT etc.
>>
>> HTH,
>>
>> Rafael
>>
>> Zengyu Lu wrote:
>>> Hi,
>>>
>>> Supposed a UML2 package has been loaded. I want to traverse this package
>>> and find out the type of each element like class, interface, or
>>> component programmatically. How could I do it?
>>>
>>> Thanks for any solutions.
>>>
>>> Zengyu
>
>
Re: how to find out the type of element in a package [message #626000 is a reply to message #476921] Mon, 11 February 2008 02:42 Go to previous message
Rafael Chaves is currently offline Rafael ChavesFriend
Messages: 362
Registered: July 2009
Senior Member
Zengyu,

Like with any ECore based metamodel, you can retrieve the metaclass of
the model element (or EObject) invoking the eClass() operation.

You can then compare the result with the metaclass literal obtained via
<metamodel>Package.Literals.<metaclass constant>. For example,
UMLPackage.Literals.CLASS, UMLPackage.Literals.INTERFACE,
UMLPackage.Literals.COMPONENT etc.

HTH,

Rafael

Zengyu Lu wrote:
> Hi,
>
> Supposed a UML2 package has been loaded. I want to traverse this package
> and find out the type of each element like class, interface, or
> component programmatically. How could I do it?
>
> Thanks for any solutions.
>
> Zengyu
Re: how to find out the type of element in a package [message #626022 is a reply to message #476922] Mon, 11 February 2008 06:40 Go to previous message
Eclipse UserFriend
Originally posted by: antti.evesti.vtt.fi

I have made it as follows in my code.
EList<Element> el = package.getOwnedElements();
for(Object o : el) {
if(o instanceof StateMachine) {
stateMachine((StateMachine) o);
} else if (o instanceof Activity){
activity((Activity) o);
} else if(o instanceof Collaboration) {
sequenceDiagram((Collaboration) o);
} else if(o instanceof Component) {
componentDiagram((Component) o);
} else {
//System.out.println("Unknown diagram type");
}

I don't know is this the best way, but it looks working.

BR,
Antti

"Rafael Chaves" <rafael@no.spam.abstratt.com> wrote in message
news:foocm6$517$1@build.eclipse.org...
> Zengyu,
>
> Like with any ECore based metamodel, you can retrieve the metaclass of
> the model element (or EObject) invoking the eClass() operation.
>
> You can then compare the result with the metaclass literal obtained via
> <metamodel>Package.Literals.<metaclass constant>. For example,
> UMLPackage.Literals.CLASS, UMLPackage.Literals.INTERFACE,
> UMLPackage.Literals.COMPONENT etc.
>
> HTH,
>
> Rafael
>
> Zengyu Lu wrote:
>> Hi,
>>
>> Supposed a UML2 package has been loaded. I want to traverse this package
>> and find out the type of each element like class, interface, or
>> component programmatically. How could I do it?
>>
>> Thanks for any solutions.
>>
>> Zengyu
Re: how to find out the type of element in a package [message #626024 is a reply to message #476921] Mon, 11 February 2008 11:48 Go to previous message
Tas Frangoullides is currently offline Tas FrangoullidesFriend
Messages: 195
Registered: July 2009
Senior Member
Zengyu,

A flexible (if somewhat more complicated) approach is to use UMLSwitch.
Switch classes are a feature of EMF's code generation and can be used to
programatically process models based on their type. Below is an example
which picks out the metaclasses you mention. Note that the casePackage
method allows recursive processing of package contents.

If you are unfamilar with Switch classes they can take a little getting
used to but they worth the initial effort.

UMLSwitch umlSwitch = new UMLSwitch() {
public Object casePackage(Package object) {
// process the contents of the package through the switch
List<PackageableElement> packageContents =
object.getPackagedElements();
for (PackageableElement element : packageContents) {
doSwitch(element);
}
return super.casePackage(object);
}

public Object caseClass(Class object) {
// do something with class
return super.caseClass(object);
}

public Object caseInterface(Interface object) {
// do something with interface
return super.caseInterface(object);
}

public Object caseComponent(Component object) {
// do somewith with a component
return super.caseComponent(object);
}

};



Model myUmlModel = ... // load model from resource here;

umlSwitch.doSwitch(myUmlModel);
Re: how to find out the type of element in a package [message #626032 is a reply to message #476923] Wed, 13 February 2008 19:49 Go to previous message
kevin is currently offline kevinFriend
Messages: 22
Registered: July 2009
Junior Member
Antti,

Actually I had a solution that is very similar like yours. Just use
if(obj instanceof internal.ClassImpl) and other implementations to find
out the type of each element in package. Well, it works. But I do not
think it is a good idea to access internal things directly. Neither does
Eclipse. :)

But thanks anyway!

Zengyu


Antti Evesti wrote:
> I have made it as follows in my code.
> EList<Element> el = package.getOwnedElements();
> for(Object o : el) {
> if(o instanceof StateMachine) {
> stateMachine((StateMachine) o);
> } else if (o instanceof Activity){
> activity((Activity) o);
> } else if(o instanceof Collaboration) {
> sequenceDiagram((Collaboration) o);
> } else if(o instanceof Component) {
> componentDiagram((Component) o);
> } else {
> //System.out.println("Unknown diagram type");
> }
>
> I don't know is this the best way, but it looks working.
>
> BR,
> Antti
>
> "Rafael Chaves" <rafael@no.spam.abstratt.com> wrote in message
> news:foocm6$517$1@build.eclipse.org...
>> Zengyu,
>>
>> Like with any ECore based metamodel, you can retrieve the metaclass of
>> the model element (or EObject) invoking the eClass() operation.
>>
>> You can then compare the result with the metaclass literal obtained via
>> <metamodel>Package.Literals.<metaclass constant>. For example,
>> UMLPackage.Literals.CLASS, UMLPackage.Literals.INTERFACE,
>> UMLPackage.Literals.COMPONENT etc.
>>
>> HTH,
>>
>> Rafael
>>
>> Zengyu Lu wrote:
>>> Hi,
>>>
>>> Supposed a UML2 package has been loaded. I want to traverse this package
>>> and find out the type of each element like class, interface, or
>>> component programmatically. How could I do it?
>>>
>>> Thanks for any solutions.
>>>
>>> Zengyu
>
>
Previous Topic:set a stereotype property value
Next Topic:set a stereotype property value
Goto Forum:
  


Current Time: Fri Aug 19 02:28:20 GMT 2022

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

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

Back to the top