mini_modify.alc 9.3 KB

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