Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Test with a cross-reference model
Test with a cross-reference model [message #1806274] Tue, 07 May 2019 10:02 Go to next message
TheEMF User is currently offline TheEMF UserFriend
Messages: 71
Registered: August 2018
Member
Hello,

I am making unit tests in my DSL named Karbura. Karbura imports another model from another a DSL named MyDSL. Below is the test I am making. To resolve the model of MyDSL I use the function beneath. When I launch an eclipse instance to test this everything works fine and it resolve "portA" in the test below. However, in my test I place the object1.mydsl at the same directory than my test but it could not resolve it. I got this error for resolving :

The ERROR :
URI : object1.mydsl
URI Resolve : __synthetic0.cy
0    [main] ERROR base.resource.BatchLinkableResource  - resolution of uriFragment '|3' failed.
java.lang.IllegalArgumentException: resolve against non-hierarchical or relative base


The TEST :

	@Test
	def void TestMyDSLImport() {
		val result = parseHelper.parse('''
			object object1 import "object1.mydsl"
                        object1.portA // It is a port from object1.mydsl
		''')
		result.assertNoErrors
		Assert.assertNotNull(result)
		Assert.assertTrue(result.eResource.errors.isEmpty)
	}



The Resolving function :
public static MyDSLModel getMyDSLObject(Object object) {
		URI new_uri = URI.createFileURI(object.getImportPath());
		MyDSLStandaloneSetup.doSetup();
		System.out.println("URI : " + new_uri);
		System.out.println("URI Resolve : " + object.eResource().getURI());
		if (new_uri.isRelative()) {
			new_uri = new_uri.resolve(object.eResource().getURI());
		}
		Resource r = object.eResource().getResourceSet().getResource(new_uri, true);
		if (r != null && r.getContents().size() > 0 && r.getContents().get(0) instanceof MyDSLModel) {
			return (MyDSLModel) r.getContents().get(0);
		} else {
			try {
				throw new Exception("No valid model found for resource " + object.getImportPath());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}


How can I achieve this test ?


Thanks

[Updated on: Wed, 08 May 2019 07:18]

Report message to a moderator

Re: Test with a cross-reference model [message #1806286 is a reply to message #1806274] Tue, 07 May 2019 11:58 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13164
Registered: July 2009
Senior Member
you need to feed all resources into the resourceset.
see org.eclipse.xtext.testing.util.ParseHelper.parse(InputStream, URI, Map<?, ?>, ResourceSet)
you can inject

Provider<ResourceSet> rsp

and rsp.get to obtain the rs.

and you can use URI.createURI("dummy.yourdsl") for the uri


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Test with a cross-reference model [message #1806293 is a reply to message #1806286] Tue, 07 May 2019 12:49 Go to previous messageGo to next message
TheEMF User is currently offline TheEMF UserFriend
Messages: 71
Registered: August 2018
Member
Thank you for your message.

I am not familiar with that, could be point me on how I can feed the resources to a resourceSet.

I also don't know how to use URI.createURI("dummy.yourdsl"), could you please help ?

Thank you a lot
Re: Test with a cross-reference model [message #1806295 is a reply to message #1806293] Tue, 07 May 2019 13:02 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13164
Registered: July 2009
Senior Member
you may find a snippet here
https://www.eclipse.org/forums/index.php/m/1745239/?srch=ParseHelper+resourceset+createResource#msg_1745239


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Test with a cross-reference model [message #1806299 is a reply to message #1806295] Tue, 07 May 2019 13:25 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13164
Registered: July 2009
Senior Member
@ExtendWith(InjectionExtension)
@InjectWith(MyDslInjectorProvider)
class MyDslParsingTest {
	@Inject
	ParseHelper<Model> parseHelper
	
	@Inject
	extension ValidationTestHelper
	
	@Inject
	Provider<ResourceSet> rsp
	
	@Test
	def void loadModel() {
		val result = parseHelper.parse('''
			Hello Xtext from Christian!
		''', URI.createURI("Xtext.mydsl1"),
		rsp.get => [
			createResource(URI.createURI("Other.mydsl1")) => [
				load(new StringInputStream('''Hello Christian!''', "UTF-8"), null)
			]
		])
		Assertions.assertNotNull(result)
		val errors = result.eResource.errors
		Assertions.assertTrue(errors.isEmpty, '''Unexpected errors: «errors.join(", ")»''')
		result.assertNoIssues
	}
}


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Test with a cross-reference model [message #1806302 is a reply to message #1806295] Tue, 07 May 2019 13:28 Go to previous messageGo to next message
TheEMF User is currently offline TheEMF UserFriend
Messages: 71
Registered: August 2018
Member
Thank you for your pointer. However I still don't know how to use this snippet. I think that I have a recent xtext version. Some classes does not exist anymore.

Do you have another example please ? Or you could just show me how I would use these elements in my previous code
Re: Test with a cross-reference model [message #1806314 is a reply to message #1806302] Tue, 07 May 2019 16:11 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13164
Registered: July 2009
Senior Member
Which class

Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Test with a cross-reference model [message #1806343 is a reply to message #1806314] Wed, 08 May 2019 00:34 Go to previous messageGo to next message
TheEMF User is currently offline TheEMF UserFriend
Messages: 71
Registered: August 2018
Member
Thank you for your response.

Never mind for the classes.

I tried your new snipped, but "Other.mydsl1" is another DSL not the same. I got a java.lang.NullPointerException on load().

I think this is because the DSL is not the same for the second file. How can I achieve something like that (notice that now the second file is mydsl2) :

val result = parseHelper.parse('''
			Hello Xtext from Christian!
		''', URI.createURI("Xtext.mydsl1"),
		rsp.get => [
			createResource(URI.createURI("Other.mydsl2")) => [
				load(new StringInputStream('''Another DSL!''', "UTF-8"), null)
			]
		])


[Updated on: Wed, 08 May 2019 00:39]

Report message to a moderator

Re: Test with a cross-reference model [message #1806349 is a reply to message #1806343] Wed, 08 May 2019 04:09 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13164
Registered: July 2009
Senior Member
So you have two dsls?
Then you have to make sure the standalonesetup is calling the one for the other too
Eg

XxxStandaloneasetup.doSetup()
Or create a custom injecorptovider that does that

e.g.

// use this one in the test
public class MyDslTestInjectorProvider extends MyDslInjectorProvider { // this is the dsl that has the reference

@Override
protected Injector internalCreateInjector() {
MyDsl2StandaloneSetup.doSetup(); // this is the dsl that is refferred to
return super.internalCreateInjector();
}

}


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Wed, 08 May 2019 04:47]

Report message to a moderator

Re: Test with a cross-reference model [message #1806355 is a reply to message #1806349] Wed, 08 May 2019 05:38 Go to previous messageGo to next message
TheEMF User is currently offline TheEMF UserFriend
Messages: 71
Registered: August 2018
Member
Thank you for your help.

Here is my Test now (below), but I still get this Error. It seems that the test recognizes other.mydsl2 and load it, but it has a problem to resolve. What could be the origin of this problem ?

I use the same function above to resolve it and it works with the UI. And btw I added the XXXStandaloneSetup.doSetup(); and it seems to help loading the model of the other DSL.

URI : other.mydsl2
URI Resolve : test.mydsl
1    [main] ERROR base.resource.BatchLinkableResource  - resolution of uriFragment '|3' failed.
java.lang.IllegalArgumentException: resolve against non-hierarchical or relative base


This error is thrown by this line in the previous function

new_uri = new_uri.resolve(object.eResource().getURI());


@Test
	def void TestMyDSLImport() {
		val result = parseHelper.parse('''
			object object1 import "other.mydsl2"
            object1.portA // It is a port from object1.mydsl
		''', URI.createURI("test.mydsl"),resourcesetProvider.get => [
			createResource(URI.createURI("other.mydsl2")) => [
				load(new StringInputStream('''
				Another DSL !
			''', "UTF-8"), null)
			]
		])
		result.assertNoErrors
		Assert.assertNotNull(result)
		Assert.assertTrue(result.eResource.errors.isEmpty)
	}


[Updated on: Wed, 08 May 2019 07:18]

Report message to a moderator

Re: Test with a cross-reference model [message #1806363 is a reply to message #1806355] Wed, 08 May 2019 07:40 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13164
Registered: July 2009
Senior Member
please provide a github/gitlab/bitbuckt/... project what reproduces the problem

Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Test with a cross-reference model [message #1806365 is a reply to message #1806363] Wed, 08 May 2019 08:05 Go to previous messageGo to next message
TheEMF User is currently offline TheEMF UserFriend
Messages: 71
Registered: August 2018
Member
Thank you for your response.

Well I am not sure whether I am allowed to share it. It is still a private project on Github.

What is missing for you in the code I provided ?

Thanks a lot
Re: Test with a cross-reference model [message #1806366 is a reply to message #1806365] Wed, 08 May 2019 08:15 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13164
Registered: July 2009
Senior Member
simply create a greeting example

Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Test with a cross-reference model [message #1806368 is a reply to message #1806365] Wed, 08 May 2019 08:15 Go to previous messageGo to next message
TheEMF User is currently offline TheEMF UserFriend
Messages: 71
Registered: August 2018
Member
Well ok, I will create that.

[Updated on: Wed, 08 May 2019 08:16]

Report message to a moderator

Re: Test with a cross-reference model [message #1806375 is a reply to message #1806368] Wed, 08 May 2019 09:17 Go to previous messageGo to next message
TheEMF User is currently offline TheEMF UserFriend
Messages: 71
Registered: August 2018
Member
Here you go : https://github.com/aduninon/GeetingDSL

I also created an example with Eclipse UI named "uitest" in the repo, and this one works fine, but not the test in org.xtext.example.mydsl.tests

Thanks for your help
Re: Test with a cross-reference model [message #1806379 is a reply to message #1806375] Wed, 08 May 2019 09:31 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13164
Registered: July 2009
Senior Member
the sample is only one dsl

Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Test with a cross-reference model [message #1806383 is a reply to message #1806379] Wed, 08 May 2019 09:48 Go to previous messageGo to next message
TheEMF User is currently offline TheEMF UserFriend
Messages: 71
Registered: August 2018
Member
Well the other DSL is not mine, you download it as an Eclipse plugin from here : http://thingml.org/dist/update2

[Updated on: Wed, 08 May 2019 09:49]

Report message to a moderator

Re: Test with a cross-reference model [message #1806390 is a reply to message #1806383] Wed, 08 May 2019 10:55 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13164
Registered: July 2009
Senior Member
there is no import path in your model.
its null.
there is a parse error.

should be

myobject object1 import "other.thingml"

then you should have a look what
URI : other.thingml
URI Resolve : test.mydsl
0 [main] ERROR xt.linking.lazy.LazyLinkingResource - resolution of uriFragment '|1' failed.
java.lang.IllegalArgumentException: resolve against non-hierarchical or relative base
at org.eclipse.emf.common.util.URI$Hierarchical.resolve(URI.java:3548)
at org.eclipse.emf.common.util.URI.resolve(URI.java:5579)
at org.xtext.example.mydsl.scoping.Helpers.getThingInThingML(Helpers.java:30)
at org.xtext.example.mydsl.scoping.Helpers.getAllPortsThing(Helpers.java:18)
at org.xtext.example.mydsl.scoping.Helpers.allPorts(Helpers.java:47)

logic makes with the uris you have
thats a emf/uri problem.

you could use file uris e.g.
''', URI.createFileURI("/test.mydsl"),resourcesetProvider.get => [
createResource(URI.createFileURI("/other.thingml"))


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Test with a cross-reference model [message #1806393 is a reply to message #1806390] Wed, 08 May 2019 11:59 Go to previous messageGo to next message
TheEMF User is currently offline TheEMF UserFriend
Messages: 71
Registered: August 2018
Member
Finally, it work when I use createFileURI.

Thanks a lot !
Re: Test with a cross-reference model [message #1806501 is a reply to message #1806390] Fri, 10 May 2019 02:45 Go to previous messageGo to next message
TheEMF User is currently offline TheEMF UserFriend
Messages: 71
Registered: August 2018
Member
How can I add more than one resource file in :

resourcesetProvider.get => [
			createResource(URI.createURI("other.mydsl2")) => [
				load(new StringInputStream('''
				Another DSL !
			''', "UTF-8"), null)
			]
		]



I tried semicolon and comma to add one more resource, how can achieve tht ?

Thanks for your help
Re: Test with a cross-reference model [message #1806507 is a reply to message #1806501] Fri, 10 May 2019 04:29 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13164
Registered: July 2009
Senior Member
Simply call it n times

resourcesetProvider.get => [
			createResource(URI.createURI("other.mydsl2")) => [
				load(new StringInputStream('''
				Another DSL !
			''', "UTF-8"), null)
			]
			createResource(URI.createURI("other2.mydsl2")) => [
				load(new StringInputStream('''
				Another DSL !
			''', "UTF-8"), null)
			]
		]




Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Fri, 10 May 2019 05:22]

Report message to a moderator

Re: Test with a cross-reference model [message #1806512 is a reply to message #1806507] Fri, 10 May 2019 05:18 Go to previous message
TheEMF User is currently offline TheEMF UserFriend
Messages: 71
Registered: August 2018
Member
Thanks a lot :)
Previous Topic:Generating a project with two languages using maven
Next Topic:Left Recursion Problem in xText
Goto Forum:
  


Current Time: Thu Oct 24 02:41:34 GMT 2019

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

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

Back to the top