Access EMF object in Xtend [message #1848207] |
Wed, 24 November 2021 18:39  |
Eclipse User |
|
|
|
Hi all,
I am trying to generate some text from a model using Xtend. In order to do that, I have followed the following steps (all in a runtime instance):
1. Create metamodel (TestMetamodel)
2.Create a dynamic instance (Root.xmi)
2. Create an Xtend project, add all the dependencies
This is how they look: 
3. Write the following code to load the meta model and model:
package xtendgeneration.xtend
import org.eclipse.emf.ecore.resource.ResourceSet
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl
import org.eclipse.emf.ecore.resource.impl.ResourceFactoryImpl
import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.emf.common.util.URI
import org.eclipse.emf.ecore.EPackage
import java.util.logging.Logger
import org.eclipse.emf.ecore.xmi.impl.EcoreResourceFactoryImpl
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl
import org.eclipse.emf.ecore.EClass
import java.io.File
import org.eclipse.emf.ecore.util.EcoreUtil
import org.eclipse.emf.ecore.EObject
import testMetamodel.Root
class G2T {
ResourceSet resourceSet
Resource resource;
EPackage myPackage;
Resource myResource
Root myRoot
EObject root
def doEMFSetup(){
resourceSet = new ResourceSetImpl();
resourceSet.resourceFactoryRegistry.extensionToFactoryMap.put("ecore", new EcoreResourceFactoryImpl());
resourceSet.resourceFactoryRegistry.extensionToFactoryMap.put("xmi", new XMIResourceFactoryImpl());
resource = resourceSet.getResource(URI.createFileURI("/Users/user/runtime-EclipseApplication/TestMetamodel/model/testMetamodel.ecore"),true);
}
def generate() {
doEMFSetup
myPackage = resource.contents.get(0) as EPackage;
resourceSet.packageRegistry.put("http://www.example.org/testMetamodel", myPackage);
myResource = resourceSet.getResource( URI.createFileURI("Root.xmi"), true);
println("This is the the root Object of the package:" + myResource.contents.get(0));
root = myResource.contents.get(0);
myRoot = root as Root;
}
def static void main(String[] args) {
new G2T().generate();
}
}
However, after running this as a Java application I get the following output in the console:
This is the the root Object of the package:org.eclipse.emf.ecore.impl.DynamicEObjectImpl@a2431d0 (eClass: org.eclipse.emf.ecore.impl.EClassImpl@b62d79 (name: Root) (instanceClassName: null) (abstract: false, interface: false))
Exception in thread "main" java.lang.ClassCastException: class org.eclipse.emf.ecore.impl.DynamicEObjectImpl cannot be cast to class testMetamodel.Root (org.eclipse.emf.ecore.impl.DynamicEObjectImpl and testMetamodel.Root are in unnamed module of loader 'app')
at graphical2textual.xtend.G2T.generate(G2T.java:57)
at graphical2textual.xtend.G2T.main(G2T.java:63)
Now, my issue here, is that I want to get this Root EObject. Instead of getting
"org.eclipse.emf.ecore.impl.DynamicEObjectImpl@a2431d0 (eClass: org.eclipse.emf.ecore.impl.EClassImpl@b62d79 (name: Root) (instanceClassName: null) (abstract: false, interface: false))" I want to get "Root", and not only as String, but as an EObject so that I can access all the attributes, and References (e.g., I can access the name of the Root in the in the Root.xmi file). I tried to cast it like I have done with the EPackage, but apparently it is not right. Any ideas on how this could be resolved?
I am also providing the files that can be used to reproduce it. As mentioned before, all in the same runtime instance.
Many thanks!
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Re: Access EMF object in Xtend [message #1848304 is a reply to message #1848295] |
Sat, 27 November 2021 06:17   |
Eclipse User |
|
|
|
Hi Ed,
Thanks for that. I now see that the resource URI is different from the eProxyUri. What I had done is that I had created an Ecore Modelling Project where I had the meta model I wanted to load as a resource, and I had copy pasted the ecore file inside the Xtend Project. Then I had loaded that file, while the reference was from the file in the original ecore modelling project. So what I did, was to get the eProxyUri , and put it in this line like this:
resource = resourceSet.getResource( URI.createFileURI(new File("file:/Users/user/runtime-EclipseApplication/TestMetamodel/model/testMetamodel.ecore").getAbsolutePath()), true);
However, when I run it, this is what happens:
Quote:
Caused by: java.io.FileNotFoundException: /Users/user/runtime-EclipseApplication/XtendGeneration/file:/Users/user/runtime-EclipseApplication/TestMetamodel/model/testMetamodel.ecore" (No such file or directory)
So the path of the Xtend file looks to be concatenated with the path that I included, while I only want the path of the file that I added, but everything I seem to do just concatenates everything I write to the path of the Xtend file.
How could I write the path, so this does not happen?
Thanks for all your help!
|
|
|
|
Re: Access EMF object in Xtend [message #1848314 is a reply to message #1848306] |
Sat, 27 November 2021 12:42  |
Eclipse User |
|
|
|
Hi
No. For new File, you can provide a very skimpy specification such as new File("My.ecore") and the rest will be filled from the current working directory and expanded so that after getAbsolutePath() you have something that can be used to create an absolute file URI.
Regards
Ed Willink
|
|
|
Powered by
FUDForum. Page generated in 0.06883 seconds