pn_interface.alc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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 "compilation_manager.alh"
  10. include "ramify.alh"
  11. include "transform.alh"
  12. Element function model_loaded(model : Element):
  13. String cmd
  14. Element attr_list_pn
  15. Element attr_keys_pn
  16. String attr_key_pn
  17. Element metamodel_element_pn
  18. String typename
  19. Boolean bottom
  20. Element other_metamodel
  21. bottom = False
  22. other_metamodel = create_node()
  23. dict_add(other_metamodel, "model", model["model"])
  24. dict_add(other_metamodel, "type_mapping", create_node())
  25. dict_add(other_metamodel, "metamodel", import_node("models/LTM_bottom"))
  26. dict_add(other_metamodel, "inheritance", other_metamodel["metamodel"]["model"]["__Inheritance"])
  27. output("Model loaded, ready for commands!")
  28. output("Use 'help' command for a list of possible commands")
  29. while (True):
  30. output("Please give your command.")
  31. cmd = input()
  32. if (cmd == "help"):
  33. output("Generic model operations:")
  34. output(" instantiate -- Create a new model element")
  35. output(" delete -- Delete an existing element")
  36. output(" attr_add -- Add an attribute to an element")
  37. output(" attr_add_code -- Add a code attribute to an element")
  38. output(" attr_del -- Delete an attribute of an element")
  39. output(" attr_def -- Define a new attribute")
  40. output(" constrain -- Add a constraint function to the model")
  41. output(" rename -- Rename an existing element")
  42. output(" modify -- Modify the attributes of an element")
  43. output(" list -- Prints the list of elements in the model")
  44. output(" types -- Prints the list of elements that can be instantiated")
  45. output(" read -- Prints the current state of a model element")
  46. output(" verify -- Check whether the model conforms to the metamodel")
  47. output(" retype -- Change the type of an element")
  48. output(" switch -- Switch between conformance bottom and the linguistic metamodel")
  49. output(" exit -- Unload the model and go back to the loading prompt")
  50. elif (cmd == "exit"):
  51. return model!
  52. elif (cmd == "attr_def"):
  53. String name
  54. String attr
  55. String type
  56. output("Which element do you want to define an attribute for?")
  57. name = input()
  58. output("What is the name of the attribute?")
  59. attr = input()
  60. output("Type of attribute?")
  61. type = input()
  62. model_define_attribute(model, name, attr, type)
  63. elif (cmd == "instantiate"):
  64. String mm_type_name
  65. output("Type to instantiate?")
  66. mm_type_name = input()
  67. if (dict_in(model["metamodel"]["model"], mm_type_name)):
  68. String element_name
  69. output("Name of new element?")
  70. element_name = input()
  71. if (dict_in(model["model"], element_name)):
  72. output("Element already exists; aborting")
  73. else:
  74. if (is_edge(model["metamodel"]["model"][mm_type_name])):
  75. output("Source name?")
  76. String src_name
  77. src_name = input()
  78. if (dict_in(model["model"], src_name)):
  79. output("Destination name?")
  80. String dst_name
  81. dst_name = input()
  82. if (dict_in(model["model"], dst_name)):
  83. instantiate_link(model, mm_type_name, element_name, src_name, dst_name)
  84. output("Instantiation successful!")
  85. else:
  86. output("Unknown destination; aborting")
  87. else:
  88. output("Unknown source; aborting")
  89. else:
  90. instantiate_node(model, mm_type_name, element_name)
  91. output("Instantiation successful!")
  92. else:
  93. output("Unknown type specified; aborting")
  94. elif (cmd == "set_inheritance"):
  95. String inh_name
  96. output("Which link in the metamodel is the inheritance link?")
  97. inh_name = input()
  98. if (dict_in(model["metamodel"]["model"], inh_name)):
  99. dict_add(model, "inheritance", model["metamodel"]["model"][inh_name])
  100. output("Set inheritance link!")
  101. else:
  102. output("Element not found in metamodel; aborting")
  103. elif (cmd == "constrain"):
  104. output("Give input to function constructors for GLOBAL constraint!")
  105. set_model_constraints(model, construct_function())
  106. elif (cmd == "modify"):
  107. String model_name
  108. output("Element to modify?")
  109. model_name = input()
  110. if (dict_in(model["model"], model_name)):
  111. Element attrs
  112. attrs = getAttributeList(model, model_name)
  113. String attr_name
  114. output("Attribute to modify?")
  115. attr_name = input()
  116. if (set_in(dict_keys(attrs), attr_name)):
  117. output("New value?")
  118. unset_attribute(model, model_name, attr_name)
  119. instantiate_attribute(model, model_name, attr_name, input())
  120. output("Modified!")
  121. else:
  122. output("No such attribute!")
  123. else:
  124. output("No such model!")
  125. elif (cmd == "attr_add"):
  126. String model_name
  127. output("Which model do you want to assign an attribute to?")
  128. model_name = input()
  129. if (dict_in(model["model"], model_name)):
  130. Element attrs
  131. attrs = getAttributeList(model, model_name)
  132. String attr_name
  133. output("Which attribute do you wish to assign?")
  134. attr_name = input()
  135. if (set_in(dict_keys(attrs), attr_name)):
  136. output("Value of attribute?")
  137. instantiate_attribute(model, model_name, attr_name, input())
  138. output("Added attribute!")
  139. else:
  140. output("No such attribute!")
  141. else:
  142. output("No such model!")
  143. elif (cmd == "attr_add_code"):
  144. String model_name
  145. output("Which model do you want to assign a coded attribute to?")
  146. model_name = input()
  147. if (dict_in(model["model"], model_name)):
  148. Element attrs
  149. attrs = getAttributeList(model, model_name)
  150. String attr_name
  151. output("Which attribute do you wish to assign?")
  152. attr_name = input()
  153. if (set_in(dict_keys(attrs), attr_name)):
  154. output("Constructors for code?")
  155. instantiate_attribute_code(model, model_name, attr_name, construct_function())
  156. output("Added code!")
  157. else:
  158. output("No such attribute!")
  159. else:
  160. output("No such model!")
  161. elif (cmd == "attr_del"):
  162. String model_name
  163. output("Which model do you want to remove an attribute of?")
  164. model_name = input()
  165. if (dict_in(model["model"], model_name)):
  166. Element attrs
  167. attrs = getAttributeList(model, model_name)
  168. String attr_name
  169. output("Which attribute do you want to delete?")
  170. attr_name = input()
  171. if (set_in(dict_keys(attrs), attr_name)):
  172. unset_attribute(model, model_name, attr_name)
  173. output("Attribute deleted!")
  174. else:
  175. output("No such attribute!")
  176. else:
  177. output("No such model!")
  178. elif (cmd == "delete"):
  179. output("What is the name of the element you want to delete?")
  180. cmd = input()
  181. if (dict_in(model["model"], cmd)):
  182. model_delete_element(model, cmd)
  183. output("Deleted!")
  184. else:
  185. output("No such element; aborting")
  186. elif (cmd == "rename"):
  187. output("Old name?")
  188. String old_name_e
  189. old_name_e = input()
  190. if (dict_in(model["model"], old_name_e)):
  191. output("New name?")
  192. String new_name_e
  193. new_name_e = input()
  194. if (dict_in(model["model"], new_name_e)):
  195. output("New name already used; aborting")
  196. else:
  197. dict_add(model["model"], new_name_e, model["model"][old_name_e])
  198. dict_delete(model["model"], old_name_e)
  199. output("Rename complete!")
  200. else:
  201. output("Unknown element; aborting")
  202. elif (cmd == "list"):
  203. Element keys_m
  204. keys_m = dict_keys(model["model"])
  205. output("List of all elements:")
  206. String v_m
  207. while (read_nr_out(keys_m) > 0):
  208. v_m = set_pop(keys_m)
  209. // Filter out anonymous objects
  210. if (bool_not(string_startswith(v_m, "__"))):
  211. typename = reverseKeyLookup(model["metamodel"]["model"], dict_read_node(model["type_mapping"], model["model"][v_m]))
  212. output(((" " + v_m) + " : ") + typename)
  213. elif (cmd == "read"):
  214. output("Element to read?")
  215. cmd = input()
  216. if (dict_in(model["model"], cmd)):
  217. Element read_elem
  218. read_elem = model["model"][cmd]
  219. metamodel_element_pn = dict_read_node(model["type_mapping"], read_elem)
  220. output("Name: " + cmd)
  221. output("Type: " + reverseKeyLookup(model["metamodel"]["model"], metamodel_element_pn))
  222. if (is_edge(read_elem)):
  223. output("Source: " + reverseKeyLookup(model["model"], read_edge_src(read_elem)))
  224. output("Destination: " + reverseKeyLookup(model["model"], read_edge_dst(read_elem)))
  225. if (cast_v2s(read_elem) != "None"):
  226. output("Value: " + cast_v2s(read_elem))
  227. output("Defines attributes:")
  228. attr_list_pn = getInstantiatableAttributes(model, cmd)
  229. attr_keys_pn = dict_keys(attr_list_pn)
  230. while (0 < read_nr_out(attr_keys_pn)):
  231. attr_key_pn = set_pop(attr_keys_pn)
  232. output((((" " + attr_key_pn) + " : ") + cast_v2s(attr_list_pn[attr_key_pn])))
  233. output("Attributes:")
  234. attr_list_pn = getAttributeList(model, cmd)
  235. attr_keys_pn = dict_keys(attr_list_pn)
  236. while (0 < read_nr_out(attr_keys_pn)):
  237. attr_key_pn = set_pop(attr_keys_pn)
  238. output(((((" " + cast_v2s(attr_key_pn)) + " : ") + cast_v2s(attr_list_pn[attr_key_pn])) + " = ") + cast_v2s(read_attribute(model, reverseKeyLookup(model["model"], read_elem), attr_key_pn)))
  239. else:
  240. output("Unknown element; aborting")
  241. elif (cmd == "verify"):
  242. output(conformance_scd(model))
  243. elif (cmd == "types"):
  244. Element keys_t
  245. keys_t = dict_keys(model["metamodel"]["model"])
  246. output("List of types:")
  247. String v_t
  248. while (read_nr_out(keys_t) > 0):
  249. v_t = set_pop(keys_t)
  250. if (bool_not(string_startswith(v_t, "__"))):
  251. output(string_join((" " + v_t) + " : ", reverseKeyLookup(model["metamodel"]["metamodel"]["model"], dict_read_node(model["metamodel"]["type_mapping"], model["metamodel"]["model"][v_t]))))
  252. elif (cmd == "retype"):
  253. output("Element to retype?")
  254. String elementname
  255. elementname = input()
  256. if (dict_in(model["model"], elementname)):
  257. output("New type")
  258. typename = input()
  259. if (dict_in(model["metamodel"]["model"], typename)):
  260. // OK, do the retyping
  261. // First try removing the previous type if it exists
  262. dict_delete_node(model["type_mapping"], model["model"][elementname])
  263. // Now add the new type
  264. dict_add(model["type_mapping"], model["model"][elementname], model["metamodel"]["model"][typename])
  265. output("Retyped!")
  266. else:
  267. output("Unknown type; aborting")
  268. else:
  269. output("Unknown element; aborting")
  270. elif (cmd == "switch"):
  271. bottom = bool_not(bottom)
  272. Element tmp_model
  273. tmp_model = model
  274. model = other_metamodel
  275. other_metamodel = tmp_model
  276. if (bottom):
  277. // The type mapping we are using is probably not complete for our model
  278. // so we completely recreate it from the model we have.
  279. output("Switching to conformance bottom mode!")
  280. generate_bottom_type_mapping(model)
  281. else:
  282. // We already switched the models and such, so we are already done!
  283. output("Switching to linguistic metamodel!")
  284. else:
  285. output("Unknown command: " + cast_v2s(cmd))
  286. output("Use command 'help' to get a list of available commands")
  287. Element function main():
  288. output("Welcome to the Model Management Interface, running live on the Modelverse!")
  289. output("Use 'help' command for a list of possible commands")
  290. String command
  291. Element root
  292. Element metamodel
  293. String name
  294. Element my_model
  295. String mm_name
  296. root = create_metamodels()
  297. while (True):
  298. output("Please give your command.")
  299. command = input()
  300. log("Exec " + command)
  301. if (command == "help"):
  302. output("Currently no model is loaded, so your operations are limited to:")
  303. output(" new -- Create a new model and save it for future use")
  304. output(" load -- Load a previously made model")
  305. output(" rename -- Rename a previously made model")
  306. output(" delete -- Delete a previously made model")
  307. output(" list -- Show a list of all stored models")
  308. output(" ramify -- RAMify a previously made model")
  309. output(" help -- Show a list of possible commands")
  310. elif (command == "new"):
  311. output("Metamodel to instantiate?")
  312. mm_name = input()
  313. if (dict_in(root, mm_name)):
  314. output("Name of model?")
  315. name = input()
  316. if (dict_in(root, name)):
  317. output("Model exists; aborting")
  318. else:
  319. my_model = instantiate_model(root[mm_name])
  320. dict_add(root, name, my_model)
  321. model_loaded(my_model)
  322. else:
  323. output("Unknown metamodel; aborting")
  324. elif (command == "load"):
  325. output("Model to load?")
  326. name = input()
  327. if (dict_in(root, name)):
  328. my_model = root[name]
  329. model_loaded(my_model)
  330. else:
  331. output("Model not found; aborting")
  332. elif (command == "list"):
  333. Element keys
  334. String m_menu_list
  335. keys = dict_keys(root)
  336. output("Found models:")
  337. while (read_nr_out(keys) > 0):
  338. m_menu_list = set_pop(keys)
  339. output(((" " + m_menu_list) + " : ") + reverseKeyLookup(root, root[m_menu_list]["metamodel"]))
  340. elif (command == "delete"):
  341. output("Model to delete?")
  342. name = input()
  343. if (dict_in(root, name)):
  344. dict_delete(root, name)
  345. output("Deleted!")
  346. else:
  347. output("Model not found; aborting")
  348. elif (command == "rename"):
  349. output("Old name?")
  350. String old_name
  351. old_name = input()
  352. if (dict_in(root, old_name)):
  353. output("New name?")
  354. String new_name
  355. new_name = input()
  356. if (dict_in(root, new_name)):
  357. output("Model exists; aborting")
  358. else:
  359. dict_add(root, new_name, root[old_name])
  360. dict_delete(root, old_name)
  361. output("Rename complete!")
  362. else:
  363. output("Model not found; aborting")
  364. elif (command == "actions"):
  365. output("Switching to compilation manager!")
  366. compilation_manager()
  367. output("Back in model manager!")
  368. elif (command == "ramify"):
  369. Element result
  370. output("Which model do you want to RAMify?")
  371. name = input()
  372. if (dict_in(root, name)):
  373. my_model = root[name]
  374. result = ramify(my_model)
  375. dict_add(root, name + "_PRE", result["pre"])
  376. dict_add(root, name + "_POST", result["post"])
  377. output("success")
  378. else:
  379. output("Model not found; aborting")
  380. elif (command == "transform"):
  381. Element result
  382. String lhs
  383. String rhs
  384. output("Which model do you want to transform?")
  385. name = input()
  386. if (dict_in(root, name)):
  387. output("Which LHS do you want to match?")
  388. lhs = input()
  389. if (dict_in(root, lhs)):
  390. output("Which RHS do you want to rewrite?")
  391. rhs = input()
  392. if (dict_in(root, rhs)):
  393. transform(root[name], root[lhs], root[rhs])
  394. else:
  395. output("Unknown RHS selected!")
  396. else:
  397. output("Unknown LHS selected!")
  398. else:
  399. output("Unknown host model selected!")
  400. else:
  401. output("Command not recognized, use 'help' for a list of possible commands")