mini_modify.alc 11 KB

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