internal.html 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <title>Internal workings &#8212; Modelverse 0.4.0 documentation</title>
  7. <link rel="stylesheet" href="_static/classic.css" type="text/css" />
  8. <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
  9. <script type="text/javascript">
  10. var DOCUMENTATION_OPTIONS = {
  11. URL_ROOT: './',
  12. VERSION: '0.4.0',
  13. COLLAPSE_INDEX: false,
  14. FILE_SUFFIX: '.html',
  15. HAS_SOURCE: true
  16. };
  17. </script>
  18. <script type="text/javascript" src="_static/jquery.js"></script>
  19. <script type="text/javascript" src="_static/underscore.js"></script>
  20. <script type="text/javascript" src="_static/doctools.js"></script>
  21. <link rel="top" title="Modelverse 0.4.0 documentation" href="index.html" />
  22. <link rel="next" title="Models" href="models.html" />
  23. <link rel="prev" title="Common problems and solutions" href="problems.html" />
  24. </head>
  25. <body role="document">
  26. <div class="related" role="navigation" aria-label="related navigation">
  27. <h3>Navigation</h3>
  28. <ul>
  29. <li class="right" style="margin-right: 10px">
  30. <a href="genindex.html" title="General Index"
  31. accesskey="I">index</a></li>
  32. <li class="right" >
  33. <a href="models.html" title="Models"
  34. accesskey="N">next</a> |</li>
  35. <li class="right" >
  36. <a href="problems.html" title="Common problems and solutions"
  37. accesskey="P">previous</a> |</li>
  38. <li class="nav-item nav-item-0"><a href="index.html">Modelverse 0.4.0 documentation</a> &#187;</li>
  39. </ul>
  40. </div>
  41. <div class="document">
  42. <div class="documentwrapper">
  43. <div class="bodywrapper">
  44. <div class="body" role="main">
  45. <div class="section" id="internal-workings">
  46. <h1>Internal workings<a class="headerlink" href="#internal-workings" title="Permalink to this headline">¶</a></h1>
  47. <p>For more detailed information on the Modelverse specification, which this project is implementing, we refer to the <a class="reference external" href="http://msdl.cs.mcgill.ca/people/yentl/files/Modelverse.pdf">Modelverse Specification</a>.</p>
  48. <p>Information on the implementation can be found below.</p>
  49. <div class="section" id="modelverse-state">
  50. <h2>Modelverse State<a class="headerlink" href="#modelverse-state" title="Permalink to this headline">¶</a></h2>
  51. <p>The Modelverse State is basically just an implementation of a graph library.
  52. As we have a particular kind of graph, this implementation is mostly done by hand at the moment.
  53. The notable exception to this is the RDF backend, proving that other implementations can also be used.</p>
  54. <p>The basic implementation just stores everything as dictionaries.
  55. All operations are then defined by doing operations on these dictionaries.
  56. The most interesting operations here are dictionary operations, which need to traverse these dictionaries in complex ways.
  57. To overcome performance problems for these operations, all results are cached (and validated afterwards).</p>
  58. <div class="section" id="rdf-backend">
  59. <h3>RDF backend<a class="headerlink" href="#rdf-backend" title="Permalink to this headline">¶</a></h3>
  60. <p>The RDF backend requires the <em>rdflib</em> module in Python.
  61. The Modelverse graph is then stored in RDF representation and all operations on it are done using SPARQL queries.
  62. Due to this level of indirection, performance is extremely slow.
  63. To increase performance, we would likely have to make more <em>composite</em> operations, or even group different requests together internally.</p>
  64. </div>
  65. <div class="section" id="status-codes">
  66. <h3>Status codes<a class="headerlink" href="#status-codes" title="Permalink to this headline">¶</a></h3>
  67. <p>The MvS returns, apart from its actual return value, a status code for the request.
  68. This value is not used by the MvK at all, since sometimes a request is expected to give an error (<em>e.g.</em>, checking whether an element is present).
  69. When debugging the MvS, however, these status codes can come in handy.</p>
  70. </div>
  71. </div>
  72. <div class="section" id="modelverse-kernel">
  73. <h2>Modelverse Kernel<a class="headerlink" href="#modelverse-kernel" title="Permalink to this headline">¶</a></h2>
  74. <p>The Modelverse Kernel is basically a graph transformation kernel.
  75. It consists of two parts: a small transformation engine (only a few lines in Python), and a copy of all rules it knows.
  76. Most code is an encoding of these transformation rules, and can (theoretically) be automatically generated by the Modelverse itself.
  77. This is currently not top priority, but will probably be implemented in the future.</p>
  78. <p>The transformation rules are an encoding of the rules presented in the specification mentioned at the top of this page.
  79. In each rule, the MvK needs to have tight communication with the MvS.
  80. For this, the rules are encoded using <em>yield</em> operations in Python.
  81. Rules therefore act as generators, outputting commands to the MvS, and immediately getting a reply.
  82. Each individual yield contains a list of operations to send to the MvS simultaneously.
  83. The result will therefore also be a list of results for each operation.</p>
  84. <p>As you can see in these rules, each entry in the list has a specific form.
  85. An entry is a tuple containing two elements: the code of the operation to execute, followed by a list of all parameters to pass.
  86. The code is a shortened name for the operation, such as <em>CN</em> for <em>create_node</em>.
  87. A full mapping can be found in the MvS, the most important ones are shown below.</p>
  88. <table border="1" class="docutils">
  89. <colgroup>
  90. <col width="20%" />
  91. <col width="80%" />
  92. </colgroup>
  93. <thead valign="bottom">
  94. <tr class="row-odd"><th class="head">Code</th>
  95. <th class="head">Function</th>
  96. </tr>
  97. </thead>
  98. <tbody valign="top">
  99. <tr class="row-even"><td>CN</td>
  100. <td>create_node</td>
  101. </tr>
  102. <tr class="row-odd"><td>CNV</td>
  103. <td>create_nodevalue</td>
  104. </tr>
  105. <tr class="row-even"><td>CE</td>
  106. <td>create_edge</td>
  107. </tr>
  108. <tr class="row-odd"><td>CD</td>
  109. <td>create_dict</td>
  110. </tr>
  111. <tr class="row-even"><td>RV</td>
  112. <td>read_value</td>
  113. </tr>
  114. <tr class="row-odd"><td>RE</td>
  115. <td>read_edge</td>
  116. </tr>
  117. <tr class="row-even"><td>RD</td>
  118. <td>read_dict</td>
  119. </tr>
  120. <tr class="row-odd"><td>RDN</td>
  121. <td>read_dict_node</td>
  122. </tr>
  123. <tr class="row-even"><td>DN</td>
  124. <td>delete_node</td>
  125. </tr>
  126. <tr class="row-odd"><td>DE</td>
  127. <td>delete_edge</td>
  128. </tr>
  129. </tbody>
  130. </table>
  131. <div class="admonition note">
  132. <p class="first admonition-title">Note</p>
  133. <p>Since each yield operation works on lists even a single operation needs to be wrapped in a list.
  134. Worse, however, is that the return value will also be a list.
  135. 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.
  136. Your syntax thus becomes:</p>
  137. <div class="highlight-default"><div class="highlight"><pre><span class="n">a</span><span class="p">,</span> <span class="o">=</span> <span class="k">yield</span> <span class="p">[(</span><span class="s">&quot;CN&quot;</span><span class="p">,</span> <span class="p">[])]</span>
  138. </pre></div>
  139. </div>
  140. <p class="last">Where a will now already contain the created node, and not a list with as first (and only) element the created node.</p>
  141. </div>
  142. <div class="section" id="primitives">
  143. <h3>Primitives<a class="headerlink" href="#primitives" title="Permalink to this headline">¶</a></h3>
  144. <p>The Modelverse Kernel also defines the primitives users can use.
  145. Primitives are necessary since we can&#8217;t go deeper at some point.
  146. It is the MvK&#8217;s responsibility to implement each and every primitive defined in the bootstrap file.</p>
  147. <p>Primitives are implemented in the bootstrap file as having a body with an empty node.
  148. When execution goes to this node, the MvK must execute the associated primitive instead.
  149. To do this, the MvK must map all these nodes to their corresponding primitive implementation during startup.</p>
  150. </div>
  151. <div class="section" id="precompiled-functions">
  152. <h3>Precompiled functions<a class="headerlink" href="#precompiled-functions" title="Permalink to this headline">¶</a></h3>
  153. <p>Similar to primitives, the MvK also supports precompiled functions.
  154. A precompiled function is similar to a primitive in terms of implementation, but they do also have an implementation inside of the Modelverse itself.
  155. This means that there are two implementations: one hardcoded, and one in action language.
  156. Both should obviously be consistent.</p>
  157. <p>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.
  158. This is interesting, since with precompiled functions, no intermediate values will be visible.
  159. Additionally, precompiled functions cannot be changed, as they are Python code instead of being explicitly modelled.</p>
  160. </div>
  161. </div>
  162. <div class="section" id="modelverse-interface">
  163. <h2>Modelverse Interface<a class="headerlink" href="#modelverse-interface" title="Permalink to this headline">¶</a></h2>
  164. <p>Finally, we come to the Modelverse Interface.
  165. 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.
  166. There are basically two kinds of operations the Modelverse can understand: direct graphs, or a list of constructors.
  167. The use of direct graphs is deprecated, as constructors are much more elegant (but slower).
  168. Future changes should make constructors as fast as the use of graphs.
  169. Nonetheless, which approach is used does not matter for the action language or model language files.</p>
  170. <p>The parser used for this simple front-end is the HUTN parser.
  171. It creates an abstract syntax tree, which is then traversed by a series of visitors.
  172. The visitors that are used, depends on the selected mode of compilation.
  173. We briefly present the most important visitors.</p>
  174. <div class="section" id="semantics-visitor">
  175. <h3>Semantics visitor<a class="headerlink" href="#semantics-visitor" title="Permalink to this headline">¶</a></h3>
  176. <p>The visitor most oftenly used is the Semantics visitor.
  177. It traverses the abstract syntax tree and constructs symbol tables and finds all declarations of functions.
  178. Additionally, it also translates the use of operators to their Modelverse functions.</p>
  179. <p>This visitor in itself does not output anything, but it modifies the existing abstract syntax tree.
  180. It is used as a first step for most other visitors.</p>
  181. </div>
  182. <div class="section" id="constructors-visitor">
  183. <h3>Constructors visitor<a class="headerlink" href="#constructors-visitor" title="Permalink to this headline">¶</a></h3>
  184. <p>The most important visitor is the constructors visitor.
  185. 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.
  186. 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.
  187. However, the order of constructors and accepted keywords needs to be defined (in action language), and made available to the users.
  188. As this interface is explicitly modelled as well, it is possible for users to modify it without affecting the interface.
  189. This is bad if the interface suddenly accepts new keywords or removes support for old keywords.</p>
  190. <p>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.
  191. 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.</p>
  192. </div>
  193. <div class="section" id="primitives-visitor">
  194. <h3>Primitives visitor<a class="headerlink" href="#primitives-visitor" title="Permalink to this headline">¶</a></h3>
  195. <p>The primitives visitor generates a Modelverse graph directly.
  196. Its use is deprecated, as the constructors are much more elegant.
  197. While direct use of this visitor is not recommended, except for performance reasons, the bootstrap visitor (discussed next) is based on this visitor.</p>
  198. <p>This visitor creates a serialized version of the graph and transmits it to the Modelverse.
  199. The Modelverse has a built-in primitive to decode this graph and store it.</p>
  200. </div>
  201. <div class="section" id="bootstrap-visitor">
  202. <h3>Bootstrap visitor<a class="headerlink" href="#bootstrap-visitor" title="Permalink to this headline">¶</a></h3>
  203. <p>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.
  204. The actual output format slightly differs from the primitives visitor, but the core concepts are identical.</p>
  205. </div>
  206. <div class="section" id="model-visitor">
  207. <h3>Model visitor<a class="headerlink" href="#model-visitor" title="Permalink to this headline">¶</a></h3>
  208. <p>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.
  209. This is a very preliminary visitor.</p>
  210. </div>
  211. </div>
  212. <div class="section" id="bootstrapping">
  213. <h2>Bootstrapping<a class="headerlink" href="#bootstrapping" title="Permalink to this headline">¶</a></h2>
  214. <p>To bootstrap, you just have to run the file <em>bootstrap.py</em>.
  215. Here, we explain what this file actually does...</p>
  216. <p>The bootstrap script primarily creates the initial graph manually.
  217. This manual creation is done by writing data to a file, which is later read by the MvS.
  218. The initial graph consists of several parts:</p>
  219. <ul class="simple">
  220. <li>The Modelverse Root node;</li>
  221. <li>A global definition of all primitives;</li>
  222. <li>The stack frame of the initial user <em>user_manager</em>, which manages all other users;</li>
  223. <li>The code for the <em>user_manager</em> user;</li>
  224. <li>The code for all new users, as assigned by the <em>user_manager</em>;</li>
  225. <li>Bindings in the compilation manager for bootstrap files.</li>
  226. </ul>
  227. <p>These are all fairly simple in themselves.
  228. For some parts, such as the code, the HUTN compiler is invoked on a temporary piece of code.
  229. All bootstrap files are also compiled and made available to the compilation manager with their MD5 hash.</p>
  230. </div>
  231. <div class="section" id="how-to-add-a-primitive">
  232. <h2>How to add a primitive<a class="headerlink" href="#how-to-add-a-primitive" title="Permalink to this headline">¶</a></h2>
  233. <p>Probably the most important reason why you would want to know about the Modelverse internals, is if you want to add a Modelverse primitive.
  234. Primitives are functions implemented in the MvK core, and thus become hardcoded.</p>
  235. <div class="admonition warning">
  236. <p class="first admonition-title">Warning</p>
  237. <p class="last">While these functions are hardcoded, their implementation needs to follow strict rules, such that their semantics is identical in all possible programming languages.
  238. For example, a primitive <em>getTime()</em> cannot simply be implemented as <em>time.time()</em> in Python, as this gives different results on Linux and Windows.</p>
  239. </div>
  240. <p>To add a primitive, follow these steps:</p>
  241. <ol class="arabic">
  242. <li><p class="first">Add the code of the primitive to the MvK implementation by adding it in the file <em>primitives.py</em>.</p>
  243. <blockquote>
  244. <div><ol class="loweralpha simple">
  245. <li>Name the function identically to how the primitive will be invoked later on.</li>
  246. <li>Take a set of parameters. Parameters for primitives are always positional and start from <em>a</em> onwards.</li>
  247. <li>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 **.</li>
  248. <li>When asking the MvS for information, use yields, just like the implementation of transformation rules in the MvK.</li>
  249. <li>Instead of return, use a <em>raise</em> with the exception <em>PrimitiveFinished</em>. This exception takes one argument: the returnvalue.</li>
  250. </ol>
  251. </div></blockquote>
  252. </li>
  253. <li><p class="first">Add the signature to <em>bootstrap.py</em> in the topmost dictionary <em>primitives</em>. 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).</p>
  254. </li>
  255. <li><p class="first">Add the declaration to <em>includes/primitives.alh</em> to make them available everywhere.</p>
  256. </li>
  257. <li><p class="first">Add the definition to <em>primitives.alc</em> and assign the Modelverse reference <em>?primitives/function_name</em>.</p>
  258. </li>
  259. <li><p class="first">Recreate the bootstrap file by running the script <em>generate_bootstrap.py</em>.</p>
  260. </li>
  261. <li><p class="first">Restart the Modelverse to reload the bootstrap file.</p>
  262. </li>
  263. <li><p class="first">From now on, all files including <em>primitives.alh</em> have access to the defined function.</p>
  264. </li>
  265. </ol>
  266. </div>
  267. <div class="section" id="adding-a-precompiled-function">
  268. <h2>Adding a precompiled function<a class="headerlink" href="#adding-a-precompiled-function" title="Permalink to this headline">¶</a></h2>
  269. <p>Adding a precompiled function is way easier: only step 1 of the addition of a primitive should be done, but in the file <em>compiled.py</em> instead of <em>primitives.py</em>.
  270. All other steps are done automatically since there is an action language implementation present.
  271. The MvK will then automatically pick the precompiled function or the explicitly modelled operation, depending on preferences.
  272. A restart of the MvK is needed for Python to pick up the new functions.</p>
  273. </div>
  274. </div>
  275. </div>
  276. </div>
  277. </div>
  278. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  279. <div class="sphinxsidebarwrapper">
  280. <h3><a href="index.html">Table Of Contents</a></h3>
  281. <ul>
  282. <li><a class="reference internal" href="#">Internal workings</a><ul>
  283. <li><a class="reference internal" href="#modelverse-state">Modelverse State</a><ul>
  284. <li><a class="reference internal" href="#rdf-backend">RDF backend</a></li>
  285. <li><a class="reference internal" href="#status-codes">Status codes</a></li>
  286. </ul>
  287. </li>
  288. <li><a class="reference internal" href="#modelverse-kernel">Modelverse Kernel</a><ul>
  289. <li><a class="reference internal" href="#primitives">Primitives</a></li>
  290. <li><a class="reference internal" href="#precompiled-functions">Precompiled functions</a></li>
  291. </ul>
  292. </li>
  293. <li><a class="reference internal" href="#modelverse-interface">Modelverse Interface</a><ul>
  294. <li><a class="reference internal" href="#semantics-visitor">Semantics visitor</a></li>
  295. <li><a class="reference internal" href="#constructors-visitor">Constructors visitor</a></li>
  296. <li><a class="reference internal" href="#primitives-visitor">Primitives visitor</a></li>
  297. <li><a class="reference internal" href="#bootstrap-visitor">Bootstrap visitor</a></li>
  298. <li><a class="reference internal" href="#model-visitor">Model visitor</a></li>
  299. </ul>
  300. </li>
  301. <li><a class="reference internal" href="#bootstrapping">Bootstrapping</a></li>
  302. <li><a class="reference internal" href="#how-to-add-a-primitive">How to add a primitive</a></li>
  303. <li><a class="reference internal" href="#adding-a-precompiled-function">Adding a precompiled function</a></li>
  304. </ul>
  305. </li>
  306. </ul>
  307. <h4>Previous topic</h4>
  308. <p class="topless"><a href="problems.html"
  309. title="previous chapter">Common problems and solutions</a></p>
  310. <h4>Next topic</h4>
  311. <p class="topless"><a href="models.html"
  312. title="next chapter">Models</a></p>
  313. <div role="note" aria-label="source link">
  314. <h3>This Page</h3>
  315. <ul class="this-page-menu">
  316. <li><a href="_sources/internal.txt"
  317. rel="nofollow">Show Source</a></li>
  318. </ul>
  319. </div>
  320. <div id="searchbox" style="display: none" role="search">
  321. <h3>Quick search</h3>
  322. <form class="search" action="search.html" method="get">
  323. <div><input type="text" name="q" /></div>
  324. <div><input type="submit" value="Go" /></div>
  325. <input type="hidden" name="check_keywords" value="yes" />
  326. <input type="hidden" name="area" value="default" />
  327. </form>
  328. </div>
  329. <script type="text/javascript">$('#searchbox').show(0);</script>
  330. </div>
  331. </div>
  332. <div class="clearer"></div>
  333. </div>
  334. <div class="related" role="navigation" aria-label="related navigation">
  335. <h3>Navigation</h3>
  336. <ul>
  337. <li class="right" style="margin-right: 10px">
  338. <a href="genindex.html" title="General Index"
  339. >index</a></li>
  340. <li class="right" >
  341. <a href="models.html" title="Models"
  342. >next</a> |</li>
  343. <li class="right" >
  344. <a href="problems.html" title="Common problems and solutions"
  345. >previous</a> |</li>
  346. <li class="nav-item nav-item-0"><a href="index.html">Modelverse 0.4.0 documentation</a> &#187;</li>
  347. </ul>
  348. </div>
  349. <div class="footer" role="contentinfo">
  350. &#169; Copyright 2016, Yentl Van Tendeloo.
  351. Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.4.6.
  352. </div>
  353. </body>
  354. </html>