21.3.11

Verify And Beautify Model-2-Text Transformation Output

One of the projects which I developed during my PhD thesis is Azmun, a model based testing framework based on various MDD technologies. One of the tasks within Azmun is to transform an UML-based test model to a model checking problem in order to use a model checker for automated test case generation. Although Azmun does not require a specific model checker, I mostly use NuSMV for my research. NuSMV has an own language to describe a system, and I developed a model-2-text (m2t) transformation from UML to NuSMV using Xpand. And here comes the problem:

Q: How do we ensure that the output of our Xpand transformation is syntactically correct, and how can we format our output?

These two questions may seem to be independend, but as you will see in a minute, we can tackle both problems with one approach. As a side project, I created the nusmv-tools Eclipselabs project, which hosts some Eclipse-based tools. One of these tools is a rich text editor based on Xtext. And now we have the solution for our problem:

A: We create a Xpand beautifier based on the Xtext parser and serializer.

The following lines show how we solve our problem in Azmun:

public final class NuSMVCodeGenerator extends WorkflowComponentWithModelSlot {
  private Generator m_generator = null;

  @Override
  public void checkConfiguration(final Issues p_issues) {
    super.checkConfiguration(p_issues);

    m_generator = new Generator();
    m_generator.addMetaModel(new UML2MetaModel());
    // add other metamodels

    m_generator.setFileEncoding("utf-8");
    m_generator.setExpand("foo::bar::UML2NuSMV() FOR " + getModelSlot());

    final Outlet outlet = new Outlet("/some/path");
    outlet.addPostprocessor(new PostProcessor() {

      @Override
      public void beforeWriteAndClose(final FileHandle p_fileHandle) {
      }

      @Override
      public void afterClose(final FileHandle p_fileHandle) {
        try {
          if (p_fileHandle.getAbsolutePath() == null 
            || !p_fileHandle.getAbsolutePath().endsWith(".nusmv")) {
            return;
          }
          NuSMVStandaloneSetup.doSetup();
          final ResourceSet resourceSet = new ResourceSetImpl();
          final URI fileURI = URI.createFileURI(p_fileHandle.getAbsolutePath());
          final Resource resource = resourceSet.getResource(fileURI, true);
          for (final Diagnostic diagnostic : resource.getWarnings()) {
            p_issues.addWarning(diagnostic.getMessage());
          }
          for (final Diagnostic diagnostic : resource.getErrors()) {
            p_issues.addError(diagnostic.getMessage());
          }
          if (!resource.getErrors().isEmpty()) {
            return;
          }
          final SaveOptions saveOptions = SaveOptions.newBuilder().format().getOptions();
          resource.save(saveOptions.toOptionsMap());
        } catch (final Exception e) {
          throw new RuntimeException(e);
        }
      }
    });
    m_generator.addOutlet(outlet);
    m_generator.checkConfiguration(p_issues);
  }

  @Override
  protected void invokeInternal(final WorkflowContext p_ctx, 
      final ProgressMonitor p_monitor, final Issues p_issues) {
    m_generator.invoke(p_ctx, p_monitor, p_issues);
    m_generator = null;
  }
}

We configure the Xpand workflow component with a PostProcessor, which is called for every file we create during the m2t transformation. In the afterClose method, we load the generated file using the standard EMF ResourceSet mechanism, and immediately save the resource. The options we add to the Resource.save method specify that we want the output to be formatted by Xtext.

With this approach, we can be sure that the output we generate in the m2t transformations conforms to the NuSMV language. And as a side-effect we format our output.

Happy transforming!

0 Kommentare: