modelling_transformation.rst 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. .. _modelling-a-model-transformation:
  2. Modelling a Model Transformation
  3. ================================
  4. A model transformation consists of a set of rules that match and rewrite parts of the model, and a schedule that governs the order in which rules are executed. In the next subsections, we explain how to create these elements. In :ref:`executing-a-model-transformation`, we explain how to execute the modelled transformation.
  5. In this section, we make use of the *TransformationEditor* toolbar:
  6. .. image:: img/transformation_editor.png
  7. Compiling a Language to a Pattern Language
  8. ------------------------------------------
  9. A prerequisite to modelling rules is to compile the language of the models you want to transform to a pattern language. This pattern language is used in the rules you model, and is very similar to the original language. This makes modelling the patterns very intuitive (since almost the same syntax is used), while changing the language to accomodate the matching and rewriting process.
  10. .. image:: img/compilation_toolbar.png
  11. To compile a language to a pattern language, press the third button on the *CompileMenu* toolbar. Navigate to the abstract syntax metamodel you want to compile:
  12. .. image:: img/compile_pattern.png
  13. Pressing the 'ok' button will create the abstract syntax metamodel *TrafficLight.pattern.metamodel* and the formalism toolbar *TrafficLight.defaultIcons.pattern.metamodel* in the */Formalisms/TrafficLight* user folder. This formalism is used alongside :ref:`the-rule-language` to model the rules of the model transformation. The pattern language differs from the original language in three ways:
  14. * Its constraints are **relaxed**. More precisely, abstract classes in the original language are made concrete, and minimum cardinalities are removed.
  15. * Elements in the language are **augmented** with four attributes: *__pLabel*, *__pMatchSubtypes*, *__pPivotIn*, and *__pPivotOut*. We will only use the two first ones. *__pLabel* is used to identify elements across LHS/RHS/NAC patterns, and *__pMatchSubtypes* indicates that instances of subclasses should also be matched.
  16. * The type of the attributes of each element is changed to *code*. This allows to model *constraints* in LHS/NAC patterns on the values of the attributes in the host model and *actions* in RHS patterns to compute the new values of the attributes after rewriting.
  17. .. warning:: Constraint and action code in patterns is written in Python!
  18. Initializing Schedules/Rules
  19. ----------------------------
  20. A formalism created with the "new formalism" button on the *MainMenu* toolbar (see :ref:`overview`) comes initialized with two schedules: *OperationalSemantics/T_OperationalSemantics.model* and *TranslationalSemantics/T_TranslationalSemantics.model*. These can be used to model the schedule of the formalism's operational semantics (a simulator) and translational semantics (a mapping onto a domain with known semantics), respectively.
  21. If you want to initialize a new schedule, use the fourth button on the *TransformationEditor* toolbar. This will present you with a file browser, which allows you to choose a location and a name for the schedule. The schedule will automatically open in a new AToMPM instance.
  22. .. note:: A schedule's name has to start with the T\_ prefix!
  23. If you want to initialize a new rule, use the fifth button on the *TransformationEditor* toolbar. This will present you with a file browser, which allows you to choose a location and a name for the rule. The rule will automatically open in a new AToMPM instance.
  24. .. note:: A rule's name has to start with the R\_ prefix!
  25. .. _the-rule-language:
  26. The Rule Language
  27. -----------------
  28. When creating a rule, it is initialized with the three basic components of every rule: a *Negative Application Condition* (NAC), a *Left-Hand-Side* (LHS), and a *Right-Hand-Side* (RHS).
  29. .. image:: img/rule_init.png
  30. :scale: 50
  31. In general, a rule has to have exactly one LHS, exactly one RHS, and zero or more NACs. The LHS is used to *match a pattern* in the host model. It is a positive application condition: if the pattern is found in the host model, the rule can be applied. If the pattern in one of the NACs is found, however, the rule is disabled. The RHS is used to *rewrite the pattern* in the host model.
  32. To model the patterns, we need to load the pattern language we compiled earlier. Press the third button on the *TransformationEditor* toolbar. Navigate to your pattern metamodel:
  33. .. image:: img/load_pattern_metamodel.png
  34. This will load the formalism toolbar of the pattern language for use in LHS, RHS, and NAC patterns.
  35. LHS and NAC
  36. ^^^^^^^^^^^
  37. The LHS and NACs are so-called *precondition patterns*. When a rule is selected for execution, it will try to find a match in the host model for the LHS. If one is found, it will try to match all the NACs. If a match is found for one of them, the rule is not executed.
  38. .. image:: img/example_LHS.png
  39. In the example LHS above, we try to find the current interrupt in the interrupt list, but only if the clock has advanced to a point in time *after or equal to* the time specified in that interrupt. To do this, we model:
  40. * An instance of *__pInterrupt* (with label '1'). Each attribute has to assign a boolean value to the 'result' variable, indicating whether the instance can be matched based on the value of the attribute. The current value can be accessed using the *getAttr()* function. For a complete overview of functions that can be called from action and condition code in patterns, see :ref:`mt-action-library`. In our example, we only want to make sure the *current* attribute is *True*. We set all conditions to *result = True* (the default), except for the *current* attribute, where we require *result = (getAttr() == True)* -- or, more simply, *result = getAttr()*.
  41. * An instance of the *AbstractInterrupt*, connected to the first interrupt. We instantiate the abstract class here, because potentially, it is the *end* of the list, and we want to make sure to match both normal interrupts and end interrupts.
  42. * The *Time* instance (which we know is a singleton).
  43. The condition that the clock needs to have advanced beyond the time specified in the current interrupt cannot be modelled as a condition on one single attribute, since it spans multiple instances (the *Time* instance and the first *Interrupt* instance). A precondition pattern also allows a global condition, which needs to be satisfied in order for the pattern to match. In this case, we get the current time, the event time, and compare them. Again, the resulting boolean is assigned to the variable *result*.
  44. When executing the transformation, AToMPM will try to find a match for each of the pattern elements. If one is found, it will map each *__pLabel* value to a matched element::
  45. {
  46. '0': <AbstractInterrupt Instance>
  47. '1': <Interrupt Instance>
  48. '2': <Next Instance>
  49. '3': <Time Instance>
  50. }
  51. This is called a binding. AToMPM will find as many matches as possible. This results in a match set.
  52. RHS
  53. ^^^
  54. The RHS is the *postcondition pattern*. It specifies how the elements matched by the LHS should be rewritten. In general:
  55. * Elements in the RHS with a *__pLabel* that cannot be found in the LHS are created.
  56. * Elements in the LHS with a *__pLabel* that cannot be found in the RHS are deleted.
  57. * Elements in the RHS with a *__pLabel* that can be found in the LHS are updated according to the actions modelled in the RHS.
  58. .. image:: img/example_RHS.png
  59. The task of the right-hand side of our rule is to change the current interrupt to the next one. The *result* attribute now should get the new value of the attribute. If nothing needs to change, one can simply write *result = getAttr()*. In this case, the *current* attribute of the first interrupt is changed to *False*, while the *current* attribute of the second interrupt is changed to *True*. The rest of the attributes remains unchanged. For a complete overview of functions that can be called from action and condition code in patterns, see :ref:`mt-action-library`.
  60. The MoTif Scheduling Language
  61. -----------------------------
  62. Rules are scheduled using a scheduling language. We use *MoTif*, which exposes a number of primitives, or *steps*. A schedule consists of a number of steps starting at the *Start* pseudostep and ending in either a *EndSuccess* or *EndFail* pseudostep. Each step is connected to two other steps with a *success* or a *fail* link. Depending on the type of step, the referenced rule(s) are executed in a particular way. The steps exposed by MoTif are:
  63. .. image: img/MoTif_toolbar.png
  64. #. **ARule**: Atomic rule. Executes the rule for one match found. If no matches are found, it fails.
  65. #. **QRule**: A query. Succeeds if the LHS matches and the NACs do not match. The RHS of the rule is ignored.
  66. #. Unused.
  67. #. Unused.
  68. #. **FRule**: For-all rule. Executes the rule for each match in the match set. It fails if no matches can be found.
  69. #. **SRule**: Sequence rule. Executes the rule until no more matches can be found. It fails if no matches can be found.
  70. #. **Start**: The start of the schedule. Can only appear once.
  71. #. **EndSuccess**: The end of the schedule (indicating success). Can occur more than once, but the schedule ends when one of them is reached.
  72. #. **EndFail**: The end of the schedule (indicating failure). Can occur more than once, but the schedule ends when one of them is reached.
  73. #. **Sequence**: Executes the referenced transformation until it fails.
  74. #. **BRule**: Allows for other steps to be nested. Executes (non-deterministically) one of the succeeding child steps.
  75. #. **BSRule**: Executes (non-deterministically) one of the succeeding child steps until none of them succeeds.
  76. #. Unused.
  77. #. Unused.
  78. #. Unused.
  79. #. Unused.
  80. .. _mt-action-library:
  81. Action Library
  82. --------------
  83. .. rst-class:: table-with-borders
  84. .. note:: The *_id* attribute can either be an abstract syntax identifier, or a *__pLabel* value.
  85. +----------------------------------------+-------------------------------------+-------------------------------------------------------------------------------+
  86. | name | parameters | Description |
  87. +========================================+=====================================+===============================================================================+
  88. | getAttr(_attr[, _id]) | * *_attr*: string | Return the value of the given attribute from the given entity, specified |
  89. | | * *_id*: string | via its abstract syntax identifier or __pLabel. If no such attribute exists, |
  90. | | | the current constraint, action, mapper, or parser is immediately interrupted |
  91. | | | and an error is presented to the user. |
  92. +----------------------------------------+-------------------------------------+-------------------------------------------------------------------------------+
  93. | hasAttr(_attr[, _id]) | * *_attr*: string | Returns *true* if the given entity, specified via its abstract syntax |
  94. | | * *_id*: string | identifier or __pLabel, has an attribute named *attr*, *false* otherwise. |
  95. +----------------------------------------+-------------------------------------+-------------------------------------------------------------------------------+
  96. | getAttrNames(_id) | * *_id*: string | Return all attribute names of the given entity, specified via its abstract |
  97. | | | syntax identifier or __pLabel. |
  98. +----------------------------------------+-------------------------------------+-------------------------------------------------------------------------------+
  99. | getAllNodes(_fulltypes) | * *_fulltypes*: list<string> | Return the abstract syntax identifiers of all entities whose types are |
  100. | | | contained within the fulltypes array. If it is omitted, return the abstract |
  101. | | | syntax identifiers of all entities. The notion of full types is best |
  102. | | | explained by example: the full type of a SimpleClassDiagram.Class entity is |
  103. | | | "/Formalisms/__LanguageSyntax__/SimpleClassDiagram/SimpleClassDiagram/Class". |
  104. +----------------------------------------+-------------------------------------+-------------------------------------------------------------------------------+
  105. | getNeighbors(_dir[, _type, _id]) | * *_dir*: string | Return neighbours of the given entity, specified via its abstract syntax |
  106. | | * *_type*: string | identifier or __pLabel. The *_dir* parameter can take on three values: "in" |
  107. | | * *_id*: string | implies that only inbound neighbours should be returned, "out" implies that |
  108. | | | only outbound neighbours should be returned, "*" implies that neighbours in |
  109. | | | either direction should be returned. Finally, the *_type* parameter can be |
  110. | | | set to indicate that only neighbours of the given full type should be |
  111. | | | returned. The notion of full types is best explained by example: the full |
  112. | | | type of a SimpleClassDiagram.Class entity is |
  113. | | | "/Formalisms/__LanguageSyntax__/SimpleClassDiagram/SimpleClassDiagram/Class". |
  114. | | | To match any type, use "*". |
  115. +----------------------------------------+-------------------------------------+-------------------------------------------------------------------------------+
  116. | print(msg) | * *msg*: string | Print the given string to the console that launched the AToMPM back-end. |
  117. +----------------------------------------+-------------------------------------+-------------------------------------------------------------------------------+
  118. | setAttr(_attr, _val[, _id]) | * *_attr*: string | Update the given attribute of the given entity, specified via its abstract |
  119. | | * *_val*: any | syntax identifier, to the given value. Note that this function is only |
  120. | | * *_id*: string | available from within meta-modelling actions. Also, beware the fact that |
  121. | | | calls to *setAttr()* are not treated like normal model updates (*i.e.*, they |
  122. | | | do not trigger pre-editing constraints and post-editing actions). |
  123. +----------------------------------------+-------------------------------------+-------------------------------------------------------------------------------+
  124. | httpReq(method, host, url, data) | * *method*: string | Perform a synchronous HTTP request given an HTTP method (GET, PUT, POST or |
  125. | | * *host*: string | DELETE), a URL and a key-value dictionary of parameters. If host is |
  126. | | * *url*: string | undefined, the request is automatically routed to the AToMPM backend. This |
  127. | | * *data*: dict | can be useful to make use of the Remote API from within rule code. |
  128. +----------------------------------------+-------------------------------------+-------------------------------------------------------------------------------+
  129. | isConnectionType(_id) | * *_id*: string | Return true if the given entity, specified via its abstract syntax identifier |
  130. | | | or its __pLabel, is a connection type, false otherwise. |
  131. +----------------------------------------+-------------------------------------+-------------------------------------------------------------------------------+
  132. | session_get(_key) | * *_key*: string | The *Transformation Session* is a sandbox of sorts that enables miscellaneous |
  133. | session_put(_key, _val) | * *_val*: any | user data to be easily accessed and stored across several rule and |
  134. | | | transformation executions. It is only ever cleared when a transformation |
  135. | | | is (re-)loaded. These methods respectively enable retrieving and |
  136. | | | setting/updating a stored value. |
  137. +----------------------------------------+-------------------------------------+-------------------------------------------------------------------------------+
  138. | sys_call(_args) | * *_args*: list<string> | Perform a system call on the machine hosting the AToMPM back-end. An example |
  139. | | | value for the _args parameter is ["ls", "-l"]. |
  140. +----------------------------------------+-------------------------------------+-------------------------------------------------------------------------------+
  141. | sys_mkdir(_path) | * *_path*: string | Create the given directory (or directories). |
  142. +----------------------------------------+-------------------------------------+-------------------------------------------------------------------------------+
  143. | sys_readf(_path) | * *_path*: string | Return the contents of the given file. |
  144. +----------------------------------------+-------------------------------------+-------------------------------------------------------------------------------+
  145. | sys_writef(_path, _content, [_append]) | * *_path*: string | Write content to the given file, overwriting its contents if the append |
  146. | | * *_content*: string | attribute is set to *false*. |
  147. | | * *_append*: boolean | |
  148. +----------------------------------------+-------------------------------------+-------------------------------------------------------------------------------+
  149. TrafficLight Example
  150. --------------------
  151. As an example, let's model the operational semantics of the *TrafficLight* language. The requirements are:
  152. * First, the global time is initialized to 0. Then, the current state is made to refer to the start state of the model. Then, the simulation continues until no more state transitions are possible.
  153. * A state transition T from the current state C to a new state (possibly the same) N occurs:
  154. * If there is a timed state transition T from the current state C to a new state (possibly the same) N, this transition will be taken if the global time + the time delay of the transition is strictly smaller than the time of the earliest interrupt notice in the external interrupt list (if any is present: the interrupt list may be empty). Combined with the description of the interrupt transition below, the strictly smaller required will ensure that an external interrupt takes priority over a timed transition, if they happen to occur at exactly the same time (a so-called event collision). The effects of this state transition are:
  155. #. The global time is updated to the global time + the time delay of the transition
  156. #. The current state is updated to state N
  157. * If there is an interrupt transition from the current state C for which the interrupt name is equal to the interrupt name in the earliest interrupt notice in the external interrupt list. The effects of this state transition are:
  158. #. The global time is updated to the time in the interrupt notice
  159. #. The current state is updated to state N
  160. #. The interrupt notice is removed from the interrupt list
  161. .. image:: img/example_schedule.png
  162. The first step in the initializes the simulation: it looks for the initial state and makes it the current state.
  163. Then, it tries executing a timed transition. If it fails, it tries executing an interrupt.
  164. At the end, the next interrupt is chosen if the time has advanced sufficiently.
  165. .. image:: img/example_rules.png
  166. The rules are shown above. Most of them do not change the structure of the model, and most of the computation is done in the actions and constraints of the patterns.