mini_modify.alc 21 KB

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