internal.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. Internal workings
  2. =================
  3. For more detailed information on the Modelverse specification, which this project is implementing, we refer to the `Modelverse Specification <http://msdl.cs.mcgill.ca/people/yentl/files/Modelverse.pdf>`_.
  4. Information on the implementation can be found below.
  5. Modelverse State
  6. ----------------
  7. The Modelverse State is basically just an implementation of a graph library.
  8. As we have a particular kind of graph, this implementation is mostly done by hand at the moment.
  9. The notable exception to this is the RDF backend, proving that other implementations can also be used.
  10. The basic implementation just stores everything as dictionaries.
  11. All operations are then defined by doing operations on these dictionaries.
  12. The most interesting operations here are dictionary operations, which need to traverse these dictionaries in complex ways.
  13. To overcome performance problems for these operations, all results are cached (and validated afterwards).
  14. RDF backend
  15. ^^^^^^^^^^^
  16. The RDF backend requires the *rdflib* module in Python.
  17. The Modelverse graph is then stored in RDF representation and all operations on it are done using SPARQL queries.
  18. Due to this level of indirection, performance is extremely slow.
  19. To increase performance, we would likely have to make more *composite* operations, or even group different requests together internally.
  20. Modelverse Kernel
  21. -----------------
  22. The Modelverse Kernel is basically a graph transformation kernel.
  23. It consists of two parts: a small transformation engine (only a few lines in Python), and a copy of all rules it knows.
  24. Most code is an encoding of these transformation rules, and can (theoretically) be automatically generated by the Modelverse itself.
  25. This is currently not top priority, but will probably be implemented in the future.
  26. The transformation rules are an encoding of the rules presented in the specification mentioned at the top of this page.
  27. In each rule, the MvK needs to have tight communication with the MvS.
  28. For this, the rules are encoded using *yield* operations in Python.
  29. Rules therefore act as generators, outputting commands to the MvS, and immediately getting a reply.
  30. Each individual yield contains a list of operations to send to the MvS simultaneously.
  31. The result will therefore also be a list of results for each operation.
  32. As you can see in these rules, each entry in the list has a specific form.
  33. An entry is a tuple containing two elements: the code of the operation to execute, followed by a list of all parameters to pass.
  34. The code is a shortened name for the operation, such as *CN* for *create_node*.
  35. A full mapping can be found in the MvS, the most important ones are shown below.
  36. ==== ================
  37. Code Function
  38. ==== ================
  39. CN create_node
  40. CNV create_nodevalue
  41. CE create_edge
  42. CD create_dict
  43. RV read_value
  44. RE read_edge
  45. RO read_outgoing
  46. RI read_incoming
  47. RD read_dict
  48. RDN read_dict_node
  49. RDE read_dict_edge
  50. RDNE read_dict_node_edge
  51. RR read_root
  52. DN delete_node
  53. DE delete_edge
  54. ==== ================
  55. .. note::
  56. Since each yield operation works on lists even a single operation needs to be wrapped in a list.
  57. Worse, however, is that the return value will also be a list.
  58. Instead of having to take the first element each time, Python allows you to write a comma after the variable, to make it expand it automatically.
  59. Your syntax thus becomes::
  60. a, = yield [("CN", [])]
  61. Where a will now already contain the created node, and not a list with as first (and only) element the created node.
  62. Primitives
  63. ^^^^^^^^^^
  64. The Modelverse Kernel also defines the primitives users can use.
  65. Primitives are necessary since we can't go deeper at some point.
  66. It is the MvK's responsibility to implement each and every primitive defined in the bootstrap file.
  67. Primitives are implemented in the bootstrap file as having a body with an empty node.
  68. When execution goes to this node, the MvK must execute the associated primitive instead.
  69. To do this, the MvK must map all these nodes to their corresponding primitive implementation during startup.
  70. Precompiled functions
  71. ^^^^^^^^^^^^^^^^^^^^^
  72. Similar to primitives, the MvK also supports precompiled functions.
  73. A precompiled function is similar to a primitive in terms of implementation, but they do also have an implementation inside of the Modelverse itself.
  74. This means that there are two implementations: one hardcoded, and one in action language.
  75. Both should obviously be consistent.
  76. Whereas primitives are required for a functional Modelverse, precompiled functions are only a performance optimization, and can be disabled by the user for debugging purposes.
  77. This is interesting, since with precompiled functions, no intermediate values will be visible.
  78. Additionally, precompiled functions cannot be changed, as they are Python code instead of being explicitly modelled.
  79. Modelling operations
  80. ^^^^^^^^^^^^^^^^^^^^
  81. Apart from the simple action language interpreter presented before, the Modelverse State is loaded with modelling logic.
  82. This includes common functions, such as conformance checking, model transformation, and instantiation.
  83. As the code is loaded in the MvS by default, the MvK will automatically start executing that code.
  84. Therefore, most users will only experience the Modelverse as a modelling environment.
  85. Nonetheless, the Modelverse Kernel is more generic than this, and would also allow for users to define their own interface.
  86. As all aspects of the Modelverse are explicitly modelled, it is possible to alter the pre-loaded model and thus update the built-in behaviour.
  87. Modelverse Interface
  88. --------------------
  89. Finally, we come to the Modelverse Interface.
  90. For the moment, this is only a simple compiler that takes action language or model language files and translates them to something the Modelverse can understand.
  91. There are basically two kinds of operations the Modelverse can understand: direct graphs, or a list of constructors.
  92. The use of direct graphs is deprecated, as constructors are much more elegant (but slower).
  93. Future changes should make constructors as fast as the use of graphs.
  94. Nonetheless, which approach is used does not matter for the action language or model language files.
  95. The parser used for this simple front-end is the HUTN parser.
  96. It creates an abstract syntax tree, which is then traversed by a series of visitors.
  97. The visitors that are used, depends on the selected mode of compilation.
  98. We briefly present the most important visitors.
  99. Semantics visitor
  100. ^^^^^^^^^^^^^^^^^
  101. The visitor most oftenly used is the Semantics visitor.
  102. It traverses the abstract syntax tree and constructs symbol tables and finds all declarations of functions.
  103. Additionally, it also translates the use of operators to their Modelverse functions.
  104. This visitor in itself does not output anything, but it modifies the existing abstract syntax tree.
  105. It is used as a first step for most other visitors.
  106. Constructors visitor
  107. ^^^^^^^^^^^^^^^^^^^^
  108. The most important visitor is the constructors visitor.
  109. It traverses the abstract syntax tree and outputs a list of strings that make it possible for the MvK to construct the required code in the Modelverse.
  110. The use of constructors has several advantages, but the primary advantage is that the interface does not have to know about the internal representation of action language in the Modelverse.
  111. However, the order of constructors and accepted keywords needs to be defined (in action language), and made available to the users.
  112. As this interface is explicitly modelled as well, it is possible for users to modify it without affecting the interface.
  113. This is bad if the interface suddenly accepts new keywords or removes support for old keywords.
  114. During the execution of the visitor, it would already be possible to start transmitting the list to the Modelverse, such that the Modelverse can create the code in parallel with the actual parsing.
  115. While this is possible, we currently split this up in different phases: first the list is generated completely, and then it is transferred to the Modelverse in a single request.
  116. Bootstrap visitor
  117. ^^^^^^^^^^^^^^^^^
  118. Despite the primitives visitor being deprecated, the bootstrap visitor still needs access to this kind of output, since it cannot use constructors: during bootstrapping, the Modelverse is not yet available.
  119. The actual output format slightly differs from the primitives visitor, but the core concepts are identical.
  120. Model visitor
  121. ^^^^^^^^^^^^^
  122. Finally, the model visitor is similar to the constructor visitor, but goes to a different constructor interface, which supports modelling operations instead of action language operations.
  123. This is a very preliminary visitor.
  124. Bootstrapping
  125. -------------
  126. To bootstrap, you just have to run the file *bootstrap.py*.
  127. Here, we explain what this file actually does...
  128. The bootstrap script primarily creates the initial graph manually.
  129. This manual creation is done by writing data to a file, which is later read by the MvS.
  130. The initial graph consists of several parts:
  131. * The Modelverse Root node;
  132. * A global definition of all primitives;
  133. * The stack frame of the initial task *task_manager*, which manages all other tasks;
  134. * The code for the *task_manager* task;
  135. * The code for all new users, as assigned by the *user_manager*;
  136. * Bindings in the compilation manager for bootstrap files.
  137. These are all fairly simple in themselves.
  138. For some parts, such as the code, the HUTN compiler is invoked on a temporary piece of code.
  139. All bootstrap files are also compiled and made available to the Modelverse.
  140. How to add a primitive
  141. ----------------------
  142. Probably the most important reason why you would want to know about the Modelverse internals, is if you want to add a Modelverse primitive.
  143. Primitives are functions implemented in the MvK core, and thus become hardcoded.
  144. .. warning::
  145. While these functions are hardcoded, their implementation needs to follow strict rules, such that their semantics is identical in all possible programming languages.
  146. For example, a primitive *getTime()* cannot simply be implemented as *time.time()* in Python, as this gives different results on Linux and Windows.
  147. To add a primitive, follow these steps:
  148. 1. Add the code of the primitive to the MvK implementation by adding it in the file *kernel/modelverse_kernel/primitives.py*.
  149. a. Name the function identically to how the primitive will be invoked later on.
  150. b. Take a set of parameters. Parameters for primitives are always positional and start from *a* onwards.
  151. c. The final parameter should be a catch-all element, as it is possible that a high number of additional information is passed. In Python, this is done with a parameter prepended with \*\*.
  152. d. The parameters are encoded using a dictionary notation, containing the keys *id* and *value*. The *value* key is the cached value of the node, or the value of a node that is still to be materialized.
  153. e. When asking the MvS for information, use yields, just like the implementation of transformation rules in the MvK.
  154. f. Instead of returning using Python code, yield a *RETURN* command, taking one argument: the returnvalue.
  155. 2. Add the signature to *bootstrap.py* in the topmost dictionary *primitives*. The key is the name of the function, and the value is a list starting with the return type, followed by all parameter types (as string).
  156. 3. Add the declaration to *includes/primitives.alh* to make them available everywhere.
  157. 4. Add the definition to *primitives.alc* and assign the Modelverse reference *?primitives/function_name*.
  158. 5. Recreate the bootstrap file by running the script *generate_bootstrap.py*.
  159. 6. Restart the Modelverse to reload the bootstrap file.
  160. 7. From now on, all files including *primitives.alh* have access to the defined function.
  161. Adding a precompiled function
  162. -----------------------------
  163. Adding a precompiled function is way easier: only step 1 of the addition of a primitive should be done, but in the file *kernel/modelverse_kernel/compiled.py* instead of *kernel/modelverse_kernel/primitives.py*.
  164. All other steps are done automatically since there is an action language implementation present.
  165. The MvK will then automatically pick the precompiled function or the explicitly modelled operation, depending on preferences.
  166. A restart of the MvK is needed for Python to pick up the new functions.