core_algorithm.alc 35 KB

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