Skip to content

Model Listener

Language Namespace : com.mbeddr.mpsutil.modellisteners

The language adds a new aspect called listeners where listeners can be created that react to various changes in the model: instance refers to the node from which the model listeners received the event, child is the child node, roleis link of a child or reference.

To listen for events in any node, select BaseConcept as the target of the model listeners. Be careful when choosing this approach as it can be slow when many events happen. Model listener can be used, for example, to calculate values when something changes or execute an additional action when a root node is removed. When a node is replaced with another one or a node is created, you might want to use node factories or concept constructors instead.

Example for an empty model listener:

model listeners for RootConcept {
    child added in role original (instance, child)->void {
    }
    before child removed in role original (instance, child)->void {
    }
    reference added in role reference1 (instance, target, role)->void {
    }
    reference removed in role reference1 (instance, target, role)->void {
    }
    property changed of property property1 (instance, propertyName, oldValue, newValue)->void {
    }
    root added (instance, model)->void {
    }
    before root removed (instance, model)->void {
    }
    root removed (instance, model)->void {
    }
}

The listeners are called synchronously. Since they are operating on the models itself, there is no access to the opened editors. That means that the cursor might jump in the editor when a node is added or removed. The listeners also might make changes to the models unpredictable and not easily revertible, so think twice before using them.