mini_modify.alc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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. Boolean verbose = True
  10. String function pretty_print(model : Element):
  11. Element keys_m
  12. String type
  13. String v_m
  14. Element attr_list
  15. Element attr_keys
  16. String attr_key
  17. String result
  18. result = ""
  19. keys_m = dict_keys(model["model"])
  20. while (set_len(keys_m) > 0):
  21. v_m = set_pop(keys_m)
  22. type = read_type(model["metamodel"], read_type(model, v_m))
  23. if (bool_or(type == "Class", type == "Association")):
  24. result = result + (((" " + v_m) + " : ") + read_type(model, v_m))
  25. result = result + "\n"
  26. if (type == "Association"):
  27. result = result + (((" " + reverseKeyLookup(model["model"], read_edge_src(model["model"][v_m]))) + " --> ") + reverseKeyLookup(model["model"], read_edge_dst(model["model"][v_m])))
  28. result = result + "\n"
  29. // Defines attributes
  30. attr_list = getInstantiatableAttributes(model, v_m)
  31. attr_keys = dict_keys(attr_list)
  32. while (set_len(attr_keys) > 0):
  33. attr_key = set_pop(attr_keys)
  34. result = result + ((((" " + attr_key) + " : ") + cast_v2s(attr_list[attr_key])))
  35. result = result + "\n"
  36. // Has attributes
  37. attr_list = getAttributeList(model, v_m)
  38. attr_keys = dict_keys(attr_list)
  39. while (set_len(attr_keys) > 0):
  40. attr_key = set_pop(attr_keys)
  41. if (element_eq(read_attribute(model, v_m, attr_key), read_root())):
  42. result = result + ((((" " + cast_v2s(attr_key)) + " : ") + cast_v2s(attr_list[attr_key])) + " = (undefined)")
  43. else:
  44. result = result + (((((" " + cast_v2s(attr_key)) + " : ") + cast_v2s(attr_list[attr_key])) + " = ") + cast_v2s(read_attribute(model, v_m, attr_key)))
  45. result = result + "\n"
  46. return result!
  47. String function cmd_help_m(write : Boolean):
  48. String result
  49. result = ""
  50. result = result + "Allowed operations:\n"
  51. if (write):
  52. result = result + " == READ/WRITE ==\n"
  53. result = result + " instantiate_node -- Create a new model element (node)\n"
  54. result = result + " instantiate_edge -- Create a new model element (edge)\n"
  55. result = result + " delete -- Delete an existing element\n"
  56. result = result + " attr_add -- Add an attribute to an element\n"
  57. result = result + " attr_add_code -- Add a coded attribute to an element\n"
  58. result = result + " attr_del -- Delete an attribute of an element\n"
  59. result = result + " attr_modify -- Modify an attribute of an element\n"
  60. result = result + " retype -- Change the type of an element\n"
  61. result = result + " upload -- Upload a completely new model\n"
  62. else:
  63. result = result + " == READ-ONLY ==\n"
  64. result = result + " read_outgoing -- Prints the list of outgoing links of an element\n"
  65. result = result + " read_incoming -- Prints the list of incoming links to an element\n"
  66. result = result + " list -- Prints the list of elements in the model\n"
  67. result = result + " list_full -- Prints the list of all elements in the model\n"
  68. result = result + " types -- Prints the list of elements that can be instantiated\n"
  69. result = result + " read -- Prints the current state of a model element\n"
  70. result = result + " verify -- Check whether the model conforms to the metamodel\n"
  71. result = result + " exit -- Leave the modification interface\n"
  72. return result!
  73. String function cmd_upload(write : Boolean, model : Element):
  74. Element new_model
  75. if (write):
  76. output("Waiting for model constructors...")
  77. new_model = construct_model_raw(model["metamodel"])
  78. dict_overwrite(model, "model", new_model["model"])
  79. dict_overwrite(model, "type_mapping", new_model["type_mapping"])
  80. return "Success"!
  81. else:
  82. return "Permission denied to write"!
  83. String function cmd_instantiate_node(write : Boolean, model : Element, mm_type_name : String, element_name : String):
  84. if (write):
  85. if (dict_in(model["metamodel"]["model"], mm_type_name)):
  86. if (dict_in(model["model"], element_name)):
  87. return "Element exists: " + element_name!
  88. else:
  89. if (is_edge(model["metamodel"]["model"][mm_type_name])):
  90. return "Element is not a node but an edge: " + mm_type_name!
  91. element_name = instantiate_node(model, mm_type_name, element_name)
  92. return "Success: " + element_name!
  93. else:
  94. return "Element not found: " + mm_type_name!
  95. else:
  96. return "Permission denied to write"!
  97. String function cmd_instantiate_edge(write : Boolean, model : Element, mm_type_name : String, element_name : String, source_name : String, target_name : String):
  98. if (write):
  99. if (dict_in(model["metamodel"]["model"], mm_type_name)):
  100. if (dict_in(model["model"], element_name)):
  101. return "Element exists: " + element_name!
  102. else:
  103. if (is_edge(model["metamodel"]["model"][mm_type_name])):
  104. if (dict_in(model["model"], source_name)):
  105. if (dict_in(model["model"], target_name)):
  106. element_name = instantiate_link(model, mm_type_name, element_name, source_name, target_name)
  107. return "Success: " + element_name!
  108. else:
  109. return "Element not found: " + target_name!
  110. else:
  111. return "Element not found: " + source_name!
  112. else:
  113. return "Element is a node not an edge: " + mm_type_name!
  114. else:
  115. return "Element not found: " + mm_type_name!
  116. else:
  117. return "Permission denied to write"!
  118. String function cmd_define_attribute(write : Boolean, model : Element, element_name : String, attr_name : String, type : String):
  119. if (write):
  120. if (dict_in(model["model"], element_name)):
  121. if (dict_in(model["model"], type)):
  122. Element attrs
  123. attrs = getAttributeList(model, element_name)
  124. if (bool_not(set_in(dict_keys(attrs), attr_name))):
  125. model_define_attribute(model, element_name, attr_name, False, type)
  126. return "Success"!
  127. else:
  128. return "Attribute already defined: " + attr_name!
  129. else:
  130. return "Element not found: " + type!
  131. else:
  132. return "Element not found: " + element_name!
  133. else:
  134. return "Permission denied to write"!
  135. String function cmd_attr_add(write : Boolean, model : Element, element_name : String, attr_name : String, value : Element):
  136. if (write):
  137. if (dict_in(model["model"], element_name)):
  138. Element attrs
  139. attrs = getAttributeList(model, element_name)
  140. if (set_in(dict_keys(attrs), attr_name)):
  141. instantiate_attribute(model, element_name, attr_name, value)
  142. return "Success"!
  143. else:
  144. return "Attribute not found: " + attr_name!
  145. else:
  146. return "Element not found: " + element_name!
  147. else:
  148. return "Permission denied to write"!
  149. String function cmd_attr_add_code(write : Boolean, model : Element, element_name : String, attr_name : String):
  150. if (write):
  151. if (dict_in(model["model"], element_name)):
  152. Element attrs
  153. attrs = getAttributeList(model, element_name)
  154. if (set_in(dict_keys(attrs), attr_name)):
  155. output("Waiting for code constructors...")
  156. instantiate_attribute_code(model, element_name, attr_name, construct_function())
  157. return "Success"!
  158. else:
  159. return "Attribute not found: " + attr_name!
  160. else:
  161. return "Element not found: " + element_name!
  162. else:
  163. return "Permission denied to write"!
  164. String function cmd_attr_del(write : Boolean, model : Element, element_name : String, attr_name : String):
  165. if (write):
  166. if (dict_in(model["model"], element_name)):
  167. Element attrs
  168. attrs = getAttributeList(model, element_name)
  169. if (set_in(dict_keys(attrs), attr_name)):
  170. unset_attribute(model, element_name, attr_name)
  171. return "Success"!
  172. else:
  173. return "Attribute not found: " + attr_name!
  174. else:
  175. return "Element not found: " + element_name!
  176. else:
  177. return "Permission denied to write"!
  178. String function cmd_delete(write : Boolean, model : Element, element_name : String):
  179. if (write):
  180. if (dict_in(model["model"], element_name)):
  181. model_delete_element(model, element_name)
  182. return "Success"!
  183. else:
  184. return "Element not found: " + element_name!
  185. else:
  186. return "Permission denied to write"!
  187. String function cmd_list(model : Element):
  188. Element keys_m
  189. String v_m
  190. String result
  191. String typename
  192. result = "Success: "
  193. keys_m = dict_keys(model["model"])
  194. while (set_len(keys_m) > 0):
  195. v_m = set_pop(keys_m)
  196. // Filter out anonymous objects
  197. if (bool_not(string_startswith(v_m, "__"))):
  198. typename = read_type(model, v_m)
  199. result = (result + (((" " + v_m) + " : ") + typename)) + "\n"
  200. return result!
  201. String function cmd_list_full(model : Element):
  202. Element keys_m
  203. String v_m
  204. String result
  205. String typename
  206. result = "Success: "
  207. keys_m = dict_keys(model["model"])
  208. while (set_len(keys_m) > 0):
  209. v_m = set_pop(keys_m)
  210. // Filter out anonymous objects
  211. typename = read_type(model, v_m)
  212. result = (result + (((" " + v_m) + " : ") + typename)) + "\n"
  213. return result!
  214. String function cmd_read_outgoing(model : Element, element_name : String, type : String):
  215. String result
  216. Element elems
  217. if (dict_in(model["model"], element_name)):
  218. result = "Success: "
  219. elems = allOutgoingAssociationInstances(model, element_name, type)
  220. while (set_len(elems) > 0):
  221. result = string_join(result, set_pop(elems)) + "\n"
  222. return result!
  223. else:
  224. return "Element not found: " + element_name!
  225. String function cmd_read_incoming(model : Element, element_name : String, type : String):
  226. String result
  227. Element elems
  228. if (dict_in(model["model"], element_name)):
  229. result = "Success: "
  230. elems = allIncomingAssociationInstances(model, element_name, type)
  231. while (set_len(elems) > 0):
  232. result = string_join(result, set_pop(elems)) + "\n"
  233. return result!
  234. else:
  235. return "Element not found: " + element_name!
  236. String function cmd_connections_between(model : Element, source_name : String, target_name : String):
  237. String result
  238. Element options
  239. if (dict_in(model["model"], source_name)):
  240. if (dict_in(model["model"], target_name)):
  241. result = "Success: "
  242. options = allowedAssociationsBetween(model, source_name, target_name)
  243. while (set_len(options) > 0):
  244. result = string_join(result, set_pop(options)) + "\n"
  245. else:
  246. return ("Element not found: " + target_name)!
  247. else:
  248. return ("Element not found: " + source_name)!
  249. String function cmd_read(model : Element, element_name : String):
  250. String result
  251. Element attr_list
  252. Element attr_keys
  253. String attr_key
  254. result = "Success: "
  255. if (dict_in(model["model"], element_name)):
  256. result = ((result + "ID: ") + element_name) + "\n"
  257. result = ((result + "Type: ") + read_type(model, element_name)) + "\n"
  258. if (is_edge(model["model"][element_name])):
  259. result = ((result + "Source: ") + reverseKeyLookup(model["model"], read_edge_src(model["model"][element_name]))) + "\n"
  260. result = ((result + "Destination: ") + reverseKeyLookup(model["model"], read_edge_dst(model["model"][element_name]))) + "\n"
  261. if (has_value(model["model"][element_name])):
  262. result = ((result + "Value: ") + cast_v2s(model["model"][element_name])) + "\n"
  263. result = result + "Defines attributes:\n"
  264. attr_list = getInstantiatableAttributes(model, element_name)
  265. attr_keys = dict_keys(attr_list)
  266. while (0 < set_len(attr_keys)):
  267. attr_key = set_pop(attr_keys)
  268. result = ((((result + " ") + attr_key) + " : ") + cast_v2s(attr_list[attr_key])) + "\n"
  269. result = result + "Attributes:\n"
  270. attr_list = getAttributeList(model, element_name)
  271. attr_keys = dict_keys(attr_list)
  272. while (0 < set_len(attr_keys)):
  273. attr_key = set_pop(attr_keys)
  274. result = ((((((result + " ") + cast_v2s(attr_key)) + " : ") + cast_v2s(attr_list[attr_key])) + " = ") + cast_v2s(read_attribute(model, element_name, attr_key))) + "\n"
  275. return result!
  276. else:
  277. return "Element not found: " + element_name!
  278. String function cmd_types(model : Element):
  279. Element keys_t
  280. String v_t
  281. String result
  282. keys_t = dict_keys(model["metamodel"]["model"])
  283. result = "Success: "
  284. while (set_len(keys_t) > 0):
  285. v_t = set_pop(keys_t)
  286. if (bool_not(string_startswith(v_t, "__"))):
  287. result = (result + string_join((" " + v_t) + " : ", read_type(model["metamodel"], v_t))) + "\n"
  288. return result!
  289. String function cmd_retype(write : Boolean, model : Element, element_name : String, new_type : String):
  290. if (write):
  291. if (dict_in(model["model"], element_name)):
  292. if (dict_in(model["metamodel"]["model"], new_type)):
  293. retype(model, element_name, new_type)
  294. return "Success"!
  295. else:
  296. return "Element not found: " + new_type!
  297. else:
  298. return "Element not found: " + element_name!
  299. else:
  300. return "Permission denied to write"!
  301. String function cmd_read_association_source(write : Boolean, model : Element, element_name : String):
  302. if (dict_in(model["model"], element_name)):
  303. if (is_edge(model["model"][element_name])):
  304. return "Success: " + readAssociationSource(model, element_name)!
  305. else:
  306. return "Not an association: " + element_name!
  307. else:
  308. return "Element not found: " + element_name!
  309. String function cmd_read_association_destination(write : Boolean, model : Element, element_name : String):
  310. if (dict_in(model["model"], element_name)):
  311. if (is_edge(model["model"][element_name])):
  312. return "Success: " + readAssociationDestination(model, element_name)!
  313. else:
  314. return "Not an association: " + element_name!
  315. else:
  316. return "Element not found: " + element_name!
  317. String function cmd_all_instances(model : Element, type : String):
  318. String result
  319. Element elems
  320. if (dict_in(model["metamodel"]["model"], type)):
  321. result = "Success: "
  322. elems = allInstances(model, type)
  323. while (set_len(elems) > 0):
  324. result = string_join(result, set_pop(elems)) + "\n"
  325. return result!
  326. else:
  327. return "Element not found: " + type!
  328. Element function modify(model : Element, write : Boolean):
  329. String cmd
  330. output("Model loaded, ready for commands!")
  331. while (True):
  332. cmd = input()
  333. if (cmd == "help"):
  334. output(cmd_help_m(write))
  335. elif (cmd == "exit"):
  336. return model!
  337. elif (cmd == "upload"):
  338. output(cmd_upload(write, model))
  339. elif (cmd == "instantiate_node"):
  340. output(cmd_instantiate_node(write, model, single_input("Type?"), single_input("Name?")))
  341. elif (cmd == "instantiate_edge"):
  342. output(cmd_instantiate_edge(write, model, single_input("Type?"), single_input("Name?"), single_input("Source?"), single_input("Target?")))
  343. elif (cmd == "attr_add"):
  344. output(cmd_attr_add(write, model, single_input("Name?"), single_input("Attribute name?"), single_input("Value?")))
  345. elif (cmd == "attr_add_code"):
  346. output(cmd_attr_add_code(write, model, single_input("Name?"), single_input("Attribute name?")))
  347. elif (cmd == "attr_del"):
  348. output(cmd_attr_del(write, model, single_input("Name?"), single_input("Attribute_name?")))
  349. elif (cmd == "delete"):
  350. output(cmd_delete(write, model, single_input("Name?")))
  351. elif (cmd == "nice_list"):
  352. output(pretty_print(model))
  353. elif (cmd == "list"):
  354. output(cmd_list(model))
  355. elif (cmd == "list_full"):
  356. output(cmd_list_full(model))
  357. elif (cmd == "read_outgoing"):
  358. output(cmd_read_outgoing(model, single_input("Name?"), single_input("Type?")))
  359. elif (cmd == "read_incoming"):
  360. output(cmd_read_incoming(model, single_input("Name?"), single_input("Type?")))
  361. elif (cmd == "read"):
  362. output(cmd_read(model, single_input("Name?")))
  363. elif (cmd == "verify"):
  364. output("Success: " + conformance_scd(model))
  365. elif (cmd == "types"):
  366. output(cmd_types(model))
  367. elif (cmd == "retype"):
  368. output(cmd_retype(write, model, single_input("Name?"), single_input("New type?")))
  369. elif (cmd == "read_association_source"):
  370. output(cmd_read_association_source(write, model, single_input("Name?")))
  371. elif (cmd == "read_association_destination"):
  372. output(cmd_read_association_destination(write, model, single_input("Name?")))
  373. elif (cmd == "connections_between"):
  374. output(cmd_connections_between(model, single_input("Source element?"), single_input("Target element?")))
  375. elif (cmd == "all_instances"):
  376. output(cmd_all_instances(model, single_input("Type?")))
  377. elif (cmd == "define_attribute"):
  378. output(cmd_define_attribute(write, model, single_input("On which element?"), single_input("Attribute name?"), single_input("Type?")))
  379. else:
  380. output("Unknown command while modelling: " + cast_v2s(cmd))
  381. output("Use command 'help' to get a list of available commands")
  382. return model!
  383. String function single_input(prompt : String):
  384. if (verbose):
  385. output(prompt)
  386. return input()!
  387. Element function set_input(prompt : String):
  388. Element result
  389. Element inp
  390. if (verbose):
  391. output(prompt)
  392. output("-- Set input: empty string to terminate set")
  393. result = set_create()
  394. while (True):
  395. inp = input()
  396. if (value_eq(inp, "")):
  397. return result!
  398. else:
  399. set_add(result, inp)
  400. Element function dict_input(prompt : String):
  401. Element result
  402. Element key
  403. if (verbose):
  404. output(prompt)
  405. output("-- Dict input: empty key to terminate dict")
  406. result = set_create()
  407. while (True):
  408. key = input()
  409. if (value_eq(key, "")):
  410. return result!
  411. else:
  412. dict_add(result, key, input())
  413. Void function set_verbose(v : Boolean):
  414. verbose = v
  415. return!