examples.txt 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. Examples
  2. ========
  3. To run this code, store it in a file (*e.g.*, test.alc), and execute the following commands::
  4. python scripts/run_local_modelverse.py 8001 &
  5. python scripts/make_parallel.py http://localhost:8001 test test.alc bootstrap/primitives.alc
  6. python scripts/prompt.py
  7. In the prompt, log on to http://localhost:8001 as user *test*.
  8. Now, all input you send, will be caught in the *input()* calls seen in the code.
  9. Results will also be printed.
  10. Note that all content will, by default, be typed as string.
  11. If you want to send integers or so, prepend the input with a backslash (\\), which allows you to directly input the JSON code.
  12. For example, input *5* will be received as the string "5".
  13. To send the integer 5, the input should be *\\5*.
  14. Fibonacci Server
  15. ----------------
  16. The first example is a simple Fibonacci server.
  17. The code is identical to the action language example from before, as this already included the *server* part (*i.e.*, the while loop).
  18. Now we will just connect to it using *prompt.py* and try out the code directly.
  19. The code is repeated below::
  20. include "primitives.alh"
  21. Integer function fib(param : Integer):
  22. if (param <= 2):
  23. return 1!
  24. else:
  25. return fib(param - 1) + fib(param - 2)!
  26. Void function main():
  27. while(True):
  28. output(fib(input()))
  29. To run this code, save it to a file (*e.g.*, fibonacci.alc) and execute the following command::
  30. python scripts/run_local_modelverse.py 8001
  31. Now, the Modelverse is running, but we still need to upload our code.
  32. To compile the file, together with the primitives.alc file, execute the following command::
  33. python scripts/make_parallel.py http://localhost:8001 test fibonacci.alc bootstrap/primitives.alc
  34. When this finishes, the Modelverse now stores a copy of our code in its own format.
  35. The Modelverse will automatically create the user *test* and start executing the *main* function as this user.
  36. We can therefore simply connect to the Modelverse as the *test* user and start seeing the responses.
  37. To start the prompt, execute the following command::
  38. python scripts/prompt.py
  39. In this prompt tool, you first have to configure the location of the Modelverse and the username.
  40. The defaults should be fine, so just press <return> twice.
  41. After that, you are in direct connection with the Modelverse.
  42. Each message you type in, is made available in the *input()* function of the code.
  43. Remember that, to send integers, you have to prefix this with a backslash (\\).
  44. To get, for example, the 1st Fibonacci number, type in the command::
  45. \1
  46. You will now see the following.
  47. .. image:: img/prompt_fibonacci.png
  48. 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.
  49. .. image:: img/prompt_fibonacci_more.png
  50. Inputting a string directly, such as *1* instead of *\\1*, will make the Modelverse crash on the execution of this code.
  51. This is normal, though in the future the Modelverse will keep running for other users: only the current user's code will be interrupted.
  52. Modelling Server
  53. ----------------