mini_modify.alc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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. log("Wait...")
  126. compiled = compile_code(input())
  127. log("Compiled!")
  128. if (is_physical_string(compiled)):
  129. return "Compilation error: " + cast_string(compiled)!
  130. instantiate_attribute_code(model, element_name, attr_name, compiled)
  131. return "Success"!
  132. else:
  133. return "Attribute not found: " + attr_name!
  134. else:
  135. return "Element not found: " + element_name!
  136. else:
  137. return "Write permission denied"!
  138. String function cmd_attr_del(write : Boolean, model : Element, element_name : String, attr_name : String):
  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. unset_attribute(model, element_name, attr_name)
  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 "Write permission denied"!
  152. String function cmd_attr_name(write : Boolean, model : Element, element_name : String, attr_name : String, new_attr_name : String):
  153. if (write):
  154. if (dict_in(model["model"], element_name)):
  155. Element attrs
  156. attrs = getInstantiatableAttributes(model, element_name, "AttributeLink")
  157. if (set_in(dict_keys(attrs), attr_name)):
  158. if (set_in(dict_keys(attrs), attr_name)):
  159. if (bool_not(set_in(dict_keys(attrs), new_attr_name))):
  160. Boolean optional
  161. String attr_edge
  162. attr_edge = reverseKeyLookup(model["model"], dict_read_edge(model["model"][element_name], attr_name))
  163. optional = read_attribute(model, attr_edge, "optional")
  164. model_undefine_attribute(model, element_name, attr_name)
  165. model_define_attribute_ID(model, element_name, new_attr_name, optional, attrs[attr_name], attr_edge)
  166. return "Success"!
  167. else:
  168. return "Attribute exists: " + new_attr_name!
  169. else:
  170. return "Attribute not found: " + attr_name!
  171. else:
  172. return "Attribute not found: " + attr_name!
  173. else:
  174. return "Element not found: " + element_name!
  175. else:
  176. return "Write permission denied"!
  177. String function cmd_attr_type(write : Boolean, model : Element, element_name : String, attr_name : String, new_attr_type : String):
  178. if (write):
  179. if (dict_in(model["model"], element_name)):
  180. Element attrs
  181. attrs = getInstantiatableAttributes(model, element_name, "AttributeLink")
  182. if (set_in(dict_keys(attrs), attr_name)):
  183. if (set_in(dict_keys(attrs), attr_name)):
  184. Boolean optional
  185. String attr_edge
  186. attr_edge = reverseKeyLookup(model["model"], dict_read_edge(model["model"][element_name], attr_name))
  187. optional = read_attribute(model, attr_edge, "optional")
  188. model_undefine_attribute(model, element_name, attr_name)
  189. model_define_attribute_ID(model, element_name, attr_name, optional, new_attr_type, attr_edge)
  190. return "Success"!
  191. else:
  192. return "Attribute not defined: " + attr_name!
  193. else:
  194. return "Attribute not found: " + attr_name!
  195. else:
  196. return "Element not found: " + element_name!
  197. else:
  198. return "Write permission denied"!
  199. String function cmd_attr_optional(write : Boolean, model : Element, element_name : String, attr_name : String, optional : Boolean):
  200. if (write):
  201. if (dict_in(model["model"], element_name)):
  202. Element attrs
  203. attrs = getInstantiatableAttributes(model, element_name, "AttributeLink")
  204. if (set_in(dict_keys(attrs), attr_name)):
  205. if (set_in(dict_keys(attrs), attr_name)):
  206. String attr_edge
  207. attr_edge = reverseKeyLookup(model["model"], dict_read_edge(model["model"][element_name], attr_name))
  208. if (optional):
  209. log("Setting to optional")
  210. else:
  211. log("Setting to mandatory")
  212. model_undefine_attribute(model, element_name, attr_name)
  213. model_define_attribute_ID(model, element_name, attr_name, optional, attrs[attr_name], attr_edge)
  214. return "Success"!
  215. else:
  216. return "Attribute not defined: " + attr_name!
  217. else:
  218. return "Attribute not found: " + attr_name!
  219. else:
  220. return "Element not found: " + element_name!
  221. else:
  222. return "Write permission denied"!
  223. String function cmd_undefine_attribute(write : Boolean, model : Element, element_name : String, attr_name : String):
  224. if (write):
  225. if (dict_in(model["model"], element_name)):
  226. Element attrs
  227. attrs = getInstantiatableAttributes(model, element_name, "AttributeLink")
  228. if (set_in(dict_keys(attrs), attr_name)):
  229. if (set_in(dict_keys(attrs), attr_name)):
  230. model_undefine_attribute(model, element_name, attr_name)
  231. log("REMOVE OK")
  232. return "Success"!
  233. else:
  234. return "Attribute not defined: " + attr_name!
  235. else:
  236. return "Attribute not found: " + attr_name!
  237. else:
  238. return "Element not found: " + element_name!
  239. else:
  240. return "Write permission denied"!
  241. String function cmd_delete(write : Boolean, model : Element, element_name : String):
  242. if (write):
  243. if (dict_in(model["model"], element_name)):
  244. model_delete_element(model, element_name)
  245. return "Success"!
  246. else:
  247. return "Element not found: " + element_name!
  248. else:
  249. return "Write permission denied"!
  250. String function cmd_list_full(model : Element):
  251. Element keys_m
  252. String v_m
  253. String result
  254. String typename
  255. result = "Success: "
  256. keys_m = dict_keys(model["model"])
  257. while (set_len(keys_m) > 0):
  258. v_m = set_pop(keys_m)
  259. // Filter out anonymous objects
  260. typename = read_type(model, v_m)
  261. result = result + " " + v_m + " : " + typename + "\n"
  262. return result!
  263. String function cmd_read_outgoing(model : Element, element_name : String, type : String):
  264. String result
  265. Element elems
  266. if (dict_in(model["model"], element_name)):
  267. result = "Success: "
  268. elems = allOutgoingAssociationInstances(model, element_name, type)
  269. while (set_len(elems) > 0):
  270. result = string_join(result, set_pop(elems)) + "\n"
  271. return result!
  272. else:
  273. return "Element not found: " + element_name!
  274. String function cmd_read_incoming(model : Element, element_name : String, type : String):
  275. String result
  276. Element elems
  277. if (dict_in(model["model"], element_name)):
  278. result = "Success: "
  279. elems = allIncomingAssociationInstances(model, element_name, type)
  280. while (set_len(elems) > 0):
  281. result = string_join(result, set_pop(elems)) + "\n"
  282. return result!
  283. else:
  284. return "Element not found: " + element_name!
  285. String function cmd_connections_between(model : Element, source_name : String, target_name : String):
  286. String result
  287. Element options
  288. if (dict_in(model["model"], source_name)):
  289. if (dict_in(model["model"], target_name)):
  290. result = "Success: "
  291. options = allowedAssociationsBetween(model, source_name, target_name)
  292. while (set_len(options) > 0):
  293. result = string_join(result, set_pop(options)) + "\n"
  294. return result!
  295. else:
  296. return ("Element not found: " + target_name)!
  297. else:
  298. return ("Element not found: " + source_name)!
  299. String function cmd_read(model : Element, element_name : String):
  300. String result
  301. Element attr_list
  302. Element attr_keys
  303. String attr_key
  304. result = "Success: "
  305. if (dict_in(model["model"], element_name)):
  306. result = result + "Type: " + read_type(model, element_name) + "\n"
  307. if (is_edge(model["model"][element_name])):
  308. result = result + "Source: " + reverseKeyLookup(model["model"], read_edge_src(model["model"][element_name])) + "\n"
  309. result = result + "Destination: " + reverseKeyLookup(model["model"], read_edge_dst(model["model"][element_name])) + "\n"
  310. if (has_value(model["model"][element_name])):
  311. result = result + "Value: " + cast_value(model["model"][element_name]) + "\n"
  312. return result!
  313. else:
  314. return "Element not found: " + element_name!
  315. String function cmd_read_attrs(model : Element, element_name : String):
  316. String result
  317. Element attr_list
  318. Element attr_keys
  319. String attr_key
  320. String attr
  321. result = "Success: "
  322. if (dict_in(model["model"], element_name)):
  323. attr_list = getAttributeList(model, element_name)
  324. attr_keys = dict_keys(attr_list)
  325. while (0 < set_len(attr_keys)):
  326. attr_key = set_pop(attr_keys)
  327. attr = read_attribute(model, element_name, attr_key)
  328. if (read_type(model["metamodel"], attr_list[attr_key]) == "ActionLanguage"):
  329. if (element_eq(attr, read_root())):
  330. result = result + attr_key + " : " + cast_value(attr_list[attr_key]) + " = {\"AL\": \"\"}\n"
  331. else:
  332. result = result + attr_key + " : " + cast_value(attr_list[attr_key]) + " = {\"AL\": " + cast_value(attr) + "}\n"
  333. else:
  334. result = result + attr_key + " : " + cast_value(attr_list[attr_key]) + " = " + cast_value(attr) + "\n"
  335. return result!
  336. else:
  337. return "Element not found: " + element_name!
  338. String function cmd_read_defined_attrs(model : Element, element_name : String):
  339. String result
  340. Element attr_list
  341. Element attr_keys
  342. String attr_key
  343. result = "Success: "
  344. if (dict_in(model["model"], element_name)):
  345. attr_list = getInstantiatableAttributes(model, element_name, "AttributeLink")
  346. attr_keys = dict_keys(attr_list)
  347. while (0 < set_len(attr_keys)):
  348. attr_key = set_pop(attr_keys)
  349. if (value_eq(read_attribute(model, reverseKeyLookup(model["model"], dict_read_edge(model["model"][element_name], attr_key)), "optional"), True)):
  350. result = string_join(result, attr_key) + " ?: " + cast_string(attr_list[attr_key]) + "\n"
  351. else:
  352. result = string_join(result, attr_key) + " : " + cast_string(attr_list[attr_key]) + "\n"
  353. return result!
  354. else:
  355. return "Element not found: " + element_name!
  356. String function cmd_types(model : Element):
  357. Element keys_t
  358. String v_t
  359. String result
  360. keys_t = dict_keys(model["metamodel"]["model"])
  361. result = "Success: "
  362. while (set_len(keys_t) > 0):
  363. v_t = set_pop(keys_t)
  364. if (bool_not(string_startswith(v_t, "__"))):
  365. result = result + string_join(" " + v_t + " : ", read_type(model["metamodel"], v_t)) + "\n"
  366. return result!
  367. String function cmd_retype(write : Boolean, model : Element, element_name : String, new_type : String):
  368. if (write):
  369. if (dict_in(model["model"], element_name)):
  370. if (dict_in(model["metamodel"]["model"], new_type)):
  371. retype(model, element_name, new_type)
  372. return "Success"!
  373. else:
  374. return "Element not found: " + new_type!
  375. else:
  376. return "Element not found: " + element_name!
  377. else:
  378. return "Write permission denied"!
  379. String function cmd_read_association_source(write : Boolean, model : Element, element_name : String):
  380. if (dict_in(model["model"], element_name)):
  381. if (is_edge(model["model"][element_name])):
  382. return "Success: " + readAssociationSource(model, element_name)!
  383. else:
  384. return "Not an association: " + element_name!
  385. else:
  386. return "Element not found: " + element_name!
  387. String function cmd_read_association_destination(write : Boolean, model : Element, element_name : String):
  388. if (dict_in(model["model"], element_name)):
  389. if (is_edge(model["model"][element_name])):
  390. return "Success: " + readAssociationDestination(model, element_name)!
  391. else:
  392. return "Not an association: " + element_name!
  393. else:
  394. return "Element not found: " + element_name!
  395. String function cmd_all_instances(model : Element, type : String):
  396. String result
  397. Element elems
  398. if (dict_in(model["metamodel"]["model"], type)):
  399. result = "Success: "
  400. elems = allInstances(model, type)
  401. while (set_len(elems) > 0):
  402. result = string_join(result, set_pop(elems)) + "\n"
  403. return result!
  404. else:
  405. return "Element not found: " + type!
  406. Boolean function modify(model : Element, write : Boolean):
  407. String cmd
  408. output("Model loaded, ready for commands!")
  409. while (True):
  410. cmd = input()
  411. if (cmd == "help"):
  412. output(cmd_help_m(write))
  413. elif (cmd == "exit"):
  414. return True!
  415. elif (cmd == "upload"):
  416. output(cmd_upload(write, model))
  417. elif (cmd == "instantiate_node"):
  418. output(cmd_instantiate_node(write, model, single_input("Type?"), single_input("Name?")))
  419. elif (cmd == "instantiate_edge"):
  420. output(cmd_instantiate_edge(write, model, single_input("Type?"), single_input("Name?"), single_input("Source?"), single_input("Target?")))
  421. elif (cmd == "attr_add"):
  422. output(cmd_attr_add(write, model, single_input("Name?"), single_input("Attribute name?"), single_input("Value?")))
  423. elif (cmd == "attr_add_code"):
  424. output(cmd_attr_add_code(write, model, single_input("Name?"), single_input("Attribute name?")))
  425. elif (cmd == "attr_delete"):
  426. output(cmd_attr_del(write, model, single_input("Name?"), single_input("Attribute name?")))
  427. elif (cmd == "attr_name"):
  428. output(cmd_attr_name(write, model, single_input("Name?"), single_input("Attribute name?"), single_input("New name?")))
  429. elif (cmd == "attr_type"):
  430. output(cmd_attr_type(write, model, single_input("Name?"), single_input("Attribute name?"), single_input("Type name?")))
  431. elif (cmd == "attr_optional"):
  432. output(cmd_attr_optional(write, model, single_input("Name?"), single_input("Attribute name?"), input()))
  433. elif (cmd == "delete"):
  434. output(cmd_delete(write, model, single_input("Name?")))
  435. elif (cmd == "list_full"):
  436. output(cmd_list_full(model))
  437. elif (cmd == "JSON"):
  438. output("Success: " + JSON_print(model))
  439. elif (cmd == "read_outgoing"):
  440. output(cmd_read_outgoing(model, single_input("Name?"), single_input("Type?")))
  441. elif (cmd == "read_incoming"):
  442. output(cmd_read_incoming(model, single_input("Name?"), single_input("Type?")))
  443. elif (cmd == "read"):
  444. output(cmd_read(model, single_input("Name?")))
  445. elif (cmd == "read_attrs"):
  446. output(cmd_read_attrs(model, single_input("Name?")))
  447. elif (cmd == "read_defined_attrs"):
  448. output(cmd_read_defined_attrs(model, single_input("Name?")))
  449. elif (cmd == "types"):
  450. output(cmd_types(model))
  451. elif (cmd == "retype"):
  452. output(cmd_retype(write, model, single_input("Name?"), single_input("New type?")))
  453. elif (cmd == "read_association_source"):
  454. output(cmd_read_association_source(write, model, single_input("Name?")))
  455. elif (cmd == "read_association_destination"):
  456. output(cmd_read_association_destination(write, model, single_input("Name?")))
  457. elif (cmd == "connections_between"):
  458. output(cmd_connections_between(model, single_input("Source element?"), single_input("Target element?")))
  459. elif (cmd == "all_instances"):
  460. output(cmd_all_instances(model, single_input("Type?")))
  461. elif (cmd == "define_attribute"):
  462. output(cmd_define_attribute(write, model, single_input("On which element?"), single_input("Attribute name?"), single_input("Type?")))
  463. elif (cmd == "undefine_attribute"):
  464. output(cmd_undefine_attribute(write, model, single_input("On which element?"), single_input("Attribute name?")))
  465. else:
  466. output("Unknown command while modelling: " + cast_value(cmd))
  467. output("Use command 'help' to get a list of available commands")
  468. String function single_input(prompt : String):
  469. if (verbose):
  470. output(prompt)
  471. return input()!
  472. Element function set_input(prompt : String):
  473. Element result
  474. Element inp
  475. if (verbose):
  476. output(prompt)
  477. output("-- Set input: empty string to terminate set")
  478. result = set_create()
  479. while (True):
  480. inp = input()
  481. if (value_eq(inp, "")):
  482. return result!
  483. else:
  484. set_add(result, inp)
  485. Element function dict_input(prompt : String):
  486. Element result
  487. Element key
  488. if (verbose):
  489. output(prompt)
  490. output("-- Dict input: empty key to terminate dict")
  491. result = set_create()
  492. while (True):
  493. key = input()
  494. if (value_eq(key, "")):
  495. return result!
  496. else:
  497. dict_add(result, key, input())
  498. Void function set_verbose(v : Boolean):
  499. verbose = v
  500. return!