|
|
|
|
|
|
Re: Increasing memory consumption [message #659135 is a reply to message #658923] |
Fri, 11 March 2011 08:44 |
Jan Koehnlein Messages: 760 Registered: July 2009 Location: Hamburg |
Senior Member |
|
|
Sorry, but this is exactly how NOT to use this API :-)
We introduced the IUnitOfWork to mark a transaction on the resource.
Every access to the model elements should happen within its exec method.
You should never pass EObjects or Resources as a result of an
IUnitOfWork, because that opens the door for memory leaks and
inconsistency in your tooling, as the resource is free to change
completely if you don't hold the lock.
If you want to keep a pointer to a model element, store its URI and
resolve it in the same IUnitOfWork in which you access it.
Am 10.03.11 14:12, schrieb Michel Simeon:
> I am not sure I fully understand your discussion, but I suspect I have
> the same problem.
>
> I am accessing my model "live" using the XText documentation:
>
> private static XtextResource getXtextResource(IEditorPart editor){
> IXtextDocument xtextDocument = ( (XtextEditor) editor).getDocument();
> XtextResource resource = xtextDocument.readOnly(
> new IUnitOfWork<XtextResource, XtextResource>() {
> public XtextResource exec(XtextResource state) throws Exception {
> return state;
> }
> });
> return resource;
> }
>
> Within my main model processing method I call the above to get the
> resource, use an IResourceValidator to find out if there are errors, and
> then access the model with:
> Model model = (Model) resource.getContents().get(0);
>
> Is it correct that the garbage collector will automatically reclaim
> memory on exit of my main model processing method ?
>
> Thanks
> MS
>
--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com
---
Get professional support from the Xtext committers at www.typefox.io
|
|
|
Re: Increasing memory consumption [message #659171 is a reply to message #659135] |
Fri, 11 March 2011 10:53 |
Michel Simeon Messages: 130 Registered: December 2009 |
Senior Member |
|
|
Jan,
Thank you for your answer.
I have no problem getting to my model by having it returned directly using IUnitOfWork .
My problem is the following:
- I can get the active editor which is the one with my DSL model;
- I need to find out if there are errors in the editor; I was using the resource to get a IResourceValidator; what is the alternative ?
- I need the IProject where my editor file is located in order to send some output there; I was using resource.getURI() to get there; again, what is the alternative ?
Thanks again for helping
MS
======
I realized I could get the URI and the list of issues from the validator through successive use of IUnitOfWork :
URI uri = xtextDocument.readOnly(
new IUnitOfWork<URI, XtextResource>() {
public URI exec(XtextResource resource) throws Exception {
return resource.getURI();
}
});
List<Issue> issues = xtextDocument.readOnly(
new IUnitOfWork<List<Issue>, XtextResource>() {
public List<Issue> exec(XtextResource resource) throws Exception {
IResourceValidator validator = resource.getResourceServiceProvider().getResourceValidator() ;
return validator.validate(resource, CheckMode.ALL, null);
}
});
Is this the proper way to do it ?
But then you said: Every access to the model elements should happen within its exec method. Does it mean that I should not do :
Model model = xtextDocument.readOnly(
new IUnitOfWork<Model,XtextResource>() {
public Model exec(XtextResource resource) throws Exception {
return (Model)resource.getContents().get(0);
}
});
and then work from there ?
You wrote:
If you want to keep a pointer to a model element, store its URI and resolve it in the same IUnitOfWork in which you access it.
I am not sure I understand. Could you elaborate.
Thanks again
MS
[Updated on: Fri, 11 March 2011 14:13] Report message to a moderator
|
|
|
Re: Increasing memory consumption [message #659209 is a reply to message #659171] |
Fri, 11 March 2011 14:31 |
Jan Koehnlein Messages: 760 Registered: July 2009 Location: Hamburg |
Senior Member |
|
|
Am 11.03.11 11:53, schrieb Michel Simeon:
> Jan,
>
> Thank you for your answer.
>
> I have no problem getting to my model by having it returned directly
> using IUnitOfWork .
I just meant you should never pass a Resource or an EObject across the
borders of an IUnitOfWork because this is the root of inconsistencies
and memory leaks. URIs are OK.
> My problem is the following:
> - I can get the active editor which is the one with my DSL model;
Was that a question? If so, it depends on the context, e.g. from within
a handler you can call
org.eclipse.xtext.ui.editor.utils.EditorUtils.getActiveXtext Editor(ExecutionEvent)
on the execution event. Otherwise, PlatformUI is your friend.
> - I need to find out if there are errors in the editor; I was using the
> resource to get a IResourceValidator; what is the alternative ?
That will revalidate the model, which is not necessary as the model
should already be validated.
Errors in the editor are stored in its annotation model. You can access
it via
xtextEditor.getDocumentProvider().getAnnotationModel(editorI nput)
Helper methods in org.eclipse.xtext.ui.util.IssueUtil help you to deal
with annotations.
If you save, the annotations are converted into markers. IFile allows
you to access these.
> - I need the IProject where my editor file is located in order to send
> some output there; I was using resource.getURI() to get there; again,
> what is the alternative ?
As editors are usually editing IFiles, get the editorInput, cast it to
IFileEditorInput, get the IFile and its IProject. If you have the
resource URI, you can use the IURI2StorageMapper to find the
corresponding IFile.
> Thanks again for helping
> MS
--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com
---
Get professional support from the Xtext committers at www.typefox.io
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.04063 seconds