Skip to main content



      Home
Home » Modeling » TMF (Xtext) » Resolving variables from an external source
Resolving variables from an external source [message #1732039] Thu, 12 May 2016 04:00 Go to next message
Eclipse UserFriend
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 11:29 Go to previous messageGo to next message
Eclipse UserFriend
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
Re: Resolving variables from an external source [message #1732215 is a reply to message #1732104] Fri, 13 May 2016 09:28 Go to previous messageGo to next message
Eclipse UserFriend
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 06:56 Go to previous messageGo to next message
Eclipse UserFriend
No Message Body

[Updated on: Wed, 18 May 2016 00:00] by Moderator

Re: Resolving variables from an external source [message #1732577 is a reply to message #1732575] Wed, 18 May 2016 06:57 Go to previous message
Eclipse UserFriend
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.
Previous Topic:Packages required to built-in xtext inside eclipse plugin
Next Topic:RuleName as Link
Goto Forum:
  


Current Time: Thu Jul 03 10:20:44 EDT 2025

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

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

Back to the top