Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Best way to copy element
Best way to copy element [message #421002] Mon, 21 July 2008 11:16 Go to next message
Timothy Marc is currently offline Timothy MarcFriend
Messages: 547
Registered: July 2009
Senior Member
Hi all,

i have a sequence diagram for which i had to implement a transformation
programatically. Except for the transformation-speficic adjustments, there
are a lot of elements, which i only want to copy from one resource to the
other. For example:

Every child element of Package org should be moved to Package new in the
other model.

I tried to iterate over the childs and to change their owner, but this
causes me a ConcurrentModificationException. Of course!

My approach was to move all the childs to the new model and then invoke the
Switch-Visitor, what EMF provides, to configure the elements for the new
model (i.e. changing the names, etc.)

It would be great, if someone can give me a hint, how to realize this.

Thx

--Timothy
Re: Best way to copy element [message #421004 is a reply to message #421002] Mon, 21 July 2008 13:04 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: cdamus.zeligsoft.com

--=-WzrkolWoJJS9TFysQ+on
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

Hi, Timothy,

Perhaps you will get better results from actually copying the elements
instead of moving them. The EcoreUtil.copyAll method will copy a bunch
of elements in one operation, including any cross-references between
them (to maintain integrity of the copied graph). You can also
specialize the EcoreUtil.Copier class to customize the algorithm, if
necessary.

Otherwise, perhaps simply copying the eContents() list of an element
before moving it will get you past the ConcurrentModificationException.

HTH,

Christian


On Mon, 2008-07-21 at 13:16 +0200, Timothy Marc wrote:

> Hi all,
>
> i have a sequence diagram for which i had to implement a transformation
> programatically. Except for the transformation-speficic adjustments, there
> are a lot of elements, which i only want to copy from one resource to the
> other. For example:
> copyA
> Every child element of Package org should be moved to Package new in the
> other model.
>
> I tried to iterate over the childs and to change their owner, but this
> causes me a ConcurrentModificationException. Of course!
>
> My approach was to move all the childs to the new model and then invoke the
> Switch-Visitor, what EMF provides, to configure the elements for the new
> model (i.e. changing the names, etc.)
>
> It would be great, if someone can give me a hint, how to realize this.
>
> Thx
>
> --Timothy
>
>

--=-WzrkolWoJJS9TFysQ+on
Content-Type: text/html; charset=utf-8

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=UTF-8">
<META NAME="GENERATOR" CONTENT="GtkHTML/3.16.0">
</HEAD>
<BODY>
Hi, Timothy,<BR>
<BR>
Perhaps you will get better results from actually copying the elements instead of moving them.&nbsp; The EcoreUtil.copyAll method will copy a bunch of elements in one operation, including any cross-references between them (to maintain integrity of the copied graph).&nbsp; You can also specialize the EcoreUtil.Copier class to customize the algorithm, if necessary.<BR>
<BR>
Otherwise, perhaps simply copying the eContents() list of an element before moving it will get you past the ConcurrentModificationException.<BR>
<BR>
HTH,<BR>
<BR>
Christian<BR>
<BR>
<BR>
On Mon, 2008-07-21 at 13:16 +0200, Timothy Marc wrote:
<BLOCKQUOTE TYPE=CITE>
<PRE>
<FONT COLOR="#000000">Hi all,</FONT>

<FONT COLOR="#000000">i have a sequence diagram for which i had to implement a transformation </FONT>
<FONT COLOR="#000000">programatically. Except for the transformation-speficic adjustments, there </FONT>
<FONT COLOR="#000000">are a lot of elements, which i only want to copy from one resource to the </FONT>
<FONT COLOR="#000000">other. For example:</FONT>
copyA
<FONT COLOR="#000000">Every child element of Package org should be moved to Package new in the </FONT>
<FONT COLOR="#000000">other model.</FONT>

<FONT COLOR="#000000">I tried to iterate over the childs and to change their owner, but this </FONT>
<FONT COLOR="#000000">causes me a ConcurrentModificationException. Of course!</FONT>

<FONT COLOR="#000000">My approach was to move all the childs to the new model and then invoke the </FONT>
<FONT COLOR="#000000">Switch-Visitor, what EMF provides, to configure the elements for the new </FONT>
<FONT COLOR="#000000">model (i.e. changing the names, etc.)</FONT>

<FONT COLOR="#000000">It would be great, if someone can give me a hint, how to realize this.</FONT>

<FONT COLOR="#000000">Thx</FONT>

<FONT COLOR="#000000">--Timothy </FONT>


</PRE>
</BLOCKQUOTE>
</BODY>
</HTML>

--=-WzrkolWoJJS9TFysQ+on--
Re: Best way to copy element [message #421006 is a reply to message #421004] Mon, 21 July 2008 13:31 Go to previous messageGo to next message
Timothy Marc is currently offline Timothy MarcFriend
Messages: 547
Registered: July 2009
Senior Member
Hey Christian,

thank you fo your rapid answer. Yeah, i found the EcoreUtil-Class in the
minute, i send this post.

But i have a question concerning the Switch mechanism, that is generated
automatically. The switches are packagewide, aren't they? In my containment
tree, i have elements from several packages, and i tried to access the
visitor methods (caseXYZ) like this (taken from the "EMF bible"):

for(final TreeIterator<EObject> it =
pstTestSequences.eAllContents();it.hasNext();){
final EObject element = it.next();
visitor.doSwitch(element);
}

But that works only for the elements, specified in the package which switch
i had extended. I thought that it would be possible to iterate over a whole
containment tree by using a problem-specific specialization of an modelwide
switch. I think this is also the intention from the Visitor pattern, isn't
it? To add additional behavior to a containment/object tree...

May i a have a false imagination of the emf visitor?

Thx
--Timothy

"Christian W. Damus" <cdamus@zeligsoft.com> schrieb im Newsbeitrag
news:1216645450.6639.6.camel@cwdlappy...
Hi, Timothy,

Perhaps you will get better results from actually copying the elements
instead of moving them. The EcoreUtil.copyAll method will copy a bunch of
elements in one operation, including any cross-references between them (to
maintain integrity of the copied graph). You can also specialize the
EcoreUtil.Copier class to customize the algorithm, if necessary.

Otherwise, perhaps simply copying the eContents() list of an element before
moving it will get you past the ConcurrentModificationException.

HTH,

Christian


On Mon, 2008-07-21 at 13:16 +0200, Timothy Marc wrote:
Hi all,

i have a sequence diagram for which i had to implement a transformation
programatically. Except for the transformation-speficic adjustments, there
are a lot of elements, which i only want to copy from one resource to the
other. For example:
copyA
Every child element of Package org should be moved to Package new in the
other model.

I tried to iterate over the childs and to change their owner, but this
causes me a ConcurrentModificationException. Of course!

My approach was to move all the childs to the new model and then invoke the
Switch-Visitor, what EMF provides, to configure the elements for the new
model (i.e. changing the names, etc.)

It would be great, if someone can give me a hint, how to realize this.

Thx

--Timothy
Re: Best way to copy element [message #421009 is a reply to message #421006] Mon, 21 July 2008 14:22 Go to previous message
Eclipse UserFriend
Originally posted by: cdamus.zeligsoft.com

--=-0byNYb/caRfKMcyyVR9d
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

Hi, Timothy,

For any EPackages that your EPackage extends (by having EClasses that
specialize EClasses from the other packages), your generated Switch will
include cases for the EClasses from those packages that your package
depends on.

For example, the UMLSwitch from the MDT UML2 component
(org.eclipse.uml2.uml) includes a caseEModelElement because uml::Element
specializes ecore::EModelElement.

If this isn't the case, then you can always set up delegation to other
switch instances in your defaultCase. e.g.,

T defaultCase(EObject object) {
Object result;
result = modelASwitch.doSwitch(object);
if (result == null) result = modelBSwitch.doSwitch(object);
if (result == null) result = modelCSwitch.doSwitch(object);
return result;
}

HTH,

Christian


On Mon, 2008-07-21 at 15:31 +0200, Timothy Marc wrote:

> Hey Christian,
>
> thank you fo your rapid answer. Yeah, i found the EcoreUtil-Class in the
> minute, i send this post.
>
> But i have a question concerning the Switch mechanism, that is generated
> automatically. The switches are packagewide, aren't they? In my containment
> tree, i have elements from several packages, and i tried to access the
> visitor methods (caseXYZ) like this (taken from the "EMF bible"):
>
> for(final TreeIterator<EObject> it =
> pstTestSequences.eAllContents();it.hasNext();){
> final EObject element = it.next();
> visitor.doSwitch(element);
> }
>
> But that works only for the elements, specified in the package which switch
> i had extended. I thought that it would be possible to iterate over a whole
> containment tree by using a problem-specific specialization of an modelwide
> switch. I think this is also the intention from the Visitor pattern, isn't
> it? To add additional behavior to a containment/object tree...
>
> May i a have a false imagination of the emf visitor?
>
> Thx
> --Timothy
>
> "Christian W. Damus" <cdamus@zeligsoft.com> schrieb im Newsbeitrag
> news:1216645450.6639.6.camel@cwdlappy...
> Hi, Timothy,
>
> Perhaps you will get better results from actually copying the elements
> instead of moving them. The EcoreUtil.copyAll method will copy a bunch of
> elements in one operation, including any cross-references between them (to
> maintain integrity of the copied graph). You can also specialize the
> EcoreUtil.Copier class to customize the algorithm, if necessary.
>
> Otherwise, perhaps simply copying the eContents() list of an element before
> moving it will get you past the ConcurrentModificationException.
>
> HTH,
>
> Christian
>
>
> On Mon, 2008-07-21 at 13:16 +0200, Timothy Marc wrote:
> Hi all,
>
> i have a sequence diagram for which i had to implement a transformation
> programatically. Except for the transformation-speficic adjustments, there
> are a lot of elements, which i only want to copy from one resource to the
> other. For example:
> copyA
> Every child element of Package org should be moved to Package new in the
> other model.
>
> I tried to iterate over the childs and to change their owner, but this
> causes me a ConcurrentModificationException. Of course!
>
> My approach was to move all the childs to the new model and then invoke the
> Switch-Visitor, what EMF provides, to configure the elements for the new
> model (i.e. changing the names, etc.)
>
> It would be great, if someone can give me a hint, how to realize this.
>
> Thx
>
> --Timothy
>
>

--=-0byNYb/caRfKMcyyVR9d
Content-Type: text/html; charset=utf-8

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=UTF-8">
<META NAME="GENERATOR" CONTENT="GtkHTML/3.16.0">
</HEAD>
<BODY>
Hi, Timothy,<BR>
<BR>
For any EPackages that your EPackage extends (by having EClasses that specialize EClasses from the other packages), your generated Switch will include cases for the EClasses from those packages that your package depends on.<BR>
<BR>
For example, the UMLSwitch from the MDT UML2 component (org.eclipse.uml2.uml) includes a caseEModelElement because uml::Element specializes ecore::EModelElement.<BR>
<BR>
If this isn't the case, then you can always set up delegation to other switch instances in your defaultCase.&nbsp; e.g.,<BR>
<BR>
&nbsp;&nbsp;&nbsp; T defaultCase(EObject object) {<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Object result;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; result = modelASwitch.doSwitch(object);<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (result == null) result = modelBSwitch.doSwitch(object);<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (result == null) result = modelCSwitch.doSwitch(object);<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return result;<BR>
&nbsp;&nbsp;&nbsp; }<BR>
<BR>
HTH,<BR>
<BR>
Christian<BR>
<BR>
<BR>
On Mon, 2008-07-21 at 15:31 +0200, Timothy Marc wrote:
<BLOCKQUOTE TYPE=CITE>
<PRE>
<FONT COLOR="#000000">Hey Christian,</FONT>

<FONT COLOR="#000000">thank you fo your rapid answer. Yeah, i found the EcoreUtil-Class in the </FONT>
<FONT COLOR="#000000">minute, i send this post.</FONT>

<FONT COLOR="#000000">But i have a question concerning the Switch mechanism, that is generated </FONT>
<FONT COLOR="#000000">automatically. The switches are packagewide, aren't they? In my containment </FONT>
<FONT COLOR="#000000">tree, i have elements from several packages, and i tried to access the </FONT>
<FONT COLOR="#000000">visitor methods (caseXYZ) like this (taken from the &quot;EMF bible&quot;):</FONT>

<FONT COLOR="#000000">for(final TreeIterator&lt;EObject&gt; it = </FONT>
<FONT COLOR="#000000">pstTestSequences.eAllContents();it.hasNext();){ </FONT>
<FONT COLOR="#000000"> final EObject element = it.next();</FONT>
<FONT COLOR="#000000"> visitor.doSwitch(element);</FONT>
<FONT COLOR="#000000">}</FONT>

<FONT COLOR="#000000">But that works only for the elements, specified in the package which switch </FONT>
<FONT COLOR="#000000">i had extended. I thought that it would be possible to iterate over a whole </FONT>
<FONT COLOR="#000000">containment tree by using a problem-specific specialization of an modelwide </FONT>
<FONT COLOR="#000000">switch. I think this is also the intention from the Visitor pattern, isn't </FONT>
<FONT COLOR="#000000">it? To add additional behavior to a containment/object tree...</FONT>

<FONT COLOR="#000000">May i a have a false imagination of the emf visitor?</FONT>

<FONT COLOR="#000000">Thx</FONT>
<FONT COLOR="#000000">--Timothy</FONT>

<FONT COLOR="#000000">&quot;Christian W. Damus&quot; &lt;<A HREF="mailto:cdamus@zeligsoft.com">cdamus@zeligsoft.com</A>&gt; schrieb im Newsbeitrag </FONT>
<FONT COLOR="#000000">news:1216645450.6639.6.camel@cwdlappy...</FONT>
<FONT COLOR="#000000">Hi, Timothy,</FONT>

<FONT COLOR="#000000">Perhaps you will get better results from actually copying the elements </FONT>
<FONT COLOR="#000000">instead of moving them. The EcoreUtil.copyAll method will copy a bunch of </FONT>
<FONT COLOR="#000000">elements in one operation, including any cross-references between them (to </FONT>
<FONT COLOR="#000000">maintain integrity of the copied graph). You can also specialize the </FONT>
<FONT COLOR="#000000">EcoreUtil.Copier class to customize the algorithm, if necessary.</FONT>

<FONT COLOR="#000000">Otherwise, perhaps simply copying the eContents() list of an element before </FONT>
<FONT COLOR="#000000">moving it will get you past the ConcurrentModificationException.</FONT>

<FONT COLOR="#000000">HTH,</FONT>

<FONT COLOR="#000000">Christian</FONT>


<FONT COLOR="#000000">On Mon, 2008-07-21 at 13:16 +0200, Timothy Marc wrote:</FONT>
<FONT COLOR="#000000">Hi all,</FONT>

<FONT COLOR="#000000">i have a sequence diagram for which i had to implement a transformation</FONT>
<FONT COLOR="#000000">programatically. Except for the transformation-speficic adjustments, there</FONT>
<FONT COLOR="#000000">are a lot of elements, which i only want to copy from one resource to the</FONT>
<FONT COLOR="#000000">other. For example:</FONT>
<FONT COLOR="#000000">copyA</FONT>
<FONT COLOR="#000000">Every child element of Package org should be moved to Package new in the</FONT>
<FONT COLOR="#000000">other model.</FONT>

<FONT COLOR="#000000">I tried to iterate over the childs and to change their owner, but this</FONT>
<FONT COLOR="#000000">causes me a ConcurrentModificationException. Of course!</FONT>

<FONT COLOR="#000000">My approach was to move all the childs to the new model and then invoke the</FONT>
<FONT COLOR="#000000">Switch-Visitor, what EMF provides, to configure the elements for the new</FONT>
<FONT COLOR="#000000">model (i.e. changing the names, etc.)</FONT>

<FONT COLOR="#000000">It would be great, if someone can give me a hint, how to realize this.</FONT>

<FONT COLOR="#000000">Thx</FONT>

<FONT COLOR="#000000">--Timothy </FONT>


</PRE>
</BLOCKQUOTE>
</BODY>
</HTML>

--=-0byNYb/caRfKMcyyVR9d--
Previous Topic:Re: JPA Temporal annotation doesn't work for EReference features
Next Topic:Resource save performance
Goto Forum:
  


Current Time: Tue Sep 24 18:35:28 GMT 2024

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

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

Back to the top