Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Reference Epackage
Reference Epackage [message #1851072] Sat, 26 March 2022 18:20 Go to next message
John Henbergs is currently offline John HenbergsFriend
Messages: 239
Registered: October 2020
Senior Member
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 #1851073 is a reply to message #1851072] Sat, 26 March 2022 18:22 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
You normally simply add Xtext nature to the project that has the ecore and add the ecore project to the dsls projects classpath and it should work ootb
You can use navigate open model element dialog to check which packages Xtext has indexed


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Reference Epackage [message #1851076 is a reply to message #1851073] Sat, 26 March 2022 20:48 Go to previous messageGo to next message
John Henbergs is currently offline John HenbergsFriend
Messages: 239
Registered: October 2020
Senior Member
Hi Christian,

When you say "to the project that has the Ecore", to what Ecore are you referring? To this one?

import "http://www.eclipse.org/emf/2002/Ecore" as ecore

or the Ecore whose package I want to reference in my dsl?

I tried to add the Ecore project of whose package I want to reference in the class path of the textual dsl, but I still don't get any suggestions when I try to reference it.
Re: Reference Epackage [message #1851079 is a reply to message #1851076] Sun, 27 March 2022 13:57 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
I mean the project containing your
Library.ecore (the one with the epackage you want to reference )
Did you check the navigate open model element dialog


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Reference Epackage [message #1851081 is a reply to message #1851079] Sun, 27 March 2022 14:13 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
index.php/fa/41815/0/

maybe you might also customize scoping similar as it is done for Xtext itself
there seems also a diff when projects use src folders or not.
you may bypass this using

public class MyDslUiModule extends AbstractMyDslUiModule {

	public MyDslUiModule(AbstractUIPlugin plugin) {
		super(plugin);
	}
	
    @Override
    public Provider<IAllContainersState> provideIAllContainersState() {
      return org.eclipse.xtext.ui.shared.Access.getWorkspaceProjectsState();
    }
}


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Sun, 27 March 2022 14:21]

Report message to a moderator

Re: Reference Epackage [message #1851085 is a reply to message #1851081] Sun, 27 March 2022 15:16 Go to previous messageGo to next message
John Henbergs is currently offline John HenbergsFriend
Messages: 239
Registered: October 2020
Senior Member
Many thanks, it worked :)
Re: Reference Epackage [message #1851134 is a reply to message #1851085] Mon, 28 March 2022 14:56 Go to previous messageGo to next message
John Henbergs is currently offline John HenbergsFriend
Messages: 239
Registered: October 2020
Senior Member
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 #1851136 is a reply to message #1851134] Mon, 28 March 2022 15:41 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

You appear to have a comma "," when surely you meant a full-stop "." in Quote:
eProxyURI: file:/Users/user/runtime-XTend_xtends/xtend/xtendinput,mydsl#|0


Regards

Ed Willink
Re: Reference Epackage [message #1851137 is a reply to message #1851136] Mon, 28 March 2022 15:43 Go to previous messageGo to next message
John Henbergs is currently offline John HenbergsFriend
Messages: 239
Registered: October 2020
Senior Member
Hi Ed,

Yes you are right, but it is a full stop in Eclipse in the console. It was my mistake when I copied it, so that is not the cause of the error unfortunately.
Re: Reference Epackage [message #1851140 is a reply to message #1851137] Mon, 28 March 2022 18:38 Go to previous messageGo to next message
John Henbergs is currently offline John HenbergsFriend
Messages: 239
Registered: October 2020
Senior Member
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.

index.php/fa/41824/0/
index.php/fa/41825/0/

I created the following instance of the grammar which is located inside the plugin. .

Hello John! "calendar"


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 #1851144 is a reply to message #1851140] Tue, 29 March 2022 04:25 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
what i dont undestand.
why do you declare the rs twice.
shouldnt you first create it and then configure the registry
and not configure the registry split in two parts.


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Reference Epackage [message #1851145 is a reply to message #1851144] Tue, 29 March 2022 04:55 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
following main works for me
import java.io.IOException;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.ecore.xmi.impl.EcoreResourceFactoryImpl;
import org.eclipse.xtext.ecore.EcoreSupportStandaloneSetup;
import org.xtext.example.mydsl.MyDslStandaloneSetup;
import org.xtext.example.mydsl.myDsl.Model;

import com.google.inject.Injector;

public class Main {
	
	public static void main(String[] args) throws IOException {
		// register ecore to xtext, needs xtext.ecore on classpath
		new EcoreSupportStandaloneSetup();
		// register the dsl
		Injector i = new MyDslStandaloneSetup().createInjectorAndDoEMFRegistration();
		ResourceSet resourceSet = i.getInstance(ResourceSet.class);
		Resource ecoreResource = resourceSet.getResource(URI.createURI("/home/dietrich/eclipse-workspaces/la2222/org.xtext.example.mydsl/model/generated/MyDsl.ecore"), true);
		Resource xtextResource = resourceSet.getResource(URI.createURI("/home/dietrich/eclipse-workspaces/la2222/org.xtext.example.mydsl.tests/test.mydsl"), true);
		ecoreResource.load(null);
		xtextResource.load(null);
		EcoreUtil.resolveAll(resourceSet);
		Model model = (Model)xtextResource.getContents().get(0);
		System.out.println(model.getGreetings().get(0).getType().getNsURI());
	}

}



see also https://www.eclipse.org/forums/index.php/m/1745239/?srch=EcoreSupportStandaloneSetup#msg_1745239


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Tue, 29 March 2022 04:58]

Report message to a moderator

Re: Reference Epackage [message #1851159 is a reply to message #1851145] Tue, 29 March 2022 09:35 Go to previous messageGo to next message
John Henbergs is currently offline John HenbergsFriend
Messages: 239
Registered: October 2020
Senior Member
Hi Christian,

Many thanks for your answer. I was wondering how you don't get any issues with this line
	ResourceSet resourceSet = i.getInstance(ResourceSet.class);

Because for me it says:

Quote:

Type mismatch: Cannot convert from Class to ResourceSet


I tried to leave it
 resourceSet = i.getInstance(ResourceSet)

but of course now it doesn't work.
This is my implementation.

package xtendunit

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
import org.eclipse.xtext.ecore.EcoreSupportStandaloneSetup

class xtend {
	
	
	ResourceSet resourceSet 
	Injector i
	Resource ecoreResource	
	Resource xtextResource	
	Model model
	
	def doEMFSetup(){
	// register ecore to xtext, needs xtext.ecore on classpath
		new EcoreSupportStandaloneSetup();
		// register the dsl
		i = new JUnitStandaloneSetup().createInjectorAndDoEMFRegistration();
		resourceSet = i.getInstance(ResourceSet)
		ecoreResource = resourceSet.getResource(URI.createFileURI(new File ("/Users/user/eclipse-workspace/org.xtext.example.junit/model/generated/JUnit.ecore").absolutePath), true)
		xtextResource = resourceSet.getResource( URI.createFileURI(new File("textual.junit").getAbsolutePath()), true);
		ecoreResource.load(null);
		xtextResource.load(null);
		EcoreUtil.resolveAll(resourceSet);
	    println(ecoreResource)
	    println(xtextResource)
		
	}	
	def getRoot() {
	doEMFSetup	
		model = (xtextResource.getContents().get(0)) as Model;
		System.out.println(model.getGreetings().get(0).name);
		System.out.println(model.getGreetings().get(0).pako);
		System.out.println(model.getGreetings().get(0).pako.nsURI);		
		
}
	def static void main(String[] args) {
		
			new xtend().getRoot();
	}
}


And this is what I get:

org.eclipse.emf.ecore.xmi.impl.EcoreResourceFactoryImpl$1@126be319 uri='file:/Users/user/eclipse-workspace/org.xtext.example.junit/model/generated/JUnit.ecore'
org.eclipse.xtext.linking.lazy.LazyLinkingResource@6c44052e uri='file:/Users/user/runtime-Transf_Generator_HOTs/Xtendunit/textual.junit'
John
org.eclipse.emf.ecore.impl.EPackageImpl@5c371e13 (eProxyURI: file:/Users/user/runtime-Transf_Generator_HOTs/Xtendunit/textual.junit#|0)
null

Re: Reference Epackage [message #1851161 is a reply to message #1851159] Tue, 29 March 2022 10:55 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33113
Registered: July 2009
Senior Member
Christian is showing Java syntax while you're using Xtend syntax. In Xtend, the reference to the class name in an expression context is implicitly like <ClassName>.class in Java, so if you add .class to that, you end up with <ClassName>.class.getClass() which is definitely not what you want because that's the same as Class.class...

Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Reference Epackage [message #1851164 is a reply to message #1851161] Tue, 29 March 2022 11:25 Go to previous messageGo to next message
John Henbergs is currently offline John HenbergsFriend
Messages: 239
Registered: October 2020
Senior Member
Hi Ed,

Yes thank you!

I tried removing .class and leave it:

resourceSet = i.getInstance(ResourceSet)


But I get the result that I have shown in the prior message.
Re: Reference Epackage [message #1851165 is a reply to message #1851164] Tue, 29 March 2022 11:31 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
you can simply use rightclick paste java code

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Reference Epackage [message #1851166 is a reply to message #1851165] Tue, 29 March 2022 11:33 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
can you please provide a git repo that helps to reproduce?

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Reference Epackage [message #1851171 is a reply to message #1851166] Tue, 29 March 2022 12:08 Go to previous messageGo to next message
John Henbergs is currently offline John HenbergsFriend
Messages: 239
Registered: October 2020
Senior Member
Hey Christian,

I am including the files as zip archives here as I cannot access Git for now.

DSL is the textual grammar dsl and is in the workspace.
EcoreToBeReferenced contains the Ecore metamodel whose package I want to reference.
Xtendunit contains the Xtend file and the instance of the dsl "textual.junit".

Re: Reference Epackage [message #1851177 is a reply to message #1851171] Tue, 29 March 2022 13:28 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
your model file looks bad

Hello John! "jUnit" looks better.
alternatively you have to use the other ecore in test
ecoreResource = resourceSet.getResource(URI.createFileURI(new File ("/Users/cdietrich/eclipse-workspaces/runtime-New_configuration/ToReference/model/toReference.ecore").absolutePath), true)
// TODO adjust path to your machine


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Reference Epackage [message #1851197 is a reply to message #1851177] Wed, 30 March 2022 11:30 Go to previous message
John Henbergs is currently offline John HenbergsFriend
Messages: 239
Registered: October 2020
Senior Member
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
Previous Topic:Xtext & Build Loops
Next Topic:AbstractFormatter2
Goto Forum:
  


Current Time: Fri Mar 29 00:26:14 GMT 2024

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

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

Back to the top