Skip to main content



      Home
Home » Modeling » TMF (Xtext) » Validating model in Eclipse UI context before interpreting it
Validating model in Eclipse UI context before interpreting it [message #724832] Tue, 13 September 2011 06:15 Go to next message
Eclipse UserFriend
Hi, I would like to know how I can validate resource in Eclipse context - i.e. without need to create a standalone injector.

I created an interpreter that gets executed from Eclipse run configuration. Before I interpret the AST I need to validate given resource and its dependencies to make sure it will run correctly. So far I did this with validator created by standalone injector, which works fine. When I try to create the validator from UI injector (which is I suppose the injector I should use in Eclipse context) and run it, it will show linking errors. I suspect that the validator created in UI context uses wrong resource set. This is the code, which does not work when I inject validator using UI plugin injector:

@Inject
// injected by UI injector
private IResourceValidator validator;

ResourceSet resourceSet = new ResourceSetImpl();
// loads all resources in project to resource set; returns the resource to interpret
Resource resource = loadResourcesToResourceSet(resourceSet, iresource);
// will show linking issues (Couldn't find reference to ...) even though there are no problems;
validator.validate(resource, CheckMode.ALL, CancelIndicator.NullImpl);


Am I doing something wrong here? Do I need to load all resources to resource set (for standalone validator I had to do this)? How does the UI editor validate resources? I need to do exactly the same thing. I don't want to use the standalone injector because I suspect it messes up Eclipse registries and after its call not everything works correctly (e.g. cleaning project does not work).

Thanks for help, anything will be appreciated.
Re: Validating model in Eclipse UI context before interpreting it [message #724840 is a reply to message #724832] Tue, 13 September 2011 06:30 Go to previous messageGo to next message
Eclipse UserFriend
Hi,

linking will only work if the linked resources are contained in the resource set, so you will have to add them here as well.

Alex
Re: Validating model in Eclipse UI context before interpreting it [message #724881 is a reply to message #724832] Tue, 13 September 2011 07:59 Go to previous messageGo to next message
Eclipse UserFriend
Hi,

please use an injected XtextResourceSetProvider to obtain an instanceof
of ResourceSet.

Regards,
Sebastian
--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com

On 13.09.11 12:15, Kristian Nagy wrote:
> Hi, I would like to know how I can validate resource in Eclipse context
> - i.e. without need to create a standalone injector.
>
> I created an interpreter that gets executed from Eclipse run
> configuration. Before I interpret the AST I need to validate given
> resource and its dependencies to make sure it will run correctly. So far
> I did this with validator created by standalone injector, which works
> fine. When I try to create the validator from UI injector (which is I
> suppose the injector I should use in Eclipse context) and run it, it
> will show linking errors. I suspect that the validator created in UI
> context uses wrong resource set. This is the code, which does not work
> when I inject validator using UI plugin injector:
>
> @Inject
> // injected by UI injector
> private IResourceValidator validator;
>
> ResourceSet resourceSet = new ResourceSetImpl();
> // loads all resources in project to resource set; returns the resource
> to interpret
> Resource resource = loadResourcesToResourceSet(resourceSet, iresource);
> // will show linking issues (Couldn't find reference to ...) even though
> there are no problems; validator.validate(resource, CheckMode.ALL,
> CancelIndicator.NullImpl);
>
>
> Am I doing something wrong here? Do I need to load all resources to
> resource set (for standalone validator I had to do this)? How does the
> UI editor validate resources? I need to do exactly the same thing. I
> don't want to use the standalone injector because I suspect it messes up
> Eclipse registries and after its call not everything works correctly
> (e.g. cleaning project does not work).
>
> Thanks for help, anything will be appreciated.
>
Re: Validating model in Eclipse UI context before interpreting it [message #724885 is a reply to message #724840] Tue, 13 September 2011 08:11 Go to previous messageGo to next message
Eclipse UserFriend
I load the resource to some resource set. Perhaps it is not the right resource set? I changed the code now like this (I try to inject ResourceSet this time) but it still shows linking issues:


@Inject
private IResourceValidator validator;

@Inject
private ResourceSet resourceSet;

void validate() {
Injector interpretationInjector = MyLangaugeActivator.getInstance().getInjector("com.examples.MyLanguage");
interpretationInjector.injectMembers(this);

// File T1 uses constructs in file T2 (no errors in UI view)
resourceSet.getResource(URI.createFileURI("C:\\T2.test"), true);
Resource resource = resourceSet.getResource(URI.createFileURI("C:\\T1.test"), true);

System.out.println(validator.validate(resource, CheckMode.ALL, null));
}

When I debug the validate function, I see that resource.getResourceSet() is populated correctly ( = SynchronizedXtextResourceSet resources=[uri='file:/C:/T2.test', uri='file:/C:/T1.test']).

What is very strange is that this works without any validation errors:

void validate() {
new MyLanguageStandaloneSetup().createInjectorAndDoEMFRegistration();

Injector interpretationInjector = MyLangaugeActivator.getInstance().getInjector("com.examples.MyLanguage");
interpretationInjector.injectMembers(this);

// File T1 uses constructs in file T2 (no errors in UI view)
resourceSet.getResource(URI.createFileURI("C:\\T2.test"), true);
Resource resource = resourceSet.getResource(URI.createFileURI("C:\\T1.test"), true);

System.out.println(validator.validate(resource, CheckMode.ALL, null));
}

This is exactly the same code, but this time I create a standalone setup as the first line. I don't use the standalone injector, it's still the UI plugin injector. Seems to me like the EMFRegistration has some side effects on validation ? Any ideas?

[Updated on: Tue, 13 September 2011 08:44] by Moderator

Re: Validating model in Eclipse UI context before interpreting it [message #724886 is a reply to message #724881] Tue, 13 September 2011 08:27 Go to previous messageGo to next message
Eclipse UserFriend
Hi Sebastian,

I tried using XtextResourceSetProvider like this:

@Inject
private IResourceValidator validator;

@Inject
private XtextResourceSetProvider resourceSetProvider;

void validate() {
Injector interpretationInjector = MyLanguageActivator.getInstance().getInjector("com.example.MyLanguage");
interpretationInjector.injectMembers(this);

ResourceSet resourceSet = resourceSetProvider.get(the_project_in_which_T1_and_T2_are);
resourceSet.getResource(URI.createFileURI("C:\\Data\\runtime-EclipseApplication\\Project4\\src\\model\\T2.test"), true);
Resource resource = resourceSet.getResource(URI.createFileURI("C:\\Data\\runtime-EclipseApplication\\Project4\\src\\model\\T1.test"), true);
System.out.println(validator.validate(resource, CheckMode.ALL, null));

}

Still gives me linking errors. Of course, unless I put the mysterious Standalone setup line there.

[Updated on: Tue, 13 September 2011 08:29] by Moderator

Re: Validating model in Eclipse UI context before interpreting it [message #724897 is a reply to message #724886] Tue, 13 September 2011 08:50 Go to previous messageGo to next message
Eclipse UserFriend
Please try to use platform:/resource uris in Eclipse.

Regards,
Sebastian
--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com

On 13.09.11 14:27, Kristian Nagy wrote:
> Hi Sebastian,
>
> I tried using XtextResourceSetProvider like this:
>
> @Inject
> private IResourceValidator validator;
>
> @Inject
> private XtextResourceSetProvider resourceSetProvider;
>
> void validate() {
> Injector interpretationInjector =
> MyLanguageActivator.getInstance().getInjector("com.example.MyLanguage");
> interpretationInjector.injectMembers(this);
>
> ResourceSet resourceSet =
> resourceSetProvider.get(the_project_in_which_T1_and_T2_are);
> resourceSet.getResource(URI.createFileURI("C:\\Data\\runtime-EclipseApplication\\Project4\\src\\model\\T2.test"),
> true);
> Resource resource =
> resourceSet.getResource(URI.createFileURI("C:\\Data\\runtime-EclipseApplication\\Project4\\src\\model\\T1.test"),
> true);
> System.out.println(validator.validate(resource, CheckMode.ALL, null));
>
> }
Re: Validating model in Eclipse UI context before interpreting it [message #724906 is a reply to message #724897] Tue, 13 September 2011 09:22 Go to previous messageGo to next message
Eclipse UserFriend
Thanks very much for the suggestion. I changed it. It almost works and we are getting very close. I think the problem is elsewhere though. I found new hints:

If I start the runtime Eclipse and try straight away to validate the file, I will get linking errors. However, if I first open the files in editor or do Project -> Clean (i.e. load them into memory), I can execute the validator without linking errors!

Well, any suggestions? Smile Could force build perhaps before I validate and interpret the resources. I use Xtext 2.0.0. I will try whether I have this issue with 2.0.1 as well.
Re: Validating model in Eclipse UI context before interpreting it [message #724950 is a reply to message #724906] Tue, 13 September 2011 11:08 Go to previous messageGo to next message
Eclipse UserFriend
Thanks for your help! It appears to be working correctly in Xtext 2.0.1. In Xtext 2.0.0 after restarting Eclipse I could not validate resource without errors - first I had to open it or clean the project.

I also it seems I don't have to load all resources into resource set. It is enough to load resource-to-validate and all dependent resources will be added automatically. The final code:

IResource toInterpret = ...
// populate resource set
ResourceSet resourceSet = resourceSetProvider.get(toInterpret.getProject());
Resource emfResource = resourceSet.getResource(URI.createURI(toInterpret.getFullPath().toPortableString()), true);

// here I validate resource and all dependent resources (resource closure)
validateResourceClosure(emfResource)


void validateResourceClosure(Resource resource) {
		for (Resource r : getResourceClosure(resource)) {
			print(validator.validate(r, CheckMode.ALL, null));
		}		
	}


Set<Resource> getResourceClosure(Resource resource) {
		Set<Resource> resourceClosure = new HashSet<Resource>();
		Queue<Resource> queue = new LinkedList<Resource>();

		resourceClosure.add(resource);
		queue.add(resource);
		while (!queue.isEmpty()) {
			resource = queue.poll();
			// follow all identifiers
			TreeIterator<EObject> it = resource.getContents().get(0).eAllContents();
			while (it.hasNext()) {
				EObject eobject = it.next();
				if (eobject instanceof Identifier) {
					Symbol symbol = ((Identifier) eobject).getSymbolRef();
					Resource referenced = symbol.eResource();
					if (referenced != null
							&& !resourceClosure.contains(referenced)) {
						resourceClosure.add(referenced);
						queue.add(referenced);
					}
				}
			}
		}
		return resourceClosure;
	}


I validate the resource closure - i.e. if one file depends on another, I require that the dependent file has no errors at all. Is there a way how to do this more easily with Xtext? Some existing implementation? My way seems quite bad and could be slow on very big files (so far works well on 3000 lines).

I tried to avoid using version 2.0.1 before. The reason was that Markers are not working in this version for me correctly. I use custom markers and place them on resource. The hover message of these markers is however not shown on left editor ruler when I move mouse over. This worked well in 2.0.0. I checked the bugzilla and could not find such problem. Could it be that it is an undiscovered bug?

[Updated on: Tue, 13 September 2011 11:22] by Moderator

Re: Validating model in Eclipse UI context before interpreting it [message #724961 is a reply to message #724950] Tue, 13 September 2011 11:22 Go to previous messageGo to next message
Eclipse UserFriend
Hi Kristian,

thanks for sharing the code.

On 13.09.11 17:08, Kristian Nagy wrote:
>
> I tried to avoid using version 2.0.1 before. The reason was that Markers
> are not working in this version for me correctly. I use custom markers
> and place them on resource. The hover message of these markers is
> however not shown on left editor ruler when I move mouse over. This
> worked well in 2.0.0. I checked the bugzilla and could not find such
> problem. Could it be that it is an undiscovered bug?


Yes, that could be the case. Can you please file a ticket with a
reproducable example attached?

Thanks,
Sebastian
--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com
Re: Validating model in Eclipse UI context before interpreting it [message #724979 is a reply to message #724961] Tue, 13 September 2011 12:25 Go to previous message
Eclipse UserFriend
yes, of course
Previous Topic:Strange errors using Xtext in a plugin
Next Topic:[Xtext] Cross References in Xbase Expressions and other questions
Goto Forum:
  


Current Time: Mon Nov 03 06:22:27 EST 2025

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

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

Back to the top