Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » use the importURI mechanism to incude an ecore model
use the importURI mechanism to incude an ecore model [message #875280] Tue, 22 May 2012 13:13 Go to next message
asma geg is currently offline asma gegFriend
Messages: 5
Registered: April 2012
Junior Member
Hi all!
I was wondering whether it is possible to use the importURI mechanism to include an ecore model?
And if it is possible, what do I need to customize in order to export all elements contained in the ecore resource?
To explain more, this is an example of the editor I want to have:

import "A.ecore" as ecore
Foreach i in ecore.Class1ofA
[...]

And I would like to have by autocompletion all EClasses of the model I imported!

The grammer I have is:
Model:
elements+=Element*;
Element:
(g+=Import)+ f=Foreach
;
Import:
'import' importURI=STRING 'as'name=ID
;
Foreach:
'Foreach' name=ID 'in' table=Table
;
Table:
ecoreTypes=[Import]'.'class=???
;

Do you have any idea ?

Thanks a lot!
Re: use the importURI mechanism to incude an ecore model [message #875475 is a reply to message #875280] Tue, 22 May 2012 20:18 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

yes this is possible. Xtext itself does such a thing (for .xtext files) but they do not use import uris.

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: use the importURI mechanism to incude an ecore model [message #875922 is a reply to message #875280] Wed, 23 May 2012 15:02 Go to previous messageGo to next message
asma geg is currently offline asma gegFriend
Messages: 5
Registered: April 2012
Junior Member
Thank you a lot, it is exactly what I wanted to have.
I have just an other question : If I want to change grammer as shown here : (using only one imported Package and deleting name=ID from the Foreach rule)


Model:
im=Import
(elements+=Foreach)+
;
Import:
'import' package=[ecore::EPackage|STRING]  //changed
;
Foreach:
'Foreach' name=ID 'in' table=Table
 (variable+=Variable)+
'endForeach'
;
Table :
class=[ecore::EClass|ID]  //changed
;
Variable :
	'$'foreach=[Foreach]'.'attribut=[ecore::EAttribute|ID]
;


How can I assure the link between the rule "Table" and the imported package?

I changed the method scope_Table_class in order to accede to the rule Import but it doesn't work.
IScope scope_Table_class(Table t, EReference reference) {
		return Scopes.scopeFor(EcoreUtil2.getAllContentsOfType(((Import) EcoreUtil2.getAllContentsOfType(t.eContainingFeature().eContainmentFeature(),Import.class)).getPackage(), EClass.class));	
	}

Can you tell me what is wrong and how to correct it?

thanks
Re: use the importURI mechanism to incude an ecore model [message #876014 is a reply to message #875922] Wed, 23 May 2012 18:40 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

id do this like this (to get content assist working)

I
Scope scope_Table_class(Model context, EReference reference) {
		return Scopes.scopeFor(EcoreUtil2.getAllContentsOfType(context.getIm().getPackage(), EClass.class));
	}


or (if that would not count)

	IScope scope_Table_class(Table t, EReference reference) {
		Model m = EcoreUtil2.getContainerOfType(t, Model.class);
		return Scopes.scopeFor(EcoreUtil2.getAllContentsOfType(m.getIm().getPackage(), EClass.class));
	}


~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: use the importURI mechanism to incude an ecore model [message #882438 is a reply to message #876014] Wed, 06 June 2012 13:12 Go to previous messageGo to next message
asma geg is currently offline asma gegFriend
Messages: 5
Registered: April 2012
Junior Member
Hi,
I developped the grammer and the scope class. Now, I have another problem : it seems that there are ambiguities between rules (rule Fonction and rule Variable) while using autocompletion, and I don't know how to resolve that.
This is my grammar : (In the Foreach rule, we can call Variable rule or Fonction rule.)
import "..." as ecore
generate ...
Model:
	im=Import
	(elements+=Template_file)*;
	
Import:
'import' package=[ecore::EPackage|STRING]
;
	
Template_file:
	Foreach 
;

Foreach:
	 'Foreach' name=ID 'in' table=Create_table 
	(tpl+=(Variable|Fonction))+    
	 'EndForeach' 	
;

Create_table:
	sgbd=SGBD_Querry | fct=Fonction
;

SGBD_Querry:
	'SELECT * FROM' class=[ecore::EClass| ID]
;

Variable:
	'$' foreach=[Foreach] '.' attribut=[ecore::EAttribute|ID] 
;

Fonction:
	'$' field=[ecore::EClass |ID] '.' Operation=[ecore::EOperation |ID] '(' (parametres+=ID (',' parametres+=ID)*)* ');'
;

And the scope class is:
public class Test1ScopeProvider 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;
	}
	
	public IScope scope_SGBD_Querry_class(Model context, EReference reference) {
		return Scopes.scopeFor(EcoreUtil2.getAllContentsOfType(context.getIm().getPackage(), EClass.class));
	}
	
	public IScope scope_Variable_attribut(Variable context,EReference reference){
		return Scopes.scopeFor(EcoreUtil2.getAllContentsOfType(context.getForeach().getTable().getSgbd().getClass_(), EAttribute.class));
	}
	
	public IScope scope_Fonction_field(Model context,EReference reference ){
		// I return only classes that have EOperations as children
		ArrayList<EObject> result= new ArrayList<EObject>();
		java.util.List<EClass> allClasses=EcoreUtil2.getAllContentsOfType(context.getIm().getPackage(), EClass.class);
		int i =0;
		while(i<allClasses.size()){
			EObject classe=allClasses.get(i);
			List<EOperation> allOperations= EcoreUtil2.getAllContentsOfType(classe,EOperation.class);
			if(!allOperations.isEmpty()){
				result.add(classe);
			}
			i++;
		}
		return Scopes.scopeFor(result);
	}
	
	public IScope scope_Fonction_Operation(Fonction context,EReference reference ){
		return Scopes.scopeFor(EcoreUtil2.getAllContentsOfType(context.getField(), EOperation.class));
	}

}


I don't have problems of compilation, but when I instantiate the editor, autocompletion tool gives wrong propositions!!

This is an example of the ecore models I use : (I can't post images,so this is the textual representation of the model)

<?xml version="1.0" encoding="UTF-8"?>
<ecore:EPackage xmi:version="2.0"
    xmlns:xmi="..." xmlns:xsi="..."
    xmlns:ecore="..." name="MyPackage"
    nsURI="..." nsPrefix="">
  <eClassifiers xsi:type="ecore:EClass" name="Class1">
    <eStructuralFeatures xsi:type="ecore:EAttribute" name="Att1"/>
    <eStructuralFeatures xsi:type="ecore:EAttribute" name="Att2"/>
  </eClassifiers>
  <eClassifiers xsi:type="ecore:EClass" name="class2">
    <eStructuralFeatures xsi:type="ecore:EAttribute" name="Att" defaultValueLiteral=""/>
  </eClassifiers>
  <eClassifiers xsi:type="ecore:EClass" name="Operation">
    <eOperations name="operation1"/>
    <eOperations name="operation2"/>
  </eClassifiers>
</ecore:EPackage>


And this what I have in the editor:
import "..."
Foreach i in SELECT * FROM Class1
$i.Att1 //Nothing was proposed after $i. so I wrote the name of the attribute
//Autocompletion proposes here $ or ( !!!
$Operation. //Nothing is proposed !!!
EndForeach


Have you an idea of the problem?
Thanks
Asma

[Updated on: Wed, 06 June 2012 13:15]

Report message to a moderator

Re: use the importURI mechanism to incude an ecore model [message #882609 is a reply to message #882438] Wed, 06 June 2012 20:56 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

hi since the grammar is highly amigous the ui parser does not know what to do until it finds /not finds the ( of the method
what about making the grammar less amigous?
or you concepts more general

~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:Guice setup in multi-plug-in setups
Next Topic:quick fix for combination of issues?
Goto Forum:
  


Current Time: Fri Apr 19 19:53:00 GMT 2024

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

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

Back to the top