pn_interface.alc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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. include "model_management.alh"
  13. Element function model_loaded(model : Element):
  14. String cmd
  15. Element attr_list_pn
  16. Element attr_keys_pn
  17. String attr_key_pn
  18. Element metamodel_element_pn
  19. String typename
  20. Boolean bottom
  21. Element other_metamodel
  22. bottom = False
  23. other_metamodel = create_node()
  24. dict_add(other_metamodel, "model", model["model"])
  25. dict_add(other_metamodel, "type_mapping", create_node())
  26. dict_add(other_metamodel, "metamodel", import_node("models/LTM_bottom"))
  27. dict_add(other_metamodel, "inheritance", other_metamodel["metamodel"]["model"]["__Inheritance"])
  28. output("Model loaded, ready for commands!")
  29. output("Use 'help' command for a list of possible commands")
  30. while (True):
  31. output("Please give your command.")
  32. cmd = input()
  33. if (cmd == "help"):
  34. output("Generic model operations:")
  35. output(" instantiate -- Create a new model element")
  36. output(" delete -- Delete an existing element")
  37. output(" attr_add -- Add an attribute to an element")
  38. output(" attr_add_code -- Add a code attribute to an element")
  39. output(" attr_del -- Delete an attribute of an element")
  40. output(" attr_def -- Define a new attribute")
  41. output(" constrain -- Add a constraint function to the model")
  42. output(" rename -- Rename an existing element")
  43. output(" modify -- Modify the attributes of an element")
  44. output(" list -- Prints the list of elements in the model")
  45. output(" types -- Prints the list of elements that can be instantiated")
  46. output(" read -- Prints the current state of a model element")
  47. output(" verify -- Check whether the model conforms to the metamodel")
  48. output(" retype -- Change the type of an element")
  49. output(" switch -- Switch between conformance bottom and the linguistic metamodel")
  50. output(" exit -- Unload the model and go back to the loading prompt")
  51. elif (cmd == "exit"):
  52. return model!
  53. elif (cmd == "attr_def"):
  54. String name
  55. String attr
  56. String type
  57. output("Which element do you want to define an attribute for?")
  58. name = input()
  59. output("What is the name of the attribute?")
  60. attr = input()
  61. output("Type of attribute?")
  62. type = input()
  63. model_define_attribute(model, name, attr, type)
  64. elif (cmd == "instantiate"):
  65. String mm_type_name
  66. output("Type to instantiate?")
  67. mm_type_name = input()
  68. if (dict_in(model["metamodel"]["model"], mm_type_name)):
  69. String element_name
  70. output("Name of new element?")
  71. element_name = input()
  72. if (dict_in(model["model"], element_name)):
  73. output("Element already exists; aborting")
  74. else:
  75. if (is_edge(model["metamodel"]["model"][mm_type_name])):
  76. output("Source name?")
  77. String src_name
  78. src_name = input()
  79. if (dict_in(model["model"], src_name)):
  80. output("Destination name?")
  81. String dst_name
  82. dst_name = input()
  83. if (dict_in(model["model"], dst_name)):
  84. instantiate_link(model, mm_type_name, element_name, src_name, dst_name)
  85. output("Instantiation successful!")
  86. else:
  87. output("Unknown destination; aborting")
  88. else:
  89. output("Unknown source; aborting")
  90. else:
  91. instantiate_node(model, mm_type_name, element_name)
  92. output("Instantiation successful!")
  93. else:
  94. output("Unknown type specified; aborting")
  95. elif (cmd == "set_inheritance"):
  96. String inh_name
  97. output("Which link in the metamodel is the inheritance link?")
  98. inh_name = input()
  99. if (dict_in(model["metamodel"]["model"], inh_name)):
  100. dict_add(model, "inheritance", model["metamodel"]["model"][inh_name])
  101. output("Set inheritance link!")
  102. else:
  103. output("Element not found in metamodel; aborting")
  104. elif (cmd == "constrain"):
  105. output("Give input to function constructors for GLOBAL constraint!")
  106. set_model_constraints(model, construct_function())
  107. elif (cmd == "modify"):
  108. String model_name
  109. output("Element to modify?")
  110. model_name = input()
  111. if (dict_in(model["model"], model_name)):
  112. Element attrs
  113. attrs = getAttributeList(model, model_name)
  114. String attr_name
  115. output("Attribute to modify?")
  116. attr_name = input()
  117. if (set_in(dict_keys(attrs), attr_name)):
  118. output("New value?")
  119. unset_attribute(model, model_name, attr_name)
  120. instantiate_attribute(model, model_name, attr_name, input())
  121. output("Modified!")
  122. else:
  123. output("No such attribute!")
  124. else:
  125. output("No such model!")
  126. elif (cmd == "attr_add"):
  127. String model_name
  128. output("Which model do you want to assign an attribute to?")
  129. model_name = input()
  130. if (dict_in(model["model"], model_name)):
  131. Element attrs
  132. attrs = getAttributeList(model, model_name)
  133. String attr_name
  134. output("Which attribute do you wish to assign?")
  135. attr_name = input()
  136. if (set_in(dict_keys(attrs), attr_name)):
  137. output("Value of attribute?")
  138. instantiate_attribute(model, model_name, attr_name, input())
  139. output("Added attribute!")
  140. else:
  141. output("No such attribute!")
  142. else:
  143. output("No such model!")
  144. elif (cmd == "attr_add_code"):
  145. String model_name
  146. output("Which model do you want to assign a coded attribute to?")
  147. model_name = input()
  148. if (dict_in(model["model"], model_name)):
  149. Element attrs
  150. attrs = getAttributeList(model, model_name)
  151. String attr_name
  152. output("Which attribute do you wish to assign?")
  153. attr_name = input()
  154. if (set_in(dict_keys(attrs), attr_name)):
  155. output("Constructors for code?")
  156. instantiate_attribute_code(model, model_name, attr_name, construct_function())
  157. output("Added code!")
  158. else:
  159. output("No such attribute!")
  160. else:
  161. output("No such model!")
  162. elif (cmd == "attr_del"):
  163. String model_name
  164. output("Which model do you want to remove an attribute of?")
  165. model_name = input()
  166. if (dict_in(model["model"], model_name)):
  167. Element attrs
  168. attrs = getAttributeList(model, model_name)
  169. String attr_name
  170. output("Which attribute do you want to delete?")
  171. attr_name = input()
  172. if (set_in(dict_keys(attrs), attr_name)):
  173. unset_attribute(model, model_name, attr_name)
  174. output("Attribute deleted!")
  175. else:
  176. output("No such attribute!")
  177. else:
  178. output("No such model!")
  179. elif (cmd == "delete"):
  180. output("What is the name of the element you want to delete?")
  181. cmd = input()
  182. if (dict_in(model["model"], cmd)):
  183. model_delete_element(model, cmd)
  184. output("Deleted!")
  185. else:
  186. output("No such element; aborting")
  187. elif (cmd == "rename"):
  188. output("Old name?")
  189. String old_name_e
  190. old_name_e = input()
  191. if (dict_in(model["model"], old_name_e)):
  192. output("New name?")
  193. String new_name_e
  194. new_name_e = input()
  195. if (dict_in(model["model"], new_name_e)):
  196. output("New name already used; aborting")
  197. else:
  198. dict_add(model["model"], new_name_e, model["model"][old_name_e])
  199. dict_delete(model["model"], old_name_e)
  200. output("Rename complete!")
  201. else:
  202. output("Unknown element; aborting")
  203. elif (cmd == "list"):
  204. Element keys_m
  205. keys_m = dict_keys(model["model"])
  206. output("List of all elements:")
  207. String v_m
  208. while (read_nr_out(keys_m) > 0):
  209. v_m = set_pop(keys_m)
  210. // Filter out anonymous objects
  211. if (bool_not(string_startswith(v_m, "__"))):
  212. typename = reverseKeyLookup(model["metamodel"]["model"], dict_read_node(model["type_mapping"], model["model"][v_m]))
  213. output(((" " + v_m) + " : ") + typename)
  214. elif (cmd == "read"):
  215. output("Element to read?")
  216. cmd = input()
  217. if (dict_in(model["model"], cmd)):
  218. Element read_elem
  219. read_elem = model["model"][cmd]
  220. metamodel_element_pn = dict_read_node(model["type_mapping"], read_elem)
  221. output("Name: " + cmd)
  222. output("Type: " + reverseKeyLookup(model["metamodel"]["model"], metamodel_element_pn))
  223. if (is_edge(read_elem)):
  224. output("Source: " + reverseKeyLookup(model["model"], read_edge_src(read_elem)))
  225. output("Destination: " + reverseKeyLookup(model["model"], read_edge_dst(read_elem)))
  226. if (cast_v2s(read_elem) != "None"):
  227. output("Value: " + cast_v2s(read_elem))
  228. output("Defines attributes:")
  229. attr_list_pn = getInstantiatableAttributes(model, cmd)
  230. attr_keys_pn = dict_keys(attr_list_pn)
  231. while (0 < read_nr_out(attr_keys_pn)):
  232. attr_key_pn = set_pop(attr_keys_pn)
  233. output((((" " + attr_key_pn) + " : ") + cast_v2s(attr_list_pn[attr_key_pn])))
  234. output("Attributes:")
  235. attr_list_pn = getAttributeList(model, cmd)
  236. attr_keys_pn = dict_keys(attr_list_pn)
  237. while (0 < read_nr_out(attr_keys_pn)):
  238. attr_key_pn = set_pop(attr_keys_pn)
  239. 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)))
  240. else:
  241. output("Unknown element; aborting")
  242. elif (cmd == "verify"):
  243. output(conformance_scd(model))
  244. elif (cmd == "types"):
  245. Element keys_t
  246. keys_t = dict_keys(model["metamodel"]["model"])
  247. output("List of types:")
  248. String v_t
  249. while (read_nr_out(keys_t) > 0):
  250. v_t = set_pop(keys_t)
  251. if (bool_not(string_startswith(v_t, "__"))):
  252. output(string_join((" " + v_t) + " : ", reverseKeyLookup(model["metamodel"]["metamodel"]["model"], dict_read_node(model["metamodel"]["type_mapping"], model["metamodel"]["model"][v_t]))))
  253. elif (cmd == "retype"):
  254. output("Element to retype?")
  255. String elementname
  256. elementname = input()
  257. if (dict_in(model["model"], elementname)):
  258. output("New type")
  259. typename = input()
  260. if (dict_in(model["metamodel"]["model"], typename)):
  261. // OK, do the retyping
  262. // First try removing the previous type if it exists
  263. dict_delete_node(model["type_mapping"], model["model"][elementname])
  264. // Now add the new type
  265. dict_add(model["type_mapping"], model["model"][elementname], model["metamodel"]["model"][typename])
  266. output("Retyped!")
  267. else:
  268. output("Unknown type; aborting")
  269. else:
  270. output("Unknown element; aborting")
  271. elif (cmd == "switch"):
  272. bottom = bool_not(bottom)
  273. Element tmp_model
  274. tmp_model = model
  275. model = other_metamodel
  276. other_metamodel = tmp_model
  277. if (bottom):
  278. // The type mapping we are using is probably not complete for our model
  279. // so we completely recreate it from the model we have.
  280. output("Switching to conformance bottom mode!")
  281. generate_bottom_type_mapping(model)
  282. else:
  283. // We already switched the models and such, so we are already done!
  284. output("Switching to linguistic metamodel!")
  285. else:
  286. output("Unknown command: " + cast_v2s(cmd))
  287. output("Use command 'help' to get a list of available commands")
  288. Element function main():
  289. output("Welcome to the Model Management Interface, running live on the Modelverse!")
  290. output("Use 'help' command for a list of possible commands")
  291. String command
  292. Element root
  293. Element metamodel
  294. String name
  295. Element my_model
  296. String mm_name
  297. root = create_metamodels()
  298. while (True):
  299. output("Please give your command.")
  300. command = input()
  301. log("Exec " + command)
  302. if (command == "help"):
  303. output("Currently no model is loaded, so your operations are limited to:")
  304. output(" new -- Create a new model and save it for future use")
  305. output(" load -- Load a previously made model")
  306. output(" rename -- Rename a previously made model")
  307. output(" delete -- Delete a previously made model")
  308. output(" list -- Show a list of all stored models")
  309. output(" upload -- Upload a model using the model constructor commands")
  310. output(" ramify -- RAMify existing metamodels to generate a language for transformations")
  311. output(" transform -- Execute a previously defined model transformation")
  312. output(" help -- Show a list of possible commands")
  313. elif (command == "new"):
  314. output("Metamodel to instantiate?")
  315. mm_name = input()
  316. if (dict_in(root, mm_name)):
  317. output("Name of model?")
  318. name = input()
  319. if (dict_in(root, name)):
  320. output("Model exists; aborting")
  321. else:
  322. my_model = instantiate_model(root[mm_name])
  323. dict_add(root, name, my_model)
  324. model_loaded(my_model)
  325. else:
  326. output("Unknown metamodel; aborting")
  327. elif (command == "upload"):
  328. construct_model()
  329. elif (command == "load"):
  330. output("Model to load?")
  331. name = input()
  332. if (dict_in(root, name)):
  333. my_model = root[name]
  334. model_loaded(my_model)
  335. else:
  336. output("Model not found; aborting")
  337. elif (command == "list"):
  338. Element keys
  339. String m_menu_list
  340. keys = dict_keys(root)
  341. output("Found models:")
  342. while (read_nr_out(keys) > 0):
  343. m_menu_list = set_pop(keys)
  344. output(((" " + m_menu_list) + " : ") + reverseKeyLookup(root, root[m_menu_list]["metamodel"]))
  345. elif (command == "delete"):
  346. output("Model to delete?")
  347. name = input()
  348. if (dict_in(root, name)):
  349. dict_delete(root, name)
  350. output("Deleted!")
  351. else:
  352. output("Model not found; aborting")
  353. elif (command == "rename"):
  354. output("Old name?")
  355. String old_name
  356. old_name = input()
  357. if (dict_in(root, old_name)):
  358. output("New name?")
  359. String new_name
  360. new_name = input()
  361. if (dict_in(root, new_name)):
  362. output("Model exists; aborting")
  363. else:
  364. dict_add(root, new_name, root[old_name])
  365. dict_delete(root, old_name)
  366. output("Rename complete!")
  367. else:
  368. output("Model not found; aborting")
  369. elif (command == "actions"):
  370. output("Switching to compilation manager!")
  371. compilation_manager()
  372. output("Back in model manager!")
  373. elif (command == "unify"):
  374. String mm1
  375. String mm2
  376. String name1
  377. String name2
  378. Element result
  379. name = input()
  380. if (dict_in(root, name)):
  381. output("Already exists; aborting")
  382. else:
  383. output("Which language do you want to unify?")
  384. mm1 = input()
  385. if (dict_in(root, mm1)):
  386. output("Name of this language in the unification")
  387. name1 = input()
  388. output("Second language to unify?")
  389. mm2 = input()
  390. if (dict_in(root, mm2)):
  391. output("Name of this language in the unification")
  392. name2 = input()
  393. log("Calling model_fuse")
  394. result = model_fuse(name1, root[mm1], name2, root[mm2])
  395. dict_add(root, name, result)
  396. else:
  397. output("Second model not found; aborting")
  398. else:
  399. output("Model not found; aborting")
  400. elif (command == "split"):
  401. String mm_name
  402. String value
  403. output("Name of the model to split up?")
  404. name = input()
  405. if (dict_in(root, name)):
  406. output("Name of new metamodel?")
  407. mm_name = input()
  408. if (dict_in(root, mm_name)):
  409. output("Typename to split?")
  410. value = input()
  411. model_retype_on_name(root[name], root[mm_name], "-", value)
  412. else:
  413. output("No such metamodel; aborting")
  414. else:
  415. output("No such model; aborting")
  416. elif (command == "join"):
  417. String mm_name
  418. String value
  419. output("Model name?")
  420. name = input()
  421. if (dict_in(root, name)):
  422. output("New metamodel?")
  423. mm_name = input()
  424. if (dict_in(root, mm_name)):
  425. value = input()
  426. model_retype_on_name(root[name], root[mm_name], "+", value)
  427. else:
  428. output("No such metamodel; aborting")
  429. else:
  430. output("No such model; aborting")
  431. elif (command == "ramify"):
  432. Element result
  433. String old_name
  434. output("Name of the RAMified metamodel to create")
  435. name = input()
  436. if (dict_in(root, name)):
  437. output("Already exists; aborting")
  438. else:
  439. output("Source language?")
  440. String source
  441. source = input()
  442. if (dict_in(root, source)):
  443. result = ramify(root[source])
  444. dict_add(root, name, result)
  445. else:
  446. output("Unknown source language; aborting")
  447. elif (command == "transform"):
  448. Element result
  449. String schedule
  450. output("Which model do you want to transform?")
  451. name = input()
  452. if (dict_in(root, name)):
  453. output("Which schedule do you want to execute?")
  454. schedule = input()
  455. if (dict_in(root, schedule)):
  456. result = transform(root[name], root[schedule])
  457. output("Transformation result: " + cast_e2s(result))
  458. else:
  459. output("Unknown transformation selected!")
  460. else:
  461. output("Unknown host model selected!")
  462. else:
  463. output("Command not recognized, use 'help' for a list of possible commands")