pn_interface.alc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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("Element to constrain (empty for global)?")
  92. String model_name
  93. model_name = input()
  94. if (model_name == ""):
  95. // Global constraint
  96. output("Give input to function constructors for GLOBAL constraint!")
  97. set_model_constraints(model, construct_function())
  98. elif (dict_in(model["model"], model_name)):
  99. // Local constraint for this model
  100. output("Give input to function constructors for LOCAL constraint!")
  101. add_constraint(model, model_name, construct_function())
  102. output("Added constraint to model!")
  103. else:
  104. // Local constraint, but model not found
  105. output("Unknown model; aborting")
  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_del"):
  144. String model_name
  145. output("Which model do you want to remove an attribute of?")
  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 want to delete?")
  152. attr_name = input()
  153. if (set_in(dict_keys(attrs), attr_name)):
  154. unset_attribute(model, model_name, attr_name)
  155. output("Attribute deleted!")
  156. else:
  157. output("No such attribute!")
  158. else:
  159. output("No such model!")
  160. elif (cmd == "delete"):
  161. output("What is the name of the element you want to delete?")
  162. cmd = input()
  163. if (dict_in(model["model"], cmd)):
  164. model_delete_element(model, cmd)
  165. output("Deleted!")
  166. else:
  167. output("No such element; aborting")
  168. elif (cmd == "rename"):
  169. output("Old name?")
  170. String old_name_e
  171. old_name_e = input()
  172. if (dict_in(model["model"], old_name_e)):
  173. output("New name?")
  174. String new_name_e
  175. new_name_e = input()
  176. if (dict_in(model["model"], new_name_e)):
  177. output("New name already used; aborting")
  178. else:
  179. dict_add(model["model"], new_name_e, model["model"][old_name_e])
  180. dict_delete(model["model"], old_name_e)
  181. output("Rename complete!")
  182. else:
  183. output("Unknown element; aborting")
  184. elif (cmd == "list"):
  185. Element keys_m
  186. keys_m = dict_keys(model["model"])
  187. output("List of all elements:")
  188. String v_m
  189. while (read_nr_out(keys_m) > 0):
  190. v_m = set_pop(keys_m)
  191. // Filter out anonymous objects
  192. if (bool_not(string_startswith(v_m, "__"))):
  193. typename = reverseKeyLookup(model["metamodel"]["model"], dict_read_node(model["type_mapping"], model["model"][v_m]))
  194. output(((" " + v_m) + " : ") + typename)
  195. elif (cmd == "read"):
  196. output("Element to read?")
  197. cmd = input()
  198. if (dict_in(model["model"], cmd)):
  199. Element read_elem
  200. read_elem = model["model"][cmd]
  201. metamodel_element_pn = dict_read_node(model["type_mapping"], read_elem)
  202. output("Name: " + cmd)
  203. output("Type: " + reverseKeyLookup(model["metamodel"]["model"], metamodel_element_pn))
  204. if (is_edge(read_elem)):
  205. output("Source: " + reverseKeyLookup(model["model"], read_edge_src(read_elem)))
  206. output("Destination: " + reverseKeyLookup(model["model"], read_edge_dst(read_elem)))
  207. if (cast_v2s(read_elem) != "None"):
  208. output("Value: " + cast_v2s(read_elem))
  209. output("Defines attributes:")
  210. attr_list_pn = getInstantiatableAttributes(model, cmd)
  211. attr_keys_pn = dict_keys(attr_list_pn)
  212. while (0 < read_nr_out(attr_keys_pn)):
  213. attr_key_pn = set_pop(attr_keys_pn)
  214. output((((" " + attr_key_pn) + " : ") + cast_v2s(attr_list_pn[attr_key_pn])))
  215. output("Attributes:")
  216. attr_list_pn = getAttributeList(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(((((" " + 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)))
  221. else:
  222. output("Unknown element; aborting")
  223. elif (cmd == "verify"):
  224. output(conformance_scd(model))
  225. elif (cmd == "types"):
  226. Element keys_t
  227. keys_t = dict_keys(model["metamodel"]["model"])
  228. output("List of types:")
  229. String v_t
  230. while (read_nr_out(keys_t) > 0):
  231. v_t = set_pop(keys_t)
  232. if (bool_not(string_startswith(v_t, "__"))):
  233. output(string_join((" " + v_t) + " : ", reverseKeyLookup(model["metamodel"]["metamodel"]["model"], dict_read_node(model["metamodel"]["type_mapping"], model["metamodel"]["model"][v_t]))))
  234. elif (cmd == "retype"):
  235. output("Element to retype?")
  236. String elementname
  237. elementname = input()
  238. if (dict_in(model["model"], elementname)):
  239. output("New type")
  240. typename = input()
  241. if (dict_in(model["metamodel"]["model"], typename)):
  242. // OK, do the retyping
  243. // First try removing the previous type if it exists
  244. dict_delete_node(model["type_mapping"], model["model"][elementname])
  245. // Now add the new type
  246. dict_add(model["type_mapping"], model["model"][elementname], model["metamodel"]["model"][typename])
  247. output("Retyped!")
  248. else:
  249. output("Unknown type; aborting")
  250. else:
  251. output("Unknown element; aborting")
  252. elif (cmd == "switch"):
  253. bottom = bool_not(bottom)
  254. Element tmp_model
  255. tmp_model = model
  256. model = other_metamodel
  257. other_metamodel = tmp_model
  258. if (bottom):
  259. // The type mapping we are using is probably not complete for our model
  260. // so we completely recreate it from the model we have.
  261. output("Switching to conformance bottom mode!")
  262. generate_bottom_type_mapping(model)
  263. else:
  264. // We already switched the models and such, so we are already done!
  265. output("Switching to linguistic metamodel!")
  266. else:
  267. output("Unknown command: " + cast_v2s(cmd))
  268. output("Use command 'help' to get a list of available commands")
  269. Element function main():
  270. output("Welcome to the Model Management Interface, running live on the Modelverse!")
  271. output("Use 'help' command for a list of possible commands")
  272. String command
  273. Element root
  274. Element metamodel
  275. String name
  276. Element my_model
  277. String mm_name
  278. root = create_metamodels()
  279. while (True):
  280. output("Please give your command.")
  281. command = input()
  282. log("Exec " + command)
  283. if (command == "help"):
  284. output("Currently no model is loaded, so your operations are limited to:")
  285. output(" new -- Create a new model and save it for future use")
  286. output(" load -- Load a previously made model")
  287. output(" rename -- Rename a previously made model")
  288. output(" delete -- Delete a previously made model")
  289. output(" list -- Show a list of all stored models")
  290. output(" ramify -- RAMify a previously made model")
  291. output(" help -- Show a list of possible commands")
  292. elif (command == "new"):
  293. output("Metamodel to instantiate?")
  294. mm_name = input()
  295. if (dict_in(root, mm_name)):
  296. output("Name of model?")
  297. name = input()
  298. if (dict_in(root, name)):
  299. output("Model exists; aborting")
  300. else:
  301. my_model = instantiate_model(root[mm_name])
  302. dict_add(root, name, my_model)
  303. model_loaded(my_model)
  304. else:
  305. output("Unknown metamodel; aborting")
  306. elif (command == "load"):
  307. output("Model to load?")
  308. name = input()
  309. if (dict_in(root, name)):
  310. my_model = root[name]
  311. model_loaded(my_model)
  312. else:
  313. output("Model not found; aborting")
  314. elif (command == "list"):
  315. Element keys
  316. String m_menu_list
  317. keys = dict_keys(root)
  318. output("Found models:")
  319. while (read_nr_out(keys) > 0):
  320. m_menu_list = set_pop(keys)
  321. output(((" " + m_menu_list) + " : ") + reverseKeyLookup(root, root[m_menu_list]["metamodel"]))
  322. elif (command == "delete"):
  323. output("Model to delete?")
  324. name = input()
  325. if (dict_in(root, name)):
  326. dict_delete(root, name)
  327. output("Deleted!")
  328. else:
  329. output("Model not found; aborting")
  330. elif (command == "rename"):
  331. output("Old name?")
  332. String old_name
  333. old_name = input()
  334. if (dict_in(root, old_name)):
  335. output("New name?")
  336. String new_name
  337. new_name = input()
  338. if (dict_in(root, new_name)):
  339. output("Model exists; aborting")
  340. else:
  341. dict_add(root, new_name, root[old_name])
  342. dict_delete(root, old_name)
  343. output("Rename complete!")
  344. else:
  345. output("Model not found; aborting")
  346. elif (command == "actions"):
  347. output("Switching to compilation manager!")
  348. compilation_manager()
  349. output("Back in model manager!")
  350. elif (command == "ramify"):
  351. Element result
  352. output("Which model do you want to RAMify?")
  353. name = input()
  354. if (dict_in(root, name)):
  355. my_model = root[name]
  356. result = ramify(my_model)
  357. dict_add(root, name + "_PRE", result["pre"])
  358. dict_add(root, name + "_POST", result["post"])
  359. output("success")
  360. else:
  361. output("Model not found; aborting")
  362. elif (command == "transform"):
  363. Element result
  364. String lhs
  365. String rhs
  366. output("Which model do you want to transform?")
  367. name = input()
  368. if (dict_in(root, name)):
  369. output("Which LHS do you want to match?")
  370. lhs = input()
  371. if (dict_in(root, lhs)):
  372. output("Which RHS do you want to rewrite?")
  373. rhs = input()
  374. if (dict_in(root, rhs)):
  375. transform(root[name], root[lhs], root[rhs])
  376. else:
  377. output("Unknown RHS selected!")
  378. else:
  379. output("Unknown LHS selected!")
  380. else:
  381. output("Unknown host model selected!")
  382. else:
  383. output("Command not recognized, use 'help' for a list of possible commands")