Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Referencing classes from different ecore using qualified names in my DSL
Referencing classes from different ecore using qualified names in my DSL [message #1740098] Tue, 09 August 2016 08:08 Go to next message
Michal Zyla is currently offline Michal ZylaFriend
Messages: 18
Registered: August 2016
Junior Member
Hi

I'm trying to build a dsl with follwing syntax to reference by qualified names EClasses from an ecore defined in seperate file. I',m aiming for the following syntax:
select Epacket1.Epacket2.MyEClass


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

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

Model:
	"select" type=[ecore::EClass]
	;


Currently I'm confused how should I aproach this. I would by glad for any pointers what should I implement, how should i change my syntax to achieve this.

Regards
Michal

[Updated on: Wed, 22 February 2017 09:51]

Report message to a moderator

Re: Referencing classes from different ecore using qualified names in my DSL [message #1740121 is a reply to message #1740098] Tue, 09 August 2016 12:40 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14664
Registered: July 2009
Senior Member
Hi,

what about

type=[ecore::EClass|QualifiedName]
;

QualifiedName:
ID ("." ID)*
;


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Referencing classes from different ecore using qualified names in my DSL [message #1740123 is a reply to message #1740121] Tue, 09 August 2016 13:18 Go to previous messageGo to next message
Michal Zyla is currently offline Michal ZylaFriend
Messages: 18
Registered: August 2016
Junior Member
Hi
just the class names are being recognized as valid. I think I am missing some crucial part to it.
I also added custom Scope provider that reads the other ecore file and delivers all classes in the following way:
public class MqlScopeProvider extends AbstractMqlScopeProvider
{
    @Override
    public IScope getScope(EObject context, EReference reference)
    {
        ResourceSet rs = new ResourceSetImpl();
        Resource resource = rs.getResource(URI.createURI("my.ecore"), true);
        List<EClass> classes = new ArrayList<>();
        TreeIterator<EObject> allContents = EcoreUtil.<EObject> getAllContents(resource, true);
        while (allContents.hasNext())
        {
            EObject next = allContents.next();
            if (next instanceof EClass)
            {
                EClass eClass = (EClass) next;
                classes.add(eClass);
            }
        }
      return Scopes.scopeFor(classes);
  }

}

regards
Michal
Re: Referencing classes from different ecore using qualified names in my DSL [message #1740125 is a reply to message #1740123] Tue, 09 August 2016 13:27 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14664
Registered: July 2009
Senior Member
you dont need a scope provider adaptation. simpy throw the ecore next to the test.mydsl file

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Referencing classes from different ecore using qualified names in my DSL [message #1740126 is a reply to message #1740125] Tue, 09 August 2016 13:39 Go to previous messageGo to next message
Michal Zyla is currently offline Michal ZylaFriend
Messages: 18
Registered: August 2016
Junior Member
So if I revert to the generated scope provider i get no references whatsoever an puting my ecore together with mydsl file doesn't help. But generally You mean it should work out of the box with the syntax you recomended and ecore being in the same directory as the dsl file, or are additional changes required?
Re: Referencing classes from different ecore using qualified names in my DSL [message #1740129 is a reply to message #1740126] Tue, 09 August 2016 13:51 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14664
Registered: July 2009
Senior Member
yes i just tested it. my grammar. project is a java project. testfile and ecore inside src folder. project has xtext nature.build automatically is on.

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Referencing classes from different ecore using qualified names in my DSL [message #1740136 is a reply to message #1740129] Tue, 09 August 2016 14:34 Go to previous messageGo to next message
Michal Zyla is currently offline Michal ZylaFriend
Messages: 18
Registered: August 2016
Junior Member
Thx that works Smile adding xtext nature did the trick. Could you tell me how is the ecore picked up? I need now to change this to work with an ecore loaded from another eclipse plugin.
Re: Referencing classes from different ecore using qualified names in my DSL [message #1740138 is a reply to message #1740136] Tue, 09 August 2016 14:45 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14664
Registered: July 2009
Senior Member
Adding the plugin to the classpath eg as required plugin a plugin projects manifest should do the trick

The magic is a resourceserviceprovider for ecore files. The rest is how xtext finds other model files from classpath


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Referencing classes from different ecore using qualified names in my DSL [message #1740139 is a reply to message #1740138] Tue, 09 August 2016 14:53 Go to previous messageGo to next message
Michal Zyla is currently offline Michal ZylaFriend
Messages: 18
Registered: August 2016
Junior Member
Thanks!
Re: Referencing classes from different ecore using qualified names in my DSL [message #1740187 is a reply to message #1740139] Wed, 10 August 2016 08:50 Go to previous messageGo to next message
Michal Zyla is currently offline Michal ZylaFriend
Messages: 18
Registered: August 2016
Junior Member
Ok so now i want to build on that. In my eclipse plugin project I want to use Embedded editor that will refer to classes from ecore defined in another plugin. I added xtext nature to the plugin with ecore files and added it as a dependency but the classes from the ecore don't get picked up. Is there a way to programatically tell the embedded editor what aditional ecores should be used?
Re: Referencing classes from different ecore using qualified names in my DSL [message #1740189 is a reply to message #1740187] Wed, 10 August 2016 09:15 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14664
Registered: July 2009
Senior Member
in your EditedResourceSetProvider.
this the stuff there set on the classpath of the resourceset?
how is the resourceset created there?
is it connected to a project or hanging in the air?
did you try to set a resourceset.classpathuricontext with a classloader that has ghe stuff on the classpath? ....

i dont know if this will work. too many parameters ...


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Referencing classes from different ecore using qualified names in my DSL [message #1740190 is a reply to message #1740189] Wed, 10 August 2016 09:20 Go to previous messageGo to next message
Michal Zyla is currently offline Michal ZylaFriend
Messages: 18
Registered: August 2016
Junior Member
There is no special project for the dsl files nor a folder on the disk. So i guess it is hanging in the air. I would like I to be in memory if it's possible.

This is the code that creates the ditor:
  IResourceSetProvider resourceSetProvider = injector.getInstance(IResourceSetProvider.class);
        FileExtensionProvider extensionProvider = injector.getInstance(FileExtensionProvider.class);

        ResourceSet resourceSet = resourceSetProvider.get(null);
        URI uri = URI.createURI("synthetic:/query." + extensionProvider.getPrimaryFileExtension());
        XtextResource xtextResource = (XtextResource) resourceSet.createResource(uri);
        resourceSet.getResources().add(xtextResource);
        Resource createResource = resourceSet.createResource(URI.createURI("platform:/plugin/my.ecore.plugin/model/model.ecore"));
        resourceSet.getResources().add(createResource);

        IEditedResourceProvider queryResourceProvider = new IEditedResourceProvider() {
            @Override
            public XtextResource createResource()
            {
                return xtextResource;
            }
        };
        EmbeddedEditorFactory editorFactory = injector.getInstance(EmbeddedEditorFactory.class);

        QueryHandlerDialog queryHandlerDialog = new QueryHandlerDialog(shell, editorFactory, queryResourceProvider);
        int open = queryHandlerDialog.open();

[Updated on: Wed, 10 August 2016 09:28]

Report message to a moderator

Re: Referencing classes from different ecore using qualified names in my DSL [message #1740192 is a reply to message #1740190] Wed, 10 August 2016 09:32 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14664
Registered: July 2009
Senior Member
does this loading help or does it not?

unfortunately i dont have the time to build the as runnable example to look what happens


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Referencing classes from different ecore using qualified names in my DSL [message #1740193 is a reply to message #1740192] Wed, 10 August 2016 09:36 Go to previous messageGo to next message
Michal Zyla is currently offline Michal ZylaFriend
Messages: 18
Registered: August 2016
Junior Member
No, adding it to the resource set doesn't help

[Updated on: Wed, 10 August 2016 09:36]

Report message to a moderator

Re: Referencing classes from different ecore using qualified names in my DSL [message #1740194 is a reply to message #1740193] Wed, 10 August 2016 09:38 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14664
Registered: July 2009
Senior Member
can you share a minimal example that reproduces this?

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Referencing classes from different ecore using qualified names in my DSL [message #1740196 is a reply to message #1740194] Wed, 10 August 2016 10:08 Go to previous messageGo to next message
Michal Zyla is currently offline Michal ZylaFriend
Messages: 18
Registered: August 2016
Junior Member
Hi Christan thanks for help. I attached a minimal project to replicate my use case. The plugin contributes a view with the embedded editor.
Re: Referencing classes from different ecore using qualified names in my DSL [message #1740197 is a reply to message #1740196] Wed, 10 August 2016 11:04 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14664
Registered: July 2009
Senior Member
i had to change the grammar to accept qualified names.

then the following works for me

XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class);
		ProjectDescription projectDescription = new ProjectDescription();
		projectDescription.setName("dummy");
		projectDescription.attachToEmfObject(resourceSet);
		Map<String, ResourceDescriptionsData> dataMap = Maps.<String, ResourceDescriptionsData>newHashMap();
        final ChunkedResourceDescriptions index = new ChunkedResourceDescriptions(dataMap, resourceSet);
        ResourceDescriptionsData data = new ResourceDescriptionsData(Lists.newArrayList());
		index.setContainer(projectDescription.getName(), data);
		FileExtensionProvider extensionProvider = injector.getInstance(FileExtensionProvider.class);
		URI uri = URI.createURI("synthetic:/query." + extensionProvider.getPrimaryFileExtension());
		XtextResource xtextResource = (XtextResource) resourceSet.createResource(uri);
		
		
		resourceSet.getResources().add(xtextResource);
		Resource createResource = resourceSet.createResource(
				URI.createURI("platform:/plugin/myModel/model/myModel.ecore"));
		try {
			createResource.load(null);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		resourceSet.getResources().add(createResource);
		IResourceDescription resourceDescription = IResourceServiceProvider.Registry.INSTANCE.getResourceServiceProvider(createResource.getURI()).getResourceDescriptionManager().getResourceDescription(createResource);
		data.addDescription(createResource.getURI(), resourceDescription);
		


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Referencing classes from different ecore using qualified names in my DSL [message #1740204 is a reply to message #1740197] Wed, 10 August 2016 12:40 Go to previous message
Michal Zyla is currently offline Michal ZylaFriend
Messages: 18
Registered: August 2016
Junior Member
Hi Christian
It works perfectly! Thank You
Previous Topic:Building heuristics - catch the changes
Next Topic:Accessing the Abstract Syntax Tree of imported files
Goto Forum:
  


Current Time: Sun Apr 14 11:37:39 GMT 2024

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

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

Back to the top