Visiting Model Elements

MagicDraw OpenAPI, scripting related questions and discussions

Moderator: Moderators

Visiting Model Elements

Postby Kristina Hildebrand » Mon Nov 14, 2005 2:17 pm

Yet another question from me.

I've followed the directions on page 4-21 of the version 10 User's
Guide, and I'm getting the following error, when trying to visit every
element in the model: "Unable to Traverse Model".

I have reason to believe that it has successfully completed traversing
the model when I get this error, because my output looks fairly
complete, on preliminary inspection. It is, however, a 1.5 MB output
file, so I can't tell for sure, until I look at it much more closely. I
believe that the error occurs when the visitor attempts to visit the
"package" called "UML Standard Profile". Is there a simple and effective
way to stop it from visiting this "Package" (and similar built-in
packages that are unrelated to my model), other than to add some if
statements, based on the name(s), to my visitPackage code?

Thanks,
Kristina Hildebrand
Kristina Hildebrand
 
Posts Rating:

RE: Visiting Model Elements

Postby Tomas Pugzlys » Mon Nov 21, 2005 12:09 am

Hello,

>Is there a simple and effective way to stop it from visiting this "Package"
>(and similar built-in packages that are unrelated to my model), other than
to add some if
>statements, based on the name(s), to my visitPackage code?

Currently Open API does not provide method to know if it is "built in"
package.
We have plans to provide this method in future MagicDraw releases.

>From your previous questions I understood that your plugin creates elements
under one "root" package.
If it is true, in all cases your plugin can traverse only your own "root"
package.

Another workaround:
//add the following visit methods to your visitor:
public void
visitPackage(com.nomagic.uml2.ext.magicdraw.classes.mdkernel.Package
element, VisitorContext context)
{
if (UML2Constants.UML_STANDARD_PROFILE_PACKAGE.equals(element.getName()))
{
return ;
}

//.....
//..... your custom Package handling and/or super.visitPackage(element,
context)
}

public void visitProfile(Profile element, VisitorContext context)
{
//ignore all readonly profiles.
if (!element.isEditable())
{
return ;
}

super.visitProfile(element, context);
}

If "Unable to Traverse Model" error still occurs, please send exception
stack trace.

Regards,
--
Tomas Pugzlys
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 : tomasp
WWW: http://www.magicdraw.com

Tomas Pugzlys
 
Posts Rating:

Re: Visiting Model Elements

Postby Kristina Hildebrand » Tue Nov 22, 2005 12:49 pm

Hello,

> From your previous questions I understood that your plugin creates elements
> under one "root" package.
> If it is true, in all cases your plugin can traverse only your own "root"
> package.

This is true, to some extent; however, I have since removed that part,
in order to avoid cluttering the model and introducing irrelevant packages.

I had it working, using the following in my Action code, but it was a
rather lousy hack:

Model m = Application.getInstance().getProject().getModel();
Iterator it = m.getOwnedElement().iterator();
while(it.hasNext())
{
Element el = (Element)it.next();
if (!el.getHumanName().startsWith("Package UML Standard"))
{
el.accept(visitor);
}
}

Side curiosity question: Is there a way to get just the element name (as
opposed to "humanName" from an element, or do you have to cast it to a
NamedElement, to do that? I'm also curious about getting the
qualifiedName (aka fullUMLName from 9.5). Are there any Model Elements
that are Elements but not NamedElements?

Your suggested work around seems to work quite well. Thank you. I look
forward to future improvements on the OpenAPI! :)

Thanks,
~Kristina Hildebrand
Kristina Hildebrand
 
Posts Rating:

Re: Visiting Model Elements

Postby Kristina Hildebrand » Tue Nov 29, 2005 2:19 pm

Hello,

I'd like to know how to visit every PresentationElement in a diagram. I
figured you could use something like getOwnedElement().iterator() to get
the presentation elements in a diagram, the same way you would to get
the model elements in a package, but this does not seem to work. I've
read the Presentation Elements chapter in the User's Guide, but it seems
to assume you've already got a handle to the PresentationElement you're
trying to delete or modify, or that you're creating a new one. The
chapter on custom diagram types is no help, either.

Inside my class that extends visitor, I have tried the following code:

public void visitPresentationElement(PresentationElement pe){
Application.getInstance().getGUILog().log(pe.getName()); //no output
}

public void visitDiagramPresentationElement(DiagramPresentationElement dpe){
Application.getInstance().getGUILog().log(dpe.getName()); //no output
}

