Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Cross references from Xtext to given instance of EMF model
Cross references from Xtext to given instance of EMF model [message #493204] Fri, 23 October 2009 15:25 Go to next message
David  is currently offline David Friend
Messages: 5
Registered: July 2009
Junior Member
Hello,

it is possible to create cross references that would point to given instance of EMF model (not created by Xtext)?

I will describe our use case. We have written grammar for our DSL. In this grammar, there are declared methods:
Method_decl: interface = ID '.' name = ID "(" Parlist_decl ')';


Than, we have EMF model. There is EObject Frame in this model. This EObject has method Interface provides() that returns all provided interfaces by given frame.
In file edited by Xtext editor we want to enable only that interfaces that are returned by this method of object in the instance of this model. This emf model is saved in file ecore_instance.xmi in the same directory as the file edited by Xtext editor.

I tried to solve the problem following way: In file DSL_name.xtext I imported given ecore model:
import 'http://www...' as anotherModel


Than I changed the grammar:
Method_decl: interface = [anotherModel::Interface] '.' name = ID "(" Parlist_decl ')';


Than I created method in Scoped provider this way:
IScope scope_Interface(Property thisProp, EClass type) { 
        File file = getFileWithEcoreInstance();
           
        ResourceSet resourceSet = new ResourceSetImpl();
        Resource resource = resourceSet.getResource(URI.createFileURI(file.getAbsolutePath()), true);
        Frame object =  (Frame) resource.getContents().get(0).eContents().get(0);
            
        EList<Interface> ifaceList = object.getProvides();
        Iterator<Interface> iterator = ifaceList.iterator();
        List<IScopedElement> newScopeElements = new ArrayList<IScopedElement>();
        for (Iterator<Interface> iterator = frame.getProvides().iterator(); iterator.hasNext();) {
            Interface iface = iterator.next();
            newScopeElements.add(ScopedElement.create(iface.getName(), iface));
        }
           
        return new SimpleScope(IScope.NULLSCOPE, newScopeElements);
}


First problem is that I don't know how to define method getFileWithEcoreInstance(). That is, how to get file ecore_instance.xmi that is in the same directory as the file that is actually edited by Xtext editor. If I could get an input of Xtext editor, I could try to cast it to FileInput and than get edited file and than get file ecore_model.xmi that is in the same directory.

If I hardcode the path to the file ecore_instance.xmi, auto competition starts working correctly - interfaces returned by method provides() are proposed by Xtext editor. Command Open Declaration opens new editor with file ecore_instance.xmi - the file where given interface is defined. However, Xtext underlines interface name - even if it is correct - and displays message Couldn't resolve reference to Interface interface_name.

Please, could you given us some advice how to solve this problem?


Thank you,
David

Re: Cross references from Xtext to given instance of EMF model [message #493318 is a reply to message #493204] Sat, 24 October 2009 18:39 Go to previous messageGo to next message
Sven Efftinge is currently offline Sven EfftingeFriend
Messages: 1823
Registered: July 2009
Senior Member
David schrieb:
> Hello,
>
> it is possible to create cross references that would point to given
> instance of EMF model (not created by Xtext)?

Sure, cross references are declared against EClasses.
So any EObject can be referenced.

>
> I will describe our use case. We have written grammar for our DSL. In
> this grammar, there are declared methods:
>
> Method_decl: interface = ID '.' name = ID "(" Parlist_decl ')';
>
>
> Than, we have EMF model. There is EObject Frame in this model. This
> EObject has method Interface provides() that returns all provided
> interfaces by given frame.
> In file edited by Xtext editor we want to enable only that interfaces
> that are returned by this method of object in the instance of this
> model. This emf model is saved in file ecore_instance.xmi in the same
> directory as the file edited by Xtext editor.
>
> I tried to solve the problem following way: In file DSL_name.xtext I
> imported given ecore model:
>
> import 'http://www...' as anotherModel
>

You should use a platform resource URI, pointing to the *.ecore file.

And don't forget to parameterize the EcoreGeneratorFragment with the
corresponding genmodel in the MWE file.

>
> Than I changed the grammar:
>
> Method_decl: interface = [anotherModel::Interface] '.' name = ID "("
> Parlist_decl ')';
>
>
> Than I created method in Scoped provider this way:
>
> IScope scope_Interface(Property thisProp, EClass type) { File
> file = getFileWithEcoreInstance();
> ResourceSet resourceSet = new ResourceSetImpl();
> Resource resource =
> resourceSet.getResource(URI.createFileURI(file.getAbsolutePa th()), true);
> Frame object = (Frame)
> resource.getContents().get(0).eContents().get(0);
> EList<Interface> ifaceList = object.getProvides();
> Iterator<Interface> iterator = ifaceList.iterator();
> List<IScopedElement> newScopeElements = new
> ArrayList<IScopedElement>();
> for (Iterator<Interface> iterator =
> frame.getProvides().iterator(); iterator.hasNext();) {
> Interface iface = iterator.next();
> newScopeElements.add(ScopedElement.create(iface.getName(),
> iface));
> }
> return new SimpleScope(IScope.NULLSCOPE,
> newScopeElements);
> }
>
>
> First problem is that I don't know how to define method
> getFileWithEcoreInstance(). That is, how to get file ecore_instance.xmi
> that is in the same directory as the file that is actually edited by
> Xtext editor. If I could get an input of Xtext editor, I could try to
> cast it to FileInput and than get edited file and than get file
> ecore_model.xmi that is in the same directory.

If it's next to the resource at hand, you can compute an URI using the
current resources URI (something like
resource.getURI().trimSegments(1).appendSegment("ecore_model.xmi ")).
Then load the resource using the resource set and lookup the EObject.

resource.getResourceSet().getResource(myComputedURI,true);

If you know the fragment of the EObject you want to find, you could even
use.

resource.getResourceSet().getEObject(URIWithFragment, true);

Cheers,
Sven

--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com
Re: Cross references from Xtext to given instance of EMF model [message #493532 is a reply to message #493318] Mon, 26 October 2009 16:14 Go to previous messageGo to next message
David  is currently offline David Friend
Messages: 5
Registered: July 2009
Junior Member
Hello,

thank you for your response. Loading the resource - file ecore_model.xmi works as you described.

However, second problem I described persists - auto completion of names of interfaces works, but error Couldn't resolve reference to Interface interface_name is still displayed. Maybe second meta model is not visible?

In grammar, this meta model is imported following way:
import 'platform:/resource/org.xxx/model/secondModel.ecore' as anotherModel


Model of DSL is generated:
generate myDsl "http://www.xtext.org/example/MyDsl"


The reference is declared following way:
Method_decl: interface = [anotherModel::Interface] '.' name = ID "(" Parlist_decl ')';


Scope with EObjects representing provided interfaces is returned from given method of ScopeProvider as I described in my first post.


Sven Efftinge wrote on Sat, 24 October 2009 14:39
And don't forget to parameterize the EcoreGeneratorFragment with the
corresponding genmodel in the MWE file.


Maybe the problem is that I didn't catch this. You meant that in file GenerateMyDsl.properties should be following?

grammarURI=classpath:/org/xtext/example/MyDsl.xtext
file.extensions=mydsl
projectName=org.xtext.example.mydsl



Sven Efftinge wrote on Sat, 24 October 2009 14:39
Sure, cross references are declared against EClasses.
So any EObject can be referenced.


I appologize for incorrect formulation. Sure, references are declared against EClasses. I meant that in concrete instance of the model (dsl file), cross reference points to some EObject.



Re: Cross references from Xtext to given instance of EMF model [message #494333 is a reply to message #493532] Fri, 30 October 2009 10:29 Go to previous message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3118
Registered: July 2009
Senior Member
Hi David,

please see below.

David schrieb:
> Hello,
>
> thank you for your response. Loading the resource - file ecore_model.xmi
> works as you described.
>
> However, second problem I described persists - auto completion of names
> of interfaces works, but error Couldn't resolve reference to Interface
> interface_name is still displayed. Maybe second meta model is not visible?
>
> In grammar, this meta model is imported following way:
>
> import 'platform:/resource/org.xxx/model/secondModel.ecore' as anotherModel
>
>
> Model of DSL is generated:
>
> generate myDsl "http://www.xtext.org/example/MyDsl"
>
>
> The reference is declared following way:
>
> Method_decl: interface = [anotherModel::Interface] '.' name = ID "("
> Parlist_decl ')';
>
>
> Scope with EObjects representing provided interfaces is returned from
> given method of ScopeProvider as I described in my first post.
>
>
> Sven Efftinge wrote on Sat, 24 October 2009 14:39
>> And don't forget to parameterize the EcoreGeneratorFragment with the
>> corresponding genmodel in the MWE file.
>
>
> Maybe the problem is that I didn't catch this. You meant that in file
> GenerateMyDsl.properties should be following?
>

Sven was refering to the GenerateMyDsl.mwe file. It contains several
generator fragments that make up the generator configuration for your
language.
Please have a look at the FAQ to see how it should work:
http://wiki.eclipse.org/Xtext/FAQ#Why_are_generated_packages _from_an_imported_grammar_A_duplicated_in_dependent_grammar_ B.C2.A0.3F


>
> grammarURI=classpath:/org/xtext/example/MyDsl.xtext
> file.extensions=mydsl
> projectName=org.xtext.example.mydsl
>
>
>
> Sven Efftinge wrote on Sat, 24 October 2009 14:39
>> Sure, cross references are declared against EClasses.
>> So any EObject can be referenced.
>
>
> I appologize for incorrect formulation. Sure, references are declared
> against EClasses. I meant that in concrete instance of the model (dsl
> file), cross reference points to some EObject.
>

That is possible. There is no constraint in Xtext about the location /
container or whatever of the references objects. If you use the
importURI mechanism, you could write something like

import "path/to/some.xmi"

and refer to the instances in the xmi without any hassle.

Regards,
Sebastian
--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com
Previous Topic:get all model elements from outside the editor
Next Topic:backtrack non-LL(*)
Goto Forum:
  


Current Time: Thu Mar 28 22:05:01 GMT 2024

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

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

Back to the top