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"

m (Heidelme.students.uni-marburg.de moved page Henshin Interpreter to Henshin/Interpreter: Convert to subpage)
m (Add category Modeling)
Line 111: Line 111:
 
'''Enabling and disabling error markers in Eclipse Java Editor''' The marker is automatically enabled on projects when ''org.eclipse.emf.henshin.interpreter'' bundle is required. Those who want's to disable the marker, can do it by right click on the project - ''Configure'' - ''Disable Henshin marker''. Once the marker is deactivated, it will never be enabled automatically on the next eclipse start except you reactivate it. Reactivating works the same way as deactivating. You can also activate the marker on projects which don't required the ''interpreter'' bundle. The marker of this Projects will be not activated automatically on the next eclipse start, because it is not necessary.
 
'''Enabling and disabling error markers in Eclipse Java Editor''' The marker is automatically enabled on projects when ''org.eclipse.emf.henshin.interpreter'' bundle is required. Those who want's to disable the marker, can do it by right click on the project - ''Configure'' - ''Disable Henshin marker''. Once the marker is deactivated, it will never be enabled automatically on the next eclipse start except you reactivate it. Reactivating works the same way as deactivating. You can also activate the marker on projects which don't required the ''interpreter'' bundle. The marker of this Projects will be not activated automatically on the next eclipse start, because it is not necessary.
  
[[Category:Henshin]]
+
[[Category:Henshin]][[Category:Modeling]]

Revision as of 06:12, 7 February 2018

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 using an API.

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 Transformation.

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, either as an IApplication in Eclipse or as a simple stand-alone Java application.

Make sure you have all dependencies fulfilled to the Henshin runtime and to EMF. For Henshin, you should include the the model and the interpreter plug-in into your classpath (e.g. as a plug-in dependency). For examples, see also the Java source codes in the documented examples page.

Loading & Saving

For loading and saving of models and transformations you can use the class HenshinResourceSet, which provides some convenience methods. Below you can see how to load models and transformations.

// Create a resource set for the working directory "my/working/directory"
HenshinResourceSet resourceSet = new HenshinResourceSet("my/working/directory");
 
// Load a model:
Resource model = resourceSet.getResource("mymodel.xmi");
 
// Load the Henshin module:
Module module = resourceSet.getModule("mytransformation.henshin");
 
// Apply the transformation (see below)...
 
// Save the model:
model.save(null);

Note that all relative file paths will be resolved using the working directory of the resource set. You should make sure that you use a single resource set for all loading and saving operations. There are also some more convenience methods for loading and saving (see the Javadoc).

Transforming and more

Here is a typical use case for the interpreter:

// Prepare the engine:
Engine engine = new EngineImpl();
 
// Initialize the graph:
EGraph graph = new EGraphImpl(model);
 
// Find the unit to be applied:
Unit unit = module.getUnit("myMainUnit");
 
// Apply the unit:
UnitApplication application = new UnitApplicationImpl(engine, graph, unit, null);
application.execute(null);

This is all code necessary to execute a transformation in Henshin! Note that engines should always be reused (you need only a single instance). The same holds for the EGraph. UnitApplications can be reused, but there is also no harm in creating new instances.

You can also use the following convenience method. It also properly updates the contents of the model resource based on the changes to the EGraph.

InterpreterUtil.applyToResource(unit, engine, model);

Setting and Getting Parameter Values This is how you can assign parameter values for the unit application (before executing it), and retrieving the resulting values (after the application):

application.setParameterValue("p1", "helloworld");
application.setParameterValue("p2", object);
 
application.execute(null);
 
Object newValue = application.getResultParameterValue("p2");

If you want to apply a single rule, you can also use RuleApplication. In this class, you can also specify partial or complete matches, and get the result match (a.k.a. co-match) after the rule application. The classes for parameter assignments and matches implement toString() -- so you can also easily inspect their details.

Canceling, Logging and Profiling Maybe you noticed already the null parameter passed to the execute() method. Here you can also base an ApplicationMonitor instance which give you the possibility to inspect and to cancel unit or rule applications. For logging, you can use the already implemented class LoggingApplicationMonitor which will print detailed logs on every rule application. You can filter the logging ask this monitor to automatically save the intermediate results of your transformation or to abort the execution after a certain amount of steps. Another very interesting implementation is ProfilingApplicationMonitor which automatically collects statistics on the execution times for rule. Just run printStats() after the transformation to find out where the bottlenecks are in your transformation. If you think that Henshin should be able to apply a particular rule faster, contact the Henshin mailing list with your profiling results.

Finding matches Sometimes, you just want to find matches for a rule without actually applying the rule. You can directly use the engine for this:

Match partialMatch = new MatchImpl(rule);  // can be also null
partialMatch.setParameterValue(p1,"foo");
 
// Iterate over all matches and print them on the console:
for (Match match : engine.findMatches(rule, graph, partialMatch)) {
  System.out.println(match);
}

Note that matches are computed on-demand and that findMatches() returns an instance of Iterable<Match>. If you need all matches anyway, you can also use InterpreterUtil.findAllMatches() which will return a list of all matches.

Checking graph isomorphy You can use this to check whether to EGraphs are isomorphic:

InterpreterUtil.areIsomorphic(graph1, graph2);  // or resources

Note that this is not really fast because the necessary metadata for a more efficient isomorphy checking is not available for plain EGraphs. Faster isomorphy checking is implemented separately in the state space tools but requires to compute hash codes first.

Nondeterministic rule application Due to optimisation reasons, the application of rules behaves deterministically by default - among multiple possible matches, the one chosen as starting point for the rule application remains the same over multiple executions. In some situations, a random behaviour is required. This can be achieved by configuring the engine's options in the following way:

engine.getOptions().put(Engine.OPTION_DETERMINISTIC, false);

Error marker

The purpose of the error marker is to mark paths, strings or variables in the code, that will throw exceptions by execution. Due to string based writing of values and paths, there could be errors. The error marker searches for these in the java files and marks them. The possible errors could be: wrong henshin resource path, not existing files in resource set, not existing parameters or false parameter type and so on. The error marker can be enabled and disabled.

Enabling and disabling error markers in Eclipse Java Editor The marker is automatically enabled on projects when org.eclipse.emf.henshin.interpreter bundle is required. Those who want's to disable the marker, can do it by right click on the project - Configure - Disable Henshin marker. Once the marker is deactivated, it will never be enabled automatically on the next eclipse start except you reactivate it. Reactivating works the same way as deactivating. You can also activate the marker on projects which don't required the interpreter bundle. The marker of this Projects will be not activated automatically on the next eclipse start, because it is not necessary.

Back to the top