mini_modify.alc 17 KB

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