Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Error when reloading resource from a context menu button before xtend 2 generation
Error when reloading resource from a context menu button before xtend 2 generation [message #1014300] Mon, 25 February 2013 14:50 Go to next message
Fabian G. is currently offline Fabian G.Friend
Messages: 60
Registered: May 2010
Location: Christchurch (NZ)
Member
Hi,

I'm having a strange issue with my dsl model. I have created a context menu button to generate a text file from a model and it seems I'm retrieving an object with the wrong subtype.

I have a grammar using a external metamodel, where the attribute "name" is defined in a supertype element. Here's a simplified example:

Element :
  SubElement1 | SubElement2 | SubElement3;

SomeRule :
  name="ID" type=[Element | FQN];

FQN :
  ID ('.'ID)*


within xtext APIs, no problem to retrieve the right element subtype. In the button handler (and in the xtend class), the type of the attribute "type" is always resolved as the first one in the Element subtype list (SubElement1, even if it is a SubElement2 ou SubElement3). I tested by removing the first in the list, regenerate xtext artifacts, and then it again resolve to the new first one (so here SubElement2).

Here is the code in the handler where I reload the model (in fact, I reload a model referenced in the selected file, maybe the problem is in the reloading process there...):

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {

	Shell shell = HandlerUtil.getActiveShell(event);
	ISelection sel = HandlerUtil.getActiveMenuSelection(event);
	IStructuredSelection selection = (IStructuredSelection) sel;
	Object object = selection.getFirstElement();
	if (object instanceof IFile && ((IFile) object).getFileExtension().equals("dadt")) {
		// get selected file
		IFile file = (IFile) object;
		XtextResourceSet resourceSet = (XtextResourceSet) injector.getInstance(XtextResourceSetProvider.class).get(file.getProject());
		resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);
		// retrieve corresponding dad model
		Resource resource = resourceSet.getResource(URI.createURI(file.getLocationURI().toString()), true);
		DADTPackage model = (DADTPackage) resource.getContents().get(0);

		String dadtfile = file.getLocationURI().toString();
		URI dadfile = URI.createURI(dadtfile.substring(0, dadtfile.lastIndexOf("/") + 1) + model.getDadmodel().substring(model.getDadmodel().lastIndexOf(".") + 1) + ".dad");

		// load the file as a resource
		resource = resourceSet.getResource(dadfile, true);
		if (resource != null) {
			// some more work in here...

			// generate new file
			generator.doGenerate(resource, fsa);

		}
	} 
	return null;
}


Can someone help me?
Thanks a lot,

Fabian
Re: Error when reloading resource from a context menu button before xtend 2 generation [message #1014311 is a reply to message #1014300] Mon, 25 February 2013 15:16 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

use an IResourceSetProvider to obtain the resource set.
otherwhise you will get linking errors as you see them
have a look at http://christiandietrich.wordpress.com/2011/10/15/xtext-calling-the-generator-from-a-context-menu/
for an example


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Error when reloading resource from a context menu button before xtend 2 generation [message #1014323 is a reply to message #1014311] Mon, 25 February 2013 15:37 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
P.S: maybe your problem is the resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Error when reloading resource from a context menu button before xtend 2 generation [message #1014333 is a reply to message #1014323] Mon, 25 February 2013 15:52 Go to previous message
Fabian G. is currently offline Fabian G.Friend
Messages: 60
Registered: May 2010
Location: Christchurch (NZ)
Member
Changing by (injected) IResourceSetProvider did not change anything.

But using URI.createPlatformResourceURI instead of URI.createURI did the trick (no idea why such a strange behaviour comes from this, maybe you know and I would be happy to understand the reasons, just for the sake of my curiosity).

Anyway, thanks a lot Christian!
Previous Topic:xtext-dsl -> enrich-model -> xtend2-template?
Next Topic:Adding a toolbar item to an xtext editor (org.eclipse.ui.editorActions is depcreated in 4.2 eclipse
Goto Forum:
  


Current Time: Thu Apr 25 08:46:02 GMT 2024

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

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

Back to the top