operations.rst 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. Operations
  2. ==========
  3. While model transformations are in many tools considered to be the only types of operations on models, this is not necessarily the case in the Modelverse.
  4. Indeed, MPM advocates to use the most appropriate formalism, and, as model transformations are models themselves, this should also apply to specifying operations.
  5. As such, we extend the notion of model transformations to more general model operations.
  6. Model operations come in three types, depending on how they are ideally specified: model transformations, manual operations, and action language.
  7. In the remainder of this section, we will show all types, and what situation they are ideally suited for.
  8. Model Transformations
  9. ---------------------
  10. When model matching and rewriting is involved, model transformations are clearly the ideal solution in most cases.
  11. The LHS is used to match elements in the host model, and the RHS is put in its place.
  12. As this could also be combined with a visual concrete syntax, it could even become possible for domain experts to create a model transformation without too much programming knowledge.
  13. Nonetheless, model transformations are a completely different paradigm, based solely on matching and rewriting.
  14. Some operations have a long history in computer science, and have therefore efficient or simple procedural algorithms.
  15. For example, computing the reachability graph of a PetriNets instance can certainly be done with model transformations, but will be overly difficult.
  16. On the other hand, procedural algorithms are relatively easy (when implemented naively).
  17. The functions on model transformations are exposed using the *transformation_add_MT* and *transformation_execute_MT* operations.
  18. Examples as shown in the previous section::
  19. >>> def callback():
  20. ... instantiate(None, "Association", edge=("PetriNets/Transition", "ReachabilityGraph/Transition"), ID="PN2RG_Transition")
  21. >>> transformation_add_MT({"PetriNets": "formalisms/PetriNets"}, {"ReachabilityGraph": "formalisms/ReachabilityGraph"}, "models/pn_analyse", open("models/pn_analyse.mvc", "r").read(), callback)
  22. A situation in which model transformations are effective, is when the transformation is to map a Domain Specific Language (DSL) to a General Purpose Language (GPL).
  23. For example, when defining a mapping between a RPGame DSL and the PetriNets formalism, to facilitate formal analysis::
  24. >>> def callback():
  25. ... instantiate(None, "Association", edge=("RPGame/Tile", "PetriNets/Place"), ID="Tracability_link")
  26. >>> rule = \
  27. ... """
  28. ... Composite composite {
  29. ... {Contains} ForAll create_places_for_tiles {
  30. ... LHS {
  31. ... Pre_RPGame/Tile pre_t1 {
  32. ... label = "tile"
  33. ... }
  34. ... }
  35. ... RHS {
  36. ... Post_RPGame/Tile post_t1 {
  37. ... label = "tile"
  38. ... }
  39. ... Post_PetriNets/Place post_p1 {
  40. ... label = "place"
  41. ... }
  42. ... Post_Tracability_link (post_t1, post_p1) {
  43. ... label = "tracability"
  44. ... }
  45. ... }
  46. ... }
  47. ... ...
  48. ... }
  49. ... Initial (composite, create_places_for_tiles) {}
  50. ... OnSuccess (create_places_for_tiles, ...) {}
  51. ... OnFailure (create_places_for_tiles, ...) {}
  52. ... ...
  53. ... """
  54. >>> transformation_add_MT({"RPGame": "formalisms/RPGame"}, {"PetriNets": "formalisms/PetriNets"}, "models/rpg_to_pn", rule, callback)
  55. >>> transformation_execute_MT("models/rpg_to_pn", {"RPGame": "models/my_rpg"}, {"PetriNets": "models/exported_rpg"})
  56. Action Language
  57. ---------------
  58. Doing operations in a procedural way is supported in the Modelverse through the use of action language models.
  59. As action language is explicitly modelled, we can create models consisting solely of action language.
  60. They are therefore similar to model transformations, which are also models, but only define a different operational semantics.
  61. Instead of having a model transformation interpreter apply the schedule and rules, we merely call the action language functions enclosed in the model.
  62. The functions on action language are exposed using the *transformation_add_AL* and *transformation_execute_AL* operations.
  63. Their signature is identical to that of model transformations.
  64. The action language model itself requires a single *main* function, or it will execute the topmost function in the specified code.
  65. This function should take a single argument, being the merged model.
  66. From this model, the merged metamodel can easily be accessed.
  67. As with model transformations, the names of model entities are prefixed with their tags.
  68. For example, when defining the PetriNets analysis in action language, which is a much better match than model transformations::
  69. >>> def callback():
  70. ... instantiate(None, "Association", edge=("PetriNets/Transition", "ReachabilityGraph/Transition"), ID="PN2RG_Transition")
  71. >>> code = \
  72. ... """
  73. ... include "primitives.alh"
  74. ... Element function explore_state(model : Element, state : Element):
  75. ... ...
  76. ...
  77. ... Element function main(model : Element):
  78. ... String initial
  79. ... initial = instantiate_node(model, "ReachabilityGraph/Initial")
  80. ... instantiate_attribute(model, initial, "name", "state_0")
  81. ... ...
  82. ... return model!
  83. ... """
  84. >>> transformation_add_AL({"PetriNets": "formalisms/PetriNets"}, {"ReachabilityGraph": "formalisms/ReachabilityGraph"}, "models/pn_analyse", code, callback)
  85. >>> transformation-execute_AL("models/pn_analyse", {"PetriNets": "models/my_pn"}, {"ReachabilityGraph": "models/generated_reachability"})
  86. Manual Operations
  87. -----------------
  88. Some other operations simply cannot be automated.
  89. For example, translating natural language descriptions of requirements of a model to the actual model is not something that can be automated, at least for now.
  90. The only solution, therefore, is to do these operations manually, thereby requiring user intervention.
  91. Even though they are manual, they can still be partially automated: merging and splitting based on tags can still be done before and after the manual operation.
  92. The functions on action language are exposed using the *transformation_add_MANUAL* and *transformation_execute_MANUAL* operations.
  93. Their signature is identical to that of model transformations, except that now there is no model to provide with the *transformation_add_MANUAL*.
  94. As with model transformations, the names of model entities are prefixed with their tags.
  95. For example, when defining an operation to refine or revise a model in a DSL with respect to the requirements, such as the RPGame language from before::
  96. >>> def callback():
  97. ... instantiate(None, "Association", edge=("Requirements/Actor", "RPGame/Player"))
  98. >>> transformation_add_MANUAL({"RPGame": "formalisms/RPGame", "Requirements": "formalisms/Requirements"}, {"RPGame": "formalisms/RPGame"}, "revise_rpg", callback)
  99. Its execution, however, differs significantly from before, as we will see next.
  100. Execution callbacks
  101. ^^^^^^^^^^^^^^^^^^^
  102. As specified before, manual operations require user intervention during execution.
  103. The *transformation_execute_MANUAL* operation is therefore not a simple function that is executed and returns after some time: user input must be given.
  104. For this, an additional argument is provided as callback function.
  105. This callback function can be any sequence of operations again, such that it is possible to create or alter the model.
  106. Certainly, if it were to be possible automatically, we could have done it that way in the Modelverse.
  107. Therefore, the callback function can do much more than just these calls: all expressive power of Python is available, such as the possibility for user input (*raw_input()*).
  108. Based on this, the callback can define whatever structure is desired, possibly even going as far as starting up a dialog box or using a graphical interface.
  109. As before, the first parameter of the operations (*model_name*) should be set to *None*.
  110. For example, to specify the operations that have to be done in a manual operation, all in Python syntax::
  111. >>> def callback():
  112. ... tiles = [instantiate(None, "RPGame/Tile") for _ in range(100)]
  113. ... left_links = [instantiate(None, "RPGame/left", edge=(tiles[i], tiles[i+1])) for i in range(len(tiles) - 1)]
  114. ... ...
  115. >>> transformation_execute_MANUAL("revise_rpg", {"RPGame": "models/my_rpg", "Requirements": "models/rpg_requirements"}, {"RPGame": "models/my_rpg"}, callback)
  116. Alternatively, the operations might only be known at runtime, thereby requiring user interaction.
  117. Note that, in this case, the code in the callback is effectively the user interface offered to the user in this specific context.
  118. As such, the code can be completely tailored to the domain and problem at hand (e.g., a *create random level* operation).
  119. >>> def callback():
  120. ... while True:
  121. ... print("Please perform operation on RPGame!)"
  122. ... inp = raw_input()
  123. ... if inp == "new tile":
  124. ... instantiate(None, "RPGame/Tile")
  125. ... elif inp == "create random level":
  126. ... ...
  127. ... ...
  128. >>> transformation_execute_MANUAL("revise_rpg", {"RPGame": "models/my_rpg", "Requirements": "models/rpg_requirements"}, {"RPGame": "models/my_rpg"}, callback)
  129. Manual operations are not the only operations which might require user input.
  130. Model transformations and action language can just as well query the users for input.
  131. This is done the same way: by defining a callback function.
  132. Note that they are slightly different, in the sense that their callback functions have no option to call any of the Modelverse operations.
  133. Instead, the callback is invoked when the action language, either standalone or in the actions/constraints of model transformations, executes the *input* or *output* operations.
  134. The return value of the callback is made available to the action language (fragment) when it executes the *input* operations, and can be a single primitive type, or a list.
  135. In case it is a list, the primitive types are offered to the action language individually, requiring multiple *input* calls.
  136. Process Model
  137. -------------
  138. A next logical step is the chaining of these various operations, or activities.
  139. For this reason, the FTG+PM was previously developed.
  140. The Modelverse provides both modelling and enactment support for the FTG+PM.
  141. When modelling, the FTG+PM is just a metamodel like all others, and instances can easily be created of it.
  142. There are notions of activities, decision nodes, forks, joins, and so on.
  143. Apart from control flow, an FTG+PM also specifies data flow: which models are used in this scope, and how are they interrelated.
  144. Indeed, we could define the sequence of activities to execute, but we still need information on what are the input models of the various activities.
  145. For example, the same activity might execute multiple times, but each time on different input models and generating different output models.
  146. A simple process model is shown, which executes the operations stored in *models/A* and *models/B* in parallel.
  147. Both operations use some data, where *models/A* modifies one of its data inputs in-place, and *models/B* generates new data::
  148. Start start {}
  149. Finish finish {}
  150. Fork fork1 {}
  151. Exec activity_A {
  152. name = "models/A"
  153. }
  154. Exec activity_B {
  155. name = "models/B"
  156. }
  157. Join join1{}
  158. Data data_a1 {
  159. name = "models/data_a1"
  160. type = "formalisms/Bottom"
  161. }
  162. Data data_a2 {
  163. name = "models/data_a2"
  164. type = "formalisms/Bottom"
  165. }
  166. Data data_b1 {
  167. name = "models/data_b1"
  168. type = "formalisms/SimpleClassDiagrams"
  169. }
  170. Data data_b2 {
  171. name = "models/data_b2"
  172. type = "formalisms/Bottom"
  173. }
  174. Next (start, fork1) {}
  175. Next (fork1, activity_A) {}
  176. Next (fork1, activity_B) {}
  177. Next (activity_A, join1) {}
  178. Next (activity_B, join1) {}
  179. Next (join1, finish) {}
  180. Consumes (activity_A, data_a1) {
  181. name = "First"
  182. }
  183. Consumes (activity_A, data_a2) {
  184. name = "Second"
  185. }
  186. Produces (activity_A, data_a2) {
  187. name = "Second"
  188. }
  189. Consumes (activity_B, data_b1) {
  190. name = "input"
  191. }
  192. Produces (activity_B, data_b2) {
  193. name = "output"
  194. }
  195. This simple Process Model is equivalent to the following statements in the code.
  196. Note that we do not know the type of the activity in the operations, and therefore put an asterisk (\*) there.
  197. Also, the order in which these operations execute is undefined, as either order is fine.
  198. We therefore leave it up to the Modelverse to decide upon an order::
  199. >>> execute_transformation_*("models/A", {"First": "my_models/data_a1", "Second": "my_models/data_a2"}, {"Second": "my_models/data_a2"})
  200. >>> execute_transformation_*("models/B", {"input": "my_models/data_b1"}, {"output": "my_models/data_b2"})
  201. In this case, it doesn't seem useful to use processes, though it is possible for such processes to contain *Decision* nodes, which can be used for arbitrary control flows.
  202. As everything runs on the Modelverse, instead of constantly requiring communication with the client, performance should be higher due to reduced network latency.
  203. When enacting, the FTG+PM is executed by starting at the initial node.
  204. The first element it points to is executed, thereby branching, deciding, or executing an activity.
  205. Due to the branching, it is possible for multiple activities to be scheduled concurrently.
  206. As for now, the Modelverse sequentializes the various activities internally.
  207. When executing an activity, which can either be a model transformation, action language, or manual operation, the operation is executed with the required operation.
  208. More information on enactment was previously given.