Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Connecting to a non-xtext language from Eclipse: Binding ResourceDescriptionStrategy
Connecting to a non-xtext language from Eclipse: Binding ResourceDescriptionStrategy [message #1777945] Wed, 06 December 2017 15:53 Go to next message
Steffen Zschaler is currently offline Steffen ZschalerFriend
Messages: 266
Registered: July 2009
Senior Member
Hi,

I'm building an Xtext DSL that needs to reference a non-Xtext DSL. I run into problems because my non-Xtext DSL comes with a bespoke ResourceDescriptionStrategy and I don't know where to bind/register this when running in Eclipse.

I have build suitable support classes based on org.eclipse.xtext.ecore.EcoreSupport and colleagues. I then call this from my StandaloneSetup. This works well for my unit tests, which are run in standalone mode. Everything is fine, the ResourceDescriptionStrategy is found and used and all my linking works.

When I start a runtime Eclipse instance, it suddenly doesn't find the resource description strategy any more, so that the linking no longer works. This isn't particularly surprising: after all, I have only registered that strategy for the standalone case.

I have searched a fair bit, but couldn't find anything that tells me where to bind/register that strategy so that it will also be found when running in Eclipse.

Any pointers?

many thanks,

Steffen
Re: Connecting to a non-xtext language from Eclipse: Binding ResourceDescriptionStrategy [message #1777946 is a reply to message #1777945] Wed, 06 December 2017 15:58 Go to previous messageGo to next message
Karsten Thoms is currently offline Karsten ThomsFriend
Messages: 762
Registered: July 2009
Location: Dortmund, Germany
Senior Member

With EcoreSupport you are already digging in the right direction. The runtime module is binding the IResourceDescriptionStrategy, and you'll need an UI plugin with an Activator that creates the UI's injector. Also there, provide an ExecutableExtensionFactory that uses the injector. Look at the org.eclipse.xtext.ui.ecore plugin as a reference. Also Christian's blog https://christiandietrich.wordpress.com/2011/07/17/xtext-2-0-and-uml/ should give ou some hints.
Re: Connecting to a non-xtext language from Eclipse: Binding ResourceDescriptionStrategy [message #1777947 is a reply to message #1777945] Wed, 06 December 2017 16:00 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
about registering resourceserviceproviders there is an extension point

<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>

<extension
point="org.eclipse.xtext.extension_resourceServiceProvider">
<resourceServiceProvider
class="org.eclipse.xtext.ui.uml.ExecutableExtensionFactory:org.eclipse.xtext.ui.resource.generic.EmfResourceUIServiceProvider"
uriExtension="uml">
</resourceServiceProvider>
</extension>

</plugin>

have a look at
https://christiandietrich.wordpress.com/2011/07/17/xtext-2-0-and-uml/
or
https://typefox.io/linking-xtext-models-with-other-emf-models


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Connecting to a non-xtext language from Eclipse: Binding ResourceDescriptionStrategy [message #1777952 is a reply to message #1777947] Wed, 06 December 2017 17:12 Go to previous messageGo to next message
Steffen Zschaler is currently offline Steffen ZschalerFriend
Messages: 266
Registered: July 2009
Senior Member
Many thanks, this looks like exactly what I was looking for. I will go away and try to implement this.

Many thanks,

Steffen
Re: Connecting to a non-xtext language from Eclipse: Binding ResourceDescriptionStrategy [message #1778000 is a reply to message #1777952] Thu, 07 December 2017 14:18 Go to previous messageGo to next message
Steffen Zschaler is currently offline Steffen ZschalerFriend
Messages: 266
Registered: July 2009
Senior Member
Thanks, that's fixed this.
Re: Connecting to a non-xtext language from Eclipse: Binding ResourceDescriptionStrategy [message #1778333 is a reply to message #1778000] Wed, 13 December 2017 08:54 Go to previous messageGo to next message
Steffen Zschaler is currently offline Steffen ZschalerFriend
Messages: 266
Registered: July 2009
Senior Member
Ha, I spoke too soon :-(

It seems that my strategy is used when linking is done, but for some strange reason is not used when the validators are executed (specifically, this seems to be when the Xtext Reconciler runs). I'm still debugging and will try and put together a minimal demo, but if any of you happen to have an idea of why this might happen or what I should look at, that would be very much appreciated.

Many thanks,

Steffen
Re: Connecting to a non-xtext language from Eclipse: Binding ResourceDescriptionStrategy [message #1778336 is a reply to message #1778333] Wed, 13 December 2017 09:04 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
which validator do you mean. what your xmi files are not validated? or that you get broken cross references?

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Connecting to a non-xtext language from Eclipse: Binding ResourceDescriptionStrategy [message #1778337 is a reply to message #1778336] Wed, 13 December 2017 09:33 Go to previous messageGo to next message
Steffen Zschaler is currently offline Steffen ZschalerFriend
Messages: 266
Registered: July 2009
Senior Member
I mean the validators I have written in Xtext. When they're run, the crossreferences are proxies again, but suddenly fail to resolve for some reason.
Re: Connecting to a non-xtext language from Eclipse: Binding ResourceDescriptionStrategy [message #1778340 is a reply to message #1778337] Wed, 13 December 2017 09:37 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
hmmm i assume there is something wrong with your implementation of the resource service provider

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Connecting to a non-xtext language from Eclipse: Binding ResourceDescriptionStrategy [message #1778341 is a reply to message #1778340] Wed, 13 December 2017 09:40 Go to previous messageGo to next message
Steffen Zschaler is currently offline Steffen ZschalerFriend
Messages: 266
Registered: July 2009
Senior Member
I see. I thought there might be an issue there (it behaves in a slightly non-standard way), but wanted to make sure I don't miss anything obvious. I will continue digging into this and report back once I figure out what's going on in more detail.

Is there somewhere where I can learn more about the assumptions the Xtext infrastructure makes on the behaviour of resource description strategies?
Re: Connecting to a non-xtext language from Eclipse: Binding ResourceDescriptionStrategy [message #1778343 is a reply to message #1778341] Wed, 13 December 2017 09:43 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
amm not sure what you mean by that. without any hints what you do its hard to tell what could be wrong.
maybe you create names the wrong way. or you create the eobjectdescriptions the wrong way, or ........


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Connecting to a non-xtext language from Eclipse: Binding ResourceDescriptionStrategy [message #1778346 is a reply to message #1778343] Wed, 13 December 2017 09:57 Go to previous messageGo to next message
Steffen Zschaler is currently offline Steffen ZschalerFriend
Messages: 266
Registered: July 2009
Senior Member
The main difference to a standard resource description strategy is that I use it to inject an adaptation layer. The XMI file that's being loaded has instances of metamodel X. In my resource description strategy I return EObjectDescriptions with instances of meta-classes from my adaptation-layer metamodel Y. I've overridden the generated implementation for Y such that non-proxy elements should always wrap a corresponding element from X and adapt data from X accordingly into data that looks like an instance from Y.

The purpose for this is that I will have X' and X'', which are similar kinds of languages, but expressed as different metamodels (because they come from different providers), but I want to be able to make statements about models in either language in my own Xtext DSL.
Re: Connecting to a non-xtext language from Eclipse: Binding ResourceDescriptionStrategy [message #1778661 is a reply to message #1778346] Tue, 19 December 2017 15:07 Go to previous messageGo to next message
Steffen Zschaler is currently offline Steffen ZschalerFriend
Messages: 266
Registered: July 2009
Senior Member
OK, so I finally got this to work.

The core of the problem was indeed related to my slightly strange resource description strategy: For some reason, in some cases Xtext seems to bypass the strategy altogether. As a result, my adapters weren't always reliably installed.

To fix this, I have implemented a full implementation of
Resource
that provides translation services and a new
TranslatingResourceSet
that installs these whenever an appropriate resource is opened or created. This resource set then replaces the
SynchronizedXTextResourceSet
in my DSLModule.

Interestingly, I had to make sure that I only do this when running in Eclipse. When running standalone, everything seems to work fine without my translating resource set. In fact, adding it in causes errors (I assume, but haven't followed this in detail, because there is now a conflict with how my resource description strategy works). This was easy enough to solve by binding the resource set in the right module, but I remain confused about why this would happen: As far as I can tell, none of my code is dependent on the environment. Yet, Xtext seems to take a substantially different path for parsing and validation depending on whether it's running standalone or in Eclipse.

Out of curiosity: does this ring a bell with anybody?

Many thanks,

Steffen
Re: Connecting to a non-xtext language from Eclipse: Binding ResourceDescriptionStrategy [message #1778667 is a reply to message #1778661] Tue, 19 December 2017 16:23 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
No why do you need a separate resourceset
How do you register your resource factory


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Connecting to a non-xtext language from Eclipse: Binding ResourceDescriptionStrategy [message #1778670 is a reply to message #1778667] Tue, 19 December 2017 17:16 Go to previous messageGo to next message
Steffen Zschaler is currently offline Steffen ZschalerFriend
Messages: 266
Registered: July 2009
Senior Member
I've not made any changes to how the resource factory is registered. However, my adaptation language probably breaks some assumptions in EMF or Xtext: Essentially, my setup is as follows:

I have three metamodels:


  1. A: my xtext DSML. This contains references to elements of B.
  2. B: my adaptation layer language. Instances of this wrap instances of C.
  3. C: a pre-existing language. This has been developed by a third party and comes with a full plugin registering the metamodel etc.


I have developed B by writing an ecore model and generating java code from it, then manually modifying the implementation to add appropriate forwarding and translation to the adaptation layer (including translation of references, etc.). It's not a hugely complicated language, so this was fairly straightforward.

I now registered a new resource description strategy for ".c" files that would produce EObjectDescriptions with suitable B instances wrapping the C objects. Effectively, I expected this to make it look to my xtext language A as if ".c" files actually contained only B objects.

This was enough for standalone use, but not quite enough for use in Eclipse. Here, I also had to introduce a wrapper for the resource and resource set. Note that C already comes with a fully-defined resource factory and registration set up, which I don't want to touch. I also don't want to have to materialise B files; their purpose is purely as an adaptation layer. As a result, there is no actual physical resource that has the B elements. Instead, I'm now providing an instance of my translating resource that is backed by the original C resource and behaves as if it is inside the same resource set.

To make sure this translating resource is picked up, I introduced the translating resource set. I couldn't think of a meaningful way of doing this otherwise, given that I only want translation to happen when ".c" files are looked at from my xtext DSL A (so I cannot simply replace the resource factory for ".c" files, if I'm not mistaken). This seems to work mostly, though, slightly annoyingly, the original resource still leaks on occasion (in particular, when the automated builders are running).

This felt like the natural way to solve this, but is beginning to increasingly feel like more complicated than it should be. I might be barking up the wrong tree here and there may be a much easier way to get what I want that I'm just not seeing. Any thoughts along those lines would of course be very much appreciated.
Re: Connecting to a non-xtext language from Eclipse: Binding ResourceDescriptionStrategy [message #1778706 is a reply to message #1778670] Wed, 20 December 2017 10:57 Go to previous messageGo to next message
Steffen Zschaler is currently offline Steffen ZschalerFriend
Messages: 266
Registered: July 2009
Senior Member
Having debugged this a bit more, it seems that what I'm really missing is the right place to inject stuff. At the moment, it seems that my resource description strategy and the translating resource/resource set are correctly picked up when parsing/linking/validating from within an editor, but are not picked up at all when the automated build is running. As a result, all works well in the editor, but I get all kinds of weird and wonderful errors as soon as I save a ".a" file.

Here's what I have done to inject the various bits and pieces. The ResourceDescriptionStrategy is injected via the Activator and ExecutableExtensionFactory (and extension point as per above) of the B plugin (I realise, I will eventually want to separate out the IDE and non-IDE bits here, but haven't done so for now):

public class AdaptationRuntimeModule extends AbstractGenericResourceRuntimeModule {

	@Override
	protected String getLanguageName() {
		return "...BEditorID";
	}

	@Override
	protected String getFileExtensions() {
		return "c"; 
	}

	public Class<? extends IDefaultResourceDescriptionStrategy> bindIDefaultResourceDescriptionStrategy() {
		return AdaptationResourceDescriptionStrategy.class;
	}

       ...
}


The translating resource set (remember, for some reason this is only needed when running from Eclipse) is injected from A's .ui plugin like this:

/**
 * Use this class to register components to be used within the Eclipse IDE.
 */
@FinalFieldsConstructor
class AUiModule extends AbstractAUiModule {
	def Class<? extends XtextResourceSet> bindXtextResourceSet() {
		TranslatingResourceSet // a sub-class of SynchronizedXtextResourceSet
	}
}


When validation etc runs in the editor, it correctly uses my resource description strategy and the new resource set. When the automated builder runs, it uses a normal SynchronizedXtextResourceSet and also doesn't seem to pick up the resource description strategy.

Looks like I need to register things in another way somewhere else. What am I missing?

Steffen
Re: Connecting to a non-xtext language from Eclipse: Binding ResourceDescriptionStrategy [message #1778712 is a reply to message #1778706] Wed, 20 December 2017 11:48 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

An approach that I use for OCL might work for you.

Externally OCL uses *.ecore, *.uml, *.ocl files that contribute different aspects of what is unified by the Pivot OCL Abstract Syntax model, which you might call an adaptation layer. It may be saved as *.oclas files.

The problem is how to reference MyClass consistently when it may come from Ecore or UML or from a saved OCL AS and how to accommodate loading of whichever alternative is available.

My initial principle was that there is only one MyClass which may have many serializations, but how does a reference find the right serialization?

I experimented with a custom pivot: protocol prefix, but found that prefixing pivot: to http: was unhelpful. (EMF's rewrite of URI may invalidate this conclusion.)

Anyway I moved on to treating *.oclas as a wrapper, so that *.ecore is an Ecore reference and *.ecore.oclas is a wrapped Ecore reference allowing the Pivot AS to be loaded directly.

The wrapping is achieved by registering a *.oclas ResourceFactory that tests whether the *.oclas file exists and if so loads it it, else it strips the *.oclas wrapper and tries again with perhaps a *.ecore that is then adapted for use as Pivot OCL internally.

As a result all the adaptation occurs within a ResourceFactory. EMF load and Xtext proxy resolution are unaffected

Regards

Ed Willink.

Re: Connecting to a non-xtext language from Eclipse: Binding ResourceDescriptionStrategy [message #1778713 is a reply to message #1778712] Wed, 20 December 2017 12:02 Go to previous messageGo to next message
Steffen Zschaler is currently offline Steffen ZschalerFriend
Messages: 266
Registered: July 2009
Senior Member
Thanks, Ed. I have been thinking that I may need to do something along these lines, as the problem does indeed seem to be that I am introducing two different interpretations for the same file name, which causes all kinds of problems in weird and wonderful places.

However, because these files don't actually exist on disk, how do I get Xtext to "find" them? I would need to install some code that finds all ".c" files and pretends to Xtext that there are corresponding ".c.b" files.

Xtext experts: Are Containers what I need to look at for something like this?

Thanks,

Steffen
Re: Connecting to a non-xtext language from Eclipse: Binding ResourceDescriptionStrategy [message #1778715 is a reply to message #1778713] Wed, 20 December 2017 12:20 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

The files may not exist, but presumably you have some algorithmic way of computing their content, so your ResourceFactory just does an in memory creation when the file access fails.

Regards

Ed Willink
Re: Connecting to a non-xtext language from Eclipse: Binding ResourceDescriptionStrategy [message #1778716 is a reply to message #1778715] Wed, 20 December 2017 12:24 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
did you consider to do a in place m2m on the original resources similar to that xtext does for Xtend/Xbase files with IDerivedStateComputer/DerivedStateAwareResourceDescriptionManager/DerivedStateAwareResource
what would prevent you from creating additional resources


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Connecting to a non-xtext language from Eclipse: Binding ResourceDescriptionStrategy [message #1778718 is a reply to message #1778716] Wed, 20 December 2017 12:30 Go to previous messageGo to next message
Steffen Zschaler is currently offline Steffen ZschalerFriend
Messages: 266
Registered: July 2009
Senior Member
Ed,

Thanks. The way Xtext reference lookup works (at least from what I understand) it wouldn't try to load those files unless I do something extra to make it "look for them". In other words, unless I do something extra there won't be any "file access" to "fail".

Many thanks,

Steffen
Re: Connecting to a non-xtext language from Eclipse: Binding ResourceDescriptionStrategy [message #1778719 is a reply to message #1778716] Wed, 20 December 2017 12:32 Go to previous messageGo to next message
Steffen Zschaler is currently offline Steffen ZschalerFriend
Messages: 266
Registered: July 2009
Senior Member
Thanks, Christian, this sounds similar to what I'm trying to do. Can you point me to the code? Is there perhaps even some documentation somewhere?
Re: Connecting to a non-xtext language from Eclipse: Binding ResourceDescriptionStrategy [message #1778721 is a reply to message #1778719] Wed, 20 December 2017 12:52 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
http://xtextcasts.org/episodes/18-model-optimization

main problem: the source model you do is not a xtext based so there is no derived state aware resource used and you have to mimic its behaviour.
but you can look where IDerivedStateComputer is used


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Connecting to a non-xtext language from Eclipse: Binding ResourceDescriptionStrategy [message #1778729 is a reply to message #1778721] Wed, 20 December 2017 13:33 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

Sorry. My earlier answers were about referencing at the EMF / XMI level.

If your problems are at the text level then the LinkingService is where I solve problems. It converts an arbitrary text token to an EObject, possibly using Scopes.

Regards

Ed Willink

Re: Connecting to a non-xtext language from Eclipse: Binding ResourceDescriptionStrategy [message #1778737 is a reply to message #1778729] Wed, 20 December 2017 15:30 Go to previous messageGo to next message
Steffen Zschaler is currently offline Steffen ZschalerFriend
Messages: 266
Registered: July 2009
Senior Member
Thanks, both. Given that I already have the translation stuff in place and it's more a matter of getting it hooked into xtext in the right place, I'm thinking about not using derived state computer. If I understand correctly, it's just a different way of getting the translation done, but I would still be left with the problem of actually hooking things up correctly. (Correct me if I'm wrong).

I suspect that the main problem is that I am doing two things for one resource URI. I don't want to pollute the resource contents with my adapter elements (which is what derived state computation seems to be doing, although there's a fair amount of cleanup going on, too). Importantly, my adapter objects have the same name as the original objects, so that might cause problems down the line if they're both in the same resource, especially as I seem to struggle getting xtext to reliably use my resource-description strategy.

So my plan is to go with something along the lines of what Ed suggests by creating virtual resources with a new ".c.b" suffix, registering my adapter language against "*.b" files. Because these files will never actually exist, I need a way of pretending to xtext that they are there by creating a virtual ".c.b" resource for every ".c" resource xtext considers in scope. I'm thinking that injecting my own version of PersistedStateProvider (via org.eclipse.xtext.ui.shared.overridingGuiceModule) is the way to go here. Then, I can override the load method to add IResourceDescriptions for the ".c.b" resources as needed. Providing a suitably "magic" resource factory for ".b" files (as sketched by Ed above) should then hopefully make sure that the translation magic is put in place as needed.

Does this sound like a sensible path? I'm a little worried about having to use org.eclipse.xtext.ui.shared.overridingGuiceModule as this makes the change global and might lead to conflicts with other xtext languages. But given that PersistedStateProvider is bound in SharedModule, I guess I don't have much choice, do I?

Many thanks,

Steffen
Re: Connecting to a non-xtext language from Eclipse: Binding ResourceDescriptionStrategy [message #1778746 is a reply to message #1778737] Wed, 20 December 2017 17:27 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
this is why id prefer a non invasive change.

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Connecting to a non-xtext language from Eclipse: Binding ResourceDescriptionStrategy [message #1779252 is a reply to message #1778746] Thu, 04 January 2018 12:58 Go to previous message
Steffen Zschaler is currently offline Steffen ZschalerFriend
Messages: 266
Registered: July 2009
Senior Member
Thanks, Christian, and Happy New Year all.

Do you have any suggestions for how I might do this non-invasively?

Many thanks,

Steffen
Previous Topic:How to parse a normal text file for unit test
Next Topic:Consistent Datatype for values
Goto Forum:
  


Current Time: Thu Mar 28 22:21:12 GMT 2024

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

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

Back to the top