Skip to main content


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 13:34 Go to next message
Huy Tran is currently offline Huy TranFriend
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 13: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 13:40 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi this is what the support class I describe is for

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: XtextResource couldn't resolve references to an external Ecore model [message #820729 is a reply to message #820721] Wed, 14 March 2012 13:51 Go to previous messageGo to next message
Huy Tran is currently offline Huy TranFriend
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 13:58 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Sorry if my answer was miss leading I am talking about the class
UmlSupport


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: XtextResource couldn't resolve references to an external Ecore model [message #820809 is a reply to message #820738] Wed, 14 March 2012 15:40 Go to previous messageGo to next message
Huy Tran is currently offline Huy TranFriend
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 15: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 15:58 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
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


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: XtextResource couldn't resolve references to an external Ecore model [message #821575 is a reply to message #820822] Thu, 15 March 2012 14:53 Go to previous messageGo to next message
Huy Tran is currently offline Huy TranFriend
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 16:26 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
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


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: XtextResource couldn't resolve references to an external Ecore model [message #821656 is a reply to message #821638] Thu, 15 March 2012 16:54 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
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());
		}

	}

}


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: XtextResource couldn't resolve references to an external Ecore model [message #822330 is a reply to message #821656] Fri, 16 March 2012 13:31 Go to previous message
Huy Tran is currently offline Huy TranFriend
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: Fri Apr 19 11:22:55 GMT 2024

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

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

Back to the top