mini_modify.alc 9.1 KB

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