mini_modify.alc 17 KB

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