Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    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 18:51 Go to next message
Mehmetcan Sinir is currently offline Mehmetcan SinirFriend
Messages: 55
Registered: September 2018
Member
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 18:53 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
hi,

can you please elaborate your exact usecase


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Creating a Resource from files in InMemoryFileSystemAccess [message #1803931 is a reply to message #1803930] Wed, 13 March 2019 19:17 Go to previous messageGo to next message
Mehmetcan Sinir is currently offline Mehmetcan SinirFriend
Messages: 55
Registered: September 2018
Member
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 19:57 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
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


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Creating a Resource from files in InMemoryFileSystemAccess [message #1803947 is a reply to message #1803932] Thu, 14 March 2019 05:39 Go to previous messageGo to next message
Mehmetcan Sinir is currently offline Mehmetcan SinirFriend
Messages: 55
Registered: September 2018
Member
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 05:43]

Report message to a moderator

Re: Creating a Resource from files in InMemoryFileSystemAccess [message #1803948 is a reply to message #1803947] Thu, 14 March 2019 06:18 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
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);

}


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Thu, 14 March 2019 06:27]

Report message to a moderator

Re: Creating a Resource from files in InMemoryFileSystemAccess [message #1803949 is a reply to message #1803948] Thu, 14 March 2019 06:27 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
	@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])
	}


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Creating a Resource from files in InMemoryFileSystemAccess [message #1803951 is a reply to message #1803949] Thu, 14 March 2019 06:40 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
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
	}
}


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Creating a Resource from files in InMemoryFileSystemAccess [message #1804500 is a reply to message #1803951] Wed, 27 March 2019 09:27 Go to previous message
Mehmetcan Sinir is currently offline Mehmetcan SinirFriend
Messages: 55
Registered: September 2018
Member
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 09:28]

Report message to a 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: Tue Apr 23 15:17:17 GMT 2024

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

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

Back to the top