pn_interface.alc 16 KB

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