mini_modify.alc 11 KB

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