Skip to main content



      Home
Home » Modeling » TMF (Xtext) » Loading a resource
Loading a resource [message #639116] Mon, 15 November 2010 06:15 Go to next message
Eclipse UserFriend
Hi,

I was trying to play around with the code given in the xtext Tutorial under Processing Xtext Models, Loading a Resource ( http://www.eclipse.org/Xtext/documentation/latest/xtext.html #processing_Xtext_models), but I cannot get it to work.

		// DomainmodelStandaloneSetup.doSetup();
		new DomainmodelStandaloneSetup().createInjectorAndDoEMFRegistration();

		ResourceSet rs = new ResourceSetImpl();
		File file = new File("C:/runtime-EclipseApplication/gettingstarted/my2.dmodel");
		Resource resource = rs.getResource(URI.createURI(file.toURI().toString()), true);
//		Resource resource = rs.getResource(URI.createURI("./my2.dmodel"), true);
		EObject eobject = resource.getContents().get(0);
		
		DomainModel domainModel = (DomainModel) eobject;
		EList<AbstractElement> elements = domainModel.getElements();
		for (AbstractElement abstractElement : elements) {
		  if (abstractElement instanceof Entity) {
		    Entity entity = ((Entity)abstractElement);
		    System.out.println("entity "
		     + entity.getName()
		     + " extends "
		     + entity.getSuperType().getName());
		  }
		}


I get this error:
Exception in thread "main" java.lang.RuntimeException: Cannot create a resource for 'file:/C:/runtime-EclipseApplication/gettingstarted/my2.dmodel'; a registered resource factory is needed
	at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.getResource(ResourceSetImpl.java:394)
	at org.eclipse.xtext.example.Test.main(Test.java:26)


from the line:
Resource resource = rs.getResource(URI.createURI(file.toURI().toString()), true);


I created the Java file in the org.eclipse.xtext.example.domainmodel project, in the package org.eclipse.xtext.example.

What am I missing?? Confused

Regard, Robin
Re: Loading a resource [message #639371 is a reply to message #639116] Tue, 16 November 2010 04:53 Go to previous messageGo to next message
Eclipse UserFriend
Are you running your code from within Eclipse (not in a separate JVM)?
If so, you shouldn't call the DomainmodelStandaloneSetup. Otherwise, use
a debugger to make sure the line

Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap( ).put( "dmodel",
resourceFactory);

in DomainmdodelStandaloneSetupGenerated is called. This is where the
resource factory should be registered.

Am 15.11.10 12:16, schrieb Robin:
> Hi,
>
> I was trying to play around with the code given in the xtext Tutorial
> under Processing Xtext Models, Loading a Resource
> ( http://www.eclipse.org/Xtext/documentation/latest/xtext.html #processing_Xtext_models),
> but I cannot get it to work.
>
>
> // DomainmodelStandaloneSetup.doSetup();
> new DomainmodelStandaloneSetup().createInjectorAndDoEMFRegistrat ion();
>
> ResourceSet rs = new ResourceSetImpl();
> File file = new
> File("C:/runtime-EclipseApplication/gettingstarted/my2.dmodel ");
> Resource resource =
> rs.getResource(URI.createURI(file.toURI().toString()), true);
> // Resource resource = rs.getResource(URI.createURI("./my2.dmodel"), true);
> EObject eobject = resource.getContents().get(0);
>
> DomainModel domainModel = (DomainModel) eobject;
> EList<AbstractElement> elements = domainModel.getElements();
> for (AbstractElement abstractElement : elements) {
> if (abstractElement instanceof Entity) {
> Entity entity = ((Entity)abstractElement);
> System.out.println("entity "
> + entity.getName()
> + " extends "
> + entity.getSuperType().getName());
> }
> }
>
>
> I get this error:
>
> Exception in thread "main" java.lang.RuntimeException: Cannot create a
> resource for
> 'file:/C:/runtime-EclipseApplication/gettingstarted/my2.dmod el'; a
> registered resource factory is needed
> at
> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.getResou rce(ResourceSetImpl.java:394)
>
> at org.eclipse.xtext.example.Test.main(Test.java:26)
>
>
> from the line:
>
> Resource resource =
> rs.getResource(URI.createURI(file.toURI().toString()), true);
>
>
> I created the Java file in the org.eclipse.xtext.example.domainmodel
> project, in the package org.eclipse.xtext.example.
>
> What am I missing?? :?
> Regard, Robin


--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com
Re: Loading a resource [message #643566 is a reply to message #639371] Tue, 07 December 2010 08:15 Go to previous messageGo to next message
Eclipse UserFriend
Since I am using the code generated by the MWE2 generation, I am running the Test.java in the same workspace. I don't see why this should be a problem, since nothing is actually running, when I run the Test.java.

How should I do this properly?? It doesn't say in the tutorial ( http://www.eclipse.org/Xtext/documentation/latest/xtext.html #processing_Xtext_models).

The line
Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("dmodel ", resourceFactory);


in DomainmodelStandaloneSetupGenerated is called. I debugged it. But still then the ResourceSetImpl.getResource method falls through every case and eventually throws the RuntimeException. So, somehow the Resource is not properly registered??

I have absolutely no clue... Sad
Re: Loading a resource [message #644567 is a reply to message #643566] Mon, 13 December 2010 05:12 Go to previous messageGo to next message
Eclipse UserFriend
Anyone?? Just some thoughts or clues to help me get in the right direction, please?? Sad
Re: Loading a resource [message #644773 is a reply to message #644567] Tue, 14 December 2010 03:28 Go to previous messageGo to next message
Eclipse UserFriend
Hi Robin

did you try to use this URI:
rs.getResource(URI.createURI("my2.dmodel"), true);
?

Basically you code looks ok. However, I'd assume that the file uri has
to use two slashes: file://c:/..

Regards,
Sebastian
--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com

Am 13.12.10 11:12, schrieb Robin:
> Anyone?? Just some thoughts or clues to help me get in the right
> direction, please?? :(
Re: Loading a resource [message #644837 is a reply to message #639116] Tue, 14 December 2010 07:15 Go to previous messageGo to next message
Eclipse UserFriend
Hi Robin,

maybe you could try this:

File file = new File("C:\\runtime-EclipseApplication\\gettingstarted\\my2.dmodel");


regards

Andy
Re: Loading a resource [message #646769 is a reply to message #639116] Thu, 30 December 2010 16:59 Go to previous messageGo to next message
Eclipse UserFriend
Hi!

Thanks for the replies. Sadly none is working.

rs.getResource(URI.createURI("my2.dmodel"), true);

gives
Exception in thread "main" java.lang.RuntimeException: Cannot create a resource for 'my2.dmodel'; a registered resource factory is needed


File file = new File("//C:/runtime-EclipseApplication/gettingstarted/my2.dmodel");

gives
Exception in thread "main" java.lang.RuntimeException: Cannot create a resource for 'file:/C:/runtime-EclipseApplication/gettingstarted/my2.dmodel'; a registered resource factory is needed


File file = new File("file://C:/runtime-EclipseApplication/gettingstarted/my2.dmodel");

also gives
Exception in thread "main" java.lang.RuntimeException: Cannot create a resource for 'file:/C:/eclipsew/org.eclipse.xtext.example.domainmodel/file:/C:/runtime-EclipseApplication/gettingstarted/my2.dmodel'; a registered resource factory is needed


maybe like this:
Resource resource = rs.getResource(URI.createURI("file://C:/runtime-EclipseApplication/gettingstarted/my2.dmodel"), true);

gives
Exception in thread "main" java.lang.RuntimeException: Cannot create a resource for 'file://C:/runtime-EclipseApplication/gettingstarted/my2.dmodel'; a registered resource factory is needed


or
File file = new File("C:\\runtime-EclipseApplication\\gettingstarted\\my2.dmodel");

also gives
Exception in thread "main" java.lang.RuntimeException: Cannot create a resource for 'file:/C:/runtime-EclipseApplication/gettingstarted/my2.dmodel'; a registered resource factory is needed


Well, I guess it is not the File or URI object since this is valid. The factory simply seems not to be registered. How can that be??? Confused

Regards, Robin
Re: Loading a resource [message #646770 is a reply to message #646769] Thu, 30 December 2010 17:15 Go to previous messageGo to next message
Eclipse UserFriend
hello robin,

can you post your complete code loading the model. seems you are not callling standalonesetup.

this code works nicely for me

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.xtext.example.DomainmodelStandaloneSetup;
import org.eclipse.xtext.example.domainmodel.DomainModel;


public class Test {
	
	public static void main(String[] args) {
		new DomainmodelStandaloneSetup().createInjectorAndDoEMFRegistration();
		ResourceSet rs = new ResourceSetImpl();
		Resource resource = rs.getResource(URI.createURI("./src/test.dmodel"), true);
		DomainModel model = (DomainModel) resource.getContents().get(0);
		System.out.println(model.getElements().size());
	}

}



~Christian
Re: Loading a resource [message #654227 is a reply to message #646770] Mon, 14 February 2011 11:43 Go to previous messageGo to next message
Eclipse UserFriend
Okay, trying again...

I plain tried your code (with the needed modifications of course), but it doesn't work. I think it's not a coding problem but more a how to do things kind of problem.

I am running the Test.java from here: org.eclipse.xtext.example.domainmodel\src\org\eclipse\xtext\ example\Test.java
Directly in the domainmodel, because the generated classes are there available. Maybe this is the wrong way??

Isn't there a full tutorial that covers the "whole way" of working with a DSL in xtext?? That truly would be great.

Regards, Robin
Re: Loading a resource [message #654257 is a reply to message #654227] Mon, 14 February 2011 14:19 Go to previous messageGo to next message
Eclipse UserFriend
hi,

can you share a runnable example

~Christian
Re: Loading a resource [message #654820 is a reply to message #654257] Thu, 17 February 2011 05:00 Go to previous messageGo to next message
Eclipse UserFriend
Well, OK, there isn't much, but I put the workspace and the runtime directory in an archive:

http://dl.dropbox.com/u/5602365/workspace_and_runtime.zip

My configuration is set up like this:
- the workspace is located on my machine at: C:\eclipsew
- the runtime environment is located on my machine at: C:\runtime-EclipseApplication
- eclipse-SDK-3.6-xtext-1.0.1-win32 is installed at: C:\eclipse

The test files are:

C:\eclipsew\org.eclipse.xtext.example.domainmodel\src\org\ec lipse\xtext\example\Test.java

and

C:\eclipsew\org.eclipse.xtext.example.domainmodel\src\org\ec lipse\xtext\example\Test2.java

Test.java is very messy and contains a lot of trying out but mostly just commented out code, Test2.java is the one with the code Christian provided.

Thanks for having a look!

Regards, Robin
Re: Loading a resource [message #654957 is a reply to message #654820] Thu, 17 February 2011 13:18 Go to previous messageGo to next message
Eclipse UserFriend
Hi,

the problem is simple but hard to find Smile you file extension is "dmodel " (ending with a blank) but your files have od course the extension "dmodel". so changing your workflow to

var grammarURI = "classpath:/org/eclipse/xtext/example/Domainmodel.xtext"
var file.extensions = "dmodel"
var projectName = "org.eclipse.xtext.example.domainmodel"
var runtimeProject = "../${projectName}"


might solve the problems Wink Note: you may have to merge plugin.xml and plugin.xml_gen

Feel free to file an enhancement request for a validation rule that complains on whitespace in the file extension.

~Christian

[Updated on: Thu, 17 February 2011 13:19] by Moderator

Re: Loading a resource [message #657564 is a reply to message #654957] Thu, 03 March 2011 05:15 Go to previous messageGo to next message
Eclipse UserFriend
Hi,
I 'd know if the org.eclipse.xtext.example.domainmodel.DomainModel interface is generated automatically or added manually because I don't find it in my project after generation in the src-gen folder
thanks
Re: Loading a resource [message #659956 is a reply to message #657564] Wed, 16 March 2011 07:13 Go to previous messageGo to next message
Eclipse UserFriend
Christian, thank you so much. It works now. I just changed

var file.extensions = "dmodel "

to

var file.extensions = "dmodel"

in GenerateDomainmodel.mwe2.

@Faiez: I have the DomainModel.java interface in org.eclipse.xtext.example.domainmodel belonging to the src-gen folder. So it is generated automatically.

Regards, Robin
Re: Loading a resource [message #901064 is a reply to message #659956] Thu, 09 August 2012 11:17 Go to previous messageGo to next message
Eclipse UserFriend
That's the key......change the extension
But why?
I read the xtext tutorial again and again but not find the source......

Robin wrote on Wed, 16 March 2011 07:13
Christian, thank you so much. It works now. I just changed

var file.extensions = "dmodel "

to

var file.extensions = "dmodel"

in GenerateDomainmodel.mwe2.

@Faiez: I have the DomainModel.java interface in org.eclipse.xtext.example.domainmodel belonging to the src-gen folder. So it is generated automatically.

Regards, Robin

Re: Loading a resource [message #901071 is a reply to message #901064] Thu, 09 August 2012 11:33 Go to previous message
Eclipse UserFriend
Having a trailing blank got a big mess with internal registries -
giving a blank as file extension does not work in most OS but in java
storing it as a key in a hash map does work well

--
Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext at itemis dot de
Previous Topic:SyntacticSequencer
Next Topic:Xtext standalone application beginner tutorial
Goto Forum:
  


Current Time: Sun May 11 10:26:01 EDT 2025

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

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

Back to the top