Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Standalone application load cross reference (Cross references are left unresolved as eproxy)
Standalone application load cross reference [message #1854392] Wed, 17 August 2022 10:48 Go to next message
jo ber is currently offline jo berFriend
Messages: 86
Registered: August 2018
Member
Hi

if needed, see also example in attachment.

In the example, there are two languages: 'define' and 'use'. the 'use' language has reference on some of the type define d in the 'define' language.

In a standalone application, below, I'm loading an instance of the 'use' language [1]. When checking the value of the referenced object [2], they are 'null' and the object are marked as 'eProxy'. What am I missing when loading my resources,. Hints would be helpful.

Thank you and kind regards

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.xtext.resource.IResourceFactory;
import org.eclipse.xtext.resource.IResourceServiceProvider;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.resource.XtextResourceSet;
import org.xtext.example.definedsl.defineDsl.DefineDslPackage;
import org.xtext.example.usedsl.UseDslStandaloneSetupGenerated;
import org.xtext.example.usedsl.useDsl.Model;
import org.xtext.example.usedsl.useDsl.Usage;

import com.google.inject.Injector;

public class Main {

	public static void main(String[] args) {
		Injector injector = new UseDslStandaloneSetupGenerated().createInjectorAndDoEMFRegistration();
		XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class);
		resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);

		IResourceFactory resourceFactory = injector.getInstance(IResourceFactory.class);
		IResourceServiceProvider serviceProvider = injector.getInstance(IResourceServiceProvider.class);

		Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("define", resourceFactory);
		IResourceServiceProvider.Registry.INSTANCE.getExtensionToFactoryMap().put("define", serviceProvider);

		DefineDslPackage.eINSTANCE.eClass();

		Resource r1 = resourceSet.getResource(URI.createFileURI("../use-definition/src/b.use"), true);       [1]
		Resource r2 = resourceSet.getResource(URI.createFileURI("../use-definition/src/a.define"), true);

		Model model = (Model) r1.getContents().get(0);
		
		for(Usage usage : model.getUses()) {
			System.out.println(usage.getName().getName() + " -- " + usage.getName().getValue());     [2]
		}

	}
}
  • Attachment: example.zip
    (Size: 502.63KB, Downloaded 9 times)
Re: Standalone application load cross reference [message #1854396 is a reply to message #1854392] Wed, 17 August 2022 11:04 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14349
Registered: July 2009
Senior Member
simply not using OPTIONS_RESOLVE_ALL should do the trick.
you can call EcoreUtil.resolveAll(resouceset) once you have loaded all files


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Wed, 17 August 2022 11:04]

Report message to a moderator

Re: Standalone application load cross reference [message #1854397 is a reply to message #1854396] Wed, 17 August 2022 11:16 Go to previous messageGo to next message
jo ber is currently offline jo berFriend
Messages: 86
Registered: August 2018
Member
thank you for the quick reply, but unfortunately, it does not solve my question.

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.xtext.resource.IResourceFactory;
import org.eclipse.xtext.resource.IResourceServiceProvider;
import org.eclipse.xtext.resource.XtextResourceSet;
import org.xtext.example.definedsl.defineDsl.DefineDslPackage;
import org.xtext.example.usedsl.UseDslStandaloneSetupGenerated;
import org.xtext.example.usedsl.useDsl.Model;
import org.xtext.example.usedsl.useDsl.Usage;

import com.google.inject.Injector;

public class Main {

	public static void main(String[] args) throws Exception {
		Injector injector = new UseDslStandaloneSetupGenerated().createInjectorAndDoEMFRegistration();
		XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class);

		IResourceFactory resourceFactory = injector.getInstance(IResourceFactory.class);
		IResourceServiceProvider serviceProvider = injector.getInstance(IResourceServiceProvider.class);

		Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("define", resourceFactory);
		IResourceServiceProvider.Registry.INSTANCE.getExtensionToFactoryMap().put("define", serviceProvider);

		DefineDslPackage.eINSTANCE.eClass();

		Resource r1 = resourceSet.getResource(URI.createFileURI("../use-definition/src/b.use"), true);
		Resource r2 = resourceSet.getResource(URI.createFileURI("../use-definition/src/a.define"), true);

		r1.load(null); // should not be necessary 
		r2.load(null); // should not be necessary

		EcoreUtil.resolveAll(resourceSet);
		
		Model model = (Model) r1.getContents().get(0);
		
		for(Usage usage : model.getUses()) {
			
			System.out.println(usage.getName().getName() + " -- " + usage.getName().getValue());
		}

	}
}
Re: Standalone application load cross reference [message #1854402 is a reply to message #1854397] Wed, 17 August 2022 11:45 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14349
Registered: July 2009
Senior Member
you shoudl also remove

Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("define", resourceFactory);
IResourceServiceProvider.Registry.INSTANCE.getExtensionToFactoryMap().put("define", serviceProvider);

DefineDslPackage.eINSTANCE.eClass();

and do

DefineDslStandaloneSetup.doSetup()
Injector injector = new UseDslStandaloneSetupGenerated().createInjectorAndDoEMFRegistration();

instead


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Standalone application load cross reference [message #1854403 is a reply to message #1854397] Wed, 17 August 2022 11:49 Go to previous messageGo to next message
Ed Merks is currently online Ed MerksFriend
Messages: 32493
Registered: July 2009
Senior Member
It's highly unlikely that a URI.createFileURI("../use-definition/src/b.use") will work correctly for resolving references within that resource. Try URI.createFileURI(new File("../use-definition/src/b.use").getAbsolutePath()). A resource should pretty much always be loaded with an absolute URI (the URI has a scheme) and that URI should have an absolute path (so for file: should started with a drive letter on windows or with a / on mac/linux).

Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Standalone application load cross reference [message #1854404 is a reply to message #1854402] Wed, 17 August 2022 11:50 Go to previous message
jo ber is currently offline jo berFriend
Messages: 86
Registered: August 2018
Member
thank you very much
this solve my problem:

DefineDslStandaloneSetup.doSetup()
Previous Topic:Use external Metamodel as library
Next Topic:Is there a way to autoformat DSL files using the command line?
Goto Forum:
  


Current Time: Mon Sep 26 13:12:42 GMT 2022

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

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

Back to the top