mini_modify.alc 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. else:
  37. output(" == READ-ONLY ==")
  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 == "instantiate"):
  47. if (write):
  48. String mm_type_name
  49. output("Type to instantiate?")
  50. mm_type_name = input()
  51. if (dict_in(model["metamodel"]["model"], mm_type_name)):
  52. String element_name
  53. output("Name of new element?")
  54. element_name = input()
  55. if (dict_in(model["model"], element_name)):
  56. output("Element already exists; aborting")
  57. else:
  58. if (is_edge(model["metamodel"]["model"][mm_type_name])):
  59. output("Source name?")
  60. String src_name
  61. src_name = input()
  62. if (dict_in(model["model"], src_name)):
  63. output("Destination name?")
  64. String dst_name
  65. dst_name = input()
  66. if (dict_in(model["model"], dst_name)):
  67. instantiate_link(model, mm_type_name, element_name, src_name, dst_name)
  68. output("Instantiation successful!")
  69. else:
  70. output("Unknown destination; aborting")
  71. else:
  72. output("Unknown source; aborting")
  73. else:
  74. instantiate_node(model, mm_type_name, element_name)
  75. output("Instantiation successful!")
  76. else:
  77. log("Could not find element in " + set_to_string(dict_keys(model["metamodel"]["model"])))
  78. output("Unknown type specified; aborting")
  79. else:
  80. output("Permission denied")
  81. elif (cmd == "attr_modify"):
  82. if (write):
  83. String model_name
  84. output("Element to modify?")
  85. model_name = input()
  86. if (dict_in(model["model"], model_name)):
  87. Element attrs
  88. attrs = getAttributeList(model, model_name)
  89. String attr_name
  90. output("Attribute to modify?")
  91. attr_name = input()
  92. if (set_in(dict_keys(attrs), attr_name)):
  93. output("New value?")
  94. unset_attribute(model, model_name, attr_name)
  95. instantiate_attribute(model, model_name, attr_name, input())
  96. output("Modified!")
  97. else:
  98. output("No such attribute!")
  99. else:
  100. output("No such model!")
  101. else:
  102. output("Permission denied")
  103. elif (cmd == "attr_add"):
  104. if (write):
  105. String model_name
  106. output("Which model do you want to assign an attribute to?")
  107. model_name = input()
  108. if (dict_in(model["model"], model_name)):
  109. Element attrs
  110. attrs = getAttributeList(model, model_name)
  111. String attr_name
  112. output("Which attribute do you wish to assign?")
  113. attr_name = input()
  114. if (set_in(dict_keys(attrs), attr_name)):
  115. output("Value of attribute?")
  116. instantiate_attribute(model, model_name, attr_name, input())
  117. output("Added attribute!")
  118. else:
  119. output("No such attribute!")
  120. else:
  121. output("No such model!")
  122. else:
  123. output("Permission denied")
  124. elif (cmd == "attr_add_code"):
  125. if (write):
  126. String model_name
  127. output("Which model do you want to assign a coded 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("Waiting for code constructors...")
  137. instantiate_attribute_code(model, model_name, attr_name, input())
  138. output("Added attribute!")
  139. else:
  140. output("No such attribute!")
  141. else:
  142. output("No such model!")
  143. else:
  144. output("Permission denied")
  145. elif (cmd == "attr_del"):
  146. if (write):
  147. String model_name
  148. output("Which model do you want to remove an attribute of?")
  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 want to delete?")
  155. attr_name = input()
  156. if (set_in(dict_keys(attrs), attr_name)):
  157. unset_attribute(model, model_name, attr_name)
  158. output("Attribute deleted!")
  159. else:
  160. output("No such attribute!")
  161. else:
  162. output("No such model!")
  163. else:
  164. output("Permission denied")
  165. elif (cmd == "delete"):
  166. if (write):
  167. output("What is the name of the element you want to delete?")
  168. cmd = input()
  169. if (dict_in(model["model"], cmd)):
  170. model_delete_element(model, cmd)
  171. output("Deleted!")
  172. else:
  173. output("No such element; aborting")
  174. else:
  175. output("Permission denied")
  176. elif (cmd == "rename"):
  177. if (write):
  178. output("Old name?")
  179. String old_name_e
  180. old_name_e = input()
  181. if (dict_in(model["model"], old_name_e)):
  182. output("New name?")
  183. String new_name_e
  184. new_name_e = input()
  185. if (dict_in(model["model"], new_name_e)):
  186. output("New name already used; aborting")
  187. else:
  188. dict_add(model["model"], new_name_e, model["model"][old_name_e])
  189. dict_delete(model["model"], old_name_e)
  190. output("Rename complete!")
  191. else:
  192. output("Unknown element; aborting")
  193. else:
  194. output("Permission denied")
  195. elif (cmd == "list"):
  196. Element keys_m
  197. keys_m = dict_keys(model["model"])
  198. output("List of all elements:")
  199. String v_m
  200. while (read_nr_out(keys_m) > 0):
  201. v_m = set_pop(keys_m)
  202. // Filter out anonymous objects
  203. if (bool_not(string_startswith(v_m, "__"))):
  204. typename = read_type(model, v_m)
  205. output(((" " + v_m) + " : ") + typename)
  206. elif (cmd == "list_full"):
  207. Element keys_m
  208. keys_m = dict_keys(model["model"])
  209. output("List of all elements:")
  210. String v_m
  211. while (read_nr_out(keys_m) > 0):
  212. v_m = set_pop(keys_m)
  213. // Filter out anonymous objects
  214. typename = read_type(model, v_m)
  215. output(((" " + v_m) + " : ") + typename)
  216. elif (cmd == "read"):
  217. output("Element to read?")
  218. cmd = input()
  219. if (dict_in(model["model"], cmd)):
  220. output("ID: " + cmd)
  221. output("Type: " + read_type(model, cmd))
  222. if (is_edge(model["model"][cmd])):
  223. output("Source: " + reverseKeyLookup(model["model"], read_edge_src(model["model"][cmd])))
  224. output("Destination: " + reverseKeyLookup(model["model"], read_edge_dst(model["model"][cmd])))
  225. if (has_value(model["model"][cmd])):
  226. output("Value: " + cast_v2s(model["model"][cmd]))
  227. output("Defines attributes:")
  228. attr_list_pn = getInstantiatableAttributes(model, cmd)
  229. attr_keys_pn = dict_keys(attr_list_pn)
  230. while (0 < read_nr_out(attr_keys_pn)):
  231. attr_key_pn = set_pop(attr_keys_pn)
  232. output((((" " + attr_key_pn) + " : ") + cast_v2s(attr_list_pn[attr_key_pn])))
  233. output("Attributes:")
  234. attr_list_pn = getAttributeList(model, cmd)
  235. attr_keys_pn = dict_keys(attr_list_pn)
  236. while (0 < read_nr_out(attr_keys_pn)):
  237. attr_key_pn = set_pop(attr_keys_pn)
  238. output(((((" " + cast_v2s(attr_key_pn)) + " : ") + cast_v2s(attr_list_pn[attr_key_pn])) + " = ") + cast_v2s(read_attribute(model, cmd, attr_key_pn)))
  239. else:
  240. output("Unknown element; aborting")
  241. elif (cmd == "verify"):
  242. output(conformance_scd(model))
  243. elif (cmd == "types"):
  244. Element keys_t
  245. keys_t = dict_keys(model["metamodel"]["model"])
  246. output("List of types:")
  247. String v_t
  248. while (read_nr_out(keys_t) > 0):
  249. v_t = set_pop(keys_t)
  250. if (bool_not(string_startswith(v_t, "__"))):
  251. output(string_join((" " + v_t) + " : ", read_type(model, v_t)))
  252. elif (cmd == "retype"):
  253. if (write):
  254. output("Element to retype?")
  255. String elementname
  256. elementname = input()
  257. if (dict_in(model["model"], elementname)):
  258. output("New type")
  259. typename = input()
  260. if (dict_in(model["metamodel"]["model"], typename)):
  261. retype(model, elementname, typename)
  262. output("Retyped!")
  263. else:
  264. output("Unknown type; aborting")
  265. else:
  266. output("Unknown element; aborting")
  267. else:
  268. output("Permission denied")
  269. else:
  270. output("Unknown command: " + cast_v2s(cmd))
  271. output("Use command 'help' to get a list of available commands")
  272. return model!