pn_interface.alc 14 KB

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