Programmatically access elements imported from another Ecore model [message #1271880] |
Sun, 16 March 2014 09:29  |
Eclipse User |
|
|
|
Hi all,
I have a simple Ecore Meta-Model which models a library that contains books (it's just a simple example). Both, library and books are characterized by a name.
I want to write a DSL that allows me to do something like this:
SelectedBook BookA
SelectedBook BookB
Where BookA and BookB are books defined in an instance of the ecore meta-model.
I followed this tutorial and I was able to implement the aforementioned DSL. Hence I can import books name for an Ecore model.
Now I need to write a model-to-text transformation that transform the DSL in java code. I'm using Epsilon.
I'm not sure if it is an Epsilon problem or not.
However when I try to access the books, the content of their fields (e.g. the string name) is null.
Do you have any idea on how it is possible to fix the problem?
Please let me know if you need further information.
|
|
|
|
|
|
|
|
|
|
Re: Programmatically access elements imported from another Ecore model [message #1272007 is a reply to message #1271970] |
Sun, 16 March 2014 17:44   |
Eclipse User |
|
|
|
Thanks again Christian.
The last question:
let's suppose that LibraryModel is importing elements from another Ecore model: AuthorsModel.
What should I do for accessing the data in author model?
I tried the same procedure but something got wrong.
See the code below.
The last line is printing null.
Thanks,
Luca
P.S.:
The DSL and the MWE2 includes the author meta model.
However the DSL is not directly importing elements of the Author Model. That's because I don't need it.
I tried to import them to see if that was the problem, but it didn't work.
// Register the XMI resource factory for the .website extension
Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE;
Map<String, Object> m = reg.getExtensionToFactoryMap();
m.put("librarydsl", new XMIResourceFactoryImpl());
m.put("librarymodel", new XMIResourceFactoryImpl());
m.put("authormodel", new XMIResourceFactoryImpl());
ResourceSet resSet = new ResourceSetImpl();
LibraryPackage.eINSTANCE.eClass();
LibraryModelSupport libraryModelSupport = new LibraryModelSupport();
libraryModelSupport.registerServices(true);
Resource libraryModelResource = resSet.getResource(URI.createURI(PATH_TO_LIBRARY_MODEL), true);
LibraryModel libraryModel = (LibraryModel) libraryModelResource.getContents().get(0);
System.out.println(libraryModel.getName()); // this works
AuthorPackage.eINSTANCE.eClass();
AuthorModelSupport authorModelSupport = new AuthorModelSupport();
authorModelSupport.registerServices(true);
Resource authorModelResource = resSet.getResource(URI.createURI(PATH_TO_AUTHOR_MODEL), true);
AuthorModel authorModel = (AuthorModel) authorModelResource.getContents().get(0);
System.out.println(authorModel.getName()); // this works
LibraryDSLPackage.eINSTANCE.eClass();
new LibraryDSLStandaloneSetup().createInjectorAndDoEMFRegistration();
Resource libraryDSLResource = resSet.getResource(URI.createURI(PATH_TO_DSL_MODEL), true);
LibraryDSL libraryDSL = (LibraryDSL) libraryDSLResource.getContents().get(0);
System.out.println(libraryDSL.getSelectedBooks.get(0).getName()); // now this prints the correct name
System.out.println(libraryDSL.getSelectedBooks.get(0).getAuthor().Name()); // this print null
[Updated on: Sun, 16 March 2014 18:18] by Moderator
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.65963 seconds