mini_modify.alc 11 KB

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