Proper use of TransformationFeedback

Post here questions and problems related to oXygen frameworks/document types.
stsk
Posts: 4
Joined: Fri Sep 11, 2015 9:56 am

Proper use of TransformationFeedback

Post by stsk »

Hi,

As a novice Java programmer, I'm having trouble figuring out how to use TransformationFeedback properly within an AuthorOperation. I'm using AuthorEditorAccess.runTransformationScenarios() to execute a transformation on the current document. The transformation is not supposed to modify the document itself, and so far everything works. The problem occurs when I try to add more functionality which modifies the current document by deleting nodes as well as certain external resources on which the transformation depends. In order to make sure that the transformation succeeds, I need to prevent these other operations from executing prematurely. I've tried to set a flag using TransformationFeedback.transformationFinished(). However, I can't seem to avoid an infinite loop while checking the flag. My first attempt was a naive busy loop, but I have also tried synchronized blocks calling wait() from doOperation() and notify() from transformationFinished(), but neither works. As you can probably tell, my grasp on threading and concurrency issues is rather shaky. Any help will be much appreciated.

The outline of my code is basically as follows:

Code: Select all

private volatile boolean transformationFinished;

public void doOperation() {
runTransformationScenarios();
while (!transformationFinished) continue;
modifyDocument();
}

private class Feedback extends TransformationFeedback {

public void transformationFinished() {
transformationFinished = true;
}

public void transformationStopped() {
transformationFinished = true;
}

}
Radu
Posts: 9041
Joined: Fri Jul 09, 2004 5:18 pm

Re: Proper use of TransformationFeedback

Post by Radu »

Hi,

Doing that while (!transformationFinished) continue; blocks the entire thread which has called the doOperation method. And that thread is the AWT thread which is very important to the GUI interraction.
Indeed the runTransformationScenarios runs on a separate thread but it also sometimes calls events on the AWT thread (like updating the status). But because you blocked the AWT thread with the while loop, the transformation will never finish.
But you could try to do something like this instead:

Code: Select all

    editor.runTransformationScenarios(null, new TransformationFeedback() {
@Override
public void transformationStopped() {
}
@Override
public void transformationFinished(boolean success) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
modifyDocument();
}
});
}
});
Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
stsk
Posts: 4
Joined: Fri Sep 11, 2015 9:56 am

Re: Proper use of TransformationFeedback

Post by stsk »

Yes, I suspected something like that and did look at invokeLater(), but I never even considered just calling the methods from transformationFinished(). Thank you for the quick reply!
Post Reply