pn_interface.alc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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. if (command == "help"):
  302. output("Currently no model is loaded, so your operations are limited to:")
  303. output(" new -- Create a new model and save it for future use")
  304. output(" load -- Load a previously made model")
  305. output(" rename -- Rename a previously made model")
  306. output(" delete -- Delete a previously made model")
  307. output(" list -- Show a list of all stored models")
  308. output(" upload -- Upload a model using the model constructor commands")
  309. output(" ramify -- RAMify existing metamodels to generate a language for transformations")
  310. output(" transform -- Execute a previously defined model transformation")
  311. output(" help -- Show a list of possible commands")
  312. elif (command == "new"):
  313. output("Metamodel to instantiate?")
  314. mm_name = input()
  315. if (dict_in(root, mm_name)):
  316. output("Name of model?")
  317. name = input()
  318. if (dict_in(root, name)):
  319. output("Model exists; aborting")
  320. else:
  321. my_model = instantiate_model(root[mm_name])
  322. dict_add(root, name, my_model)
  323. model_loaded(my_model)
  324. else:
  325. output("Unknown metamodel; aborting")
  326. elif (command == "upload"):
  327. construct_model()
  328. elif (command == "load"):
  329. output("Model to load?")
  330. name = input()
  331. if (dict_in(root, name)):
  332. my_model = root[name]
  333. model_loaded(my_model)
  334. else:
  335. output("Model not found; aborting")
  336. elif (command == "list"):
  337. Element keys
  338. String m_menu_list
  339. keys = dict_keys(root)
  340. output("Found models:")
  341. while (read_nr_out(keys) > 0):
  342. m_menu_list = set_pop(keys)
  343. output(((" " + m_menu_list) + " : ") + reverseKeyLookup(root, root[m_menu_list]["metamodel"]))
  344. elif (command == "delete"):
  345. output("Model to delete?")
  346. name = input()
  347. if (dict_in(root, name)):
  348. dict_delete(root, name)
  349. output("Deleted!")
  350. else:
  351. output("Model not found; aborting")
  352. elif (command == "rename"):
  353. output("Old name?")
  354. String old_name
  355. old_name = input()
  356. if (dict_in(root, old_name)):
  357. output("New name?")
  358. String new_name
  359. new_name = input()
  360. if (dict_in(root, new_name)):
  361. output("Model exists; aborting")
  362. else:
  363. dict_add(root, new_name, root[old_name])
  364. dict_delete(root, old_name)
  365. output("Rename complete!")
  366. else:
  367. output("Model not found; aborting")
  368. elif (command == "actions"):
  369. output("Switching to compilation manager!")
  370. compilation_manager()
  371. output("Back in model manager!")
  372. elif (command == "unify"):
  373. String mm1
  374. String mm2
  375. String name1
  376. String name2
  377. Element result
  378. name = input()
  379. if (dict_in(root, name)):
  380. output("Already exists; aborting")
  381. else:
  382. output("Which language do you want to unify?")
  383. mm1 = input()
  384. if (dict_in(root, mm1)):
  385. output("Name of this language in the unification")
  386. name1 = input()
  387. output("Second language to unify?")
  388. mm2 = input()
  389. if (dict_in(root, mm2)):
  390. output("Name of this language in the unification")
  391. name2 = input()
  392. log("Calling model_fuse")
  393. result = model_fuse(name1, root[mm1], name2, root[mm2])
  394. dict_add(root, name, result)
  395. else:
  396. output("Second model not found; aborting")
  397. else:
  398. output("Model not found; aborting")
  399. elif (command == "split"):
  400. String mm_name
  401. String value
  402. output("Name of the model to split up?")
  403. name = input()
  404. if (dict_in(root, name)):
  405. output("Name of new metamodel?")
  406. mm_name = input()
  407. if (dict_in(root, mm_name)):
  408. output("Typename to split?")
  409. value = input()
  410. model_retype_on_name(root[name], root[mm_name], "-", value)
  411. else:
  412. output("No such metamodel; aborting")
  413. else:
  414. output("No such model; aborting")
  415. elif (command == "join"):
  416. String mm_name
  417. String value
  418. output("Model name?")
  419. name = input()
  420. if (dict_in(root, name)):
  421. output("New metamodel?")
  422. mm_name = input()
  423. if (dict_in(root, mm_name)):
  424. value = input()
  425. model_retype_on_name(root[name], root[mm_name], "+", value)
  426. else:
  427. output("No such metamodel; aborting")
  428. else:
  429. output("No such model; aborting")
  430. elif (command == "ramify"):
  431. Element result
  432. String old_name
  433. output("Name of the RAMified metamodel to create")
  434. name = input()
  435. if (dict_in(root, name)):
  436. output("Already exists; aborting")
  437. else:
  438. output("Source language?")
  439. String source
  440. source = input()
  441. if (dict_in(root, source)):
  442. result = ramify(root[source])
  443. dict_add(root, name, result)
  444. else:
  445. output("Unknown source language; aborting")
  446. elif (command == "transform"):
  447. Element result
  448. String schedule
  449. output("Which model do you want to transform?")
  450. name = input()
  451. if (dict_in(root, name)):
  452. output("Which schedule do you want to execute?")
  453. schedule = input()
  454. if (dict_in(root, schedule)):
  455. result = transform(root[name], root[schedule])
  456. output("Transformation result: " + cast_v2s(result))
  457. else:
  458. output("Unknown transformation selected!")
  459. else:
  460. output("Unknown host model selected!")
  461. else:
  462. output("Command not recognized, use 'help' for a list of possible commands")