Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Not quite understanding how x-referencing works
Not quite understanding how x-referencing works [message #1456947] Thu, 30 October 2014 22:06 Go to next message
Arsenij Solovjev is currently offline Arsenij SolovjevFriend
Messages: 9
Registered: October 2014
Junior Member
Hi,

I'm new to Xtext (and compilers apparently) and I am trying to parse a DSL with the following relevant piece of grammar:
Architecture:
	(components+=Component)+
	(connectors+=Connector)*;

Component:
	'component' name=ID ':' namespace=PackageName;

Connector:
	from=[Component|ID]( '=>' to=[Component|ID]);


The files written in this DSL parse fine, with the exception of Connector.from and Connector.to not being resolved. In other words
architecture.getConnectors().get(0).getFrom()

returns null!

All this is done in a standalone (non-Eclipse) application. To parse I inject org.eclipse.xtext.parser.IParser and I set it up with createInjectorAndDoEMFRegistration() and injectMembers(), the exact same way as it is done in Approach 1 in Dave Hoffman's blog (the forum won't let me post the hyperlink, sorry :/ ).

Am I missing something really obvious here? How come references from within the same file aren't resolved? Can anyone please help?

Re: Not quite understanding how x-referencing works [message #1457843 is a reply to message #1456947] Fri, 31 October 2014 19:27 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14669
Registered: July 2009
Senior Member
Hi,

if you just "parse" no wonder cross refs dont work. using iparser is not the standard usecase.
have a look at https://wiki.eclipse.org/Xtext/FAQ#How_do_I_load_my_model_in_a_standalone_Java_application.C2.A0.3F
how the ususal way of reading models is


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Not quite understanding how x-referencing works [message #1460404 is a reply to message #1457843] Mon, 03 November 2014 14:34 Go to previous messageGo to next message
Arsenij Solovjev is currently offline Arsenij SolovjevFriend
Messages: 9
Registered: October 2014
Junior Member
Thanks for the reply Christian, however that doesn't seem to work.

This is what I tried to do:
new org.eclipse.emf.mwe.utils.StandaloneSetup().setPlatformUri("../");
Injector injector = new ArchnemesisStandaloneSetup().createInjectorAndDoEMFRegistration();
resourceSet = injector.getInstance(XtextResourceSet.class);
resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);

...

Resource resource = resourceSet.createResource(URI.createURI("dummy:/inmemory.chralx"));
resource.load(in, resourceSet.getLoadOptions());
linker.linkModel(resource.getContents().get(0), consumer);
return resource.getContents().get(0);


The reference
architecture.getConnectors().get(0).getFrom()

is still null though :/

EDIT architecture.getConnectors().get(0) isn't null

[Updated on: Mon, 03 November 2014 14:53]

Report message to a moderator

Re: Not quite understanding how x-referencing works [message #1460419 is a reply to message #1460404] Mon, 03 November 2014 14:50 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14669
Registered: July 2009
Senior Member
Hi, do you have serveral model files? if so dont use the resolve all options. load all model files. then call EcoreUtil.resolveall


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Not quite understanding how x-referencing works [message #1460437 is a reply to message #1460419] Mon, 03 November 2014 15:12 Go to previous messageGo to next message
Arsenij Solovjev is currently offline Arsenij SolovjevFriend
Messages: 9
Registered: October 2014
Junior Member
I have only one model file, the architecture description. Maybe a little background, I'm trying to use Xtext within PMD (static analysis tool).
Without getting too technical PMD goes through each source-file in a given Java project and calls the method which then uses Xtext. Currently
I call all the code posted here for each source-file, but that's rather a "work-in-progress" issue, Ideally the model will be loaded once, but I'm
not there yet.

I now called resolveAll on both the RootASTElement and the whole ResourceSet separately. It's still not resolving :/

Should I post some more code?
Re: Not quite understanding how x-referencing works [message #1460441 is a reply to message #1460437] Mon, 03 November 2014 15:18 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14669
Registered: July 2009
Senior Member
Hi,

can you share a reduced sample grammar, model file and the main class that loads the model?


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Not quite understanding how x-referencing works [message #1460475 is a reply to message #1460441] Mon, 03 November 2014 16:00 Go to previous messageGo to next message
Arsenij Solovjev is currently offline Arsenij SolovjevFriend
Messages: 9
Registered: October 2014
Junior Member
Gladly. In case of confusion I have two parse() methods and I'm only using parse2().

Grammar:

grammar de.fu_berlin.inf.archnemesis.Archnemesis with org.eclipse.xtext.common.Terminals

generate archnemesis "http //www.fu_berlin.de/inf/archnemesis/Archnemesis"

Architecture:
	rootComponent=RootComponent
	(components+=Component)+
	(connectors+=Connector)*;

Element:
	Component | Connector | Constraint;

RootComponent:
	'root' name=ID ':' namespace=PackageName;

Component:
	'component' name=ID ':' namespace=PackageName;

Connector:
	from=[Component] '=>' to=[Component]
;


Model File:
root saros: de.fu_berlin.dpp

component session_management: de.fu_berlin.dpp.project

component operational_transformation: de.fu_berlin.dpp.concurrent

component network: de.fu_berlin.dpp.net 

session_management => operational_transformation
//session_management => network
//invitation => network


Main class:
	public class XtextParser {

		// @Inject
		// private IParser parser;
		//
		// @Inject
		// private ILinker linker;

		// @Inject
		private XtextResourceSet resourceSet;

		public XtextParser() {
			setupParser();
		}

		private void setupParser() {

			new org.eclipse.emf.mwe.utils.StandaloneSetup()
					.setPlatformUri("../");
			Injector injector = new ArchnemesisStandaloneSetup()
					.createInjectorAndDoEMFRegistration();
			resourceSet = injector.getInstance(XtextResourceSet.class);
		}

		/**
		 * Parses data provided by an input reader using Xtext and returns the
		 * root node of the resulting object tree.
		 * 
		 * @param reader
		 *            Input reader
		 * @return root object node
		 * @throws IOException
		 *             when errors occur during the parsing process
		 */
		public EObject parse(Reader reader) throws IOException {
			IParseResult result = parser.parse(reader);

			if (result.hasSyntaxErrors()) {
				throw new ParseException(
						"Provided input contains syntax errors.");
			}

			return result.getRootASTElement();
		}

		public EObject parse2(InputStream in) throws IOException {
			Resource resource = resourceSet.createResource(URI
					.createURI("dummy:/inmemory.chralx"));
			// EcoreUtil.resolveAll(resourceSet);
			resource.load(in, resourceSet.getLoadOptions());
			IDiagnosticConsumer consumer = new IDiagnosticConsumer() {

				@Override
				public void consume(Diagnostic diagnostic, Severity severity) {
					// TODO Auto-generated method stub

				}

				@Override
				public boolean hasConsumedDiagnostics(Severity severity) {
					// TODO Auto-generated method stub
					return true;
				}

			};

			EcoreUtil.resolveAll(resourceSet);
			return resource.getContents().get(0);
		}
	}

[Updated on: Mon, 03 November 2014 16:01]

Report message to a moderator

Re: Not quite understanding how x-referencing works [message #1460486 is a reply to message #1460475] Mon, 03 November 2014 16:13 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14669
Registered: July 2009
Senior Member
Hi,

unfortunately your grammar ist not complete

i asume the following one

Architecture:
	rootComponent=RootComponent
	(components+=Component)+
	(connectors+=Connector)*;

Element:
	Component | Connector;

RootComponent:
	'root' name=ID ':' namespace=PackageName;

Component:
	'component' name=ID ':' namespace=PackageName;

Connector:
	from=[Component] '=>' to=[Component]
;

PackageName:
	ID ("." ID)*
;


then the follonging test works fine for me

package org.xtext.example.mydsl2.tests

import org.eclipse.emf.common.util.URI
import org.eclipse.emf.ecore.resource.ResourceSet
import org.junit.Test
import org.xtext.example.mydsl2.MyDslStandaloneSetup
import org.xtext.example.mydsl2.myDsl.Architecture

class SampleTest {
	
	@Test
	def void testIt() {
		val i = new MyDslStandaloneSetup().createInjectorAndDoEMFRegistration()
		val rs = i.getInstance(ResourceSet)
		val r = rs.getResource(URI.createURI("test.mydsl2"), true)
		r.load(null)
		val root = r.contents.get(0)
		if (root instanceof Architecture) {
			println('''
			«FOR c : root.connectors»
			«c.from.namespace» «c.to.namespace»
			«ENDFOR»
			''')
		}
	}
	
}


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Not quite understanding how x-referencing works [message #1460495 is a reply to message #1460486] Mon, 03 November 2014 16:21 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14669
Registered: July 2009
Senior Member
or to be a biote more xtendish

@RunWith(XtextRunner)
@InjectWith(MyDslInjectorProvider)
class SampleTest {
	
	@Inject extension ParseHelper<Architecture>
	
	@Test
	def void testIt() {
		val a = '''
		root saros: de.fu_berlin.dpp

component session_management: de.fu_berlin.dpp.project

component operational_transformation: de.fu_berlin.dpp.concurrent

component network: de.fu_berlin.dpp.net 

session_management => operational_transformation
//session_management => network
//invitation => network'''.parse
			println('''
			«FOR c : a.connectors»
			«c.from.namespace» «c.to.namespace»
			«ENDFOR»
			''')
		}
	
	
}


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Not quite understanding how x-referencing works [message #1460496 is a reply to message #1460486] Mon, 03 November 2014 16:22 Go to previous messageGo to next message
Arsenij Solovjev is currently offline Arsenij SolovjevFriend
Messages: 9
Registered: October 2014
Junior Member
Ok, you are using getResource, I'll try that
Re: Not quite understanding how x-referencing works [message #1460504 is a reply to message #1460496] Mon, 03 November 2014 16:33 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14669
Registered: July 2009
Senior Member
Hi,

createresource and using a stream works as well


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Not quite understanding how x-referencing works [message #1460523 is a reply to message #1460496] Mon, 03 November 2014 16:57 Go to previous messageGo to next message
Arsenij Solovjev is currently offline Arsenij SolovjevFriend
Messages: 9
Registered: October 2014
Junior Member
Hmm, now it seems to work (using EcoreUtil.resolveAll), without me changing anything o.O

But I get the following: http://s4.postimg.org/j538aljpp/Workspace_1_019.png

From and To are there, but their features('name' and 'namespace') are not resolved.

If this doesn't work with Java, does it make sense to use Xtend?

[Updated on: Mon, 03 November 2014 17:09]

Report message to a moderator

Re: Not quite understanding how x-referencing works [message #1460563 is a reply to message #1460523] Mon, 03 November 2014 17:55 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14669
Registered: July 2009
Senior Member
looks like the proxys are not resolved yet. how/where do you call this code.

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Not quite understanding how x-referencing works [message #1460582 is a reply to message #1460563] Mon, 03 November 2014 18:24 Go to previous messageGo to next message
Arsenij Solovjev is currently offline Arsenij SolovjevFriend
Messages: 9
Registered: October 2014
Junior Member
This the parser client class (relevant excerpt)

        @Override
	public Object visit(ASTPackageDeclaration node, Object data) {

		File chralx = new File(getChralx((RuleContext) data));

		Architecture architecture = null;

		try {
			architecture = parseChralx2(new FileInputStream(chralx));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		Map<Component, Set<Component>> accessMap = new HashMap<Component, Set<Component>>();

		// init
		for (Component c : architecture.getComponents()) {
			accessMap.put(c, new HashSet<Component>());
		}
		// create mapping of component to allowed access
		for (Connector c : architecture.getConnectors()) {
			Component from = c.getFrom();
			Component to = c.getTo();
			EList<EObject> as = from.eCrossReferences();
			accessMap.get(from).add(to);



This is parseChralx2() from the snippet above

Architecture parseChralx2(InputStream chralx) throws IOException {
		XtextParser p = new XtextParser();
		return (Architecture) p.parse2(chralx);
	}


And the same snippet with EcoreUtil.resolveAll as above

public EObject parse2(InputStream in) throws IOException {
			Resource resource = resourceSet.createResource(URI
					.createURI("dummy:/inmemory.chralx"));
			// EcoreUtil.resolveAll(resourceSet);
			resource.load(in, resourceSet.getLoadOptions());
			IDiagnosticConsumer consumer = new IDiagnosticConsumer() {

				@Override
				public void consume(Diagnostic diagnostic, Severity severity) {
					// TODO Auto-generated method stub

				}

				@Override
				public boolean hasConsumedDiagnostics(Severity severity) {
					// TODO Auto-generated method stub
					return true;
				}

			};

			EcoreUtil.resolveAll(resourceSet);
			return resource.getContents().get(0);
		}

Re: Not quite understanding how x-referencing works [message #1460734 is a reply to message #1460582] Mon, 03 November 2014 22:13 Go to previous messageGo to next message
Arsenij Solovjev is currently offline Arsenij SolovjevFriend
Messages: 9
Registered: October 2014
Junior Member
Would it be possible to resolve the EProxyURI manually, akin to what Benoit tries here?
https://www.eclipse.org/forums/index.php/t/491525/
Re: Not quite understanding how x-referencing works [message #1460762 is a reply to message #1460734] Mon, 03 November 2014 22:52 Go to previous message
Arsenij Solovjev is currently offline Arsenij SolovjevFriend
Messages: 9
Registered: October 2014
Junior Member
I had forgotten to update my model file, that's why the references weren't resolved.
Thanks a ton for your time and sorry for my sketchy work here
Previous Topic:CfP: EclipseCon NA 2015
Next Topic:interactive command line tool sample
Goto Forum:
  


Current Time: Sat Apr 27 03:51:05 GMT 2024

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

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

Back to the top