Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Reference ecore model elements from Xtext based DSLs
Reference ecore model elements from Xtext based DSLs [message #925315] Thu, 27 September 2012 15:53 Go to next message
Reiner Jung is currently offline Reiner JungFriend
Messages: 10
Registered: June 2010
Location: Uni Kiel
Junior Member
Hello everyone,

there have already been serveral posts to that topic. However, they seem not to work. I've checked:
* Joern's topic: http://www.eclipse.org/forums/index.php/m/551084/?srch=referencing+EMF+model+elements#msg_551084
* Andreas' topic: http://www.eclipse.org/forums/index.php?t=msg&th=170756&start=0&S=81127b7c17990f3eddb47e01037a6405
* Christian's blog: christiandietrich.wordpress.com /2011/07/17/xtext-2-0-and-uml/

with litte success. The primary idea is to be able to access ecore model elements in a DSL. My first idea was to implement that similar to Xtext itself with:
Model:
	'package' name = QualifiedName
	(metaModels += LoadMetaModel)*
	...
;

LoadMetaModel :
	'load' name=ID ePackage=[ecore::EPackage|STRING] 
;

Probe:
	'probe' name=ID ':' type=[ecore::EClassifier|QualifiedName] ... ;


This did not work. When running an editor for the above DSL, and enter
package demo

load myModel "platform:/resource/MyProject/MyModel.ecore"


the editor complains about the URI (Couldn't resolve reference to EPackage). This can be reproduced with every selfmade or foreign ecore-model. An analysis of the Xtext code did not help. So I googled and came up with different solutions. First, when using
LoadMetaModel:
	'load' name=ID importURI = STRING
;

The editor was able to find the model. However, I was still not able to use it in any other expression, like the Probe-rule above. I also tried the example of Christian replacing the uml specific part with an ecore specific part (which is available in org.eclipse.xtext.ecore and org.eclipse.xtext.ui.ecore, as his post implies). Still, it does not work.

My primary goal is to have a language, where I can import variuous EPackages (just like in Xtext), assign them to an alias and use the declarations in these EPackages in normal identifiers.

So if anyone has any suggestions, what I should check or fix or use, please let me know.

Thanks
Reiner

Re: Reference ecore model elements from Xtext based DSLs [message #925426 is a reply to message #925315] Thu, 27 September 2012 17:57 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
Hi,

this works out of the box

Here a very simple example (using namespace uri imports)
grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals

generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"

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


Model:
elements+=Element*;
Element:
(g+=Import)+ f=Foreach
;
Import:
'import' package=[ecore::EPackage|STRING] 'as'name=ID
;
Foreach:
'Foreach' name=ID 'in' table=Table
;
Table:
ecoreTypes=[Import]'.'class=[ecore::EClass|ID]
;



public class MyDslScopeProvider extends AbstractDeclarativeScopeProvider {
	
	
	public IScope scope_Import_package(Import context, EReference reference) {
		IScope result = new FilteringScope(delegateGetScope(context, reference), new Predicate<IEObjectDescription>() {
			
			@Override
			public boolean apply(IEObjectDescription input) {
				String isNSURI = input.getUserData("nsURI");
				return "true".equals(isNSURI);
			}
		});
		return result;
	}
	
	IScope scope_Table_class(Table t, EReference reference) {
		return Scopes.scopeFor(EcoreUtil2.getAllContentsOfType(t.getEcoreTypes().getPackage(), EClass.class));
	}

}


public class MyDslRuntimeModule extends org.xtext.example.mydsl.AbstractMyDslRuntimeModule {
	
	public Class<? extends IQualifiedNameConverter> bindIQualifiedNameConverter() {
		return XtextQualifiedNameConverter.class;
	}

}


~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Reference ecore model elements from Xtext based DSLs [message #926076 is a reply to message #925426] Fri, 28 September 2012 09:27 Go to previous message
Reiner Jung is currently offline Reiner JungFriend
Messages: 10
Registered: June 2010
Location: Uni Kiel
Junior Member
Hello Christian,

thanks for your code snippets. At first, it still didn't work for some strange reason. Right now I have a working example and as soon as it work in my real projects, I will blog about it.

Best wishes
Reiner
Previous Topic:Unable to generate from Arithmetics ex
Next Topic:Multipage Editor with Xtext
Goto Forum:
  


Current Time: Fri Mar 29 08:17:30 GMT 2024

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

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

Back to the top