Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Loading a resource
Loading a resource [message #639116] Mon, 15 November 2010 11:15 Go to next message
Robin  is currently offline Robin Friend
Messages: 25
Registered: August 2010
Junior Member
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 09:53 Go to previous messageGo to next message
Jan Koehnlein is currently offline Jan KoehnleinFriend
Messages: 760
Registered: July 2009
Location: Hamburg
Senior Member
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


---
Get professional support from the Xtext committers at www.typefox.io
Re: Loading a resource [message #643566 is a reply to message #639371] Tue, 07 December 2010 13:15 Go to previous messageGo to next message
Robin  is currently offline Robin Friend
Messages: 25
Registered: August 2010
Junior Member
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 10:12 Go to previous messageGo to next message
Robin  is currently offline Robin Friend
Messages: 25
Registered: August 2010
Junior Member
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 08:28 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3108
Registered: July 2009
Senior Member
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 12:15 Go to previous messageGo to next message
Eisfeld Andrej is currently offline Eisfeld AndrejFriend
Messages: 3
Registered: December 2010
Junior Member
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 21:59 Go to previous messageGo to next message
Robin  is currently offline Robin Friend
Messages: 25
Registered: August 2010
Junior Member
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 22:15 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13601
Registered: July 2009
Senior Member
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


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: Loading a resource [message #654227 is a reply to message #646770] Mon, 14 February 2011 16:43 Go to previous messageGo to next message
Robin  is currently offline Robin Friend
Messages: 25
Registered: August 2010
Junior Member
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 19:19 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13601
Registered: July 2009
Senior Member
hi,

can you share a runnable example

~Christian


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: Loading a resource [message #654820 is a reply to message #654257] Thu, 17 February 2011 10:00 Go to previous messageGo to next message
Robin  is currently offline Robin Friend
Messages: 25
Registered: August 2010
Junior Member
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 18:18 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13601
Registered: July 2009
Senior Member
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


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: Thu, 17 February 2011 18:19]

Report message to a moderator

Re: Loading a resource [message #657564 is a reply to message #654957] Thu, 03 March 2011 10:15 Go to previous messageGo to next message
Fa Za is currently offline Fa ZaFriend
Messages: 245
Registered: March 2010
Senior Member
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 11:13 Go to previous messageGo to next message
Robin  is currently offline Robin Friend
Messages: 25
Registered: August 2010
Junior Member
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 15:17 Go to previous messageGo to next message
Lau sy is currently offline Lau syFriend
Messages: 1
Registered: August 2012
Junior Member
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 15:33 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13601
Registered: July 2009
Senior Member
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


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
Previous Topic:SyntacticSequencer
Next Topic:Xtext standalone application beginner tutorial
Goto Forum:
  


Current Time: Wed Sep 23 04:42:41 GMT 2020

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

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

Back to the top