core_algorithm.alc 35 KB

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