mini_modify.alc 11 KB

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