pn_interface.alc 13 KB

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