mini_modify.alc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. String function pretty_print(model : Element):
  11. Element keys_m
  12. String type
  13. String v_m
  14. Element attr_list
  15. Element attr_keys
  16. String attr_key
  17. String result
  18. result = ""
  19. keys_m = dict_keys(model["model"])
  20. while (read_nr_out(keys_m) > 0):
  21. v_m = set_pop(keys_m)
  22. type = read_type(model["metamodel"], read_type(model, v_m))
  23. if (bool_or(type == "Class", type == "Association")):
  24. result = result + (((" " + v_m) + " : ") + read_type(model, v_m))
  25. result = result + "\n"
  26. if (type == "Association"):
  27. result = result + (((" " + reverseKeyLookup(model["model"], read_edge_src(model["model"][v_m]))) + " --> ") + reverseKeyLookup(model["model"], read_edge_dst(model["model"][v_m])))
  28. result = result + "\n"
  29. // Defines attributes
  30. attr_list = getInstantiatableAttributes(model, v_m)
  31. attr_keys = dict_keys(attr_list)
  32. while (0 < read_nr_out(attr_keys)):
  33. attr_key = set_pop(attr_keys)
  34. result = result + ((((" " + attr_key) + " : ") + cast_v2s(attr_list[attr_key])))
  35. result = result + "\n"
  36. // Has attributes
  37. attr_list = getAttributeList(model, v_m)
  38. attr_keys = dict_keys(attr_list)
  39. while (0 < read_nr_out(attr_keys)):
  40. attr_key = set_pop(attr_keys)
  41. if (element_eq(read_attribute(model, v_m, attr_key), read_root())):
  42. result = result + ((((" " + cast_v2s(attr_key)) + " : ") + cast_v2s(attr_list[attr_key])) + " = (undefined)")
  43. else:
  44. result = result + (((((" " + cast_v2s(attr_key)) + " : ") + cast_v2s(attr_list[attr_key])) + " = ") + cast_v2s(read_attribute(model, v_m, attr_key)))
  45. result = result + "\n"
  46. return result!
  47. Element function modify(model : Element, write : Boolean):
  48. String cmd
  49. Element attr_list_pn
  50. Element attr_keys_pn
  51. String attr_key_pn
  52. Element metamodel_element_pn
  53. String typename
  54. output("Model loaded, ready for commands!")
  55. output("Use 'help' command for a list of possible commands")
  56. while (True):
  57. output("Please give your command.")
  58. cmd = input()
  59. if (cmd == "help"):
  60. output("Allowed operations:")
  61. if (write):
  62. output(" == READ/WRITE ==")
  63. output(" instantiate -- Create a new model element")
  64. output(" delete -- Delete an existing element")
  65. output(" attr_add -- Add an attribute to an element")
  66. output(" attr_add_code -- Add a coded attribute to an element")
  67. output(" attr_del -- Delete an attribute of an element")
  68. output(" attr_modify -- Modify an attribute of an element")
  69. output(" retype -- Change the type of an element")
  70. output(" upload -- Upload a completely new model")
  71. else:
  72. output(" == READ-ONLY ==")
  73. output(" list -- Prints the list of elements in the model")
  74. output(" list_full -- Prints the list of all elements in the model")
  75. output(" types -- Prints the list of elements that can be instantiated")
  76. output(" read -- Prints the current state of a model element")
  77. output(" verify -- Check whether the model conforms to the metamodel")
  78. output(" exit -- Leave the modification interface")
  79. elif (cmd == "exit"):
  80. return model!
  81. elif (cmd == "upload"):
  82. Element new_model
  83. output("Waiting for model constructors...")
  84. new_model = construct_model_raw(model["metamodel"])
  85. dict_overwrite(model, "model", new_model["model"])
  86. dict_overwrite(model, "type_mapping", new_model["type_mapping"])
  87. elif (cmd == "instantiate"):
  88. if (write):
  89. String mm_type_name
  90. output("Type to instantiate?")
  91. mm_type_name = input()
  92. if (dict_in(model["metamodel"]["model"], mm_type_name)):
  93. String element_name
  94. output("Name of new element?")
  95. element_name = input()
  96. if (dict_in(model["model"], element_name)):
  97. output("Element already exists; aborting")
  98. else:
  99. if (is_edge(model["metamodel"]["model"][mm_type_name])):
  100. output("Source name?")
  101. String src_name
  102. src_name = input()
  103. if (dict_in(model["model"], src_name)):
  104. output("Destination name?")
  105. String dst_name
  106. dst_name = input()
  107. if (dict_in(model["model"], dst_name)):
  108. instantiate_link(model, mm_type_name, element_name, src_name, dst_name)
  109. output("Instantiation successful!")
  110. else:
  111. output("Unknown destination; aborting")
  112. else:
  113. log("Unknown source!")
  114. log("SRC: " + src_name)
  115. log("In set: " + set_to_string(dict_keys(model["model"])))
  116. output("Unknown source; aborting")
  117. else:
  118. instantiate_node(model, mm_type_name, element_name)
  119. output("Instantiation successful!")
  120. else:
  121. output("Unknown type specified; aborting")
  122. else:
  123. output("Permission denied")
  124. elif (cmd == "attr_modify"):
  125. if (write):
  126. String model_name
  127. output("Element to modify?")
  128. model_name = input()
  129. if (dict_in(model["model"], model_name)):
  130. Element attrs
  131. attrs = getAttributeList(model, model_name)
  132. String attr_name
  133. output("Attribute to modify?")
  134. attr_name = input()
  135. if (set_in(dict_keys(attrs), attr_name)):
  136. output("New value?")
  137. unset_attribute(model, model_name, attr_name)
  138. instantiate_attribute(model, model_name, attr_name, input())
  139. output("Modified!")
  140. else:
  141. output("No such attribute!")
  142. else:
  143. output("No such model!")
  144. else:
  145. output("Permission denied")
  146. elif (cmd == "attr_add"):
  147. if (write):
  148. String model_name
  149. output("Which model do you want to assign an attribute to?")
  150. model_name = input()
  151. if (dict_in(model["model"], model_name)):
  152. Element attrs
  153. attrs = getAttributeList(model, model_name)
  154. String attr_name
  155. output("Which attribute do you wish to assign?")
  156. attr_name = input()
  157. if (set_in(dict_keys(attrs), attr_name)):
  158. output("Value of attribute?")
  159. instantiate_attribute(model, model_name, attr_name, input())
  160. output("Added attribute!")
  161. else:
  162. output("No such attribute!")
  163. else:
  164. output("No such model!")
  165. else:
  166. output("Permission denied")
  167. elif (cmd == "attr_add_code"):
  168. if (write):
  169. String model_name
  170. output("Which model do you want to assign a coded attribute to?")
  171. model_name = input()
  172. if (dict_in(model["model"], model_name)):
  173. Element attrs
  174. attrs = getAttributeList(model, model_name)
  175. String attr_name
  176. output("Which attribute do you wish to assign?")
  177. attr_name = input()
  178. if (set_in(dict_keys(attrs), attr_name)):
  179. output("Waiting for code constructors...")
  180. instantiate_attribute_code(model, model_name, attr_name, input())
  181. output("Added attribute!")
  182. else:
  183. output("No such attribute!")
  184. else:
  185. output("No such model!")
  186. else:
  187. output("Permission denied")
  188. elif (cmd == "attr_del"):
  189. if (write):
  190. String model_name
  191. output("Which model do you want to remove an attribute of?")
  192. model_name = input()
  193. if (dict_in(model["model"], model_name)):
  194. Element attrs
  195. attrs = getAttributeList(model, model_name)
  196. String attr_name
  197. output("Which attribute do you want to delete?")
  198. attr_name = input()
  199. if (set_in(dict_keys(attrs), attr_name)):
  200. unset_attribute(model, model_name, attr_name)
  201. output("Attribute deleted!")
  202. else:
  203. output("No such attribute!")
  204. else:
  205. output("No such model!")
  206. else:
  207. output("Permission denied")
  208. elif (cmd == "delete"):
  209. if (write):
  210. output("What is the name of the element you want to delete?")
  211. cmd = input()
  212. if (dict_in(model["model"], cmd)):
  213. model_delete_element(model, cmd)
  214. output("Deleted!")
  215. else:
  216. output("No such element; aborting")
  217. else:
  218. output("Permission denied")
  219. elif (cmd == "nice_list"):
  220. pretty_print(model)
  221. elif (cmd == "list"):
  222. Element keys_m
  223. keys_m = dict_keys(model["model"])
  224. output("List of all elements:")
  225. String v_m
  226. while (read_nr_out(keys_m) > 0):
  227. v_m = set_pop(keys_m)
  228. // Filter out anonymous objects
  229. if (bool_not(string_startswith(v_m, "__"))):
  230. typename = read_type(model, v_m)
  231. output(((" " + v_m) + " : ") + typename)
  232. elif (cmd == "list_full"):
  233. Element keys_m
  234. keys_m = dict_keys(model["model"])
  235. output("List of all elements:")
  236. String v_m
  237. while (read_nr_out(keys_m) > 0):
  238. v_m = set_pop(keys_m)
  239. // Filter out anonymous objects
  240. typename = read_type(model, v_m)
  241. output(((" " + v_m) + " : ") + typename)
  242. elif (cmd == "read"):
  243. output("Element to read?")
  244. cmd = input()
  245. if (dict_in(model["model"], cmd)):
  246. output("ID: " + cmd)
  247. output("Type: " + read_type(model, cmd))
  248. if (is_edge(model["model"][cmd])):
  249. output("Source: " + reverseKeyLookup(model["model"], read_edge_src(model["model"][cmd])))
  250. output("Destination: " + reverseKeyLookup(model["model"], read_edge_dst(model["model"][cmd])))
  251. if (has_value(model["model"][cmd])):
  252. output("Value: " + cast_v2s(model["model"][cmd]))
  253. output("Defines attributes:")
  254. attr_list_pn = getInstantiatableAttributes(model, cmd)
  255. attr_keys_pn = dict_keys(attr_list_pn)
  256. while (0 < read_nr_out(attr_keys_pn)):
  257. attr_key_pn = set_pop(attr_keys_pn)
  258. output((((" " + attr_key_pn) + " : ") + cast_v2s(attr_list_pn[attr_key_pn])))
  259. output("Attributes:")
  260. attr_list_pn = getAttributeList(model, cmd)
  261. attr_keys_pn = dict_keys(attr_list_pn)
  262. while (0 < read_nr_out(attr_keys_pn)):
  263. attr_key_pn = set_pop(attr_keys_pn)
  264. output(((((" " + cast_v2s(attr_key_pn)) + " : ") + cast_v2s(attr_list_pn[attr_key_pn])) + " = ") + cast_v2s(read_attribute(model, cmd, attr_key_pn)))
  265. else:
  266. output("Unknown element; aborting")
  267. elif (cmd == "verify"):
  268. output(conformance_scd(model))
  269. elif (cmd == "types"):
  270. Element keys_t
  271. keys_t = dict_keys(model["metamodel"]["model"])
  272. output("List of types:")
  273. String v_t
  274. while (read_nr_out(keys_t) > 0):
  275. v_t = set_pop(keys_t)
  276. if (bool_not(string_startswith(v_t, "__"))):
  277. output(string_join((" " + v_t) + " : ", read_type(model, v_t)))
  278. elif (cmd == "retype"):
  279. if (write):
  280. output("Element to retype?")
  281. String elementname
  282. elementname = input()
  283. if (dict_in(model["model"], elementname)):
  284. output("New type")
  285. typename = input()
  286. if (dict_in(model["metamodel"]["model"], typename)):
  287. retype(model, elementname, typename)
  288. output("Retyped!")
  289. else:
  290. output("Unknown type; aborting")
  291. else:
  292. output("Unknown element; aborting")
  293. else:
  294. output("Permission denied")
  295. else:
  296. output("Unknown command: " + cast_v2s(cmd))
  297. output("Use command 'help' to get a list of available commands")
  298. return model!