Reference Epackage [message #1851072] |
Sat, 26 March 2022 14:20  |
Eclipse User |
|
|
|
Hi all,
I have an Ecore metamodel and I am trying to make a textual syntax for it. One of the classes of my metamodel is generated in Xtext as follows:
House returns House:
{House}
'House'
name=STRING
'{'
(type=[ecore::EPackage|STRING])
'}'
;
where "ecore" is imported at the beginning of the xtext file.
When I use the tree editor, I load resource and then for the type of the house I can choose from loaded resources. How can I do the same in Xtext? To load the resource (EPackage) and then reference it in the type of the house?
Thank you!
|
|
|
|
|
|
|
|
Re: Reference Epackage [message #1851134 is a reply to message #1851085] |
Mon, 28 March 2022 10:56   |
Eclipse User |
|
|
|
Hi Christian,
Unfortunately I am now having issues with eProxyURIs.
I added Library.ecore to the class path of the project where I have the concrete syntax of my textual DSL.
I also loaded the dsl in Xtend, as you suggested in my other post.
My Xtend code now looks like this.
resourceSet = new ResourceSetImpl();
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(Resource.Factory.Registry.DEFAULT_EXTENSION, new XMIResourceFactoryImpl());
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(Resource.Factory.Registry.DEFAULT_EXTENSION, new EcoreResourceFactoryImpl());
injector = new MyDslStandaloneSetup().createInjectorAndDoEMFRegistration;
resourceSet = injector.getInstance(ResourceSet)
resourceSet.getResource(URI.createFileURI(new File ("/Users/user/eclipse-workspace/Library/model/library.ecore").absolutePath), true)
resource = resourceSet.getResource( URI.createFileURI(new File("xtendinput.mydsl").getAbsolutePath()), true);
root = resource.contents.get(0);
modelRoot = root as ModelRoot;
println(modelRoot.package)
In the println I am expecting to get the EPackage of Library because that is what I have defined in the package attribute in the ModelRoot class of xtnedinput.mydsl.
However, I am getting the following
Quote:
org.eclipse.emf.ecore.impl.EPackageImpl@1e8823d2 (eProxyURI: file:/Users/user/runtime-XTend_xtends/xtend/xtendinput,mydsl#|0)
I also tried EcoreUtil.resolveAll(resourceSet), but no luck.
When I check the resourceSet I have
Quote:
org.eclipse.emf.ecore.xmi.impl.EcoreResourceFactoryImpl$1@1255b1d1 uri='file:/Users/user/eclipse-workspace/Library/model/library.ecore'
|
|
|
|
|
Re: Reference Epackage [message #1851140 is a reply to message #1851137] |
Mon, 28 March 2022 14:38   |
Eclipse User |
|
|
|
I created another project to test it as follows:
In the eclipse workspace I created the following grammar:
grammar org.xtext.example.junit.JUnit with org.eclipse.xtext.common.Terminals
generate jUnit "http://www.xtext.org/example/junit/JUnit"
import "http://www.eclipse.org/emf/2002/Ecore" as ecore
Model:
greetings+=Greeting*;
Greeting:
'Hello' name=ID '!'
pako = [ecore::EPackage | STRING]
;
Then I launched the runtime and I created a plugin project, a package, and inside an Xtend class. This is how the Xtend file looks like:
package xtendunit
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl
import org.eclipse.emf.ecore.xmi.impl.EcoreResourceFactoryImpl
import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.emf.ecore.resource.ResourceSet
import org.eclipse.emf.common.util.URI
import java.io.File
import com.google.inject.Injector
import org.xtext.example.junit.JUnitStandaloneSetup
import org.eclipse.emf.ecore.EObject
import org.xtext.example.junit.jUnit.Model
import org.eclipse.emf.ecore.util.EcoreUtil
import org.eclipse.emf.ecore.EPackage
class xtend {
ResourceSet resourceSet
Injector injector
Resource resource
EObject root
EPackage p
Model m
def doEMFSetup(){
resourceSet = new ResourceSetImpl();
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(Resource.Factory.Registry.DEFAULT_EXTENSION, new XMIResourceFactoryImpl());
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(Resource.Factory.Registry.DEFAULT_EXTENSION, new EcoreResourceFactoryImpl());
injector = new JUnitStandaloneSetup().createInjectorAndDoEMFRegistration;
resourceSet = injector.getInstance(ResourceSet)
resourceSet.getResource(URI.createFileURI(new File ("/Users/user/eclipse-workspace/Calendar/model/calendar.ecore").absolutePath), true)
resourceSet.getResource(URI.createFileURI(new File ("/Users/user/eclipse-workspace/org.xtext.example.junit/model/generated/JUnit.ecore").absolutePath), true)
resource = resourceSet.getResource( URI.createFileURI(new File("textual.junit").getAbsolutePath()), true);
println(resourceSet)
println(resource)
}
def getRoot() {
doEMFSetup
root = resource.contents.get(0);
m = root as Model;
EcoreUtil.resolveAll(resourceSet)
for (greetings : m.greetings){
println (greetings.name)
println (greetings.pako.name)
}
}
def static void main(String[] args) {
new xtend().getRoot();
}
}
In the workspace I have another Ecore metamodel Calendar, which I also imported in the runtime and I added it to the class path of the Xtend project by using class folder.


I created the following instance of the grammar which is located inside the plugin. .
When I run Xtend I get the following:
org.eclipse.xtext.resource.SynchronizedXtextResourceSet@47747fb9 resources=[org.eclipse.emf.ecore.xmi.impl.EcoreResourceFactoryImpl$1@3113a37 uri='file:/Users/user/eclipse-workspace/Calendar/model/calendar.ecore', org.eclipse.emf.ecore.xmi.impl.EcoreResourceFactoryImpl$1@213e3629 uri='file:/Users/user/eclipse-workspace/org.xtext.example.junit/model/generated/JUnit.ecore', org.eclipse.xtext.linking.lazy.LazyLinkingResource@4e9658b5 uri='file:/Users/user/runtime-test/Xtendunit/textual.junit']
org.eclipse.xtext.linking.lazy.LazyLinkingResource@4e9658b5 uri='file:/Users/user/runtime-test/Xtendunit/textual.junit'
John
null
|
|
|
|
|
|
|
|
|
|
|
|
Re: Reference Epackage [message #1851197 is a reply to message #1851177] |
Wed, 30 March 2022 07:30  |
Eclipse User |
|
|
|
Many thanks for all your help. That was the issue, that I was trying to reference the package from "toReference", but I was actually loading the resource of junit.
Best,
John
|
|
|
Powered by
FUDForum. Page generated in 0.07293 seconds