core_algorithm.alc 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
  1. include "modelling.alh"
  2. include "library.alh"
  3. include "primitives.alh"
  4. include "constructors.alh"
  5. include "object_operations.alh"
  6. include "mini_modify.alh"
  7. include "model_management.alh"
  8. include "ramify.alh"
  9. include "conformance_scd.alh"
  10. include "transform.alh"
  11. include "metamodels.alh"
  12. Element core = ?
  13. Void function main():
  14. // Initialize the Core Formalism
  15. String core_location
  16. String core_model
  17. String core_model_location
  18. String core_formalism_model
  19. String scd_location
  20. String scd_model
  21. String admin_group
  22. String admin_user
  23. scd_location = "models/SimpleClassDiagrams"
  24. core_location = "models/CoreFormalism"
  25. core_model_location = "models/core"
  26. // Create the Model itself and make public
  27. core = instantiate_model(import_node(core_location))
  28. export_node(core_model_location, core)
  29. // Create admin group
  30. admin_group = instantiate_node(core, "Group", "")
  31. instantiate_attribute(core, admin_group, "name", "admin")
  32. // Create admin user
  33. admin_user = instantiate_node(core, "User", "")
  34. output("Desired username for admin user?")
  35. instantiate_attribute(core, admin_user, "name", input())
  36. instantiate_attribute(core, admin_user, "admin", True)
  37. // Create link between admin user and group
  38. instantiate_link(core, "ownedBy", "", admin_group, admin_user)
  39. instantiate_link(core, "belongsTo", "", admin_user, admin_group)
  40. // Add the SimpleClassDiagrams formalism already
  41. scd_model = instantiate_node(core, "Model", "")
  42. instantiate_attribute(core, scd_model, "name", "SimpleClassDiagrams")
  43. instantiate_attribute(core, scd_model, "location", scd_location)
  44. instantiate_attribute(core, scd_model, "permissions", "221")
  45. instantiate_link(core, "instanceOf", "", scd_model, scd_model)
  46. // Make necessary links for the formalism to the owners
  47. instantiate_link(core, "group", "", scd_model, admin_group)
  48. instantiate_link(core, "owner", "", scd_model, admin_user)
  49. // Add the core formalism already
  50. core_formalism_model = instantiate_node(core, "Model", "")
  51. instantiate_attribute(core, core_formalism_model, "name", "CoreFormalism")
  52. instantiate_attribute(core, core_formalism_model, "location", core_location)
  53. instantiate_attribute(core, core_formalism_model, "permissions", "221")
  54. instantiate_link(core, "instanceOf", "", core_formalism_model, scd_model)
  55. // Make necessary links for the formalism to the owners
  56. instantiate_link(core, "group", "", core_formalism_model, admin_group)
  57. instantiate_link(core, "owner", "", core_formalism_model, admin_user)
  58. // Add the core model
  59. core_model = instantiate_node(core, "Model", "")
  60. instantiate_attribute(core, core_model, "name", "core")
  61. instantiate_attribute(core, core_model, "location", core_model_location)
  62. instantiate_attribute(core, core_model, "permissions", "200")
  63. instantiate_link(core, "instanceOf", "", core_model, core_formalism_model)
  64. // Make necessary links for the formalism to the owners
  65. instantiate_link(core, "group", "", core_model, admin_group)
  66. instantiate_link(core, "owner", "", core_model, admin_user)
  67. // Switch all new users to the user_function
  68. // This accesses the bootstrap level, so do not change this unless you know what you are doing
  69. Element root
  70. Element prev
  71. root = read_root()
  72. root = root["__hierarchy"]["__IP"]
  73. while (value_neq(root, !call)):
  74. prev = root
  75. root = root["next"]
  76. dict_delete(prev, "next")
  77. dict_add(prev, "next", user_function["body"])
  78. // Call this for ourselves as well
  79. user_function_skip_init(admin_user)
  80. // Done, so finish up
  81. // Admin user will have been deleted by the user_function as usual
  82. // Note that if there are no admin users left, it will be very difficult to manage, as nobody will have admin permissions!
  83. return !
  84. Integer function get_relation_to_model(user_id : String, model_id : String):
  85. if (set_in(allAssociationDestinations(core, model_id, "owner"), user_id)):
  86. // We are the owner
  87. return 0!
  88. else:
  89. String group_id
  90. group_id = set_pop(allAssociationDestinations(core, model_id, "group"))
  91. if (set_in(allAssociationDestinations(core, user_id, "belongsTo"), group_id)):
  92. // We are in the owning group
  93. return 1!
  94. else:
  95. // We are not related whatsoever
  96. return 2!
  97. Boolean function is_admin(user_id : String):
  98. if (read_attribute(core, user_id, "admin")):
  99. return True!
  100. else:
  101. return False!
  102. Boolean function allow_read(user_id : String, model_id : String):
  103. if (is_admin(user_id)):
  104. // Is admin, so always allow
  105. return True!
  106. else:
  107. // Check permissions
  108. String permission
  109. permission = string_get(read_attribute(core, model_id, "permissions"), get_relation_to_model(user_id, model_id))
  110. if (bool_or(permission == "1", permission == "2")):
  111. return True!
  112. else:
  113. return False!
  114. Boolean function allow_write(user_id : String, model_id : String):
  115. if (is_admin(user_id)):
  116. // Is admin, so always allow
  117. return True!
  118. else:
  119. // Check permissions
  120. String permission
  121. permission = string_get(read_attribute(core, model_id, "permissions"), get_relation_to_model(user_id, model_id))
  122. if (permission == "2"):
  123. return True!
  124. else:
  125. return False!
  126. Boolean function allow_change_metadata(user_id : String, model_id : String):
  127. if (is_admin(user_id)):
  128. // Is admin, so always allow
  129. return True!
  130. else:
  131. if (get_relation_to_model(user_id, model_id) == 0):
  132. // Only owner can chmod
  133. return True!
  134. else:
  135. return False!
  136. Boolean function allow_group_modify(user_id : String, group_id : String):
  137. if (is_admin(user_id)):
  138. // Is admin, so always allow
  139. return True!
  140. else:
  141. if (read_nr_out(set_overlap(allIncomingAssociationInstances(core, user_id, "owner"), allOutgoingAssociationInstances(core, group_id, "owner"))) > 0):
  142. // We are an owner
  143. return True!
  144. else:
  145. return False!
  146. Boolean function check_login(user_id : String):
  147. // TODO
  148. return False!
  149. Element function user_function():
  150. String username
  151. String user_id
  152. output("Log on as which user?")
  153. // Load in all global variables, as this code is hotloaded!
  154. Element root
  155. root = read_root()
  156. root = root["__hierarchy"]["objects"]
  157. exec(root["bootstrap/modelling.alc"]["initializers"])
  158. exec(root["bootstrap/library.alc"]["initializers"])
  159. exec(root["bootstrap/constructors.alc"]["initializers"])
  160. exec(root["bootstrap/object_operations.alc"]["initializers"])
  161. exec(root["core/mini_modify.alc"]["initializers"])
  162. exec(root["bootstrap/model_management.alc"]["initializers"])
  163. exec(root["bootstrap/ramify.alc"]["initializers"])
  164. exec(root["bootstrap/transform.alc"]["initializers"])
  165. exec(root["bootstrap/conformance_scd.alc"]["initializers"])
  166. exec(root["core/core_algorithm.alc"]["initializers"])
  167. core = import_node("models/core")
  168. username = input()
  169. user_id = get_user_id(username)
  170. if (user_id == ""):
  171. // New user
  172. // Add user to Core Formalism
  173. user_id = instantiate_node(core, "User", "")
  174. instantiate_attribute(core, user_id, "name", username)
  175. instantiate_attribute(core, user_id, "admin", False)
  176. // Now call with user created
  177. user_function_skip_init(user_id)
  178. else:
  179. if (check_login(user_id)):
  180. user_function_skip_init(user_id)
  181. // User destroyed already, so just stop execution
  182. return create_node()!
  183. String function get_model_id(name : String):
  184. Element models
  185. String model
  186. models = allInstances(core, "Model")
  187. while (read_nr_out(models) > 0):
  188. model = set_pop(models)
  189. if (value_eq(name, read_attribute(core, model, "name"))):
  190. return model!
  191. return ""!
  192. String function get_user_id(name : String):
  193. Element users
  194. String user
  195. users = allInstances(core, "User")
  196. while (read_nr_out(users) > 0):
  197. user = set_pop(users)
  198. if (value_eq(read_attribute(core, user, "name"), name)):
  199. return user!
  200. return ""!
  201. String function get_group_id(name : String):
  202. Element groups
  203. String group
  204. groups = allInstances(core, "Group")
  205. while (read_nr_out(groups) > 0):
  206. group = set_pop(groups)
  207. if (value_eq(read_attribute(core, group, "name"), name)):
  208. return group!
  209. return ""!
  210. Void function model_create(model : Element, name : String, user_id : String, type_id : String):
  211. String location
  212. String model_id
  213. location = "/models/" + cast_id2s(model)
  214. export_node(location, model)
  215. // Manage meta-info
  216. model_id = instantiate_node(core, "Model", "")
  217. instantiate_attribute(core, model_id, "name", name)
  218. instantiate_attribute(core, model_id, "location", location)
  219. instantiate_attribute(core, model_id, "permissions", "200")
  220. instantiate_link(core, "owner", "", model_id, user_id)
  221. instantiate_link(core, "instanceOf", "", model_id, type_id)
  222. return!
  223. Void function model_overwrite(model : Element, name : String):
  224. String location
  225. String model_id
  226. location = "/models/" + cast_id2s(model)
  227. export_node(location, model)
  228. model_id = get_model_id(name)
  229. // Change location in meta-data
  230. unset_attribute(core, model_id, "location")
  231. instantiate_attribute(core, model_id, "location", location)
  232. return!
  233. Void function user_function_skip_init(user_id : String):
  234. Boolean do_continue
  235. String cmd
  236. do_continue = True
  237. output("Welcome to the Model Management Interface v2.0!")
  238. output("Use the 'help' command for a list of possible commands")
  239. while (do_continue):
  240. output("Ready for command...")
  241. cmd = input()
  242. if (cmd == "help"):
  243. output("Model operations")
  244. output(" model_add -- Add a new model")
  245. output(" model_modify -- Modify an existing model")
  246. output(" model_delete -- [TODO] Delete a model and all related transformations")
  247. output(" model_list -- List all models")
  248. output(" model_list_full -- List all models with full info")
  249. output(" model_overwrite -- Overwrites a model with an uploaded model, leaving all metadata")
  250. output("")
  251. output("Transformation-specific operations")
  252. output(" transformation_add_MT_language -- Create a RAMified metamodel")
  253. output(" transformation_reRAMify -- RAMify a merged metamodel again")
  254. output(" transformation_add_MT -- Initialize a new model transformation")
  255. output(" transformation_add_AL -- [TODO] Initialize a new action language transformation")
  256. output(" transformation_add_EXT -- [TODO] Initialize a new external tool transformation")
  257. output(" transformation_execute -- Execute a transformation on a set of input models")
  258. output("")
  259. output("Model permission operations")
  260. output(" permission_modify -- Change model permissions")
  261. output(" permission_owner -- Change model owner")
  262. output(" permission_group -- Change model group")
  263. output("")
  264. output("Group operations")
  265. output(" group_create -- Create a group")
  266. output(" group_delete -- Delete a group")
  267. output(" group_owner_add -- Add group owner")
  268. output(" group_owner_delete -- Remove group owner")
  269. output(" group_join -- Add someone to your group")
  270. output(" group_kick -- Kick someone from your group")
  271. output(" group_list -- List all groups you are a member of")
  272. output("")
  273. output("Admin operations")
  274. output(" admin_promote -- Promote a user to admin status")
  275. output(" admin_demote -- Demote a user to normal status")
  276. output("")
  277. output("General operations")
  278. output(" account_delete -- Remove current user and revoke all permissions ")
  279. elif (cmd == "model_add"):
  280. // Model addition operation, which uses model upload commands of the compiler
  281. String name
  282. String type
  283. String location
  284. String type_id
  285. Element new_model
  286. String new_model_id
  287. output("Creating new model!")
  288. output("Model type?")
  289. type_id = get_model_id(input())
  290. if (type_id != ""):
  291. // Type exists
  292. if (allow_read(user_id, type_id)):
  293. // And is readable
  294. output("Model name?")
  295. name = input()
  296. if (get_model_id(name) == ""):
  297. // Model doesn't exist yet
  298. output("Waiting for model constructors...")
  299. // TODO Update construct_model call to this interface
  300. new_model = construct_model_raw(import_node(read_attribute(core, type_id, "location")))
  301. model_create(new_model, name, user_id, type_id)
  302. output("Model upload success!")
  303. else:
  304. output("Model with that name already exists!")
  305. else:
  306. output("Permission denied")
  307. else:
  308. output("Could not find type model!")
  309. elif (cmd == "transformation_execute"):
  310. // Execute a transformation, whatever type it is
  311. // First we detect the type, so we know how to prepare for invocation
  312. String transformation_id
  313. String exact_type
  314. Element sources
  315. Element targets
  316. String source
  317. String target
  318. String name_id
  319. Element inputs
  320. Element outputs
  321. Element trace_links
  322. output("Which transformation do you want to execute?")
  323. transformation_id = get_model_id(input())
  324. if (transformation_id != ""):
  325. if (allow_read(user_id, transformation_id)):
  326. if (is_nominal_instance(core, transformation_id, "Transformation")):
  327. // Read out source and target links
  328. sources = allOutgoingAssociationInstances(core, transformation_id, "transformInput")
  329. inputs = create_node()
  330. while (read_nr_out(source) > 0):
  331. source = set_pop(sources)
  332. output(string_join("Which model to bind for source element ", read_attribute(core, source, "name")))
  333. name_id = get_model_id(input())
  334. if (name_id != ""):
  335. if (allow_read(user_id, name_id)):
  336. dict_add(inputs, read_attribute(core, source, "name"), name_id)
  337. else:
  338. output("Permission denied")
  339. set_add(sources, source)
  340. else:
  341. output("No such model")
  342. set_add(sources, source)
  343. targets = allOutgoingAssociationInstances(core, transformation_id, "transformOutput")
  344. outputs = create_node()
  345. while (read_nr_out(targets) > 0):
  346. target = set_pop(targets)
  347. output(string_join("Which model to create for target element ", read_attribute(core, target, "name")))
  348. name_id = get_model_id(input())
  349. dict_add(outputs, read_attribute(core, target, "name"), name_id)
  350. exact_type = read_type(core, transformation_id)
  351. if (exact_type == "ModelTransformation"):
  352. // Model transformation is always in-place and uses only a single metamodel
  353. // Therefore, we must:
  354. // 1) Create an empty model, instance of merged metamodel
  355. // 2) Merge the different source models and retype
  356. // 3) Perform the transformation on the merged model
  357. // 4) Split the resulting model based on the target formalisms
  358. //
  359. // There is one exception: if the target model is bound to a source model, that model is overwritten
  360. // This allows for some optimizations when it is a simple in-place transformation (skip model copy, join, and split)
  361. // First check for this exception, as it is much faster
  362. Element input_model
  363. Element schedule_model
  364. String trace_link_id
  365. Element merged_model
  366. String merged_metamodel_id
  367. String ramified_metamodel_id
  368. schedule_model = import_node(read_attribute(core, transformation_id, "location"))
  369. if (bool_and(bool_and(read_nr_out(inputs) == 1, read_nr_out(outputs) == 1), set_equality(inputs, outputs))):
  370. // inputs and outputs have the same values and there is only one: keep in-place without additional bookkeeping
  371. input_model = import_node(read_attribute(core, set_pop(inputs), "location"))
  372. transform(input_model, schedule_model)
  373. else:
  374. // Need to fall back to the default approach, which is way slower
  375. // 1) Create empty instance of merged metamodel
  376. ramified_metamodel_id = set_pop(followAssociation(core, transformation_id, "instanceOf"))
  377. trace_links = allOutgoingAssociationInstances(core, ramified_metamodel_id, "tracability")
  378. merged_metamodel_id = ""
  379. while (read_nr_out(trace_links) > 0):
  380. trace_link_id = set_pop(trace_links)
  381. if (value_eq(read_attribute(core, trace_link_id, "type"), "ramified")):
  382. merged_metamodel_id = readAssociationDestination(core, trace_link_id)
  383. if (merged_metamodel_id != ""):
  384. merged_model = instantiate_model(import_node(read_attribute(core, merged_metamodel_id, "location")))
  385. // 2) Merge source models
  386. String key
  387. Element keys
  388. Element input_keys
  389. Element output_keys
  390. input_keys = dict_keys(inputs)
  391. while (read_nr_out(input_keys) > 0):
  392. key = set_pop(input_keys)
  393. model_join(merged_model, import_node(read_attribute(core, inputs[key], "location")), key)
  394. // 3) Transform
  395. transform(merged_model, schedule_model)
  396. // 4) Split in different files depending on type
  397. String desired_metamodel_id
  398. Element split_off_model
  399. output_keys = dict_keys(outputs)
  400. while (read_nr_out(output_keys) > 0):
  401. key = set_pop(output_keys)
  402. desired_metamodel_id = set_pop(followAssociation(core, outputs[key], "instanceOf"))
  403. split_off_model = model_split(merged_model, import_node(read_attribute(core, desired_metamodel_id, "location")), key)
  404. // Check if the destination model already exists
  405. if (get_model_id(outputs[key]) == ""):
  406. // New model
  407. model_create(split_off_model, outputs[key], user_id, desired_metamodel_id)
  408. else:
  409. // Model exists, so we overwrite
  410. model_overwrite(split_off_model, outputs[key])
  411. else:
  412. output("Could not resolve intermediate merged metamodel")
  413. elif (exact_type == "ActionLanguage"):
  414. output("Not Implemented yet!")
  415. else:
  416. output("Did not know how to interpret model of type " + exact_type)
  417. else:
  418. output("Model is not an executable transformation")
  419. else:
  420. output("Permission denied")
  421. else:
  422. output("No such transformation")
  423. elif (cmd == "model_overwrite"):
  424. // Overwrites an existing model without changing any metadata
  425. String model_id
  426. Element new_model
  427. output("Which model to overwrite?")
  428. model_id = get_model_id(input())
  429. if (model_id == ""):
  430. if (allow_write(user_id, model_id)):
  431. if (allow_read(user_id, set_pop(followAssociation(core, model_id, "instanceOf")))):
  432. output("Waiting for model constructors...")
  433. new_model = construct_model_raw(import_node(read_attribute(core, set_pop(followAssociation(core, model_id, "instanceOf")), "location")))
  434. model_overwrite(new_model, model_id)
  435. output("Model overwrite success!")
  436. else:
  437. output("Permission denied")
  438. else:
  439. output("Permission denied")
  440. else:
  441. output("No such model")
  442. elif (cmd == "model_modify"):
  443. // Model modify operation, which uses the mini_modify.alc operations, though with extensions for access control
  444. String model_id
  445. String type_id
  446. output("Which model do you want to modify?")
  447. model_id = get_model_id(input())
  448. if (model_id != ""):
  449. if (allow_read(user_id, model_id)):
  450. type_id = set_pop(allAssociationDestinations(core, model_id, "instanceOf"))
  451. if (allow_read(user_id, type_id)):
  452. modify(import_node(read_attribute(core, model_id, "location")), allow_write(user_id, model_id))
  453. else:
  454. output("Permission denied")
  455. else:
  456. output("Permission denied")
  457. else:
  458. output("Could not find model!")
  459. elif (cmd == "model_delete"):
  460. // Delete a model and all of its related transformations
  461. String model_id
  462. output("=================================================")
  463. output("WARNING: Deletion is a very destructive operation")
  464. output(" as it also deletes all transformations ")
  465. output(" defined which make use of this model! ")
  466. output("=================================================")
  467. output("")
  468. output("Currently not supported!")
  469. elif (cmd == "model_list"):
  470. // List all models
  471. Element models
  472. String m
  473. models = allInstances(core, "Model")
  474. while (read_nr_out(models) > 0):
  475. m = set_pop(models)
  476. output(string_join((string_join(" ", read_attribute(core, m, "name")) + " : "), read_attribute(core, set_pop(followAssociation(core, m, "instanceOf")), "name")))
  477. elif (cmd == "model_list_full"):
  478. // List all models with full info
  479. Element models
  480. String m
  481. String permissions
  482. String owner
  483. String group
  484. String name
  485. String type
  486. String size
  487. models = allInstances(core, "Model")
  488. while (read_nr_out(models) > 0):
  489. m = set_pop(models)
  490. permissions = read_attribute(core, m, "permissions")
  491. owner = read_attribute(core, set_pop(allAssociationDestinations(core, m, "owner")), "name")
  492. group = read_attribute(core, set_pop(allAssociationDestinations(core, m, "group")), "name")
  493. name = read_attribute(core, m, "name")
  494. size = cast_i2s(read_nr_out(dict_read(import_node(read_attribute(core, m, "location")), "model")))
  495. type = read_attribute(core, set_pop(allAssociationDestinations(core, m, "instanceOf")), "name")
  496. output(((((((((((" " + permissions) + " ") + owner) + " ") + group) + " ") + size) + " ") + name) + " : ") + type)
  497. elif (cmd == "transformation_add_MT_language"):
  498. // Create a model transformation language from a set of input and output formalisms
  499. String name
  500. String model_id
  501. Element source
  502. Element target
  503. Element all_formalisms
  504. Element merged_formalism
  505. Element ramified_formalism
  506. String old_type_id
  507. String type_id
  508. String location
  509. String new_model_id
  510. source = create_node()
  511. target = create_node()
  512. old_type_id = ""
  513. // Read involved formalisms
  514. output("Formalisms to include (terminate with empty string)?")
  515. name = input()
  516. while (name != ""):
  517. model_id = get_model_id(name)
  518. if (model_id != ""):
  519. if (allow_read(user_id, model_id)):
  520. type_id = set_pop(allAssociationDestinations(core, model_id, "instanceOf"))
  521. if (bool_or(old_type_id == "", type_id == old_type_id)):
  522. set_add(all_formalisms, create_tuple(name, import_node(read_attribute(core, model_id, "location"))))
  523. old_type_id = type_id
  524. elif (old_type_id != type_id):
  525. // Already have a previous type_id and now another: CLASH
  526. output("Cannot add model as types not compatible with previous models; try again")
  527. else:
  528. output("Model not readable; try again")
  529. else:
  530. output("No such model; try again")
  531. name = input()
  532. // Merge both into a single metamodel
  533. if (read_nr_out(source) > 0):
  534. if (read_nr_out(target) > 0):
  535. output("Name of the RAMified tranformation metamodel?")
  536. name = input()
  537. if (get_model_id(name) == ""):
  538. String merged_formalism_id
  539. String ramified_formalism_id
  540. String source_formalism_id
  541. String tracability_link
  542. // New location is available, so write
  543. merged_formalism = model_fuse(all_formalisms)
  544. location = "/models/" + cast_id2s(merged_formalism)
  545. export_node(location, merged_formalism)
  546. // Manage meta-info
  547. new_model_id = instantiate_node(core, "Model", "")
  548. merged_formalism_id = new_model_id
  549. instantiate_attribute(core, new_model_id, "name", "__merged_" + name)
  550. instantiate_attribute(core, new_model_id, "location", location)
  551. instantiate_attribute(core, new_model_id, "permissions", "200")
  552. instantiate_link(core, "owner", "", new_model_id, user_id)
  553. instantiate_link(core, "instanceOf", "", new_model_id, type_id)
  554. // Add tracability links at this level
  555. while (read_nr_out(all_formalisms) > 0):
  556. source_formalism_id = list_read(set_pop(all_formalisms), 1)
  557. tracability_link = instantiate_link(core, "tracability", "", merged_formalism_id, source_formalism_id)
  558. instantiate_attribute(core, tracability_link, "type", "merged")
  559. // Merge complete, now RAMify!
  560. ramified_formalism = ramify(merged_formalism)
  561. location = "/models/" + cast_id2s(ramified_formalism)
  562. export_node(location, ramified_formalism)
  563. // Manage meta-info
  564. new_model_id = instantiate_node(core, "Model", "")
  565. instantiate_attribute(core, new_model_id, "name", name)
  566. instantiate_attribute(core, new_model_id, "location", location)
  567. instantiate_attribute(core, new_model_id, "permissions", "200")
  568. instantiate_link(core, "owner", "", new_model_id, user_id)
  569. instantiate_link(core, "instanceOf", "", new_model_id, type_id)
  570. // Add tracability link at this level
  571. tracability_link = instantiate_link(core, "tracability", "", ramified_formalism_id, merged_formalism_id)
  572. instantiate_attribute(core, tracability_link, "type", "RAMified")
  573. else:
  574. output("Model already exists!")
  575. else:
  576. output("At least one target formalism is required")
  577. else:
  578. output("At least one source formalism is required")
  579. elif (cmd == "transformation_reRAMify"):
  580. String model_id
  581. String source_id
  582. Element trace_links
  583. String trace_link
  584. String merged_model_id
  585. Element new_model
  586. String location
  587. output("Merged metamodel to RAMify again?")
  588. model_id = get_model_id(input())
  589. if (model_id != ""):
  590. if (allow_write(user_id, model_id)):
  591. // We must be able to write the RAMification model, as we will update it in-place
  592. trace_links = allOutgoingAssociationInstances(core, model_id, "tracability")
  593. merged_model_id = ""
  594. while (read_nr_out(trace_links) > 0):
  595. trace_link = set_pop(trace_links)
  596. if (value_eq(read_attribute(core, trace_link, "type"), "RAMified")):
  597. // Found a RAMification link, so redo it on the original model
  598. merged_model_id = readAssociationDestination(core, trace_link)
  599. if (merged_model_id != ""):
  600. new_model = ramify(core["model"][merged_model_id])
  601. model_overwrite(new_model, model_id)
  602. else:
  603. output("Could not determine original model of RAMified metamodel!")
  604. else:
  605. output("Permission denied")
  606. else:
  607. output("No such model")
  608. elif (cmd == "transformation_add_MT"):
  609. // Add a model transformation model
  610. // Just a usual model instantiation, but need to add the source and target links based on user info
  611. String ramified_metamodel_id
  612. String model_id
  613. Element models
  614. Element links
  615. String link_id
  616. Element supported
  617. Element source
  618. Element target
  619. String name
  620. String new_model_id
  621. source = create_node()
  622. target = create_node()
  623. supported = create_node()
  624. output("RAMified metamodel to use?")
  625. ramified_metamodel_id = get_model_id(input())
  626. if (ramified_metamodel_id != ""):
  627. if (allow_read(user_id, ramified_metamodel_id)):
  628. output("Supported metamodels:")
  629. links = allOutgoingAssociationInstances(core, ramified_metamodel_id, "tracability")
  630. while (read_nr_out(links) > 0):
  631. link_id = set_pop(links)
  632. if (value_eq(read_attribute(core, link_id, "type"), "merged")):
  633. output(string_join(" ", read_attribute(core, readAssociationDestination(core, link_id), "name")))
  634. set_add(supported, readAssociationDestination(core, link_id))
  635. output("")
  636. output("Which ones do you want to use as source (empty string to finish)?")
  637. name = input()
  638. while (name != ""):
  639. model_id = get_model_id(name)
  640. if (model_id != ""):
  641. if (set_in(supported, model_id)):
  642. if (bool_not(set_in(source, model_id))):
  643. set_add(source, model_id)
  644. output("Model added as source")
  645. else:
  646. output("Model already selected as source")
  647. else:
  648. output("Model is not supported by RAMified metamodel!")
  649. else:
  650. output("No such model; try again")
  651. name = input()
  652. output("Which ones do you want to use as target (empty string to finish)?")
  653. name = input()
  654. while (name != ""):
  655. model_id = get_model_id(name)
  656. if (model_id != ""):
  657. if (set_in(supported, model_id)):
  658. if (bool_not(set_in(target, model_id))):
  659. set_add(target, model_id)
  660. output("Model added as target")
  661. else:
  662. output("Model already selected as target")
  663. else:
  664. output("Model is not supported by RAMified metamodel!")
  665. else:
  666. output("No such model; try again")
  667. name = input()
  668. output("Name of new transformation?")
  669. name = input()
  670. if (get_model_id(name) == ""):
  671. String new_model
  672. // Finished with all information, now create the model itself!
  673. new_model = instantiate_model(import_node(read_attribute(core, ramified_metamodel_id, "location")))
  674. model_create(new_model, name, user_id, ramified_metamodel_id)
  675. model_id = get_model_id(name)
  676. // Extend metadata with info on source and target
  677. while (read_nr_out(source) > 0):
  678. instantiate_link(core, "transformInput", "", new_model_id, set_pop(source))
  679. while (read_nr_out(target) > 0):
  680. instantiate_link(core, "transformOutput", "", new_model_id, set_pop(target))
  681. output("Meta-info correctly set!")
  682. else:
  683. output("Model already exists")
  684. else:
  685. output("Permission denied")
  686. else:
  687. output("No such model")
  688. elif (cmd == "permission_modify"):
  689. String permissions
  690. Integer permission
  691. String model_id
  692. output("Which model do you want to change permissions of?")
  693. model_id = get_model_id(input())
  694. if (model_id != ""):
  695. if (get_relation_to_model(user_id, model_id) == 0):
  696. output("New permissions?")
  697. permissions = input()
  698. Boolean fail
  699. Integer i
  700. i = 0
  701. if (string_len(permissions) != 3):
  702. fail = True
  703. while (bool_and(bool_not(fail), i < 3)):
  704. permission = cast_s2i(string_get(permissions, i))
  705. if (bool_or(permission < 0, permission > 2)):
  706. fail = True
  707. if (bool_not(fail)):
  708. unset_attribute(core, model_id, "permissions")
  709. instantiate_attribute(core, model_id, "permissions", permissions)
  710. else:
  711. output("Permissions must be a string of three characters with each character being a digit between 0 and 2")
  712. else:
  713. output("Permission denied!")
  714. else:
  715. output("No such model!")
  716. elif (cmd == "permission_owner"):
  717. String permissions
  718. String model_id
  719. String user_id
  720. output("Which model do you want to change owner of?")
  721. model_id = get_model_id(input())
  722. if (model_id != ""):
  723. if (bool_or(get_relation_to_model(user_id, model_id) == 0, is_admin(user_id))):
  724. output("New owner?")
  725. user_id = get_user_id(input())
  726. if (user_id != ""):
  727. model_delete_element(core, set_pop(allOutgoingAssociationInstances(core, model_id, "owner")))
  728. instantiate_link(core, "owner", "", model_id, user_id)
  729. else:
  730. output("No such user!")
  731. else:
  732. output("Permission denied!")
  733. else:
  734. output("No such model!")
  735. elif (cmd == "permission_group"):
  736. String permissions
  737. String model_id
  738. String group_id
  739. output("Which model do you want to change permissions of?")
  740. model_id = get_model_id(input())
  741. if (model_id != ""):
  742. if (bool_or(get_relation_to_model(user_id, model_id) == 0, is_admin(user_id))):
  743. output("New group?")
  744. group_id = get_group_id(input())
  745. if (group_id != ""):
  746. model_delete_element(core, set_pop(allOutgoingAssociationInstances(core, model_id, "group")))
  747. instantiate_link(core, "group", "", model_id, group_id)
  748. else:
  749. output("No such group!")
  750. else:
  751. output("Permission denied!")
  752. else:
  753. output("No such model!")
  754. elif (cmd == "group_create"):
  755. // Create a new group and become its owner
  756. String group_id
  757. String name
  758. output("Which group do you want to create?")
  759. name = input()
  760. group_id = get_group_id(name)
  761. if (group_id == ""):
  762. group_id = instantiate_node(core, "Group", "")
  763. instantiate_attribute(core, group_id, "name", name)
  764. instantiate_link(core, "belongsTo", "", user_id, group_id)
  765. instantiate_link(core, "owner", "", group_id, user_id)
  766. output("Group created!")
  767. else:
  768. output("Group already exists")
  769. elif (cmd == "group_delete"):
  770. // Delete an existing group
  771. String group_id
  772. String name
  773. output("Which group do you want to delete?")
  774. name = input()
  775. group_id = get_group_id(name)
  776. if (group_id != ""):
  777. if (allow_group_modify(user_id, group_id)):
  778. model_delete_element(core, group_id)
  779. else:
  780. output("Permission denied")
  781. else:
  782. output("No such group")
  783. elif (cmd == "group_owner_add"):
  784. // Add an owner to your group
  785. String group_id
  786. String other_user_id
  787. output("Which group do you want to add an owner to?")
  788. group_id = get_group_id(input())
  789. if (group_id != ""):
  790. if (allow_group_modify(user_id, group_id)):
  791. output("Which user do you want to make an owner?")
  792. other_user_id = get_user_id(input())
  793. if (other_user_id != ""):
  794. Element overlap
  795. overlap = set_overlap(allIncomingAssociationInstances(core, user_id, "owner"), allOutgoingAssociationInstances(core, group_id, "owner"))
  796. if (read_nr_out(overlap) == 0):
  797. instantiate_link(core, "owner", "", group_id, user_id)
  798. overlap = set_overlap(allOutgoingAssociationInstances(core, user_id, "belongsTo"), allIncomingAssociationInstances(core, group_id, "belongsTo"))
  799. if (read_nr_out(overlap) == 0):
  800. instantiate_link(core, "belongsTo", "", user_id, group_id)
  801. output("New owner added to group!")
  802. else:
  803. output("User is already an owner!")
  804. else:
  805. output("No such user")
  806. else:
  807. output("Permission denied!")
  808. else:
  809. output("No such group")
  810. elif (cmd == "group_owner_delete"):
  811. // Remove an owner from your group
  812. String group_id
  813. String other_user_id
  814. output("Which group do you want to disown someone from?")
  815. group_id = get_group_id(input())
  816. if (group_id != ""):
  817. if (allow_group_modify(user_id, group_id)):
  818. output("Which user do you want to disown?")
  819. other_user_id = get_user_id(input())
  820. if (other_user_id != ""):
  821. Element overlap
  822. overlap = set_overlap(allOutgoingAssociationInstances(core, user_id, "belongsTo"), allIncomingAssociationInstances(core, group_id, "belongsTo"))
  823. if (read_nr_out(overlap) > 0):
  824. overlap = set_overlap(allIncomingAssociationInstances(core, user_id, "owner"), allOutgoingAssociationInstances(core, group_id, "owner"))
  825. if (read_nr_out(overlap) > 0):
  826. model_delete_element(core, set_pop(overlap))
  827. output("Disowned group from user!")
  828. else:
  829. output("User is not even an owner of the group!")
  830. else:
  831. output("User is not even a member of the group!")
  832. else:
  833. output("No such user")
  834. else:
  835. output("Permission denied!")
  836. else:
  837. output("No such group")
  838. elif (cmd == "group_join"):
  839. // Add someone to your group
  840. String group_id
  841. String other_user_id
  842. output("Which group do you want to add someone to?")
  843. group_id = get_group_id(input())
  844. if (group_id != ""):
  845. if (allow_group_modify(user_id, group_id)):
  846. output("Which user do you want to add?")
  847. other_user_id = get_user_id(input())
  848. if (other_user_id != ""):
  849. Element overlap
  850. overlap = set_overlap(allOutgoingAssociationInstances(core, user_id, "belongsTo"), allIncomingAssociationInstances(core, group_id, "belongsTo"))
  851. if (read_nr_out(overlap) == 0):
  852. instantiate_link(core, "belongsTo", "", user_id, group_id)
  853. output("User added to the group!")
  854. else:
  855. output("User is already a member of the group!")
  856. else:
  857. output("No such user")
  858. else:
  859. output("Permission denied!")
  860. else:
  861. output("No such group")
  862. elif (cmd == "group_kick"):
  863. // Remove someone from your group
  864. String group_id
  865. String other_user_id
  866. output("Which group do you want to kick someone from?")
  867. group_id = get_group_id(input())
  868. if (group_id != ""):
  869. if (allow_group_modify(user_id, group_id)):
  870. output("Which user do you want to kick?")
  871. other_user_id = get_user_id(input())
  872. if (other_user_id != ""):
  873. Element overlap
  874. overlap = set_overlap(allOutgoingAssociationInstances(core, user_id, "belongsTo"), allIncomingAssociationInstances(core, group_id, "belongsTo"))
  875. if (read_nr_out(overlap) > 0):
  876. model_delete_element(core, set_pop(overlap))
  877. // Check if user was an owner as well
  878. overlap = set_overlap(allIncomingAssociationInstances(core, user_id, "owner"), allOutgoingAssociationInstances(core, group_id, "owner"))
  879. if (read_nr_out(overlap) > 0):
  880. model_delete_element(core, set_pop(overlap))
  881. output("User kicked!")
  882. else:
  883. output("User is not even a member of the group!")
  884. else:
  885. output("No such user")
  886. else:
  887. output("Permission denied!")
  888. else:
  889. output("No such group")
  890. elif (cmd == "group_list"):
  891. // List all groups you are a member of (and whether you are admin or not!)
  892. Element groups
  893. String group_id
  894. String admin
  895. groups = allAssociationDestinations(core, user_id, "belongsTo")
  896. while (True):
  897. group_id = set_pop(groups)
  898. if (set_in(allOutgoingAssociationInstances(core, group_id, "owner"), user_id)):
  899. admin = " A "
  900. else:
  901. admin = " "
  902. output(string_join(admin, read_attribute(core, group_id, "name")))
  903. elif (cmd == "admin_promote"):
  904. // Promote a user to admin status
  905. if (is_admin(user_id)):
  906. String other_user_id
  907. output("Which user do you want to promote?")
  908. other_user_id = get_user_id(input())
  909. if (other_user_id != ""):
  910. unset_attribute(core, other_user_id, "admin")
  911. instantiate_attribute(core, other_user_id, "admin", True)
  912. output("Permissions granted!")
  913. else:
  914. output("No such user!")
  915. else:
  916. output("Permission denied!")
  917. elif (cmd == "admin_demote"):
  918. // Demote a user to normal status
  919. if (is_admin(user_id)):
  920. String other_user_id
  921. output("Which user do you want to demote?")
  922. other_user_id = get_user_id(input())
  923. if (other_user_id != ""):
  924. unset_attribute(core, other_user_id, "admin")
  925. instantiate_attribute(core, other_user_id, "admin", False)
  926. output("Permissions revoked!")
  927. else:
  928. output("No such user!")
  929. else:
  930. output("Permission denied!")
  931. elif (cmd == "exit"):
  932. // Exit by actually removing the user and decoupling it from all of its models
  933. // Restarting with the same user name will NOT grant you access to anything of the previous user with that same name
  934. do_continue = False
  935. // Delete user from Core Formalism
  936. model_delete_element(core, user_id)
  937. output("Goodbye!")
  938. return !