pn_interface.alc 14 KB

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