pn_interface.alc 13 KB

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