123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- Examples
- ========
- To run this code, store it in a file (*e.g.*, test.alc), and execute the following commands::
- python scripts/run_local_modelverse.py 8001 &
- python scripts/make_parallel.py http://localhost:8001 test test.alc bootstrap/primitives.alc
- python scripts/prompt.py
- In the prompt, log on to http://localhost:8001 as user *test*.
- Now, all input you send, will be caught in the *input()* calls seen in the code.
- Results will also be printed.
- 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 *5* will be received as the string "5".
- To send the integer 5, the input should be *\\5*.
- Fibonacci Server
- ----------------
- The first example is a simple Fibonacci server.
- The code is identical to the action language example from before, as this already included the *server* part (*i.e.*, the while loop).
- Now we will just connect to it using *prompt.py* and try out the code directly.
- The code is repeated below::
- 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()))
- To run this code, save it to a file (*e.g.*, fibonacci.alc) and execute the following command::
- python scripts/run_local_modelverse.py 8001
-
- 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::
- python scripts/make_parallel.py http://localhost:8001 test fibonacci.alc bootstrap/primitives.alc
- When this finishes, the Modelverse now stores a copy of our code in its own format.
- The Modelverse will automatically create the user *test* and start executing the *main* function as this user.
- We can therefore simply connect to the Modelverse as the *test* user and start seeing the responses.
- To start the prompt, execute the following command::
- python scripts/prompt.py
-
- 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 *input()* 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::
- \1
- You will now see the following.
- .. image:: img/prompt_fibonacci.png
- Since we are in an unconditional loop, you can send as many requests as you want, as long as they are understandable by the *fib* function.
- .. image:: img/prompt_fibonacci_more.png
- Inputting a string directly, such as *1* instead of *\\1*, 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.
- Modelling Server
- ----------------
|