public void visitDiagram(Diagram d, VisitorContext vc) {
Application.getInstance().getGUILog().log(d.getHumanName());
//outputs the diagram name

Iterator i = d.getOwnedElement().iterator();
Application.getInstance().getGUILog().log(i.hasNext() + "");
//outputs "false"

//obviously, this loop doesn't execute
while (i.hasNext()){
PresentationElement e = (PresentationElement)i.next();
Rectangle bounds = e.getBounds();

Application.getInstance().getGUILog().log(e.getName() + " is " +
bounds.width + " by " + bounds.height + " at " + bounds.x + ", " +
bounds.y);
}
}

How do I convince it to visit all of the PresentationElements in each
diagram?

Thanks,
Kristina Hildebrand

PS: Sorry if I'm getting annoying with all my questions! Some of the
expired newsgroup messages look like they may be relevant to this
question, but the messages expire too quickly for a newsgroup that moves
as slowly as this one does...
Kristina Hildebrand
 
Posts Rating:

Re: Visiting Model Elements

Postby Donatas Simkunas » Wed Nov 30, 2005 5:55 am

Hello,

If you want to get inner elements from diagram please use method PresentationElement.getPresentationElements().

Visitor for presentation elements is not fully supported in open API if you derive your visitor from
com.nomagic.magicdraw.uml.InheritanceVisitor
Then you can overide visiting for DiagramPresentationElement,PathConnector,PathElement,PresentationElement,ShapeElement.


Regards



--
Donatas Simkunas
Senior Programmer
No Magic Lithuanian Development Center
Savanoriu pr. 363, LT 49425 Kaunas
Phone: +370 37 324032   Fax: +370 37 320670
WWW: http://www.magicdraw.com

Donatas Simkunas
 
Posts Rating:

RE: Visiting Model Elements

Postby Tomas Pugzlys » Thu Dec 01, 2005 5:37 am

Hello,
>Are there any Model Elements that are Elements but not NamedElements?
Yes, there are.
For example the following model elements are not named elements:
Generalization, Exception Handler, Template Parameters, Slots...

>Side curiosity question: Is there a way to get just the element name (as
>opposed to "humanName" from an element, or do you have to cast it to a
>NamedElement, to do that?
The best way would be to use instanceof/cast:
String name = el instanceof NamedElement ? ((NamedElement) el).getName() :
null;

Also you can make some helper method that performs the same.

>I'm also curious about getting the qualifiedName (aka fullUMLName from
9.5).
Use the following method to get the full UML name:
RepresentationTextCreator.getFullUMLName(element);

Regards,
--
Tomas Pugzlys
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 : tomasp
WWW: http://www.magicdraw.com

Tomas Pugzlys
 
Posts Rating:

Re: Visiting Model Elements

Postby Kristina Hildebrand » Wed Dec 07, 2005 2:38 pm

Thank you for the response. Further questions are below (I apologize for
the wordiness).

Tomas Pugzlys wrote:
>>Are there any Model Elements that are Elements but not NamedElements?
>
> Yes, there are.
> For example the following model elements are not named elements:
> Generalization, Exception Handler, Template Parameters, Slots...

Say you've got a model containing a lot of packages, classes,
operations, properties, and dependencies. It may have some instances of
Generalization, too. Say you have a seperate "factbase" (a list of
calls relationships, classes, methods, etc.), and you would like to
merge this seperate factbase into the model. I've already programmed
the removal of anything in the model but not the factbase, but I'm
trying to figure out how to add Relationships that are in the external
factbase, but are not already in the MagicDraw model.

The main problem is this: The MagicDraw model may have Generalization
in some places where the 'factbase' only knows how to treat it as being
a relationship, and for this reason I don't want to replace it
(information loss). Since Generalization is unnamed, I need to know how
to look up the fact that there exists a Directed Relationship (of any
type) between two specific Classes in the model. I know how to use
ElementFinder.find, but this only works for Elements that have names.

So, I need a way to return a list of Relationships, that I can check one
by one for matching supplier and client elements. Or, even better, some
way to get all of the relationships with a particular client or
supplier. It looks like there's a getRelationships(Package) in
ModelHelper. I presume it returns a List of Relationships that are in a
specific package. Is this safe to use for this purpose? It's not
documented in the javadoc.

> Use the following method to get the full UML name:
> RepresentationTextCreator.getFullUMLName(element);
Thank you, this will simplify my code a lot.
~Kristina Hildebrand
Kristina Hildebrand
 
Posts Rating:

RE: Visiting Model Elements

Postby Tomas Pugzlys » Thu Dec 08, 2005 5:43 am

