Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Parsing from a tree(problem with unresolved EProxies )
Parsing from a tree [message #1777338] Tue, 28 November 2017 15:46 Go to next message
Nicolas Hili is currently offline Nicolas HiliFriend
Messages: 40
Registered: March 2017
Member
Hi,

I am trying to parse a String to get the corresponding AST in Xtext. To do so, I followed this example: https://wiki.eclipse.org/Xtext/FAQ#How_do_I_load_my_model_in_a_standalone_Java_application.C2.A0.3F

However, my grammar refers to the UML metamodel and therefore, I have cross-references between the model that is parsed by my Xtext grammar and some model elements defined in a UML resource. To support the cross-referencing when parsing my String, I tried the following:

MyDSLActivator activator = MyDSLActivator.getInstance();
						Injector injector = activator.getInjector(MyDSLActivator.MyDSL_MyDSL);
						
						XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class);
						resourceSet.getResource(uri, true); // uri is the URI of the UML resource
						Resource resource = resourceSet.createResource(URI.createURI("dummy:/example.mydsl")); // I also create a dummy resource for my parsed model
						
						InputStream in = new ByteArrayInputStream(myStringToParse.getBytes());
						try {
							resource.load(in, resourceSet.getLoadOptions());
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						Action model = (Action) resource.getContents().get(0);


I am able to parse my string but all references to UML elements are proxies. I tried to add the following after the last line:

EcoreUtil2.resolveAll(resource);


But it does not seem to resolve any proxy references. Did I miss something? I would have thought that if both my UML resource and my MyDSL resource (that refers to the first one) are in the same Xtext resource set, proxies would be automatically resolved.

Thanks in advance for your answer,

Nicolas
Re: Parsing from a tree [message #1777340 is a reply to message #1777338] Tue, 28 November 2017 15:59 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7658
Registered: July 2009
Senior Member
Hi

You MUST use UMLResourcesUtil.init(resourceSet) to initialize UML for standalone usage.

Regards

Ed Willink
Re: Parsing from a tree [message #1777345 is a reply to message #1777340] Tue, 28 November 2017 16:26 Go to previous messageGo to next message
Karsten Thoms is currently offline Karsten ThomsFriend
Messages: 762
Registered: July 2009
Location: Dortmund, Germany
Senior Member

Also consider https://www.google.de/amp/s/christiandietrich.wordpress.com/2011/07/17/xtext-2-0-and-uml/amp/
Re: Parsing from a tree [message #1777369 is a reply to message #1777345] Tue, 28 November 2017 22:56 Go to previous messageGo to next message
Nicolas Hili is currently offline Nicolas HiliFriend
Messages: 40
Registered: March 2017
Member
Hi,

Thanks both for your answers,

I have already followed Christian's blog and my UML elements are already exported. I am already capable of parsing with an StyledTextXtextAdaptor:

styledText = new StyledText(parent, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL);
		styledText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		styledText.setFont(JFaceResources.getTextFont());
		styledText.layout();

		// adapt to xtext
		xtextAdapter = new StyledTextXtextAdapter(injector, IXtextFakeContextResourcesProvider.NULL_CONTEXT_PROVIDER);
		xtextAdapter.adapt(styledText);

		styledTextObservable = new StyledTextObservableValue(styledText, null, SWT.FocusOut);

		styledTextObservable.addChangeListener(new IChangeListener() {

			@Override
			public void handleChange(ChangeEvent event) {

				IParseResult xtextParseResult = xtextAdapter.getXtextParseResult();
				Action root = (Action)xtextParseResult.getRootASTElement();
}


Interestingly, the code above works and all references are solved. However, when trying to parse in standalone following this link https://wiki.eclipse.org/Xtext/FAQ#How_do_I_load_my_model_in_a_standalone_Java_application.C2.A0.3F, it does not resolve the references.

I tried Ed's solution and added UMLResourcesUtil.init(resourceSet), but it does not change anything,

Nicolas
Re: Parsing from a tree [message #1777384 is a reply to message #1777369] Wed, 29 November 2017 05:34 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14677
Registered: July 2009
Senior Member
Are you standalone or are you not. Are you standalone but keep the application running. Do you have multiple files involved. Resolveall is bad and you need to add all,files . I miss some context here. Do you call umlstandalonesetup?

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Parsing from a tree [message #1777457 is a reply to message #1777384] Wed, 29 November 2017 13:56 Go to previous messageGo to next message
Nicolas Hili is currently offline Nicolas HiliFriend
Messages: 40
Registered: March 2017
Member
I am executing Xtext inside Eclipse (not in standalone). I am not calling UMLStandaloneSetup. I have defined an Xtext grammar where some rules refer to the UML metamodel. All the UML elements I am referring to are contained into a single UML file that is added to the same resource set as my Xtext file. I checked and the resource is correctly loaded in my resource set (with umlResource.isLoaded()). I use Xtext at two different places:



  1. Within a StyledText: it works fine and all references are resolved. In my previous message, the line
    Action root = (Action)xtextParseResult.getRootASTElement();
    returns the expected result (with all references resolved)
  2. Without a StyledText: it does not work fine. I have a String as input and use the code here: https://wiki.eclipse.org/Xtext/FAQ#How_do_I_load_my_model_in_a_standalone_Java_application.C2.A0.3F However, as I am running in Eclipse, I have not copied the two lines:
     new org.eclipse.emf.mwe.utils.StandaloneSetup().setPlatformUri("../");
     Injector injector = new MyDslStandaloneSetup().createInjectorAndDoEMFRegistration();
     
    Because I have already the injector via the plugin org.eclipse.xtext.example.mydsl.ui that is generated with Xtext. In the first message, the line
    Action model = (Action) resource.getContents().get(0);
    returns my model with the references that are not resolved.

Re: Parsing from a tree [message #1777459 is a reply to message #1777457] Wed, 29 November 2017 14:01 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14677
Registered: July 2009
Senior Member
still no sure what you are doing

new MyDslStandaloneSetup().createInjectorAndDoEMFRegistration();

is BAAAAAAAAD in eclipse
same for org.eclipse.emf.mwe.utils.StandaloneSetup()

so can you explain what you actually try to do.


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Parsing from a tree [message #1777469 is a reply to message #1777459] Wed, 29 November 2017 14:34 Go to previous messageGo to next message
Nicolas Hili is currently offline Nicolas HiliFriend
Messages: 40
Registered: March 2017
Member
I am not sure how I can make it clearer...

I am *not* using MyDslStandaloneSetup (cf my first message).

I am inside Eclipse, I already have my injector:
Injector injector = activator.getInjector(MyDSLActivator.MyDSL_MyDSL);


Still in Eclipse, I have a String to parse and I want to get my object tree. I use the following code:
InputStream in = new ByteArrayInputStream(myStringToParse.getBytes());
						try {
							resource.load(in, resourceSet.getLoadOptions());
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						Action model = (Action) resource.getContents().get(0);


I took it from the following link: https://wiki.eclipse.org/Xtext/FAQ#How_do_I_load_my_model_in_a_standalone_Java_application.C2.A0.3F even though in the link, it is said that it is for loading a model in a standalone application (which I am not doing). However, I could not find any other links that explain how to load a model from a String within Eclipse, so I adapted this code.

As a prerequisite, as my grammar refers to UML elements defined in another UML file (I have already exported the UML element as explained in your blog), I create a XtextResourceSet containing both a resource containing the String to parse (cf. the code above) and my UML file:

Injector injector = activator.getInjector(MyDSLActivator.MyDSL_MyDSL);
XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class);
resourceSet.getResource(uri, true); // uri is the URI of the UML resource
Resource resource = resourceSet.createResource(URI.createURI("dummy:/example.mydsl"));


I also added the line
UMLResourcesUtil.init(resourceSet)
as I was told by Ed that I must do it. I also try other stuff, such as:
resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);


But still, when getting the model from my parsed String:
Action model = (Action) resource.getContents().get(0);

References are unresolved.

I was assuming the scenario is quite common:
Quote:
I am inside Eclipse and I want to parse a String. That's is. Nothing more.


The only specificities (that causes me these issues) is that my String to parse contains cross-references to a single UML file. But I have already made steps to ensure that 1) the UML resource is loaded in the same resource set, and 2) the "resolved all" option of the resource set is loaded. Besides, I have an example where I can do it in a StyledText. So cross-references from my Xtext resource to my UML resource work.

Really, I do not think how to make it clearer. The problem seems to be quite simple. I am still surprised that there is no code snippet showing how to parse a String with Xtext within Eclipse and that the only resource mentioned how to do it outside (as a standalone application).

Nicolas
Re: Parsing from a tree [message #1777471 is a reply to message #1777469] Wed, 29 November 2017 14:42 Go to previous messageGo to next message
Karsten Thoms is currently offline Karsten ThomsFriend
Messages: 762
Registered: July 2009
Location: Dortmund, Germany
Senior Member

If you
a) running inside Eclipse
b) have all resources in one ResourceSet (your string content resource, and all UML resources you are dependent on, including transitive)
c) have UML elements in the index
it should work out of the box.

To debug into this, you'd have to examine the evaluated scope in the error case. A starting point is DefaultLinkingService#getLinkedObjects. There set a breakpoint on "Collections.emptyList()". If you hit that, drop the stack frame and start over where getScope() is called. The scope needs to contain the name of your referenced element.
Re: Parsing from a tree [message #1777472 is a reply to message #1777469] Wed, 29 November 2017 14:45 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14677
Registered: July 2009
Senior Member
- dont use the resolve all loadoption


Injector injector = activator.getInjector(MyDSLActivator.MyDSL_MyDSL);
XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class);
resourceSet.getResource(uri, true); // uri is the URI of the UML resource
Resource resource = resourceSet.createResource(URI.createURI("dummy:/example.mydsl"));


instead: (pseudo code)

Injector injector = activator.getInjector(MyDSLActivator.MyDSL_MyDSL);
ResourceSet resourceSet = injector.getInstance(IResourceSetProvider.class).get(projectOfUml);
resourceSet.getResource(uri, true); // uri is the URI of the UML resource
Resource resource = resourceSet.createResource(URI.createURI("platform:/resource/project/(src)/example.mydsl"));
[/code]



Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Parsing from a tree [message #1777598 is a reply to message #1777471] Thu, 30 November 2017 16:27 Go to previous messageGo to next message
Nicolas Hili is currently offline Nicolas HiliFriend
Messages: 40
Registered: March 2017
Member
Thanks Karsten, Christian, for your answers,

Last Christian's answer works, by applying the modification, all the references are now resolved.

Karsten Thoms wrote on Wed, 29 November 2017 14:42
If you
b) have all resources in one ResourceSet (your string content resource, and all UML resources you are dependent on, including transitive)


That might explain it. I guess this is what happens when getting the resource set from the specified project. That would be worth adding this example to the Xtext FAQ.

Thanks again,

Nicolas
Re: Parsing from a tree [message #1777613 is a reply to message #1777598] Thu, 30 November 2017 18:36 Go to previous message
Karsten Thoms is currently offline Karsten ThomsFriend
Messages: 762
Registered: July 2009
Location: Dortmund, Germany
Senior Member

Good to hear that it finally works for you
Previous Topic:Xtend 2: M2T how to skip generation of expression result in output file
Next Topic:How to delete generated files
Goto Forum:
  


Current Time: Tue May 14 05:43:26 GMT 2024

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

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

Back to the top