examples.rst 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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. ----------------
  54. The simple Fibonacci server is not very relevant to the primary concern of the Modelverse: (meta-)modelling.
  55. But since we can create whatever kind of server we want, a simple (meta-)modelling server is created.
  56. The code offers an interface for users to execute modelling operations.
  57. The interface itself mostly just relays the incoming messages and operations to the internal modelling library.
  58. The full code is seen below::
  59. include "primitives.alh"
  60. include "constructors.alh"
  61. include "object_operations.alh"
  62. include "library.alh"
  63. include "conformance_scd.alh"
  64. include "io.alh"
  65. include "metamodels.alh"
  66. include "modelling.alh"
  67. include "compilation_manager.alh"
  68. Element function model_loaded(model : Element):
  69. String cmd
  70. Element attr_list_pn
  71. Element attr_keys_pn
  72. String attr_key_pn
  73. Element metamodel_element_pn
  74. String typename
  75. Boolean bottom
  76. Element other_metamodel
  77. bottom = False
  78. other_metamodel = create_node()
  79. dict_add(other_metamodel, "model", model["model"])
  80. dict_add(other_metamodel, "type_mapping", create_node())
  81. dict_add(other_metamodel, "metamodel", import_node("models/LTM_bottom"))
  82. dict_add(other_metamodel, "inheritance", other_metamodel["metamodel"]["model"]["__Inheritance"])
  83. output("Model loaded, ready for commands!")
  84. output("Use 'help' command for a list of possible commands")
  85. while (True):
  86. output("Please give your command.")
  87. cmd = input()
  88. if (cmd == "help"):
  89. output("Generic model operations:")
  90. output(" instantiate -- Create a new model element")
  91. output(" delete -- Delete an existing element")
  92. output(" attr_add -- Add an attribute to an element")
  93. output(" attr_del -- Delete an attribute of an element")
  94. output(" constrain -- Add a constraint function to the model")
  95. output(" rename -- Rename an existing element")
  96. output(" modify -- Modify the attributes of an element")
  97. output(" list -- Prints the list of elements in the model")
  98. output(" types -- Prints the list of elements that can be instantiated")
  99. output(" read -- Prints the current state of a model element")
  100. output(" verify -- Check whether the model conforms to the metamodel")
  101. output(" retype -- Change the type of an element")
  102. output(" switch -- Switch between conformance bottom and the linguistic metamodel")
  103. output(" exit -- Unload the model and go back to the loading prompt")
  104. elif (cmd == "exit"):
  105. return model!
  106. elif (cmd == "instantiate"):
  107. String mm_type_name
  108. output("Type to instantiate?")
  109. mm_type_name = input()
  110. if (dict_in(model["metamodel"]["model"], mm_type_name)):
  111. String element_name
  112. output("Name of new element?")
  113. element_name = input()
  114. if (dict_in(model["model"], element_name)):
  115. output("Element already exists; aborting")
  116. else:
  117. if (is_edge(model["metamodel"]["model"][mm_type_name])):
  118. output("Source name?")
  119. String src_name
  120. src_name = input()
  121. if (dict_in(model["model"], src_name)):
  122. output("Destination name?")
  123. String dst_name
  124. dst_name = input()
  125. if (dict_in(model["model"], dst_name)):
  126. instantiate_link(model, mm_type_name, element_name, src_name, dst_name)
  127. output("Instantiation successful!")
  128. else:
  129. output("Unknown destination; aborting")
  130. else:
  131. output("Unknown source; aborting")
  132. else:
  133. instantiate_node(model, mm_type_name, element_name)
  134. output("Instantiation successful!")
  135. else:
  136. output("Unknown type specified; aborting")
  137. elif (cmd == "set_inheritance"):
  138. String inh_name
  139. output("Which link in the metamodel is the inheritance link?")
  140. inh_name = input()
  141. if (dict_in(model["metamodel"]["model"], inh_name)):
  142. dict_add(model, "inheritance", model["metamodel"]["model"][inh_name])
  143. output("Set inheritance link!")
  144. else:
  145. output("Element not found in metamodel; aborting")
  146. elif (cmd == "constrain"):
  147. output("Element to constrain (empty for global)?")
  148. String model_name
  149. model_name = input()
  150. if (model_name == ""):
  151. // Global constraint
  152. output("Give input to function constructors for GLOBAL constraint!")
  153. set_model_constraints(model, construct_function())
  154. elif (dict_in(model["model"], model_name)):
  155. // Local constraint for this model
  156. output("Give input to function constructors for LOCAL constraint!")
  157. add_constraint(model, model_name, construct_function())
  158. output("Added constraint to model!")
  159. else:
  160. // Local constraint, but model not found
  161. output("Unknown model; aborting")
  162. elif (cmd == "modify"):
  163. String model_name
  164. output("Element to modify?")
  165. model_name = input()
  166. if (dict_in(model["model"], model_name)):
  167. Element attrs
  168. attrs = getAttributeList(model, model_name)
  169. String attr_name
  170. output("Attribute to modify?")
  171. attr_name = input()
  172. if (set_in(dict_keys(attrs), attr_name)):
  173. output("New value?")
  174. unset_attribute(model, model_name, attr_name)
  175. instantiate_attribute(model, model_name, attr_name, input())
  176. output("Modified!")
  177. else:
  178. output("No such attribute!")
  179. else:
  180. output("No such model!")
  181. elif (cmd == "attr_add"):
  182. String model_name
  183. output("Which model do you want to assign an attribute to?")
  184. model_name = input()
  185. if (dict_in(model["model"], model_name)):
  186. Element attrs
  187. attrs = getAttributeList(model, model_name)
  188. String attr_name
  189. output("Which attribute do you wish to assign?")
  190. attr_name = input()
  191. if (set_in(dict_keys(attrs), attr_name)):
  192. output("Value of attribute?")
  193. instantiate_attribute(model, model_name, attr_name, input())
  194. output("Added attribute!")
  195. else:
  196. output("No such attribute!")
  197. else:
  198. output("No such model!")
  199. elif (cmd == "attr_del"):
  200. String model_name
  201. output("Which model do you want to remove an attribute of?")
  202. model_name = input()
  203. if (dict_in(model["model"], model_name)):
  204. Element attrs
  205. attrs = getAttributeList(model, model_name)
  206. String attr_name
  207. output("Which attribute do you want to delete?")
  208. attr_name = input()
  209. if (set_in(dict_keys(attrs), attr_name)):
  210. unset_attribute(model, model_name, attr_name)
  211. output("Attribute deleted!")
  212. else:
  213. output("No such attribute!")
  214. else:
  215. output("No such model!")
  216. elif (cmd == "delete"):
  217. output("What is the name of the element you want to delete?")
  218. cmd = input()
  219. if (dict_in(model["model"], cmd)):
  220. model_delete_element(model, cmd)
  221. output("Deleted!")
  222. else:
  223. output("No such element; aborting")
  224. elif (cmd == "rename"):
  225. output("Old name?")
  226. String old_name_e
  227. old_name_e = input()
  228. if (dict_in(model["model"], old_name_e)):
  229. output("New name?")
  230. String new_name_e
  231. new_name_e = input()
  232. if (dict_in(model["model"], new_name_e)):
  233. output("New name already used; aborting")
  234. else:
  235. dict_add(model["model"], new_name_e, model["model"][old_name_e])
  236. dict_delete(model["model"], old_name_e)
  237. output("Rename complete!")
  238. else:
  239. output("Unknown element; aborting")
  240. elif (cmd == "list"):
  241. Element keys_m
  242. keys_m = dict_keys(model["model"])
  243. output("List of all elements:")
  244. String v_m
  245. while (read_nr_out(keys_m) > 0):
  246. v_m = set_pop(keys_m)
  247. // Filter out anonymous objects
  248. if (bool_not(string_startswith(v_m, "__"))):
  249. typename = reverseKeyLookup(model["metamodel"]["model"], dict_read_node(model["type_mapping"], model["model"][v_m]))
  250. output(((" " + v_m) + " : ") + typename)
  251. elif (cmd == "read"):
  252. output("Element to read?")
  253. cmd = input()
  254. if (dict_in(model["model"], cmd)):
  255. Element read_elem
  256. read_elem = model["model"][cmd]
  257. metamodel_element_pn = dict_read_node(model["type_mapping"], read_elem)
  258. output("Name: " + cmd)
  259. output("Type: " + reverseKeyLookup(model["metamodel"]["model"], metamodel_element_pn))
  260. if (is_edge(read_elem)):
  261. output("Source: " + reverseKeyLookup(model["model"], read_edge_src(read_elem)))
  262. output("Destination: " + reverseKeyLookup(model["model"], read_edge_dst(read_elem)))
  263. if (cast_v2s(read_elem) != "None"):
  264. output("Value: " + cast_v2s(read_elem))
  265. output("Defines attributes:")
  266. attr_list_pn = getInstantiatableAttributes(model, read_elem)
  267. attr_keys_pn = dict_keys(attr_list_pn)
  268. while (0 < read_nr_out(attr_keys_pn)):
  269. attr_key_pn = set_pop(attr_keys_pn)
  270. output((((" " + attr_key_pn) + " : ") + cast_v2s(attr_list_pn[attr_key_pn])))
  271. output("Attributes:")
  272. attr_list_pn = getAttributeList(model, cmd)
  273. attr_keys_pn = dict_keys(attr_list_pn)
  274. while (0 < read_nr_out(attr_keys_pn)):
  275. attr_key_pn = set_pop(attr_keys_pn)
  276. output(((((" " + cast_v2s(attr_key_pn)) + " : ") + cast_v2s(attr_list_pn[attr_key_pn])) + " = ") + cast_v2s(read_attribute(model, reverseKeyLookup(model["model"], read_elem), attr_key_pn)))
  277. else:
  278. output("Unknown element; aborting")
  279. elif (cmd == "verify"):
  280. output(conformance_scd(model))
  281. elif (cmd == "types"):
  282. Element keys_t
  283. keys_t = dict_keys(model["metamodel"]["model"])
  284. output("List of types:")
  285. String v_t
  286. while (read_nr_out(keys_t) > 0):
  287. v_t = set_pop(keys_t)
  288. if (bool_not(string_startswith(v_t, "__"))):
  289. output(string_join((" " + v_t) + " : ", reverseKeyLookup(model["metamodel"]["metamodel"]["model"], dict_read_node(model["metamodel"]["type_mapping"], model["metamodel"]["model"][v_t]))))
  290. elif (cmd == "retype"):
  291. output("Element to retype?")
  292. String elementname
  293. elementname = input()
  294. if (dict_in(model["model"], elementname)):
  295. output("New type")
  296. typename = input()
  297. if (dict_in(model["metamodel"]["model"], typename)):
  298. // OK, do the retyping
  299. // First try removing the previous type if it exists
  300. dict_delete(model["type_mapping"], model["model"][elementname])
  301. // Now add the new type
  302. dict_add(model["type_mapping"], model["model"][elementname], model["metamodel"]["model"][typename])
  303. output("Retyped!")
  304. else:
  305. output("Unknown type; aborting")
  306. else:
  307. output("Unknown element; aborting")
  308. elif (cmd == "switch"):
  309. bottom = bool_not(bottom)
  310. Element tmp_model
  311. tmp_model = model
  312. model = other_metamodel
  313. other_metamodel = tmp_model
  314. if (bottom):
  315. // The type mapping we are using is probably not complete for our model
  316. // so we completely recreate it from the model we have.
  317. output("Switching to conformance bottom mode!")
  318. generate_bottom_type_mapping(model)
  319. else:
  320. // We already switched the models and such, so we are already done!
  321. output("Switching to linguistic metamodel!")
  322. else:
  323. output("Unknown command: " + cast_v2s(cmd))
  324. output("Use command 'help' to get a list of available commands")
  325. Element function main():
  326. output("Welcome to the Model Management Interface, running live on the Modelverse!")
  327. output("Use 'help' command for a list of possible commands")
  328. String command
  329. Element root
  330. Element metamodel
  331. String name
  332. Element my_model
  333. String mm_name
  334. root = create_metamodels()
  335. while (True):
  336. output("Please give your command.")
  337. command = input()
  338. if (command == "help"):
  339. output("Currently no model is loaded, so your operations are limited to:")
  340. output(" new -- Create a new model and save it for future use")
  341. output(" load -- Load a previously made model")
  342. output(" rename -- Rename a previously made model")
  343. output(" delete -- Delete a previously made model")
  344. output(" list -- Show a list of all stored models")
  345. output(" help -- Show a list of possible commands")
  346. elif (command == "new"):
  347. output("Metamodel to instantiate?")
  348. mm_name = input()
  349. if (dict_in(root, mm_name)):
  350. output("Name of model?")
  351. name = input()
  352. if (dict_in(root, name)):
  353. output("Model exists; aborting")
  354. else:
  355. my_model = instantiate_model(root[mm_name])
  356. dict_add(root, name, my_model)
  357. model_loaded(my_model)
  358. else:
  359. output("Unknown metamodel; aborting")
  360. elif (command == "load"):
  361. output("Model to load?")
  362. name = input()
  363. if (dict_in(root, name)):
  364. my_model = root[name]
  365. model_loaded(my_model)
  366. else:
  367. output("Model not found; aborting")
  368. elif (command == "list"):
  369. Element keys
  370. String m_menu_list
  371. keys = dict_keys(root)
  372. output("Found models:")
  373. while (read_nr_out(keys) > 0):
  374. m_menu_list = set_pop(keys)
  375. output(((" " + m_menu_list) + " : ") + reverseKeyLookup(root, root[m_menu_list]["metamodel"]))
  376. elif (command == "delete"):
  377. output("Model to delete?")
  378. name = input()
  379. if (dict_in(root, name)):
  380. dict_delete(root, name)
  381. output("Deleted!")
  382. else:
  383. output("Model not found; aborting")
  384. elif (command == "rename"):
  385. output("Old name?")
  386. String old_name
  387. old_name = input()
  388. if (dict_in(root, old_name)):
  389. output("New name?")
  390. String new_name
  391. new_name = input()
  392. if (dict_in(root, new_name)):
  393. output("Model exists; aborting")
  394. else:
  395. dict_add(root, new_name, root[old_name])
  396. dict_delete(root, old_name)
  397. output("Rename complete!")
  398. else:
  399. output("Model not found; aborting")
  400. elif (command == "actions"):
  401. output("Switching to compilation manager!")
  402. compilation_manager()
  403. output("Back in model manager!")
  404. else:
  405. output("Command not recognized, use 'help' for a list of possible commands")
  406. This code implements a very simple (meta-)modelling tool.
  407. Its use is documented with the provided *help* function.
  408. A simple example of its use is shown below.
  409. .. image:: img/prompt_pn_interface.png
  410. In this case, note that the value of tokens is the string 3 instead of the integer (or natural) 3.
  411. Therefore, the conformance check will flag this value as incorrectly typed.
  412. Upload model
  413. ------------
  414. Uploading a model, and using it, is very similar to what you usually do.
  415. The exception is that your model will be added before any code is executed.
  416. That way, you can just execute the same modelling server, but include a model in it.
  417. Doing that, your interface will see additional models with the *list* command.
  418. The command for this is (instead of the *make_all.py* script)::
  419. python scripts/execute_model.py http://localhost:8001 test bootstrap/*.alc integration/code/pn_interface.alc integration/code/rpgame.mvc