Hello,

>It looks like there's a getRelationships(Package) in
>ModelHelper. I presume it returns a List of Relationships that are in a
>specific package. Is this safe to use for this purpose?
Yes, it is safe to use, but not the fastest way.

You may get all client/supplier relationships of the element in the
following way:
Element el = ...;
if (el.has_relationshipOfRelatedElement())
{
Collection relationships = el.get_relationshipOfRelatedElement();
//...
}

get_directedRelationshipOfSource() method returns all client directed
relationships of the element.
get_directedRelationshipOfTarge() method returns all supplier directed
relationships of the element.

These methods return only relationships that are subclasses of
com.nomagic.uml2.ext.magicdraw.classes.mdkernel.Relationship.

Also they do not include Transition, Control Flow, Object Flow, Connector
relationships.

Regards,
--
Tomas Pugzlys
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 : tomasp
WWW: http://www.magicdraw.com

-----Original Message-----
From: nntp://news.nomagic.com/nomagic.products.magicdrawuml.openapi
[mailto:kdhildeb]
Sent: Thursday, December 08, 2005 9:57 AM
To: Tomas Pugzlys
Subject: Re: Visiting Model Elements


Thank you for the response. Further questions are below (I apologize for
the wordiness).

Tomas Pugzlys wrote:
>>Are there any Model Elements that are Elements but not NamedElements?
>
> Yes, there are.
> For example the following model elements are not named elements:
> Generalization, Exception Handler, Template Parameters, Slots...

Say you've got a model containing a lot of packages, classes,
operations, properties, and dependencies. It may have some instances of
Generalization, too. Say you have a seperate "factbase" (a list of
calls relationships, classes, methods, etc.), and you would like to
merge this seperate factbase into the model. I've already programmed
the removal of anything in the model but not the factbase, but I'm
trying to figure out how to add Relationships that are in the external
factbase, but are not already in the MagicDraw model.

The main problem is this: The MagicDraw model may have Generalization
in some places where the 'factbase' only knows how to treat it as being
a relationship, and for this reason I don't want to replace it
(information loss). Since Generalization is unnamed, I need to know how
to look up the fact that there exists a Directed Relationship (of any
type) between two specific Classes in the model. I know how to use
ElementFinder.find, but this only works for Elements that have names.

So, I need a way to return a list of Relationships, that I can check one
by one for matching supplier and client elements. Or, even better, some
way to get all of the relationships with a particular client or
supplier. It looks like there's a getRelationships(Package) in
ModelHelper. I presume it returns a List of Relationships that are in a
specific package. Is this safe to use for this purpose? It's not
documented in the javadoc.

> Use the following method to get the full UML name:
> RepresentationTextCreator.getFullUMLName(element);
Thank you, this will simplify my code a lot.
~Kristina Hildebrand

Tomas Pugzlys
 
Posts Rating:

Re: Visiting Model Elements

Postby Kristina Hildebrand » Thu Dec 08, 2005 10:32 am

Tomas Pugzlys wrote:
> get_directedRelationshipOfSource() method returns all client directed
> relationships of the element.
> get_directedRelationshipOfTarge() method returns all supplier directed
> relationships of the element.

I'm a little confused on the wording here. Say I'm looking for
dependency "a" from client to supplier:

______ __________
|client|---a--->| supplier |
------ ----------

Will the collection returned by
client.get_directedRelationshipOfSource() contain this
DirectedRelationship in the list, or is it the other way around?

Thanks,
Kristina Hildebrand
Kristina Hildebrand
 
Posts Rating:

Re: Visiting Model Elements

Postby Kristina Hildebrand » Thu Dec 08, 2005 11:44 am

Kristina Hildebrand wrote:
> Tomas Pugzlys wrote:
>
>> get_directedRelationshipOfSource() method returns all client directed
>> relationships of the element.
>> get_directedRelationshipOfTarge() method returns all supplier directed
>> relationships of the element.
>
>
> I'm a little confused on the wording here. Say I'm looking for
> dependency "a" from client to supplier:
>
> ______ __________
> |client|---a--->| supplier |
> ------ ----------
>
> Will the collection returned by
> client.get_directedRelationshipOfSource() contain this
> DirectedRelationship in the list, or is it the other way around?
>
> Thanks,
> Kristina Hildebrand

Nevermind, I figured it out by trial and error, and it appears that it
is indeed how I wrote it above. :)

~K
Kristina Hildebrand
 
Posts Rating:

Next

Return to Programmatic Extendibility

Who is online

Users browsing this forum: No registered users and 1 guest