Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » XtextResource couldn't resolve references to an external Ecore model
XtextResource couldn't resolve references to an external Ecore model [message #820714] Wed, 14 March 2012 09:34 Go to next message
Huy Tran is currently offline Huy Tran
Messages: 7
Registered: December 2011
Junior Member
Hi all,

I followed Christian's guidance from h**p://christiandietrich.wordpress.com/2011/07/17/xtext-2-0-and-uml (many thanks, Christian) to develop a DSL that consists of references to an Ecore model.

The Domain Ecore model (for the readability, I'm using Emfatic syntax).
@namespace(uri="hxxp://domain", prefix="domain")
package domain;
class Model {
 attr String name;
 val A refA;
}
class A {
 attr String name;
}

The Domain Xtext DSL:
Model: 'module' name=FQN imports+=Import* crossReference+=CrossReference*;

CrossReference: name=ID 'references to' referenced+=[domain::A|FQN];

FQN returns ecore::EString: ID ("." ID)* ;

FqnWildcard: FQN '.*'? ;

Import: 'import' importedNamespace = FqnWildcard ;


Then I created simple instances of those. The instance of the Domain Ecore model (My.domaind) is

<domain:Model ... name="My">
  <refA name="anInstanceOfA"/>
</domain:Model>


and the instance of the DSL (My.domaindsl):
module mydomain
referenceToAnInstanceOfA references to My.anInstanceOfA


The Xtext editor of the Domain DSL can auto-complete the cross-reference to "My.anInstanceOfA" (with the help of an additional "IResourceServiceProvider" as mentioned in Christian's article), so I'm supposed there are no issues with the DSL editors. Unfortunately, I've got a problem when trying to load the aforementioned Xtext DSL in a simple standalone Java program:
String dslPath = "My.domaindsl";
String modelPath = "My.domain";
DomainDslStandaloneSetup setup = new DomainDslStandaloneSetup();
Injector injector = setup.createInjectorAndDoEMFRegistration();
XtextResourceSet set = injector.getInstance(XtextResourceSet.class);
set.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);
set.getPackageRegistry().put(DomainPackage.eNS_URI, DomainPackage.eINSTANCE); 
		set.getResourceFactoryRegistry().getExtensionToFactoryMap().put("domain", new XMIResourceFactoryImpl());
Resource dslResource = set.getResource(URI.createFileURI(new File(dslPath).getAbsolutePath()), true);
Resource modelResource = set.getResource(URI.createFileURI(new File(modelPath).getAbsolutePath()), true);
if (dslResource != null)
{
  dslResource.load(Collections.EMPTY_MAP);
  if (dslResource.getErrors() != null)
  {
    for (Diagnostic d : dslResource.getErrors())
       System.err.println(d.getMessage());
  }
}


The aforementioned code showed the error "Couldn't resolve reference to A 'My.anInstanceOfA'". When I printed out dslResource.getContents().get(0), the result is:
<?xml version="1.0" encoding="UTF-8"?>
<domainDsl:Model xmi:version="2.0" ... name="mydomain">
  <crossReference name="referenceToAnInstanceOfA">
   <referenced href="file:/.../domain-test/My.domaindsl#xtextLink_::0.2.0::1::/4"/>
  </crossReference>
</domainDsl:Model>


My question is: What should be added to the Java code in order to properly resolve the cross-references?

Thank you very much in advance for your time and support.
Huy

[Updated on: Wed, 14 March 2012 09:35]

Report message to a moderator

Re: XtextResource couldn't resolve references to an external Ecore model [message #820721 is a reply to message #820714] Wed, 14 March 2012 09:40 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6127
Registered: July 2009
Senior Member
Hi this is what the support class I describe is for
Re: XtextResource couldn't resolve references to an external Ecore model [message #820729 is a reply to message #820721] Wed, 14 March 2012 09:51 Go to previous messageGo to next message
Huy Tran is currently offline Huy Tran
Messages: 7
Registered: December 2011
Junior Member
Thanks Christian for your promptly response. As I'm not mistaken, the classes you mentioned in the article are for supporting the Xtext DSL editor (i.e., the Domain DSL Editor). In my case, I tried to load the My.domaindsl using XtextResource. Could you please explain a bit how I can use your approach here.

Thanks.
Huy
PS. I had found your other related posts recommending to load all related models (e.g., My.domain) using EMF Resource. That's why in the code there is an additional part for loading the Domain Ecore. That still does not help though.
Re: XtextResource couldn't resolve references to an external Ecore model [message #820738 is a reply to message #820729] Wed, 14 March 2012 09:58 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6127
Registered: July 2009
Senior Member
Sorry if my answer was miss leading I am talking about the class
UmlSupport
Re: XtextResource couldn't resolve references to an external Ecore model [message #820809 is a reply to message #820738] Wed, 14 March 2012 11:40 Go to previous messageGo to next message
Huy Tran is currently offline Huy Tran
Messages: 7
Registered: December 2011
Junior Member
Many thanks, Christian. It seems to work with MWE/MWE2. As I'm using XtendFacade to invoke an Xtend function directly in Java, do you have an idea how can I use the class XxxSupport. Or in general, can I use XxxSupport outside the context of MWE/MWE2 workflows?
Huy

[Updated on: Wed, 14 March 2012 11:40]

Report message to a moderator

Re: XtextResource couldn't resolve references to an external Ecore model [message #820822 is a reply to message #820809] Wed, 14 March 2012 11:58 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6127
Registered: July 2009
Senior Member
there is some other stuff that should do the same e.g. calling
register services .
But: make sure you are really standalone
Re: XtextResource couldn't resolve references to an external Ecore model [message #821575 is a reply to message #820822] Thu, 15 March 2012 10:53 Go to previous messageGo to next message
Huy Tran is currently offline Huy Tran
Messages: 7
Registered: December 2011
Junior Member
Thanks Christian for your info. I've just figured out a way to make it work after some trial&errors. This is just a quick-and-dirty solution - I include here in case anyone else are also interested in. What I tried is to use the same XtextResourceSet to load the related Ecore models. These models (Xtext DSL and Ecore models) then can be fed to Xtend functions via XtendFacade.

public static List<EObject> load(String dslPath, String referencedModelPath)
{
  if (path == null || path.isEmpty())
    return null;
  try
  {
    Injector injector = new DomainDSLStandaloneSetup().createInjectorAndDoEMFRegistration();
    XtextResourceSet set = injector.getInstance(XtextResourceSet.class);
    set.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);

    Resource resource = set.getResource(URI.createFileURI(new File(dslPath).getAbsolutePath()), true);

    resource.load(Collections.EMPTY_MAP);
    if (resource != null)
    {
      if (referencedModelPath != null)
      {
        set.getPackageRegistry().put(DomainPackage.eNS_URI, DomainPackage.eINSTANCE);
        set.getResourceFactoryRegistry().getExtensionToFactoryMap().put(DomainPackage.eNAME, new XMIResourceFactoryImpl());
        Resource r = set.getResource(URI.createFileURI(new File(referencedModelPath).getAbsolutePath()), true);
        r.load(Collections.EMPTY_MAP);
        resource.getContents().addAll(r.getContents());
      }
      return resource.getContents();
   }
  } catch (Exception e)
  {
    e.printStackTrace();
  }
  return null;
}


