May 29, 2013

Henshin 0.9.8: Working with Lists

We are happy to announce the release of Henshin 0.9.8! Henshin is a graph-based model transformation language for EMF. Besides a number of bugfixes and performance improvements for the UI, the new version brings support for edge indices, which allow you to work with lists.

Since Henshin is based on graph transformations, it did not allow you to access and manipulate the order of elements in multi-valued references -- until now. This was in fact a design choice, because in most cases we do not care about the order of elements. There are models and applications though were the order matters and is in fact crucial for the semantics of the models. The standard example that comes to my mind is if you build a model from a textual language. There it is natural to represent the order of statements or commands in a list. For example, JaMoPP constructs models from Java source code and in these models, the order of, say, statements is crucial. And of course, we want to be able to apply Henshin to such models as well, e.g., to implement program transformations like refactorings, translations to other languages, or to define an operational semantics (essentially building an interpreter).

Henshin 0.9.8 comes with a new feature called edge indices, which allow you to access and manipulate the order of elements in multi-valued references. We added a new string attribute called index to the Edge class which you can use to specify an expression that evaluates to an integer. In the graphical editor, we use an array notation to specify the indices. For example, if you have an edge of type ref, you could write ref[0] to access the first element in the reference list. We adopted a notation from Python where negative indices can be used to access elements starting from the end of the list, e.g., ref[-1] matches the last element of any list.


If your rule has a parameter i, you can write ref[i] to match the i-th element. Note that if no value for i is passed as a parameter, the match finder will try to find a valid value for you such that a match of the graph pattern can be found. You can also define a change in the index, i.e., you can move elements in a list. For example, if you have parameters i and j, you can write ref[i->j] to move the element at position i to the position j. The rule swap in the screenshot shows an example for this. It matches two EClasses with names x and y in a package and swaps their positions in the eClassifiers reference list if (and only if) the condition (i<j) && (x>y) holds where (x>y) refers to the lexicographical order of strings (JavaScript syntax). Thus, if we apply this rule in a loop, we achieve a sorting of the EClasses in the package by their names. Note that the engine will match all parameters automatically.

Let me say that it is not our intent to implement sorting algorithms (or any other algorithms for that matter) in Henshin. Henshin rules are declarative in nature and not designed for doing such things. Nevertheless, it is important that we can deal with lists in Henshin in order to apply it to order-sensitive models like program models.

Henshin 0.9.8 is available at our release update site and the above sorting example can be found in the examples plugin shipped with the release (or as available in our source code repository).

2 comments:

  1. Hi,
    that's a cool feature however i get

    java.lang.IndexOutOfBoundsException:
    targetIndex=1, size=1

    when calling inputEvents[-1 -> 0] with inputEvents.size == 1
    It should move the last element (index 0) to the start of the array (index 0)
    is this a bug?

    Have a nice weekend!
    David

    ReplyDelete
  2. Hi David,
    We actually have unit tests for this. To do anything we need at least a stack trace or better: the example files.

    Cheers,
    Christian

    ReplyDelete