Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Resolving variables from an external source
Resolving variables from an external source [message #1732039] Thu, 12 May 2016 08:00 Go to next message
Mathias Immerkær is currently offline Mathias ImmerkærFriend
Messages: 2
Registered: May 2016
Junior Member
I'm new to working with Xtext and Xtend and I have stumbled upon a problem which I hope someone can help me solve. What I'm trying to achieve is to resolve variables from an external source rather than declaring them explicitly in the DSL. I got the following example to demonstrate: Here is the grammar:

grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.xbase.Xbase


Model:
configUrl=ConfigUrl
devices+=Device*
test=Test
;

ConfigUrl:
"ConfigURL=" url=STRING
;

Device:
'Device' name=ID
'has channels: ' (channels+=Channel (',' channels+=Channel)*)?
;

Channel:
name=ID
;

Test:
'DoSomething' channel=[Channel|QualifiedName]
;


and here is an example usage:

ConfigURL="---some url goes here---"
Device Light has channels: state
DoSomething Light.state


Instead of explicitly declaring the devices in the DSL I would like to resolve them from and external source (the ConfigURL variable). As far as I can tell, what I'm looking for is related to the scoping functionality of Xtend. I have tried to implement the IScope interface and got the getAllElements to work (entries shows up in the autocompletion), but cannot get getSingleElement to work and thus why the variable cannot be resolved once entered.

Thanks,
Re: Resolving variables from an external source [message #1732104 is a reply to message #1732039] Thu, 12 May 2016 15:29 Go to previous messageGo to next message
Karsten Thoms is currently offline Karsten ThomsFriend
Messages: 712
Registered: July 2009
Location: Dortmund, Germany
Senior Member

From what I've got from this description you would have to achieve the following:

The configUrl specifies an URI for an external resource. This resource has to be considered in the global scope. Therefore you would use the ImportUriGlobalScopeProvider. In order to make things not more complicated rename attribute "url" to "importURI", since this is what ImportUriResolver would try to find. Otherwise this would need to be customized also.

Next, the external resource has to be recognized as a resource which Xtext can handle. This needs the implementation of an IResourceServiceProvider. There is support for non-xtext resources in package org.eclipse.xtext.resource.generic. Have a look at the sources of plugin org.eclipse.xtext.ecore, which adds support for .ecore resources. For your use case, you would have to implement something similar for your external resource.

HTH,
~Karsten


Need professional support for Xtext, Xpand, EMF?
Go to: http://xtext.itemis.com
Twitter : @kthoms
Blog : www.karsten-thoms.de
Re: Resolving variables from an external source [message #1732215 is a reply to message #1732104] Fri, 13 May 2016 13:28 Go to previous messageGo to next message
Mathias Immerkær is currently offline Mathias ImmerkærFriend
Messages: 2
Registered: May 2016
Junior Member
Just in case, let me elaborate a bit further. I want to be able to omit the need for explicitly declaring some variables/objects when using the DSL.

I want to be able to go from:
Device Light has channels: state
DoSomething Light.state

to
ConfigURL="http://localhost:8080/devices"
DoSomething Light.state

where http://localhost:8080/devices might contain something like
{
  "devices": {
    "1": {
      "name": "Light",
      "channels": {
        "1": {
          "name": "state"
        }
      }
    },
    "2": {
      "name": "Thermostat",
      "channels": {
        "1": {
          "name": "temperature"
        }
      }
    }
  }
}

So I would need to implement something that can transform that external resource into something that Xtext can understand (so that the devices and channels can be suggested by auto-completion in the IDE etc.).

I have briefly stumbled upon the IResourceProvider interface so you might be onto something. But I have yet to find a good example of how to properly implement the interface; how is the implementation supposed to be registered etc.?
Many of the examples I come by, is from a previous version of Xtext and it makes it hard for me to know how to implement it.
Pardon my ignorance, but this is the first time I'm trying to do something a little bit more advanced with Xtext.
Re: Resolving variables from an external source [message #1732575 is a reply to message #1732215] Wed, 18 May 2016 10:56 Go to previous messageGo to next message
Jan Koehnlein is currently offline Jan KoehnleinFriend
Messages: 760
Registered: July 2009
Location: Hamburg
Senior Member
No Message Body

---
Get professional support from the Xtext committers at www.typefox.io

[Updated on: Wed, 18 May 2016 04:00]

Report message to a moderator

Re: Resolving variables from an external source [message #1732577 is a reply to message #1732575] Wed, 18 May 2016 10:57 Go to previous message
Jan Koehnlein is currently offline Jan KoehnleinFriend
Messages: 760
Registered: July 2009
Location: Hamburg
Senior Member
Do you really want to cross-reference data you fetch live from the internet? Scoping, linking etc. are live services, i.e. they are called on user actions like keystrokes and should not have to deal with long running operations. Imagine content assist blocking because of a temporary internet problem...

Nevertheless, in order to refer to elements from non-Xtext based models, you have to register an implementation for the minimum infrastructure that Xtext needs. This is as Karsten pointed out mainly about index entries for the referable elements.

If you don't want to deep dive into the innards of Xtext, you could also build an Xtext grammar that is able to parse these files. I'd not be suprised if there already was an Xtext grammar for JSON.


---
Get professional support from the Xtext committers at www.typefox.io
Previous Topic:Packages required to built-in xtext inside eclipse plugin
Next Topic:RuleName as Link
Goto Forum:
  


Current Time: Wed Nov 13 17:58:57 GMT 2019

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

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

Back to the top