mini_modify.alc 12 KB

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