mini_modify.alc 9.4 KB

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