Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "Henshin/Interpreter"

(New page: The Henshin interpreter is the default engine for executing model transformations defined in Henshin. The interpreter can be invoked either using a wizard or programmatically. == Interpre...)
 
Line 4: Line 4:
  
 
[[Image:Henshin-interpreter-wizard.png|320px|thumb|right|Henshin Interpreter Wizard]]
 
[[Image:Henshin-interpreter-wizard.png|320px|thumb|right|Henshin Interpreter Wizard]]
The interpreter wizard can be invoked by a right-click on a *.henshin file in the Package Explorer and selecting ''Henshin -> Apply with Henshin''.
+
The interpreter wizard can be invoked by a right-click on a *.henshin file in the Package Explorer and selecting ''Henshin→Apply with Henshin''.
  
 
In the first page of the wizard, you need to enter the following information:
 
In the first page of the wizard, you need to enter the following information:
* Choose a transformation rule or unit to be applied.
+
* Choose a transformation rule or unit to be applied.
* Specify a model file to be transformed using the rule or unit.
+
* Specify a model file to be transformed using the rule or unit.
* Enter possible parameters of the rule or unit. Make sure that the type of the parameters is correctly set. ''Ignore'' means that the parameter is not set and will be matched automatically by the interpreter.
+
* Enter possible parameters of the rule or unit. Make sure that the type of the parameters is correctly set. ''Ignore'' means that the parameter is not set and will be matched automatically by the interpreter.
  
 
If you click ''Preview'' you should either see the modifications to the model or get a message that the rule or unit could not be applied. If you think it should be applicable but still get a message that it is not, make sure the parameters are all correctly set including their types.
 
If you click ''Preview'' you should either see the modifications to the model or get a message that the rule or unit could not be applied. If you think it should be applicable but still get a message that it is not, make sure the parameters are all correctly set including their types.
Line 16: Line 16:
  
 
== Interpreter API ==
 
== Interpreter API ==
 +
 +
The interpreter can be also invoked programatically. To do this, you need to set up an Eclipse Plug-In including a dependency to the Henshin run-time.
 +
 +
=== Setting up the Plug-In Project ===
 +
 +
If you want to create a new plug-in from scratch, go to ''New...→Other...→Plug-in Development→Plug-in Project'' and follow the wizard.
 +
 +
If you have an existing project which is not a plug-in project (e.g. just a Java project), right-click on the project in the Package Explorer and select ''Configure→Convert to Plug-in projects...''.
 +
 +
Now you need to make sure that the dependency to the Henshin interpreter plug-in is set. For this, open either the ''plugin.xml'' or the ''META-INF/MANIFEST.MF'' file in your project and click on the ''Dependencies'' tab. If it is not yet listed there, add the following plug-in dependencies
 +
* ''org.eclipse.core.runtime''
 +
* ''org.eclipse.core.resources''
 +
* ''org.eclipse.emf.ecore''
 +
* ''org.eclipse.emf.ecore.xmi''
 +
* ''org.eclipse.emf.henshin.interpreter''
 +
When you are done, save and close the file.
 +
 +
=== Invoking the Interpreter ===
 +
 +
