internal.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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="prev" title="Common problems and solutions" href="problems.html" />
  23. </head>
  24. <body role="document">
  25. <div class="related" role="navigation" aria-label="related navigation">
  26. <h3>Navigation</h3>
  27. <ul>
  28. <li class="right" style="margin-right: 10px">
  29. <a href="genindex.html" title="General Index"
  30. accesskey="I">index</a></li>
  31. <li class="right" >
  32. <a href="problems.html" title="Common problems and solutions"
  33. accesskey="P">previous</a> |</li>
  34. <li class="nav-item nav-item-0"><a href="index.html">Modelverse 0.4.0 documentation</a> &#187;</li>
  35. </ul>
  36. </div>
  37. <div class="document">
  38. <div class="documentwrapper">
  39. <div class="bodywrapper">
  40. <div class="body" role="main">
  41. <div class="section" id="internal-workings">
  42. <h1>Internal workings<a class="headerlink" href="#internal-workings" title="Permalink to this headline">¶</a></h1>
  43. <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>
  44. <p>Information on the implementation can be found below.</p>
  45. <div class="section" id="modelverse-state">
  46. <h2>Modelverse State<a class="headerlink" href="#modelverse-state" title="Permalink to this headline">¶</a></h2>
  47. <p>The Modelverse State is basically just an implementation of a graph library.
  48. As we have a particular kind of graph, this implementation is mostly done by hand at the moment.
  49. The notable exception to this is the RDF backend, proving that other implementations can also be used.</p>
  50. <p>The basic implementation just stores everything as dictionaries.
  51. All operations are then defined by doing operations on these dictionaries.
  52. The most interesting operations here are dictionary operations, which need to traverse these dictionaries in complex ways.
  53. To overcome performance problems for these operations, all results are cached (and validated afterwards).</p>
  54. <div class="section" id="rdf-backend">
  55. <h3>RDF backend<a class="headerlink" href="#rdf-backend" title="Permalink to this headline">¶</a></h3>
  56. <p>The RDF backend requires the <em>rdflib</em> module in Python.
  57. The Modelverse graph is then stored in RDF representation and all operations on it are done using SPARQL queries.
  58. Due to this level of indirection, performance is extremely slow.
  59. To increase performance, we would likely have to make more <em>composite</em> operations, or even group different requests together internally.</p>
  60. </div>
  61. <div class="section" id="status-codes">
  62. <h3>Status codes<a class="headerlink" href="#status-codes" title="Permalink to this headline">¶</a></h3>
  63. <p>The MvS returns, apart from its actual return value, a status code for the request.
  64. 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).
  65. When debugging the MvS, however, these status codes can come in handy.</p>
  66. </div>
  67. </div>
  68. <div class="section" id="modelverse-kernel">
  69. <h2>Modelverse Kernel<a class="headerlink" href="#modelverse-kernel" title="Permalink to this headline">¶</a></h2>
  70. <div class="section" id="precompiled-functions">
  71. <h3>Precompiled functions<a class="headerlink" href="#precompiled-functions" title="Permalink to this headline">¶</a></h3>
  72. </div>
  73. </div>
  74. <div class="section" id="modelverse-interface">
  75. <h2>Modelverse Interface<a class="headerlink" href="#modelverse-interface" title="Permalink to this headline">¶</a></h2>
  76. <div class="section" id="semantics-visitor">
  77. <h3>Semantics visitor<a class="headerlink" href="#semantics-visitor" title="Permalink to this headline">¶</a></h3>
  78. </div>
  79. <div class="section" id="constructors-visitor">
  80. <h3>Constructors visitor<a class="headerlink" href="#constructors-visitor" title="Permalink to this headline">¶</a></h3>
  81. </div>
  82. <div class="section" id="primitives-visitor">
  83. <h3>Primitives visitor<a class="headerlink" href="#primitives-visitor" title="Permalink to this headline">¶</a></h3>
  84. </div>
  85. <div class="section" id="bootstrap-visitor">
  86. <h3>Bootstrap visitor<a class="headerlink" href="#bootstrap-visitor" title="Permalink to this headline">¶</a></h3>
  87. </div>
  88. <div class="section" id="model-visitor">
  89. <h3>Model visitor<a class="headerlink" href="#model-visitor" title="Permalink to this headline">¶</a></h3>
  90. </div>
  91. </div>
  92. <div class="section" id="bootstrapping">
  93. <h2>Bootstrapping<a class="headerlink" href="#bootstrapping" title="Permalink to this headline">¶</a></h2>
  94. <p>To bootstrap, you just have to run the file <em>bootstrap.py</em>.
  95. Here, we explain what this file actually does...</p>
  96. <p>The bootstrap script primarily creates the initial graph manually.
  97. This manual creation is done by writing data to a file, which is later read by the MvS.
  98. The initial graph consists of several parts:</p>
  99. <ul class="simple">
  100. <li>The Modelverse Root node;</li>
  101. <li>A global definition of all primitives;</li>
  102. <li>The stack frame of the initial user <em>user_manager</em>, which manages all other users;</li>
  103. <li>The code for the <em>user_manager</em> user;</li>
  104. <li>The code for all new users, as assigned by the <em>user_manager</em>;</li>
  105. <li>Bindings in the compilation manager for bootstrap files.</li>
  106. </ul>
  107. <p>These are all fairly simple in themselves.
  108. For some parts, such as the code, the HUTN compiler is invoked on a temporary piece of code.
  109. All bootstrap files are also compiled and made available to the compilation manager with their MD5 hash.</p>
  110. </div>
  111. <div class="section" id="how-to-add-a-primitive">
  112. <h2>How to add a primitive<a class="headerlink" href="#how-to-add-a-primitive" title="Permalink to this headline">¶</a></h2>
  113. <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.
  114. Primitives are functions implemented in the MvK core, and thus become hardcoded.</p>
  115. <div class="admonition warning">
  116. <p class="first admonition-title">Warning</p>
  117. <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.
  118. 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>
  119. </div>
  120. <p>To add a primitive, follow these steps:</p>
  121. <ol class="arabic">
  122. <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>
  123. <blockquote>
  124. <div><ol class="loweralpha simple">
  125. <li>Name the function identically to how the primitive will be invoked later on.</li>
  126. <li>Take a set of parameters. Parameters for primitives are always positional and start from <em>a</em> onwards.</li>
  127. <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>
  128. <li>When asking the MvS for information, use yields, just like the implementation of transformation rules in the MvK.</li>
  129. <li>Instead of return, use a <em>raise</em> with the exception <em>PrimitiveFinished</em>. This exception takes one argument: the returnvalue.</li>
  130. </ol>
  131. </div></blockquote>
  132. </li>
  133. <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>
  134. </li>
  135. <li><p class="first">Add the declaration to <em>includes/primitives.alh</em> to make them available everywhere.</p>
  136. </li>
  137. <li><p class="first">Add the definition to <em>primitives.alc</em> and assign the Modelverse reference <em>?primitives/function_name</em>.</p>
  138. </li>
  139. <li><p class="first">Recreate the bootstrap file by running the script <em>generate_bootstrap.py</em>.</p>
  140. </li>
  141. <li><p class="first">Restart the Modelverse to reload the bootstrap file.</p>
  142. </li>
  143. <li><p class="first">From now on, all files including <em>primitives.alh</em> have access to the defined function.</p>
  144. </li>
  145. </ol>
  146. </div>
  147. <div class="section" id="adding-a-precompiled-function">
  148. <h2>Adding a precompiled function<a class="headerlink" href="#adding-a-precompiled-function" title="Permalink to this headline">¶</a></h2>
  149. <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>.
  150. All other steps are done automatically since there is an action language implementation present.
  151. The MvK will then automatically pick the precompiled function or the explicitly modelled operation, depending on preferences.
  152. A restart of the MvK is needed for Python to pick up the new functions.</p>
  153. </div>
  154. </div>
  155. </div>
  156. </div>
  157. </div>
  158. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  159. <div class="sphinxsidebarwrapper">
  160. <h3><a href="index.html">Table Of Contents</a></h3>
  161. <ul>
  162. <li><a class="reference internal" href="#">Internal workings</a><ul>
  163. <li><a class="reference internal" href="#modelverse-state">Modelverse State</a><ul>
  164. <li><a class="reference internal" href="#rdf-backend">RDF backend</a></li>
  165. <li><a class="reference internal" href="#status-codes">Status codes</a></li>
  166. </ul>
  167. </li>
  168. <li><a class="reference internal" href="#modelverse-kernel">Modelverse Kernel</a><ul>
  169. <li><a class="reference internal" href="#precompiled-functions">Precompiled functions</a></li>
  170. </ul>
  171. </li>
  172. <li><a class="reference internal" href="#modelverse-interface">Modelverse Interface</a><ul>
  173. <li><a class="reference internal" href="#semantics-visitor">Semantics visitor</a></li>
  174. <li><a class="reference internal" href="#constructors-visitor">Constructors visitor</a></li>
  175. <li><a class="reference internal" href="#primitives-visitor">Primitives visitor</a></li>
  176. <li><a class="reference internal" href="#bootstrap-visitor">Bootstrap visitor</a></li>
  177. <li><a class="reference internal" href="#model-visitor">Model visitor</a></li>
  178. </ul>
  179. </li>
  180. <li><a class="reference internal" href="#bootstrapping">Bootstrapping</a></li>
  181. <li><a class="reference internal" href="#how-to-add-a-primitive">How to add a primitive</a></li>
  182. <li><a class="reference internal" href="#adding-a-precompiled-function">Adding a precompiled function</a></li>
  183. </ul>
  184. </li>
  185. </ul>
  186. <h4>Previous topic</h4>
  187. <p class="topless"><a href="problems.html"
  188. title="previous chapter">Common problems and solutions</a></p>
  189. <div role="note" aria-label="source link">
  190. <h3>This Page</h3>
  191. <ul class="this-page-menu">
  192. <li><a href="_sources/internal.txt"
  193. rel="nofollow">Show Source</a></li>
  194. </ul>
  195. </div>
  196. <div id="searchbox" style="display: none" role="search">
  197. <h3>Quick search</h3>
  198. <form class="search" action="search.html" method="get">
  199. <div><input type="text" name="q" /></div>
  200. <div><input type="submit" value="Go" /></div>
  201. <input type="hidden" name="check_keywords" value="yes" />
  202. <input type="hidden" name="area" value="default" />
  203. </form>
  204. </div>
  205. <script type="text/javascript">$('#searchbox').show(0);</script>
  206. </div>
  207. </div>
  208. <div class="clearer"></div>
  209. </div>
  210. <div class="related" role="navigation" aria-label="related navigation">
  211. <h3>Navigation</h3>
  212. <ul>
  213. <li class="right" style="margin-right: 10px">
  214. <a href="genindex.html" title="General Index"
  215. >index</a></li>
  216. <li class="right" >
  217. <a href="problems.html" title="Common problems and solutions"
  218. >previous</a> |</li>
  219. <li class="nav-item nav-item-0"><a href="index.html">Modelverse 0.4.0 documentation</a> &#187;</li>
  220. </ul>
  221. </div>
  222. <div class="footer" role="contentinfo">
  223. &#169; Copyright 2016, Yentl Van Tendeloo.
  224. Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.4.6.
  225. </div>
  226. </body>
  227. </html>