mini_modify.alc 10 KB

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