Skip to main content



      Home
Home » Modeling » TMF (Xtext) » Creating a Resource from files in InMemoryFileSystemAccess
Creating a Resource from files in InMemoryFileSystemAccess [message #1803929] Wed, 13 March 2019 14:51 Go to next message
Eclipse UserFriend
Normally, it is possible to create a Resource from a file defined in the file system like this:


URI uri = URI.createFileURI(f.getAbsolutePath());
resourceSet.getResource(uri, true);


How can I achieve the same thing using the InMemoryFileSystemAccess

If I pass a URI using that, I get a bad URI exception?

Map<String, Object> files = fileAccess.getAllFiles();
		ResourceSet resourceSet = new ResourceSetImpl();
		URI uri = fileAccess.getURI("path/to/file.ext", "DefaultOutputConfig");
		resourceSet.getResource(uri, true); //here I get a bad URI Exception
Re: Creating a Resource from files in InMemoryFileSystemAccess [message #1803930 is a reply to message #1803929] Wed, 13 March 2019 14:53 Go to previous messageGo to next message
Eclipse UserFriend
hi,

can you please elaborate your exact usecase
Re: Creating a Resource from files in InMemoryFileSystemAccess [message #1803931 is a reply to message #1803930] Wed, 13 March 2019 15:17 Go to previous messageGo to next message
Eclipse UserFriend
My general use case is testing generated code

the generators are already implemented and I am trying to test the generated code, i.e. compare expected code with generated code.

generators expect an abstact project model (MyDslModel) as argument . This Model is created from a ResourceSet ((org.eclipse.emf.ecore.ResourceSet)

To simplify it:
I need to run :
generator. generate(MyDslModel, FileSystemAccess)


for this I need
MyDslModel model = new MyDslModel(ResourceSet)


and for this I need to create a resourceSet from using a rootFolder.

This can be done with normal files using URIs, but I haven't been able to do it with Files created by InMemoryFileSystemAccess

Re: Creating a Resource from files in InMemoryFileSystemAccess [message #1803932 is a reply to message #1803931] Wed, 13 March 2019 15:57 Go to previous messageGo to next message
Eclipse UserFriend
i am not sure and when and how you call this code.
ususal is

(1) load the models into the resourceset
(2) start generation

=> can you give more context when and how you execute this and where you get the initial model from

see

https://stackoverflow.com/questions/47110291/xtext-standalone-and-validation/47113093#47113093
Re: Creating a Resource from files in InMemoryFileSystemAccess [message #1803947 is a reply to message #1803932] Thu, 14 March 2019 01:39 Go to previous messageGo to next message
Eclipse UserFriend
yes but how can I call getResource on a File that is not in the file system?

Resource resource = rs.getResource(URI.createURI("test.mydsl"), true);


let's say test.mydsl is not in the filesystem (it is save in the InMemoryFileSystemAccess object) like this:

@Inject
private InMemoryFileSystemAccess fileAccess;

@Test
myTestMethod() {

  String content = "Friend friend"

  fileAccess.generateFile("mydsl.test", "DEFAULT_OUTPUT", content);

}



Now how can I create a resource from "mydsl.test"? Below .doesn't work because test.mydsl is not in the file system?

ResourceSet rs = injector.getInstance(ResourceSet.class);
Resource resource = rs.getResource(URI.createURI("test.mydsl"), true);


Or am I using InMemoryFileSystemAccess wrong here? I am trying to use it in my tests, first I am using it to generate InMemory code that will be the input for my generators. Hope that makes sense to you.

[Updated on: Thu, 14 March 2019 01:43] by Moderator

Re: Creating a Resource from files in InMemoryFileSystemAccess [message #1803948 is a reply to message #1803947] Thu, 14 March 2019 02:18 Go to previous messageGo to next message
Eclipse UserFriend
hi
InMemoryFileSystemAccess is not for loading model files

i still dont understand what you want to do. can you describe the complete usecase in pseudocode



@Test
myTestMethod() {

String content = "Friend friend"
// 1 create resourceset
// 2 create resource
// 3 call resource.load with stream
// 4 call generator
// dont know why you call generate file here
// you can also use parse helper to load a resource from a string
fileAccess.generateFile("mydsl.test", "DEFAULT_OUTPUT", content);

}

[Updated on: Thu, 14 March 2019 02:27] by Moderator

Re: Creating a Resource from files in InMemoryFileSystemAccess [message #1803949 is a reply to message #1803948] Thu, 14 March 2019 02:27 Go to previous messageGo to next message
Eclipse UserFriend
	@Inject
	ParseHelper<Model> parseHelper
	
	@Test
	def void loadModel() {
		val result = parseHelper.parse('''
			Hello Xtext!
		''')
		val resource  = result.eResource





	@Inject
	Provider<ResourceSet> rsp
	
	@Test
	def void loadModel() {
		val rs = rsp.get
		val r = rs.createResource(URI.createURI("demo.mydsl"))
		val content = '''
			Hello A!
			Hello B!
		'''
		r.load(new StringInputStream(content), null)
		val errors = r.errors
		Assertions.assertTrue(errors.isEmpty, '''Unexpected errors: «errors.join(", ")»''')
		val model = r.contents.head as Model
		println(model.greetings.map[name])
	}
Re: Creating a Resource from files in InMemoryFileSystemAccess [message #1803951 is a reply to message #1803949] Thu, 14 March 2019 02:40 Go to previous messageGo to next message
Eclipse UserFriend
or you make use of the inmemory uri handler

@Inject
	Provider<ResourceSet> rsp
	
	@Test
	def void loadModel() {
		val rs = rsp.get
		val handler = new InMemoryURIHandler()
		handler += "inmemory:/demo.mydsl" -> '''Hello A! Hello B!'''
		
		rs.URIConverter.URIHandlers.add(0, handler)
		
		val r = rs.getResource(URI.createURI("inmemory:/demo.mydsl"), true)
		r.load(null)
		val errors = r.errors
		Assertions.assertTrue(errors.isEmpty, '''Unexpected errors: «errors.join(", ")»''')
		val model = r.contents.head as Model
		println(model.greetings.map[name])
	}
	
	def void +=(InMemoryURIHandler handler, Pair<String, String> file) {
		val f = handler.getInMemoryFile(URI.createURI(file.key))
		f.contents = file.value.bytes
		f.exists = true
	}
}
Re: Creating a Resource from files in InMemoryFileSystemAccess [message #1804500 is a reply to message #1803951] Wed, 27 March 2019 05:27 Go to previous message
Eclipse UserFriend
Hi Christian

Thank you for your replies.

We have currently solved this issue.

The use case was to test if the generators are generating the expected output.

Due to Project Constraints, I could not use the inMemoryFileSystemAccess. I solved the issue by using the TemporaryFolder Framework from JUNIT, i.e. run generators and the output files will be generated in a temporary folder. Then I compare them with the expected output.

However, your replies are very informative and will certainly come in handy for future problems.

Thanks again!

[Updated on: Wed, 27 March 2019 05:28] by Moderator

Previous Topic:Heads-up: Eclipse 2019-03 needs all new project wizards to have an icon
Next Topic:Xtext-Web / Referencing Elements
Goto Forum:
  


Current Time: Sun Apr 27 23:55:48 EDT 2025

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

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

Back to the top