Hope this helps.
Huy
Re: XtextResource couldn't resolve references to an external Ecore model [message #821638 is a reply to message #821575] Thu, 15 March 2012 12:26 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6127
Registered: July 2009
Senior Member
Hi,

this is not the "clean" solution.
the main problem/feature is: the global scope is built by the resourceset if you are in standalone mode.
=> you have to load the model resource and all referenced models (xtext based or not xtext based)
to the resourceset. second: only resources that have a resourceserviceprovider registered.
thus you have to execute XXXSupport().registerServices() to get it actually working too

~Christian
Re: XtextResource couldn't resolve references to an external Ecore model [message #821656 is a reply to message #821638] Thu, 15 March 2012 12:54 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6127
Registered: July 2009
Senior Member
Just be complete: here a sample snippet

public class Test {

	public static void main(String[] args) throws IOException {
		// register the testpackage to ecore
		EPackage.Registry.INSTANCE.put(TestPackage.eNS_URI, TestPackage.eINSTANCE);
		Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("test", new XMIResourceFactoryImpl());
		// register the resourceserviceprovider
		new TestSupport().registerServices(true);
		// register the dsl
		Injector injector = new MyDslStandaloneSetup().createInjectorAndDoEMFRegistration();
		// load all model file to the resourceset
		ResourceSet rs = injector.getInstance(ResourceSet.class);
		Resource r1 = rs.getResource(URI.createURI("src/test.test"), true);
		Resource r2 = rs.getResource(URI.createURI("src/test.mydsl"), true);
		// work with the resources (the elements come from test.test)
		r2.load(null);
		for (ElementRef ref : ((Model)r2.getContents().get(0)).getRef()) {
			System.out.println(ref.getElement().getName());
		}

	}

}
Re: XtextResource couldn't resolve references to an external Ecore model [message #822330 is a reply to message #821656] Fri, 16 March 2012 09:31 Go to previous message
Huy Tran is currently offline Huy Tran
Messages: 7
Registered: December 2011
Junior Member
Thanks for the great suggestion, Christian. I greatly appreciate your helps.

MfG, Huy.
Previous Topic:Create own notice elements
Next Topic:Exporting Xtext Project to Plugin
Goto Forum:
  


Current Time: Thu Jul 24 06:59:06 EDT 2014

Powered by FUDForum. Page generated in 0.01884 seconds