examples.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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>Examples &#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="Advanced examples" href="advanced.html" />
  23. <link rel="prev" title="Modelling Language" href="modellanguage.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="advanced.html" title="Advanced examples"
  34. accesskey="N">next</a> |</li>
  35. <li class="right" >
  36. <a href="modellanguage.html" title="Modelling Language"
  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="examples">
  46. <h1>Examples<a class="headerlink" href="#examples" title="Permalink to this headline">¶</a></h1>
  47. <p>To run this code, store it in a file (<em>e.g.</em>, test.alc), and execute the following commands:</p>
  48. <div class="highlight-default"><div class="highlight"><pre><span class="n">python</span> <span class="n">scripts</span><span class="o">/</span><span class="n">run_local_modelverse</span><span class="o">.</span><span class="n">py</span> <span class="mi">8001</span> <span class="o">&amp;</span>
  49. <span class="n">python</span> <span class="n">scripts</span><span class="o">/</span><span class="n">make_parallel</span><span class="o">.</span><span class="n">py</span> <span class="n">http</span><span class="p">:</span><span class="o">//</span><span class="n">localhost</span><span class="p">:</span><span class="mi">8001</span> <span class="n">test</span> <span class="n">test</span><span class="o">.</span><span class="n">alc</span> <span class="n">bootstrap</span><span class="o">/</span><span class="n">primitives</span><span class="o">.</span><span class="n">alc</span>
  50. <span class="n">python</span> <span class="n">scripts</span><span class="o">/</span><span class="n">prompt</span><span class="o">.</span><span class="n">py</span>
  51. </pre></div>
  52. </div>
  53. <p>In the prompt, log on to <a class="reference external" href="http://localhost:8001">http://localhost:8001</a> as user <em>test</em>.
  54. Now, all input you send, will be caught in the <em>input()</em> calls seen in the code.
  55. Results will also be printed.</p>
  56. <p>Note that all content will, by default, be typed as string.
  57. If you want to send integers or so, prepend the input with a backslash (\), which allows you to directly input the JSON code.
  58. For example, input <em>5</em> will be received as the string &#8220;5&#8221;.
  59. To send the integer 5, the input should be <em>\5</em>.</p>
  60. <div class="section" id="fibonacci-server">
  61. <h2>Fibonacci Server<a class="headerlink" href="#fibonacci-server" title="Permalink to this headline">¶</a></h2>
  62. <p>The first example is a simple Fibonacci server.
  63. The code is identical to the action language example from before, as this already included the <em>server</em> part (<em>i.e.</em>, the while loop).
  64. Now we will just connect to it using <em>prompt.py</em> and try out the code directly.</p>
  65. <p>The code is repeated below:</p>
  66. <div class="highlight-default"><div class="highlight"><pre>include &quot;primitives.alh&quot;
  67. Integer function fib(param : Integer):
  68. if (param &lt;= 2):
  69. return 1!
  70. else:
  71. return fib(param - 1) + fib(param - 2)!
  72. Void function main():
  73. while(True):
  74. output(fib(input()))
  75. </pre></div>
  76. </div>
  77. <p>To run this code, save it to a file (<em>e.g.</em>, fibonacci.alc) and execute the following command:</p>
  78. <div class="highlight-default"><div class="highlight"><pre><span class="n">python</span> <span class="n">scripts</span><span class="o">/</span><span class="n">run_local_modelverse</span><span class="o">.</span><span class="n">py</span> <span class="mi">8001</span>
  79. </pre></div>
  80. </div>
  81. <p>Now, the Modelverse is running, but we still need to upload our code.
  82. To compile the file, together with the primitives.alc file, execute the following command:</p>
  83. <div class="highlight-default"><div class="highlight"><pre><span class="n">python</span> <span class="n">scripts</span><span class="o">/</span><span class="n">make_parallel</span><span class="o">.</span><span class="n">py</span> <span class="n">http</span><span class="p">:</span><span class="o">//</span><span class="n">localhost</span><span class="p">:</span><span class="mi">8001</span> <span class="n">test</span> <span class="n">fibonacci</span><span class="o">.</span><span class="n">alc</span> <span class="n">bootstrap</span><span class="o">/</span><span class="n">primitives</span><span class="o">.</span><span class="n">alc</span>
  84. </pre></div>
  85. </div>
  86. <p>When this finishes, the Modelverse now stores a copy of our code in its own format.
  87. The Modelverse will automatically create the user <em>test</em> and start executing the <em>main</em> function as this user.
  88. We can therefore simply connect to the Modelverse as the <em>test</em> user and start seeing the responses.
  89. To start the prompt, execute the following command:</p>
  90. <div class="highlight-default"><div class="highlight"><pre><span class="n">python</span> <span class="n">scripts</span><span class="o">/</span><span class="n">prompt</span><span class="o">.</span><span class="n">py</span>
  91. </pre></div>
  92. </div>
  93. <p>In this prompt tool, you first have to configure the location of the Modelverse and the username.
  94. The defaults should be fine, so just press &lt;return&gt; twice.
  95. After that, you are in direct connection with the Modelverse.
  96. Each message you type in, is made available in the <em>input()</em> function of the code.
  97. Remember that, to send integers, you have to prefix this with a backslash (\).
  98. To get, for example, the 1st Fibonacci number, type in the command:</p>
  99. <div class="highlight-default"><div class="highlight"><pre>\<span class="mi">1</span>
  100. </pre></div>
  101. </div>
  102. <p>You will now see the following.</p>
  103. <img alt="_images/prompt_fibonacci.png" src="_images/prompt_fibonacci.png" />
  104. <p>Since we are in an unconditional loop, you can send as many requests as you want, as long as they are understandable by the <em>fib</em> function.</p>
  105. <img alt="_images/prompt_fibonacci_more.png" src="_images/prompt_fibonacci_more.png" />
  106. <p>Inputting a string directly, such as <em>1</em> instead of <em>\1</em>, will make the Modelverse crash on the execution of this code.
  107. This is normal, though in the future the Modelverse will keep running for other users: only the current user&#8217;s code will be interrupted.</p>
  108. </div>
  109. <div class="section" id="modelling-server">
  110. <h2>Modelling Server<a class="headerlink" href="#modelling-server" title="Permalink to this headline">¶</a></h2>
  111. </div>
  112. </div>
  113. </div>
  114. </div>
  115. </div>
  116. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  117. <div class="sphinxsidebarwrapper">
  118. <h3><a href="index.html">Table Of Contents</a></h3>
  119. <ul>
  120. <li><a class="reference internal" href="#">Examples</a><ul>
  121. <li><a class="reference internal" href="#fibonacci-server">Fibonacci Server</a></li>
  122. <li><a class="reference internal" href="#modelling-server">Modelling Server</a></li>
  123. </ul>
  124. </li>
  125. </ul>
  126. <h4>Previous topic</h4>
  127. <p class="topless"><a href="modellanguage.html"
  128. title="previous chapter">Modelling Language</a></p>
  129. <h4>Next topic</h4>
  130. <p class="topless"><a href="advanced.html"
  131. title="next chapter">Advanced examples</a></p>
  132. <div role="note" aria-label="source link">
  133. <h3>This Page</h3>
  134. <ul class="this-page-menu">
  135. <li><a href="_sources/examples.txt"
  136. rel="nofollow">Show Source</a></li>
  137. </ul>
  138. </div>
  139. <div id="searchbox" style="display: none" role="search">
  140. <h3>Quick search</h3>
  141. <form class="search" action="search.html" method="get">
  142. <div><input type="text" name="q" /></div>
  143. <div><input type="submit" value="Go" /></div>
  144. <input type="hidden" name="check_keywords" value="yes" />
  145. <input type="hidden" name="area" value="default" />
  146. </form>
  147. </div>
  148. <script type="text/javascript">$('#searchbox').show(0);</script>
  149. </div>
  150. </div>
  151. <div class="clearer"></div>
  152. </div>
  153. <div class="related" role="navigation" aria-label="related navigation">
  154. <h3>Navigation</h3>
  155. <ul>
  156. <li class="right" style="margin-right: 10px">
  157. <a href="genindex.html" title="General Index"
  158. >index</a></li>
  159. <li class="right" >
  160. <a href="advanced.html" title="Advanced examples"
  161. >next</a> |</li>
  162. <li class="right" >
  163. <a href="modellanguage.html" title="Modelling Language"
  164. >previous</a> |</li>
  165. <li class="nav-item nav-item-0"><a href="index.html">Modelverse 0.4.0 documentation</a> &#187;</li>
  166. </ul>
  167. </div>
  168. <div class="footer" role="contentinfo">
  169. &#169; Copyright 2016, Yentl Van Tendeloo.
  170. Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.4.6.
  171. </div>
  172. </body>
  173. </html>