Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Problems when referencing Ecore elements using an imported Ecore model
Problems when referencing Ecore elements using an imported Ecore model [message #1754821] Wed, 22 February 2017 18:47 Go to next message
Lorenzo Bettini is currently offline Lorenzo BettiniFriend
Messages: 1737
Registered: July 2009
Location: Firenze, Italy
Senior Member
Hi

in a new DSL I started to work on, I refer to Ecore elements in the DSL, something like

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

generate myDsl "...my uri..."
...
Program:
...
	('metamodel' metamodels+=[ecore::EPackage|STRING])*
...
;


So far so good, everything works. Now I'd like to switch to a manually maintained ecore model. I performed the steps to switch to an imported ecore model (starting from the one that Xtext had previously generated).

As soon as I switch in the grammar:

...
import "...my uri..."
...


The grammar editor starts complaining with a warning for the import:

"The imported package refers to elements in the package registry
instead of using the instances from the workspace"

and for the assignment metamodels+=[ecore::EPackage|STRING]) I get an error:

"Cannot find compatible feature metamodels in sealed EClass
Program from imported package myuri:
The type 'EPackage' [org.eclipse.emf.ecore.EPackage] used in
the reference 'metamodels' is inconsistent. Probably this is
due to an unsupported kind of metamodel hierarchy."

However, the workflow mwe2 still runs (of course, I updated it so that it references my genmodel).

The warning (not the error) proposes a quickfix to "update the imported package". The quickfix transforms in my ecore the reference:

<eStructuralFeatures xsi:type="ecore:EReference" name="metamodels" unique="false"
        upperBound="-1" eType="ecore:EClass http://www.eclipse.org/emf/2002/Ecore#//EPackage"/>


into

<eStructuralFeatures xsi:type="ecore:EReference" name="metamodels" unique="false"
        upperBound="-1" eType="ecore:EClass ../../../org.eclipse.emf.ecore/model/Ecore.ecore#//EPackage"/>


Now the editor is happy (no more errors nor warnings). However, now it is the mwe2 workflow that complains with the same error I used to have in the editor:

"Cannot find compatible feature metamodels in sealed EClass EdeltaProgram from imported package ...myuri...: The type 'EPackage' used in the reference 'metamodels' is inconsistent. Probably this is due to an unsupported kind of metamodel hierarchy. (ErrorCode: CannotCreateTypeInSealedMetamodel)"

is there anything I could do to make "everyone" happy?

Could this be related https://github.com/eclipse/xtext-core/issues/41?

