123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
-
- <title>Examples — Modelverse 0.4.0 documentation</title>
-
- <link rel="stylesheet" href="_static/classic.css" type="text/css" />
- <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
-
- <script type="text/javascript">
- var DOCUMENTATION_OPTIONS = {
- URL_ROOT: './',
- VERSION: '0.4.0',
- COLLAPSE_INDEX: false,
- FILE_SUFFIX: '.html',
- HAS_SOURCE: true
- };
- </script>
- <script type="text/javascript" src="_static/jquery.js"></script>
- <script type="text/javascript" src="_static/underscore.js"></script>
- <script type="text/javascript" src="_static/doctools.js"></script>
- <link rel="top" title="Modelverse 0.4.0 documentation" href="index.html" />
- <link rel="next" title="Advanced examples" href="advanced.html" />
- <link rel="prev" title="Modelling Language" href="modellanguage.html" />
- </head>
- <body role="document">
- <div class="related" role="navigation" aria-label="related navigation">
- <h3>Navigation</h3>
- <ul>
- <li class="right" style="margin-right: 10px">
- <a href="genindex.html" title="General Index"
- accesskey="I">index</a></li>
- <li class="right" >
- <a href="advanced.html" title="Advanced examples"
- accesskey="N">next</a> |</li>
- <li class="right" >
- <a href="modellanguage.html" title="Modelling Language"
- accesskey="P">previous</a> |</li>
- <li class="nav-item nav-item-0"><a href="index.html">Modelverse 0.4.0 documentation</a> »</li>
- </ul>
- </div>
- <div class="document">
- <div class="documentwrapper">
- <div class="bodywrapper">
- <div class="body" role="main">
-
- <div class="section" id="examples">
- <h1>Examples<a class="headerlink" href="#examples" title="Permalink to this headline">¶</a></h1>
- <p>To run this code, store it in a file (<em>e.g.</em>, test.alc), and execute the following commands:</p>
- <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">&</span>
- <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>
- <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>
- </pre></div>
- </div>
- <p>In the prompt, log on to <a class="reference external" href="http://localhost:8001">http://localhost:8001</a> as user <em>test</em>.
- Now, all input you send, will be caught in the <em>input()</em> calls seen in the code.
- Results will also be printed.</p>
- <p>Note that all content will, by default, be typed as string.
- If you want to send integers or so, prepend the input with a backslash (\), which allows you to directly input the JSON code.
- For example, input <em>5</em> will be received as the string “5”.
- To send the integer 5, the input should be <em>\5</em>.</p>
- <div class="section" id="fibonacci-server">
- <h2>Fibonacci Server<a class="headerlink" href="#fibonacci-server" title="Permalink to this headline">¶</a></h2>
- <p>The first example is a simple Fibonacci server.
- 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).
- Now we will just connect to it using <em>prompt.py</em> and try out the code directly.</p>
- <p>The code is repeated below:</p>
- <div class="highlight-default"><div class="highlight"><pre>include "primitives.alh"
- Integer function fib(param : Integer):
- if (param <= 2):
- return 1!
- else:
- return fib(param - 1) + fib(param - 2)!
- Void function main():
- while(True):
- output(fib(input()))
- </pre></div>
- </div>
- <p>To run this code, save it to a file (<em>e.g.</em>, fibonacci.alc) and execute the following command:</p>
- <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>
- </pre></div>
- </div>
- <p>Now, the Modelverse is running, but we still need to upload our code.
- To compile the file, together with the primitives.alc file, execute the following command:</p>
- <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>
- </pre></div>
- </div>
- <p>When this finishes, the Modelverse now stores a copy of our code in its own format.
- The Modelverse will automatically create the user <em>test</em> and start executing the <em>main</em> function as this user.
- We can therefore simply connect to the Modelverse as the <em>test</em> user and start seeing the responses.
- To start the prompt, execute the following command:</p>
- <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>
- </pre></div>
- </div>
- <p>In this prompt tool, you first have to configure the location of the Modelverse and the username.
- The defaults should be fine, so just press <return> twice.
- After that, you are in direct connection with the Modelverse.
- Each message you type in, is made available in the <em>input()</em> function of the code.
- Remember that, to send integers, you have to prefix this with a backslash (\).
- To get, for example, the 1st Fibonacci number, type in the command:</p>
- <div class="highlight-default"><div class="highlight"><pre>\<span class="mi">1</span>
- </pre></div>
- </div>
- <p>You will now see the following.</p>
- <img alt="_images/prompt_fibonacci.png" src="_images/prompt_fibonacci.png" />
- <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>
- <img alt="_images/prompt_fibonacci_more.png" src="_images/prompt_fibonacci_more.png" />
- <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.
- This is normal, though in the future the Modelverse will keep running for other users: only the current user’s code will be interrupted.</p>
- </div>
- <div class="section" id="modelling-server">
- <h2>Modelling Server<a class="headerlink" href="#modelling-server" title="Permalink to this headline">¶</a></h2>
- </div>
- </div>
- </div>
- </div>
- </div>
- <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
- <div class="sphinxsidebarwrapper">
- <h3><a href="index.html">Table Of Contents</a></h3>
- <ul>
- <li><a class="reference internal" href="#">Examples</a><ul>
- <li><a class="reference internal" href="#fibonacci-server">Fibonacci Server</a></li>
- <li><a class="reference internal" href="#modelling-server">Modelling Server</a></li>
- </ul>
- </li>
- </ul>
- <h4>Previous topic</h4>
- <p class="topless"><a href="modellanguage.html"
- title="previous chapter">Modelling Language</a></p>
- <h4>Next topic</h4>
- <p class="topless"><a href="advanced.html"
- title="next chapter">Advanced examples</a></p>
- <div role="note" aria-label="source link">
- <h3>This Page</h3>
- <ul class="this-page-menu">
- <li><a href="_sources/examples.txt"
- rel="nofollow">Show Source</a></li>
- </ul>
- </div>
- <div id="searchbox" style="display: none" role="search">
- <h3>Quick search</h3>
- <form class="search" action="search.html" method="get">
- <div><input type="text" name="q" /></div>
- <div><input type="submit" value="Go" /></div>
- <input type="hidden" name="check_keywords" value="yes" />
- <input type="hidden" name="area" value="default" />
- </form>
- </div>
- <script type="text/javascript">$('#searchbox').show(0);</script>
- </div>
- </div>
- <div class="clearer"></div>
- </div>
- <div class="related" role="navigation" aria-label="related navigation">
- <h3>Navigation</h3>
- <ul>
- <li class="right" style="margin-right: 10px">
- <a href="genindex.html" title="General Index"
- >index</a></li>
- <li class="right" >
- <a href="advanced.html" title="Advanced examples"
- >next</a> |</li>
- <li class="right" >
- <a href="modellanguage.html" title="Modelling Language"
- >previous</a> |</li>
- <li class="nav-item nav-item-0"><a href="index.html">Modelverse 0.4.0 documentation</a> »</li>
- </ul>
- </div>
- <div class="footer" role="contentinfo">
- © Copyright 2016, Yentl Van Tendeloo.
- Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.4.6.
- </div>
- </body>
- </html>
|