mini_modify.alc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. include "primitives.alh"
  2. include "constructors.alh"
  3. include "object_operations.alh"
  4. include "library.alh"
  5. include "conformance_scd.alh"
  6. include "io.alh"
  7. include "metamodels.alh"
  8. include "modelling.alh"
  9. include "compilation_manager.alh"
  10. Boolean verbose = True
  11. String function pretty_print(model : Element):
  12. Element keys_m
  13. String type
  14. String v_m
  15. Element attr_list
  16. Element attr_keys
  17. String attr_key
  18. String result
  19. result = ""
  20. keys_m = dict_keys(model["model"])
  21. while (read_nr_out(keys_m) > 0):
  22. v_m = set_pop(keys_m)
  23. type = read_type(model["metamodel"], read_type(model, v_m))
  24. if (bool_or(type == "Class", type == "Association")):
  25. result = result + (((" " + v_m) + " : ") + read_type(model, v_m))
  26. result = result + "\n"
  27. if (type == "Association"):
  28. result = result + (((" " + reverseKeyLookup(model["model"], read_edge_src(model["model"][v_m]))) + " --> ") + reverseKeyLookup(model["model"], read_edge_dst(model["model"][v_m])))
  29. result = result + "\n"
  30. // Defines attributes
  31. attr_list = getInstantiatableAttributes(model, v_m)
  32. attr_keys = dict_keys(attr_list)
  33. while (0 < read_nr_out(attr_keys)):
  34. attr_key = set_pop(attr_keys)
  35. result = result + ((((" " + attr_key) + " : ") + cast_v2s(attr_list[attr_key])))
  36. result = result + "\n"
  37. // Has attributes
  38. attr_list = getAttributeList(model, v_m)
  39. attr_keys = dict_keys(attr_list)
  40. while (0 < read_nr_out(attr_keys)):
  41. attr_key = set_pop(attr_keys)
  42. if (element_eq(read_attribute(model, v_m, attr_key), read_root())):
  43. result = result + ((((" " + cast_v2s(attr_key)) + " : ") + cast_v2s(attr_list[attr_key])) + " = (undefined)")
  44. else:
  45. result = result + (((((" " + cast_v2s(attr_key)) + " : ") + cast_v2s(attr_list[attr_key])) + " = ") + cast_v2s(read_attribute(model, v_m, attr_key)))
  46. result = result + "\n"
  47. return result!
  48. String function cmd_help_m(write : Boolean):
  49. String result
  50. result = ""
  51. result = result + "Allowed operations:\n"
  52. if (write):
  53. result = result + " == READ/WRITE ==\n"
  54. result = result + " instantiate_node -- Create a new model element (node)\n"
  55. result = result + " instantiate_edge -- Create a new model element (edge)\n"
  56. result = result + " delete -- Delete an existing element\n"
  57. result = result + " attr_add -- Add an attribute to an element\n"
  58. result = result + " attr_add_code -- Add a coded attribute to an element\n"
  59. result = result + " attr_del -- Delete an attribute of an element\n"
  60. result = result + " attr_modify -- Modify an attribute of an element\n"
  61. result = result + " retype -- Change the type of an element\n"
  62. result = result + " upload -- Upload a completely new model\n"
  63. else:
  64. result = result + " == READ-ONLY ==\n"
  65. result = result + " read_outgoing -- Prints the list of outgoing links of an element\n"
  66. result = result + " read_incoming -- Prints the list of incoming links to an element\n"
  67. result = result + " list -- Prints the list of elements in the model\n"
  68. result = result + " list_full -- Prints the list of all elements in the model\n"
  69. result = result + " types -- Prints the list of elements that can be instantiated\n"
  70. result = result + " read -- Prints the current state of a model element\n"
  71. result = result + " verify -- Check whether the model conforms to the metamodel\n"
  72. result = result + " exit -- Leave the modification interface\n"
  73. return result!
  74. String function cmd_upload(write : Boolean, model : Element):
  75. Element new_model
  76. if (write):
  77. output("Waiting for model constructors...")
  78. new_model = construct_model_raw(model["metamodel"])
  79. dict_overwrite(model, "model", new_model["model"])
  80. dict_overwrite(model, "type_mapping", new_model["type_mapping"])
  81. return "Success"!
  82. else:
  83. return "Permission denied to write"!
  84. String function cmd_instantiate_node(write : Boolean, model : Element, mm_type_name : String, element_name : String):
  85. if (write):
  86. if (dict_in(model["metamodel"]["model"], mm_type_name)):
  87. if (dict_in(model["model"], element_name)):
  88. return "Element exists: " + element_name!
  89. else:
  90. if (is_edge(model["metamodel"]["model"][mm_type_name])):
  91. return "Element is not a node but an edge: " + mm_type_name!
  92. element_name = instantiate_node(model, mm_type_name, element_name)
  93. return "Success: " + element_name!
  94. else:
  95. return "Element not found: " + mm_type_name!
  96. else:
  97. return "Permission denied to write"!
  98. String function cmd_instantiate_edge(write : Boolean, model : Element, mm_type_name : String, element_name : String, source_name : String, target_name : String):
  99. if (write):
  100. if (dict_in(model["metamodel"]["model"], mm_type_name)):
  101. if (dict_in(model["model"], element_name)):
  102. return "Element exists: " + element_name!
  103. else:
  104. if (is_edge(model["metamodel"]["model"][mm_type_name])):
  105. if (dict_in(model["model"], source_name)):
  106. if (dict_in(model["model"], target_name)):
  107. element_name = instantiate_link(model, mm_type_name, element_name, source_name, target_name)
  108. return "Success: " + element_name!
  109. else:
  110. return "Element not found: " + target_name!
  111. else:
  112. return "Element not found: " + source_name!
  113. else:
  114. return "Element is a node not an edge: " + mm_type_name!
  115. else:
  116. return "Element not found: " + mm_type_name!
  117. else:
  118. return "Permission denied to write"!
  119. String function cmd_attr_add(write : Boolean, model : Element, element_name : String, attr_name : String, value : Element):
  120. if (write):
  121. if (dict_in(model["model"], element_name)):
  122. Element attrs
  123. attrs = getAttributeList(model, element_name)
  124. if (set_in(dict_keys(attrs), attr_name)):
  125. instantiate_attribute(model, element_name, attr_name, value)
  126. return "Success"!
  127. else:
  128. return "Attribute not found: " + attr_name!
  129. else:
  130. return "Element not found: " + element_name!
  131. else:
  132. return "Permission denied to write"!
  133. String function cmd_attr_add_code(write : Boolean, model : Element, element_name : String, attr_name : String):
  134. if (write):
  135. if (dict_in(model["model"], element_name)):
  136. Element attrs
  137. attrs = getAttributeList(model, element_name)
  138. if (set_in(dict_keys(attrs), attr_name)):
  139. output("Waiting for code constructors...")
  140. instantiate_attribute_code(model, element_name, attr_name, construct_function())
  141. return "Success"!
  142. else:
  143. return "Attribute not found: " + attr_name!
  144. else:
  145. return "Element not found: " + element_name!
  146. else:
  147. return "Permission denied to write"!
  148. String function cmd_attr_del(write : Boolean, model : Element, element_name : String, attr_name : String):
  149. if (write):
  150. if (dict_in(model["model"], element_name)):
  151. Element attrs
  152. attrs = getAttributeList(model, element_name)
  153. if (set_in(dict_keys(attrs), attr_name)):
  154. unset_attribute(model, element_name, attr_name)
  155. return "Success"!
  156. else:
  157. return "Attribute not found: " + attr_name!
  158. else:
  159. return "Element not found: " + element_name!
  160. else:
  161. return "Permission denied to write"!
  162. String function cmd_delete(write : Boolean, model : Element, element_name : String):
  163. if (write):
  164. if (dict_in(model["model"], element_name)):
  165. model_delete_element(model, element_name)
  166. return "Success"!
  167. else:
  168. return "Element not found: " + element_name!
  169. else:
  170. return "Permission denied to write"!
  171. String function cmd_list(model : Element):
  172. Element keys_m
  173. String v_m
  174. String result
  175. String typename
  176. result = "Success: "
  177. keys_m = dict_keys(model["model"])
  178. while (read_nr_out(keys_m) > 0):
  179. v_m = set_pop(keys_m)
  180. // Filter out anonymous objects
  181. if (bool_not(string_startswith(v_m, "__"))):
  182. typename = read_type(model, v_m)
  183. result = (result + (((" " + v_m) + " : ") + typename)) + "\n"
  184. return result!
  185. String function cmd_list_full(model : Element):
  186. Element keys_m
  187. String v_m
  188. String result
  189. String typename
  190. result = "Success: "
  191. keys_m = dict_keys(model["model"])
  192. while (read_nr_out(keys_m) > 0):
  193. v_m = set_pop(keys_m)
  194. // Filter out anonymous objects
  195. typename = read_type(model, v_m)
  196. result = (result + (((" " + v_m) + " : ") + typename)) + "\n"
  197. return result!
  198. String function cmd_read_outgoing(model : Element, element_name : String, type : String):
  199. String result
  200. Element elems
  201. result = "Success: "
  202. if (dict_in(model["model"], element_name)):
  203. elems = allOutgoingAssociationInstances(model, element_name, type)
  204. while (read_nr_out(elems) > 0):
  205. result = string_join(result, set_pop(elems)) + "\n"
  206. return result!
  207. else:
  208. return "Element not found: " + element_name!
  209. String function cmd_read_incoming(model : Element, element_name : String, type : String):
  210. String result
  211. Element elems
  212. result = ""
  213. if (dict_in(model["model"], element_name)):
  214. elems = allIncomingAssociationInstances(model, element_name, type)
  215. while (read_nr_out(elems) > 0):
  216. result = string_join(result, set_pop(elems)) + "\n"
  217. return result!
  218. else:
  219. return "Element not found: " + element_name!
  220. String function cmd_read(model : Element, element_name : String):
  221. String result
  222. Element attr_list
  223. Element attr_keys
  224. String attr_key
  225. result = "Success: "
  226. if (dict_in(model["model"], element_name)):
  227. result = ((result + "ID: ") + element_name) + "\n"
  228. result = ((result + "Type: ") + read_type(model, element_name)) + "\n"
  229. if (is_edge(model["model"][element_name])):
  230. result = ((result + "Source: ") + reverseKeyLookup(model["model"], read_edge_src(model["model"][element_name]))) + "\n"
  231. result = ((result + "Destination: ") + reverseKeyLookup(model["model"], read_edge_dst(model["model"][element_name]))) + "\n"
  232. if (has_value(model["model"][element_name])):
  233. result = ((result + "Value: ") + cast_v2s(model["model"][element_name])) + "\n"
  234. result = result + "Defines attributes:\n"
  235. attr_list = getInstantiatableAttributes(model, element_name)
  236. attr_keys = dict_keys(attr_list)
  237. while (0 < read_nr_out(attr_keys)):
  238. attr_key = set_pop(attr_keys)
  239. result = ((((result + " ") + attr_key) + " : ") + cast_v2s(attr_list[attr_key])) + "\n"
  240. result = result + "Attributes:\n"
  241. attr_list = getAttributeList(model, element_name)
  242. attr_keys = dict_keys(attr_list)
  243. while (0 < read_nr_out(attr_keys)):
  244. attr_key = set_pop(attr_keys)
  245. result = ((((((result + " ") + cast_v2s(attr_key)) + " : ") + cast_v2s(attr_list[attr_key])) + " = ") + cast_v2s(read_attribute(model, element_name, attr_key))) + "\n"
  246. return result!
  247. else:
  248. return "Element not found: " + element_name!
  249. String function cmd_types(model : Element):
  250. Element keys_t
  251. String v_t
  252. String result
  253. keys_t = dict_keys(model["metamodel"]["model"])
  254. result = "Success: "
  255. while (read_nr_out(keys_t) > 0):
  256. v_t = set_pop(keys_t)
  257. if (bool_not(string_startswith(v_t, "__"))):
  258. result = (result + string_join((" " + v_t) + " : ", read_type(model["metamodel"], v_t))) + "\n"
  259. return result!
  260. String function cmd_retype(write : Boolean, model : Element, element_name : String, new_type : String):
  261. if (write):
  262. if (dict_in(model["model"], element_name)):
  263. if (dict_in(model["metamodel"]["model"], new_type)):
  264. retype(model, element_name, new_type)
  265. return "Success"!
  266. else:
  267. return "Element not found: " + new_type!
  268. else:
  269. return "Element not found: " + element_name!
  270. else:
  271. return "Permission denied to write"!
  272. String function cmd_read_association_source(write : Boolean, model : Element, element_name : String):
  273. if (dict_in(model["model"], element_name)):
  274. if (is_edge(model["model"][element_name])):
  275. return "Success: " + readAssociationSource(model, element_name)!
  276. else:
  277. return "Not an association: " + element_name!
  278. else:
  279. return "Element not found: " + element_name!
  280. String function cmd_read_association_destination(write : Boolean, model : Element, element_name : String):
  281. if (dict_in(model["model"], element_name)):
  282. if (is_edge(model["model"][element_name])):
  283. return "Success: " + readAssociationDestination(model, element_name)!
  284. else:
  285. return "Not an association: " + element_name!
  286. else:
  287. return "Element not found: " + element_name!
  288. Element function modify(model : Element, write : Boolean):
  289. String cmd
  290. output("Model loaded, ready for commands!")
  291. if (write):
  292. output("Mode: r/w")
  293. else:
  294. output("Mode: r")
  295. output("Use 'help' command for a list of possible commands")
  296. while (True):
  297. cmd = input()
  298. if (cmd == "help"):
  299. output(cmd_help_m(write))
  300. elif (cmd == "exit"):
  301. return model!
  302. elif (cmd == "upload"):
  303. output(cmd_upload(write, model))
  304. elif (cmd == "instantiate_node"):
  305. output(cmd_instantiate_node(write, model, single_input("Type?"), single_input("Name?")))
  306. elif (cmd == "instantiate_edge"):
  307. output(cmd_instantiate_edge(write, model, single_input("Type?"), single_input("Name?"), single_input("Source?"), single_input("Target?")))
  308. elif (cmd == "attr_add"):
  309. output(cmd_attr_add(write, model, single_input("Name?"), single_input("Attribute name?"), single_input("Value?")))
  310. elif (cmd == "attr_add_code"):
  311. output(cmd_attr_add_code(write, model, single_input("Name?"), single_input("Attribute name?")))
  312. elif (cmd == "attr_del"):
  313. output(cmd_attr_del(write, model, single_input("Name?"), single_input("Attribute_name?")))
  314. elif (cmd == "delete"):
  315. output(cmd_delete(write, model, single_input("Name?")))
  316. elif (cmd == "nice_list"):
  317. output(pretty_print(model))
  318. elif (cmd == "list"):
  319. output(cmd_list(model))
  320. elif (cmd == "list_full"):
  321. output(cmd_list_full(model))
  322. elif (cmd == "read_outgoing"):
  323. output(cmd_read_outgoing(model, single_input("Name?"), single_input("Type?")))
  324. elif (cmd == "read_incoming"):
  325. output(cmd_read_incoming(model, single_input("Name?"), single_input("Type?")))
  326. elif (cmd == "read"):
  327. output(cmd_read(model, single_input("Name?")))
  328. elif (cmd == "verify"):
  329. output("Success: " + conformance_scd(model))
  330. elif (cmd == "types"):
  331. output(cmd_types(model))
  332. elif (cmd == "retype"):
  333. output(cmd_retype(write, model, single_input("Name?"), single_input("New type?")))
  334. elif (cmd == "read_association_source"):
  335. output(cmd_read_association_source(write, model, single_input("Name?")))
  336. elif (cmd == "read_association_destination"):
  337. output(cmd_read_association_destination(write, model, single_input("Name?")))
  338. else:
  339. output("Unknown command while modelling: " + cast_v2s(cmd))
  340. output("Use command 'help' to get a list of available commands")
  341. return model!
  342. String function single_input(prompt : String):
  343. if (verbose):
  344. output(prompt)
  345. return input()!
  346. Element function set_input(prompt : String):
  347. Element result
  348. Element inp
  349. if (verbose):
  350. output(prompt)
  351. output("-- Set input: empty string to terminate set")
  352. result = create_node()
  353. while (True):
  354. inp = input()
  355. if (value_eq(inp, "")):
  356. return result!
  357. else:
  358. set_add(result, inp)
  359. Element function dict_input(prompt : String):
  360. Element result
  361. Element key
  362. if (verbose):
  363. output(prompt)
  364. output("-- Dict input: empty key to terminate dict")
  365. result = create_node()
  366. while (True):
  367. key = input()
  368. if (value_eq(key, "")):
  369. return result!
  370. else:
  371. dict_add(result, key, input())
  372. Void function set_verbose(v : Boolean):
  373. verbose = v
  374. return!