thanks in advance
Lorenzo
Re: Problems when referencing Ecore elements using an imported Ecore model [message #1754822 is a reply to message #1754821] Wed, 22 February 2017 18:50 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 11361
Registered: July 2009
Senior Member
yes this is what i took from that issue => you have to use the old style workflow

Need professional support for Xtext, Xpand, EMF?
Go to: http://xtext.itemis.com
Twitter : @chrdietrich
Blog : christiandietrich.wordpress.com
Re: Problems when referencing Ecore elements using an imported Ecore model [message #1754823 is a reply to message #1754822] Wed, 22 February 2017 19:04 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 11361
Registered: July 2009
Senior Member
interestingly this xcore setup seems to work (i did not test if what comes out acutally runs

@GenModel(nsUri="http://www.example.com//zzz")
package zzz

import org.eclipse.emf.ecore.EPackage

class Model {
	refers EPackage[] metamodels
}


grammar org.xtext.example.mydsl8.MyDsl with org.eclipse.xtext.common.Terminals

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

Model returns Model:
	{Model}
	'Model'
	'{'
		('metamodels' '(' metamodels+=[ecore::EPackage|STRING] ( "," metamodels+=[ecore::EPackage|STRING])* ')' )?
	'}';


		language = StandardLanguage {
			name = "org.xtext.example.mydsl8.MyDsl"
			fileExtensions = "mydsl8"
			referencedResource = "platform:/resource/org.eclipse.emf.ecore/model/Ecore.genmodel"
			referencedResource = "platform:/resource/zzz.xcore/model/zzz.xcore"
			


Need professional support for Xtext, Xpand, EMF?
Go to: http://xtext.itemis.com
Twitter : @chrdietrich
Blog : christiandietrich.wordpress.com
Re: Problems when referencing Ecore elements using an imported Ecore model [message #1754825 is a reply to message #1754822] Wed, 22 February 2017 19:10 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 5467
Registered: July 2009
Senior Member
Hi

I had the same problems for the OCL and QVTd grammars.

Unless you really want to have Ecore as an open project, import it by its nsURI and NEVER reference relatively.
Conversely metamodels in your own projects refer to by platform:/resource... and ALWAYS reference relatively.

grammar org.eclipse.ocl.xtext.base.Base hidden(WS, ML_COMMENT, SL_COMMENT) //with org.eclipse.xtext.common.Terminals
import "http://www.eclipse.org/emf/2002/Ecore" as ecore
import "platform:/resource/org.eclipse.ocl.pivot/model/Pivot.ecore" as pivot
import "platform:/resource/org.eclipse.ocl.xtext.base/model/BaseCS.ecore"

This isn't really what Xtext likes, but it is essential if the references in your models are to be compatible with other modelling tools. Xtext provides a project-specific preference that enables you to ignore the "package not imported through URI" warning.

Regards

Ed Willink
Re: Problems when referencing Ecore elements using an imported Ecore model [message #1754854 is a reply to message #1754825] Thu, 23 February 2017 09:13 Go to previous messageGo to next message
Lorenzo Bettini is currently offline Lorenzo BettiniFriend
Messages: 1737
Registered: July 2009
Location: Firenze, Italy
Senior Member
Ed, I'm not sure I understand what you're suggesting to do:

I modified my grammar as follows (I restored the original ecore, thus the ecore references are of the shape eType="ecore:EClass http://www.eclipse.org/emf/2002/Ecore#//EPackage"):

import "http://www.eclipse.org/emf/2002/Ecore" as ecore
import "platform:/resource/org.eclipse.xtext.xbase/model/Xbase.ecore" as xbase
import "platform:/resource/org.eclipse.xtext.xbase/model/Xtype.ecore" as xtype
import "platform:/resource/org.eclipse.xtext.common.types/model/JavaVMTypes.ecore" as types

//generate mydsl "myuri"
import "platform:/resource/myproject/model/custom/MyDsl.ecore"


But the Xtext editor still complains for the assignments for the assignment metamodels+=[ecore::EPackage|STRING])
Re: Problems when referencing Ecore elements using an imported Ecore model [message #1754855 is a reply to message #1754854] Thu, 23 February 2017 09:17 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 11361
Registered: July 2009
Senior Member
i assume you should use "platform:/resource/org.eclipse.emf.ecore/model/Ecore.ecore"

Need professional support for Xtext, Xpand, EMF?
Go to: http://xtext.itemis.com
Twitter : @chrdietrich
Blog : christiandietrich.wordpress.com
Re: Problems when referencing Ecore elements using an imported Ecore model [message #1754860 is a reply to message #1754855] Thu, 23 February 2017 09:48 Go to previous messageGo to next message
Lorenzo Bettini is currently offline Lorenzo BettiniFriend
Messages: 1737
Registered: July 2009
Location: Firenze, Italy
Senior Member
Christian Dietrich wrote on Thu, 23 February 2017 09:17
i assume you should use "platform:/resource/org.eclipse.emf.ecore/model/Ecore.ecore"


where? in the grammar or in the ecore?

That's not what Ed was suggesting though...
Re: Problems when referencing Ecore elements using an imported Ecore model [message #1754862 is a reply to message #1754860] Thu, 23 February 2017 10:01 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 11361
Registered: July 2009
Senior Member
in the grammar.

Need professional support for Xtext, Xpand, EMF?
Go to: http://xtext.itemis.com
Twitter : @chrdietrich
Blog : christiandietrich.wordpress.com
Re: Problems when referencing Ecore elements using an imported Ecore model [message #1754867 is a reply to message #1754862] Thu, 23 February 2017 10:53 Go to previous messageGo to next message
Lorenzo Bettini is currently offline Lorenzo BettiniFriend
Messages: 1737
Registered: July 2009
Location: Firenze, Italy
Senior Member
OK, I added that in the grammar:

import "platform:/resource/org.eclipse.emf.ecore/model/Ecore.ecore" as ecore
import "platform:/resource/org.eclipse.xtext.xbase/model/Xbase.ecore" as xbase
import "platform:/resource/org.eclipse.xtext.xbase/model/Xtype.ecore" as xtype
import "platform:/resource/org.eclipse.xtext.common.types/model/JavaVMTypes.ecore" as types


The Xtext editor still complains with errors and now also mwe2 fails with the same problems, so both now fail consistently.

If I now modify the ecore manually using platform uris for ecore references, the editor is happy but now the mwe2 fails with a different error:

java.lang.IllegalStateException: Validation Error in edelta.Edelta: The rule 'STRING' is not valid for a cross reference since it does not return an EString. You'll have to wrap it in a data type rule.
at org.eclipse.xtext.xtext.generator.XtextGeneratorLanguage$1.add(XtextGeneratorLanguage.java:300)

If I redefine the terminal rule STRING in my grammar

@Override 
terminal STRING: 
			'"' ( '\\' . /* ('b'|'t'|'n'|'f'|'r'|'u'|'"'|"'"|'\\') */ | !('\\'|'"') )* '"'? |
			"'" ( '\\' . /* ('b'|'t'|'n'|'f'|'r'|'u'|'"'|"'"|'\\') */ | !('\\'|"'") )* "'"?;


Then mwe2 throws another exception:

java.lang.RuntimeException: Problems running workflow edelta.GenerateEdelta: Problem parsing 'file:/home/bettini/work/xtext/edelta/edelta.parent/edelta/../edelta/src/edelta/Edelta.xtext':
TransformationDiagnostic: null:92 Cannot inherit from terminal rule and return another type. (ErrorCode: InvalidSupertype)

I guess there's no easy way to deceive the EMF registry...
Re: Problems when referencing Ecore elements using an imported Ecore model [message #1754873 is a reply to message #1754867] Thu, 23 February 2017 12:06 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 11361
Registered: July 2009
Senior Member
terminal STRING returns ecore::EString does not work?

Need professional support for Xtext, Xpand, EMF?
Go to: http://xtext.itemis.com
Twitter : @chrdietrich
Blog : christiandietrich.wordpress.com
Re: Problems when referencing Ecore elements using an imported Ecore model [message #1754874 is a reply to message #1754873] Thu, 23 February 2017 12:08 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 11361
Registered: July 2009
Senior Member
and it again looks like the ecore from terminals is not the same as ecore from your xtext
=> maybe omit terminals and copy all the rules solves this

unfortunately i dont have enhough insight to work on this ugly bug and i dont get any support


Need professional support for Xtext, Xpand, EMF?
Go to: http://xtext.itemis.com
Twitter : @chrdietrich
Blog : christiandietrich.wordpress.com
Re: Problems when referencing Ecore elements using an imported Ecore model [message #1754875 is a reply to message #1754874] Thu, 23 February 2017 12:19 Go to previous messageGo to next message
Lorenzo Bettini is currently offline Lorenzo BettiniFriend
Messages: 1737
Registered: July 2009
Location: Firenze, Italy
Senior Member
By omitting terminals you mean not inheriting from Terminals grammar? If that is the case, I can't do that, since I inherit from Xbase...

This does not work

terminal STRING returns ecore::EString


because Xtext still detects that you're trying to override the rule, see the error in my previous post, (with a different type since the two are incomparable because of the two instances of ecore)...
Re: Problems when referencing Ecore elements using an imported Ecore model [message #1754876 is a reply to message #1754875] Thu, 23 February 2017 12:28 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 11361
Registered: July 2009
Senior Member
hmmm.

did you try to use xcore instead of ecore?


Need professional support for Xtext, Xpand, EMF?
Go to: http://xtext.itemis.com
Twitter : @chrdietrich
Blog : christiandietrich.wordpress.com
Re: Problems when referencing Ecore elements using an imported Ecore model [message #1754878 is a reply to message #1754876] Thu, 23 February 2017 12:47 Go to previous messageGo to next message
Lorenzo Bettini is currently offline Lorenzo BettiniFriend
Messages: 1737
Registered: July 2009
Location: Firenze, Italy
Senior Member
Using xcore usually drags in many more problems... :/ I'll also try to use xcore later.

I was thinking of playing with resource set initialization but XtextGeneratorLanguage does not expose resourceSetInitializer, so I can't do something like

language = StandardLanguage {
  ...
  resourceSetInitializer = MyResourceSetInitializer


how should I use injection in this context (mwe2) to inject my resource set initializer?
Re: Problems when referencing Ecore elements using an imported Ecore model [message #1754880 is a reply to message #1754878] Thu, 23 February 2017 12:58 Go to previous messageGo to next message
Lorenzo Bettini is currently offline Lorenzo BettiniFriend
Messages: 1737
Registered: July 2009
Location: Firenze, Italy
Senior Member
OK, I can answer myself:

class CustomGeneratorModule extends DefaultGeneratorModule {
	def Class<? extends XtextGeneratorResourceSetInitializer> bindXtextGeneratorResourceSetInitializer() {
		CustomXtextGeneratorResourceSetInitializer
	}
}


Workflow {
	
	component = XtextGenerator {
		configuration = CustomGeneratorModule {
			project = StandardProjectConfig {...


and (note the commented line):

@Log
class CustomXtextGeneratorResourceSetInitializer extends XtextGeneratorResourceSetInitializer {
	
	override initialize(ResourceSet resourceSet, List<String> referencedResources) {
		val delegate = new StandaloneSetup
		delegate.resourceSet = resourceSet
//		resourceSet.packageRegistry.put(EcorePackage.eNS_URI, EcorePackage.eINSTANCE)
		referencedResources.forEach[
			loadResource(resourceSet)
		]
		registerGenModels(resourceSet)
		registerEPackages(resourceSet)
	}
... everything else copied and pasted from the superclass since it's all private


This makes mwe2 finally work... (and tests are still green)
I don't know how this could be generalized for the bug...

I'll also try to revert the imports in the grammar with platform uris and see what happens
Re: Problems when referencing Ecore elements using an imported Ecore model [message #1754883 is a reply to message #1754880] Thu, 23 February 2017 13:50 Go to previous messageGo to next message
Lorenzo Bettini is currently offline Lorenzo BettiniFriend
Messages: 1737
Registered: July 2009
Location: Firenze, Italy
Senior Member
Moreover, in the grammar, you can simply use URIs for imports (as long as in the ecore you refer to ecore stuff with platform uris).
Re: Problems when referencing Ecore elements using an imported Ecore model [message #1754892 is a reply to message #1754855] Thu, 23 February 2017 16:02 Go to previous message
Ed Willink is currently offline Ed WillinkFriend
Messages: 5467
Registered: July 2009
Senior Member
Christian Dietrich wrote on Thu, 23 February 2017 04:17
i assume you should use "platform:/resource/org.eclipse.emf.ecore/model/Ecore.ecore"


If Xtext tooling is the limit of your modeling aspirations, platform:/resource/org.eclipse.emf.ecore/model/Ecore.ecore may be convenient since you benefit from an Xtext-ism (? the Xtext index) whereby Xtext tooling doesn't actually use the Ecore.ecore that you say should be used.

However if you want to use your *.xtext model in another regular tool, in my case in an Xtext2LPG M2M, you need to use a correct reference; the nsURI.

I have no idea why the nsURI reference does not work for you, but without seeing all your tooling, who knows what magic you may have accidentally done.

Regards

Ed Willink
Previous Topic:Producing multiple objects from one rule
Next Topic:[SOLVED] Bring rule call and cross-reference under one feature
Goto Forum:
  


Current Time: Mon Nov 20 07:49:45 GMT 2017

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

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