Proper use of TransformationFeedback

Post here questions and problems related to oXygen frameworks/document types.
stsk
Posts: 4

Proper use of TransformationFeedback

Fri Sep 11, 2015 10:44 am

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: 5514

Re: Proper use of TransformationFeedback

Fri Sep 11, 2015 11:14 am

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

Re: Proper use of TransformationFeedback

Fri Sep 11, 2015 12:13 pm

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!

Return to “SDK-API, Frameworks - Document Types”

Who is online

Users browsing this forum: No registered users and 1 guest