mini_modify.alc 10 KB

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