mini_modify.alc 17 KB

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