Skip to main content



      Home
Home » Modeling » TMF (Xtext) » Test with a cross-reference model
Test with a cross-reference model [message #1806274] Tue, 07 May 2019 06:02 Go to next message
Eclipse UserFriend
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 03:18] by Moderator

Re: Test with a cross-reference model [message #1806286 is a reply to message #1806274] Tue, 07 May 2019 07:58 Go to previous messageGo to next message
Eclipse UserFriend
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
Re: Test with a cross-reference model [message #1806293 is a reply to message #1806286] Tue, 07 May 2019 08:49 Go to previous messageGo to next message
Eclipse UserFriend
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 09:02 Go to previous messageGo to next message
Eclipse UserFriend
you may find a snippet here
https://www.eclipse.org/forums/index.php/m/1745239/?srch=ParseHelper+resourceset+createResource#msg_1745239
Re: Test with a cross-reference model [message #1806299 is a reply to message #1806295] Tue, 07 May 2019 09:25 Go to previous messageGo to next message
Eclipse UserFriend
@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
	}
}
Re: Test with a cross-reference model [message #1806302 is a reply to message #1806295] Tue, 07 May 2019 09:28 Go to previous messageGo to next message
Eclipse UserFriend
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 12:11 Go to previous messageGo to next message
Eclipse UserFriend
Which class
Re: Test with a cross-reference model [message #1806343 is a reply to message #1806314] Tue, 07 May 2019 20:34 Go to previous messageGo to next message
Eclipse UserFriend
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: Tue, 07 May 2019 20:39] by Moderator

Re: Test with a cross-reference model [message #1806349 is a reply to message #1806343] Wed, 08 May 2019 00:09 Go to previous messageGo to next message
Eclipse UserFriend
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();
}

}

[Updated on: Wed, 08 May 2019 00:47] by Moderator

Re: Test with a cross-reference model [message #1806355 is a reply to message #1806349] Wed, 08 May 2019 01:38 Go to previous messageGo to next message
Eclipse UserFriend
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 03:18] by Moderator

Re: Test with a cross-reference model [message #1806363 is a reply to message #1806355] Wed, 08 May 2019 03:40 Go to previous messageGo to next message
Eclipse UserFriend
please provide a github/gitlab/bitbuckt/... project what reproduces the problem
Re: Test with a cross-reference model [message #1806365 is a reply to message #1806363] Wed, 08 May 2019 04:05 Go to previous messageGo to next message
Eclipse UserFriend
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 04:15 Go to previous messageGo to next message
Eclipse UserFriend
simply create a greeting example
Re: Test with a cross-reference model [message #1806368 is a reply to message #1806365] Wed, 08 May 2019 04:15 Go to previous messageGo to next message
Eclipse UserFriend
Well ok, I will create that.

[Updated on: Wed, 08 May 2019 04:16] by Moderator

Re: Test with a cross-reference model [message #1806375 is a reply to message #1806368] Wed, 08 May 2019 05:17 Go to previous messageGo to next message
Eclipse UserFriend
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 05:31 Go to previous messageGo to next message
Eclipse UserFriend
the sample is only one dsl
Re: Test with a cross-reference model [message #1806383 is a reply to message #1806379] Wed, 08 May 2019 05:48 Go to previous messageGo to next message
Eclipse UserFriend
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 05:49] by Moderator

Re: Test with a cross-reference model [message #1806390 is a reply to message #1806383] Wed, 08 May 2019 06:55 Go to previous messageGo to next message
Eclipse UserFriend
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"))
Re: Test with a cross-reference model [message #1806393 is a reply to message #1806390] Wed, 08 May 2019 07:59 Go to previous messageGo to next message
Eclipse UserFriend
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] Thu, 09 May 2019 22:45 Go to previous messageGo to next message
Eclipse UserFriend
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 00:29 Go to previous messageGo to next message
Eclipse UserFriend
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)
			]
		]


[Updated on: Fri, 10 May 2019 01:22] by Moderator

Re: Test with a cross-reference model [message #1806512 is a reply to message #1806507] Fri, 10 May 2019 01:18 Go to previous message
Eclipse UserFriend
Thanks a lot :)
Previous Topic:Generating a project with two languages using maven
Next Topic:Left Recursion Problem in xText
Goto Forum:
  


Current Time: Wed May 07 08:43:38 EDT 2025

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

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

Back to the top