Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » copy an element
copy an element [message #1800281] Sun, 23 December 2018 10:32 Go to next message
Marzieh ghorbani is currently offline Marzieh ghorbaniFriend
Messages: 33
Registered: September 2017
Member
Hi all
I want to copy an element of a model to another model by using EcoreUtile.copy(..). But there is a problem. As is seen in the attached picture, the copied element(column "email") has not been added to its corresponding table. In other words the copied element must added in to Table "Student". can you guide me and pleas say me How can I solve this problem?

Best regards,
Marzieh

code:

ResourceSet rs = new ResourceSetImpl();
Resource resource2= resourseSet.getResource(URI.createURI("Model/Target.xmi"), true);
EObject copyElement= EcoreUtil.copy((EObject)ElementAddTarget);
resource2.getContents().add(copyElement);

/// save model
@SuppressWarnings("rawtypes")
Map options1 = new HashMap();
options1.put(XMIResource.OPTION_SCHEMA_LOCATION, Boolean.TRUE);
try {
resource1.save(options1);
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
  • Attachment: Capture.JPG
    (Size: 17.03KB, Downloaded 77 times)
Re: copy an element [message #1800282 is a reply to message #1800281] Sun, 23 December 2018 12:03 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 6460
Registered: July 2009
Senior Member
Hi

What you observe is exactly what is intended.

Just imagine that EcoreUtil.copy had a magic heuristic to deduce the container, this heurisrtic would frequently fail causing inappropriate modification and notifications of the deduced container. These would have to be repeated in reverse when the failed heuristic is remedied. Making the caller responsible for the ongoing containment is much better. I suppose there could be a more bloated API that took a container argument, but an extra function just for a simple call doesn't seem wise.

Regards

Ed Willink
Re: copy an element [message #1800294 is a reply to message #1800281] Sun, 23 December 2018 20:52 Go to previous messageGo to next message
Erdal Karaca is currently offline Erdal KaracaFriend
Messages: 853
Registered: July 2009
Senior Member
You would need to add the copied element to its target container:

getStudentTable().getColumns().add(copy)


Marzieh ghorbani wrote on Sun, 23 December 2018 11:32
Hi all
I want to copy an element of a model to another model by using EcoreUtile.copy(..). But there is a problem. As is seen in the attached picture, the copied element(column "email") has not been added to its corresponding table. In other words the copied element must added in to Table "Student". can you guide me and pleas say me How can I solve this problem?

Best regards,
Marzieh

code:

ResourceSet rs = new ResourceSetImpl();
Resource resource2= resourseSet.getResource(URI.createURI("Model/Target.xmi"), true);
EObject copyElement= EcoreUtil.copy((EObject)ElementAddTarget);
resource2.getContents().add(copyElement);

/// save model
@SuppressWarnings("rawtypes")
Map options1 = new HashMap();
options1.put(XMIResource.OPTION_SCHEMA_LOCATION, Boolean.TRUE);
try {
resource1.save(options1);
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}

Re: copy an element [message #1800375 is a reply to message #1800294] Wed, 26 December 2018 08:02 Go to previous messageGo to next message
Marzieh ghorbani is currently offline Marzieh ghorbaniFriend
Messages: 33
Registered: September 2017
Member
Hi Erdal Karaca
Thank you for your answer. but I couldn't add the copied element to its target container. "getStudentTable()" and "getColumns()" not defined.


Best regards,
Marzieh

[Updated on: Sat, 29 December 2018 07:42]

Report message to a moderator

Re: copy an element [message #1800437 is a reply to message #1800375] Fri, 28 December 2018 08:41 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 30467
Registered: July 2009
Senior Member
You seem to have invested very little effort in understanding how access the contents of the resource containing your Tables. If you want to add a Column to a Table you must locate the Table instance to which you want to add the Column instance. Likely you have an interface called Table with a method getColumns(). You must decide and find which Table instance to which you want to add the copied Column instance and do so using table.getColumns().add(column). Surely you've somehow located a Column that you are copying, so why are you not able to figure how how to locate a Table?
Re: copy an element [message #1800441 is a reply to message #1800437] Fri, 28 December 2018 12:54 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 30467
Registered: July 2009
Senior Member
It doesn't do much good for anyone if you to change the question after I've answered it. Your post shows "[Updated on: Fri, 28 December 2018 11:19]" but I answered "Fri, 28 December 2018 09:41".

I've already explained that you need to locate your "target container", a Table presumably, and use table.getColumns().add(column), so that's the ongoing problem? Can't locate the Table? If not, explain first how you located the Column that you copied.
Re: copy an element [message #1800473 is a reply to message #1800441] Sat, 29 December 2018 08:02 Go to previous messageGo to next message
Marzieh ghorbani is currently offline Marzieh ghorbaniFriend
Messages: 33
Registered: September 2017
Member
Hi Ed Merks
Excuse me for mistake occurred. Thank you for your attention. I'm trying to implement the code as you described it.
Best Regard
Marzieh

[Updated on: Sat, 29 December 2018 08:25]

Report message to a moderator

Re: copy an element [message #1800695 is a reply to message #1800441] Sat, 05 January 2019 19:22 Go to previous messageGo to next message
Marzieh ghorbani is currently offline Marzieh ghorbaniFriend
Messages: 33
Registered: September 2017
Member
Hi Ed Merks

I get the copied element Container with the following command. (for example element is "column email" and the container is "Table Student")[1]. Then I create a new element in another model and I want to set the container of this new element as the container of the element that I Already achieved it. I'm using the following code. But the container doesn't set.[2]
please guide me ?
Also I don't use this code: "table.getColumns().add(column)".

Best Regard
Marzieh
Re: copy an element [message #1800697 is a reply to message #1800695] Sat, 05 January 2019 22:03 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 6460
Registered: July 2009
Senior Member
Hi

JPGs are totally unsuitable for conveying text. They can't be cut and paste and executed. They aren't even shown concurrently. Difficult to know what you are doing.

EMF, like,UML, maintains a strict single containment policy, consequently a common cause of 'bad' containers is child stealing.

If for instance A1::child contains A2 and B1::child contains B2, assignment of B2 to A1::child displaces A2 making A2 an orphan and steals B2 from A2::child making A2 childless.

Regards

Ed Willink
Re: copy an element [message #1800893 is a reply to message #1800697] Wed, 09 January 2019 13:31 Go to previous messageGo to next message
Marzieh ghorbani is currently offline Marzieh ghorbaniFriend
Messages: 33
Registered: September 2017
Member
Hi all

I have one model ("Targets.xmi" ). I want to add a new element in to "Table School". I created a new element, then I set the properties of the element.(properties: "Name" and "Table"). when I run my code, the following error was showed: "Exception in thread "main" java.lang.ClassCastException: The value of type 'class java.lang.String' must be of type 'org.eclipse.emf.ecore.impl.EClassImpl@146ba0ac (name: Table) (instanceClassName: null) (abstract: false, interface: false)'".


Is it possible to change the variable type from Object or string to Table type?
If it is possible, I don't Know, how can I do this. Please guide me?
The minimal example is attached. It contains " EtlStandaloneExample.java" file. Please run it to see the errors.

Best Regard
Marzieh
Re: copy an element [message #1800919 is a reply to message #1800893] Wed, 09 January 2019 17:51 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 30467
Registered: July 2009
Senior Member
Have you use the debugger yet? You can set an exception-breakpoint in the breakpoints view (the little J!) tool item and figure out where you are passing in a String value where you should pass in an EObject of the correct type instead.
Previous Topic:[CDO] Object is unexpectedly non-writable
Next Topic:Problem when use generator in java code
Goto Forum:
  


Current Time: Mon Sep 23 11:45:17 GMT 2019

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

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

Back to the top