In your Java class, e.g. an IApplication (see [http://www.eclipsezone.com/eclipse/forums/t99762.html here] for a hello world example) you can now invoke the interpreter as follows.
  
 
<pre>
 
<pre>
 +
import org.eclipse.equinox.app.IApplication;
 +
import org.eclipse.equinox.app.IApplicationContext;
 +
 +
import org.eclipse.emf.ecore.EObject;
 +
import org.eclipse.emf.ecore.EPackage;
 +
import org.eclipse.emf.henshin.interpreter.EmfEngine;
 +
import org.eclipse.emf.henshin.interpreter.RuleApplication;
 +
import org.eclipse.emf.henshin.interpreter.util.Match;
 +
import org.eclipse.emf.henshin.interpreter.util.ModelHelper;
 +
import org.eclipse.emf.henshin.matching.EmfGraph;
 +
import org.eclipse.emf.henshin.model.Rule;
 +
import org.eclipse.emf.henshin.model.TransformationSystem;
 +
import org.eclipse.emf.henshin.model.impl.HenshinPackageImpl;
 +
 +
public class MyTransformation implements IApplication {
 +
 +
  public Object start(IApplicationContext context) throws Exception {
 +
 +
    // Register file extensions:
 +
    ModelHelper.registerFileExtension("henshin");
 +
    // You may need to register the extensions of your model files here too
 +
 +
    // Register packages:
 +
    HenshinPackageImpl.init();
 +
    // Add your generated packages here...
 +
 +
    // Load the transformation system:
 +
    TransformationSystem trans = (TransformationSystem) ModelHelper.loadFile("model/myTransformation.henshin");
 +
 +
    // Load the model to be transformed:
 +
    EObject root = ModelHelper.loadFile("model/myModel.xmi"); // or another registered file extension
 +
 +
    // Initialize the Henshin interpreter:
 +
    EmfGraph graph = new EmfGraph();
 +
    graph.addRoot(root);
 +
   
 +
    EmfEngine engine = new EmfEngine(graph);
 +
 +
    // Load a rule (or a transformation unit)
 +
    Rule myRule = trans.findRuleByName("MyRule");
 +
 +
    // Create a RuleApplication (or UnitApplication)
 +
    RuleApplication application = new RuleApplication(engine, myRule);
 +
 +
    // Find a match:
 +
    Match match = myRukle.findMatch();
 +
    if (match!=null) {
 +
 +
      // Apply the rule:
 +
      application.setMatch(match);
 +
      application.apply();
 +
 +
      // Save the transformed model to a file:
 +
      ModelHelper.saveFile("model/transformed.xmi", root);
 +
 +
    } else {
 +
      System.out.println("Rule " + myRule.getName() + " not applicable");
 +
    }
 +
 +
    return IApplication.EXIT_OK;
 +
  }
 +
 +
  public void stop() {
 +
  }
  
EmfGraph graph = ...
+
}
  
 
</pre>
 
</pre>

Revision as of 05:16, 7 December 2011

The Henshin interpreter is the default engine for executing model transformations defined in Henshin. The interpreter can be invoked either using a wizard or programmatically.

Interpreter Wizard

Henshin Interpreter Wizard

The interpreter wizard can be invoked by a right-click on a *.henshin file in the Package Explorer and selecting Henshin→Apply with Henshin.

In the first page of the wizard, you need to enter the following information:

  • Choose a transformation rule or unit to be applied.
  • Specify a model file to be transformed using the rule or unit.
  • Enter possible parameters of the rule or unit. Make sure that the type of the parameters is correctly set. Ignore means that the parameter is not set and will be matched automatically by the interpreter.

If you click Preview you should either see the modifications to the model or get a message that the rule or unit could not be applied. If you think it should be applicable but still get a message that it is not, make sure the parameters are all correctly set including their types.

If you click Transform the model will be transformed and saved, if possible.

Interpreter API

The interpreter can be also invoked programatically. To do this, you need to set up an Eclipse Plug-In including a dependency to the Henshin run-time.

Setting up the Plug-In Project

If you want to create a new plug-in from scratch, go to New...→Other...→Plug-in Development→Plug-in Project and follow the wizard.

If you have an existing project which is not a plug-in project (e.g. just a Java project), right-click on the project in the Package Explorer and select Configure→Convert to Plug-in projects....

Now you need to make sure that the dependency to the Henshin interpreter plug-in is set. For this, open either the plugin.xml or the META-INF/MANIFEST.MF file in your project and click on the Dependencies tab. If it is not yet listed there, add the following plug-in dependencies

  • org.eclipse.core.runtime
  • org.eclipse.core.resources
  • org.eclipse.emf.ecore
  • org.eclipse.emf.ecore.xmi
  • org.eclipse.emf.henshin.interpreter

When you are done, save and close the file.

Invoking the Interpreter

In your Java class, e.g. an IApplication (see here for a hello world example) you can now invoke the interpreter as follows.

import org.eclipse.equinox.app.IApplication;
import org.eclipse.equinox.app.IApplicationContext;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.henshin.interpreter.EmfEngine;
import org.eclipse.emf.henshin.interpreter.RuleApplication;
import org.eclipse.emf.henshin.interpreter.util.Match;
import org.eclipse.emf.henshin.interpreter.util.ModelHelper;
import org.eclipse.emf.henshin.matching.EmfGraph;
import org.eclipse.emf.henshin.model.Rule;
import org.eclipse.emf.henshin.model.TransformationSystem;
import org.eclipse.emf.henshin.model.impl.HenshinPackageImpl;

public class MyTransformation implements IApplication {

  public Object start(IApplicationContext context) throws Exception {

    // Register file extensions:
    ModelHelper.registerFileExtension("henshin"); 
    // You may need to register the extensions of your model files here too

    // Register packages:
    HenshinPackageImpl.init();
    // Add your generated packages here...

    // Load the transformation system:
    TransformationSystem trans = (TransformationSystem) ModelHelper.loadFile("model/myTransformation.henshin");

    // Load the model to be transformed:
    EObject root = ModelHelper.loadFile("model/myModel.xmi"); // or another registered file extension

    // Initialize the Henshin interpreter:
    EmfGraph graph = new EmfGraph();
    graph.addRoot(root);
    
    EmfEngine engine = new EmfEngine(graph);

    // Load a rule (or a transformation unit)
    Rule myRule = trans.findRuleByName("MyRule");

    // Create a RuleApplication (or UnitApplication)
    RuleApplication application = new RuleApplication(engine, myRule);

    // Find a match:
    Match match = myRukle.findMatch();
    if (match!=null) {

       // Apply the rule:
       application.setMatch(match);
       application.apply();

       // Save the transformed model to a file:
       ModelHelper.saveFile("model/transformed.xmi", root);

    } else {
       System.out.println("Rule " + myRule.getName() + " not applicable");
    }

    return IApplication.EXIT_OK;
  }

  public void stop() {
  }

}

Back to the top