mini_modify.alc 9.9 KB

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