mini_modify.alc 17 KB

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