core_algorithm.alc 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  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 user_function_skip_init(user_id : String):
  163. Boolean do_continue
  164. String cmd
  165. do_continue = True
  166. output("Welcome to the Model Management Interface v2.0!")
  167. output("Use the 'help' command for a list of possible commands")
  168. while (do_continue):
  169. output("Ready for command...")
  170. cmd = input()
  171. if (cmd == "help"):
  172. output("Model operations")
  173. output(" model_add -- Add a new model")
  174. output(" model_modify -- Modify an existing model")
  175. output(" model_delete -- [TODO] Delete a model and all related transformations")
  176. output(" model_list -- List all models")
  177. output(" model_list_full -- List all models with full info")
  178. output(" model_overwrite -- Overwrites a model with an uploaded model, leaving all metadata")
  179. output("")
  180. output("Transformation-specific operations")
  181. output(" transformation_add_MT_language -- Create a RAMified metamodel")
  182. output(" transformation_reRAMify -- RAMify a merged metamodel again")
  183. output(" transformation_add_MT -- Initialize a new model transformation")
  184. output(" transformation_add_AL -- TODO")
  185. output(" transformation_source_add -- TODO")
  186. output(" transformation_source_delete -- TODO")
  187. output(" transformation_target_add -- TODO")
  188. output(" transformation_target_delete -- TODO")
  189. output(" transformation_execute -- TODO")
  190. output("")
  191. output("Model permission operations")
  192. output(" permission_modify -- Change model permissions")
  193. output(" permission_owner -- Change model owner")
  194. output(" permission_group -- Change model group")
  195. output("")
  196. output("Group operations")
  197. output(" group_create -- Create a group")
  198. output(" group_delete -- Delete a group")
  199. output(" group_owner_add -- Add group owner")
  200. output(" group_owner_delete -- Remove group owner")
  201. output(" group_join -- Add someone to your group")
  202. output(" group_kick -- Kick someone from your group")
  203. output(" group_list -- List all groups you are a member of")
  204. output("")
  205. output("Admin operations")
  206. output(" admin_promote -- Promote a user to admin status")
  207. output(" admin_demote -- Demote a user to normal status")
  208. output("")
  209. output("General operations")
  210. output(" account_delete -- Remove current user and revoke all permissions ")
  211. elif (cmd == "model_add"):
  212. // Model addition operation, which uses model upload commands of the compiler
  213. String name
  214. String type
  215. String location
  216. String type_id
  217. Element new_model
  218. String new_model_id
  219. output("Creating new model!")
  220. output("Model type?")
  221. type_id = get_model_id(input())
  222. if (type_id != ""):
  223. // Type exists
  224. if (allow_read(user_id, type_id)):
  225. // And is readable
  226. output("Model name?")
  227. name = input()
  228. if (get_model_id(name) == ""):
  229. // Model doesn't exist yet
  230. output("Waiting for model constructors...")
  231. // TODO Update construct_model call to this interface
  232. new_model = construct_model(import_node(read_attribute(core, type_id, "location")))
  233. output("Model upload success!")
  234. location = "/models/" + cast_id2s(new_model)
  235. export_node(new_model, location)
  236. // Manage meta-info
  237. new_model_id = instantiate_node(core, "Model", "")
  238. instantiate_attribute(core, new_model_id, "name", name)
  239. instantiate_attribute(core, new_model_id, "location", location)
  240. instantiate_attribute(core, new_model_id, "permissions", "200")
  241. instantiate_link(core, "owner", "", new_model_id, user_id)
  242. instantiate_link(core, "instanceOf", "", new_model_id, type_id)
  243. output("Meta-info correctly set!")
  244. else:
  245. output("Model with that name already exists!")
  246. else:
  247. output("You are not allowed to read this type model!")
  248. else:
  249. output("Could not find type model!")
  250. elif (cmd == "model_overwrite"):
  251. // Overwrites an existing model without changing any metadata
  252. output("Which model to overwrite?")
  253. model_id = get_model_id(input())
  254. if (model_id == ""):
  255. if (allow_write(user_id, model_id)):
  256. if (allow_read(user_id, followAssociation(core, model_id, "instanceOf"))):
  257. output("Waiting for model constructors...")
  258. new_model = construct_model(import_node(read_attribute(core, followAssociation(core, model_id, "instanceOf"), "location")))
  259. output("Model upload success!")
  260. location = "/models/" + cast_id2s(new_model)
  261. export_node(new_model, location)
  262. // Change location in meta-data
  263. unset_attribute(core, model_id, "location")
  264. instantiate_attribute(core, model_id, "location", location)
  265. output("Model completely overwritten")
  266. else:
  267. output("Permission denied")
  268. else:
  269. output("Permission denied")
  270. else:
  271. output("No such model")
  272. elif (cmd == "model_modify"):
  273. // Model modify operation, which uses the mini_modify.alc operations, though with extensions for access control
  274. String model_id
  275. String type_id
  276. output("Which model do you want to modify?")
  277. model_id = get_model_id(input())
  278. if (model_id != ""):
  279. if (allow_read(user_id, model_id)):
  280. type_id = set_pop(allAssociationDestinations(core, model_id, "instanceOf"))
  281. if (allow_read(user_id, type_id)):
  282. modify(import_node(read_attribute(core, model_id, "location")), allow_write(user_id, model_id))
  283. else:
  284. output("You are not allowed to read the type model!")
  285. else:
  286. output("You are not allowed to read this model!")
  287. else:
  288. output("Could not find model!")
  289. elif (cmd == "model_delete"):
  290. // Delete a model and all of its related transformations
  291. String model_id
  292. output("=================================================")
  293. output("WARNING: Deletion is a very destructive operation")
  294. output(" as it also deletes all transformations ")
  295. output(" defined which make use of this model! ")
  296. output("=================================================")
  297. output("")
  298. output("Currently not supported!")
  299. elif (cmd == "model_list"):
  300. // List all models
  301. Element models
  302. String m
  303. models = allInstances(core, "Model")
  304. while (read_nr_out(models) > 0):
  305. m = set_pop(models)
  306. output(string_join((string_join(" ", read_attribute(core, m, "name")) + " : "), read_attribute(core, set_pop(allAssociationDestinations(core, m, "instanceOf")), "name")))
  307. elif (cmd == "model_list_full"):
  308. // List all models with full info
  309. Element models
  310. String m
  311. String permissions
  312. String owner
  313. String group
  314. String name
  315. String type
  316. String size
  317. models = allInstances(core, "Model")
  318. while (read_nr_out(models) > 0):
  319. m = set_pop(models)
  320. permissions = read_attribute(core, m, "permissions")
  321. owner = read_attribute(core, set_pop(allAssociationDestinations(core, m, "owner")), "name")
  322. group = read_attribute(core, set_pop(allAssociationDestinations(core, m, "group")), "name")
  323. name = read_attribute(core, m, "name")
  324. size = cast_i2s(read_nr_out(dict_read(import_node(read_attribute(core, m, "location")), "model")))
  325. type = read_attribute(core, set_pop(allAssociationDestinations(core, m, "instanceOf")), "name")
  326. output(((((((((((" " + permissions) + " ") + owner) + " ") + group) + " ") + size) + " ") + name) + " : ") + type)
  327. elif (cmd == "transformation_add_MT_language"):
  328. // Create a model transformation language from a set of input and output formalisms
  329. String name
  330. String model_id
  331. Element source
  332. Element target
  333. Element all_formalisms
  334. Element merged_formalism
  335. Element ramified_formalism
  336. String old_type_id
  337. String type_id
  338. String location
  339. String new_model_id
  340. source = create_node()
  341. target = create_node()
  342. old_type_id = ""
  343. // Read involved formalisms
  344. output("Formalisms to include (terminate with empty string)?")
  345. name = input()
  346. while (name != ""):
  347. model_id = get_model_id(name)
  348. if (model_id != ""):
  349. if (allow_read(user_id, model_id)):
  350. type_id = set_pop(allAssociationDestinations(core, model_id, "instanceOf"))
  351. if (bool_or(old_type_id == "", type_id == old_type_id)):
  352. set_add(all_formalisms, create_tuple(name, import_node(read_attribute(core, model_id, "location"))))
  353. old_type_id = type_id
  354. elif (old_type_id != type_id):
  355. // Already have a previous type_id and now another: CLASH
  356. output("Cannot add model as types not compatible with previous models; try again")
  357. else:
  358. output("Model not readable; try again")
  359. else:
  360. output("No such model; try again")
  361. name = input()
  362. // Merge both into a single metamodel
  363. if (read_nr_out(source) > 0):
  364. if (read_nr_out(target) > 0):
  365. output("Name of the RAMified tranformation metamodel?")
  366. name = input()
  367. if (get_model_id(name) == ""):
  368. String merged_formalism_id
  369. String ramified_formalism_id
  370. String source_formalism_id
  371. String tracability_link
  372. // New location is available, so write
  373. merged_formalism = model_fuse(all_formalisms)
  374. location = "/models/" + cast_id2s(merged_formalism)
  375. export_node(merged_formalism, location)
  376. // Manage meta-info
  377. new_model_id = instantiate_node(core, "Model", "")
  378. merged_formalism_id = new_model_id
  379. instantiate_attribute(core, new_model_id, "name", "__merged_" + name)
  380. instantiate_attribute(core, new_model_id, "location", location)
  381. instantiate_attribute(core, new_model_id, "permissions", "200")
  382. instantiate_link(core, "owner", "", new_model_id, user_id)
  383. instantiate_link(core, "instanceOf", "", new_model_id, type_id)
  384. // Add tracability links at this level
  385. while (read_nr_out(all_formalisms) > 0):
  386. source_formalism_id = list_read(set_pop(all_formalisms), 1)
  387. tracability_link = instantiate_link(core, "tracability", "", merged_formalism_id, source_formalism_id)
  388. instantiate_attribute(core, tracability_link, "type", "merged")
  389. // Merge complete, now RAMify!
  390. ramified_formalism = ramify(merged_formalism)
  391. location = "/models/" + cast_id2s(ramified_formalism)
  392. export_node(ramified_formalism, location)
  393. // Manage meta-info
  394. new_model_id = instantiate_node(core, "Model", "")
  395. instantiate_attribute(core, new_model_id, "name", name)
  396. instantiate_attribute(core, new_model_id, "location", location)
  397. instantiate_attribute(core, new_model_id, "permissions", "200")
  398. instantiate_link(core, "owner", "", new_model_id, user_id)
  399. instantiate_link(core, "instanceOf", "", new_model_id, type_id)
  400. // Add tracability link at this level
  401. tracability_link = instantiate_link(core, "tracability", "", ramified_formalism_id, merged_formalism_id)
  402. instantiate_attribute(core, tracability_link, "type", "RAMified")
  403. else:
  404. output("Model already exists!")
  405. else:
  406. output("At least one target formalism is required")
  407. else:
  408. output("At least one source formalism is required")
  409. elif (cmd == "transformation_reRAMify"):
  410. String model_id
  411. String source_id
  412. Element trace_links
  413. String trace_link
  414. String merged_model_id
  415. Element new_model
  416. String location
  417. output("Merged metamodel to RAMify again?")
  418. model_id = get_model_id(input())
  419. if (model_id != ""):
  420. if (allow_write(user_id, model_id)):
  421. // We must be able to write the RAMification model, as we will update it in-place
  422. trace_links = allOutgoingAssociationInstances(core, model_id, "tracability")
  423. merged_model_id = ""
  424. while (read_nr_out(trace_links) > 0):
  425. trace_link = set_pop(trace_links)
  426. if (value_eq(read_attribute(core, trace_link, "type"), "RAMified")):
  427. // Found a RAMification link, so redo it on the original model
  428. merged_model_id = readAssociationDestination(core, trace_link)
  429. if (merged_model_id != ""):
  430. new_model = ramify(core["model"][merged_model_id])
  431. location = "/models/" + cast_id2s(new_model)
  432. export_node(new_model, location)
  433. // Update meta-info
  434. unset_attribute(core, model_id, "location")
  435. instantiate_attribute(core, model_id, "location", location)
  436. else:
  437. output("Could not determine original model of RAMified metamodel!")
  438. else:
  439. output("Permission denied")
  440. else:
  441. output("No such model")
  442. elif (cmd == "transformation_add_MT"):
  443. // Add a model transformation model
  444. // Just a usual model instantiation, but need to add the source and target links based on user info
  445. String ramified_metamodel_id
  446. String model_id
  447. Element models
  448. Element links
  449. String link_id
  450. Element supported
  451. Element source
  452. Element target
  453. String name
  454. String new_model_id
  455. source = create_node()
  456. target = create_node()
  457. supported = create_node()
  458. output("RAMified metamodel to use?")
  459. ramified_metamodel_id = get_model_id(input())
  460. if (ramified_metamodel_id != ""):
  461. if (allow_read(user_id, ramified_metamodel_id)):
  462. output("Supported metamodels:")
  463. links = allOutgoingAssociationInstances(core, ramified_metamodel_id, "tracability")
  464. while (read_nr_out(links) > 0):
  465. link_id = set_pop(linkss)
  466. if (value_eq(read_attribute(core, link_id, "type"), "merged")):
  467. output(string_join(" ", read_attribute(core, readAssociationDestination(core, link_id), "name")))
  468. set_add(supported, readAssociationDestination(core, link_id))
  469. output("")
  470. output("Which ones do you want to use as source (empty string to finish)?")
  471. name = input()
  472. while (name != ""):
  473. model_id = get_model_id(name)
  474. if (model_id != ""):
  475. if (set_in(supported, model_id)):
  476. if (bool_not(set_in(source, model_id))):
  477. set_add(source, model_id)
  478. output("Model added as source")
  479. else:
  480. output("Model already selected as source")
  481. else:
  482. output("Model is not supported by RAMified metamodel!")
  483. else:
  484. output("No such model; try again")
  485. name = input()
  486. output("Which ones do you want to use as target (empty string to finish)?")
  487. name = input()
  488. while (name != ""):
  489. model_id = get_model_id(name)
  490. if (model_id != ""):
  491. if (set_in(supported, model_id)):
  492. if (bool_not(set_in(target, model_id))):
  493. set_add(target, model_id)
  494. output("Model added as target")
  495. else:
  496. output("Model already selected as target")
  497. else:
  498. output("Model is not supported by RAMified metamodel!")
  499. else:
  500. output("No such model; try again")
  501. name = input()
  502. output("Name of new transformation?")
  503. name = input()
  504. if (get_model_id(name) == ""):
  505. // Finished with all information, now create the model itself!
  506. new_model = instantiate_model(import_node(read_attribute(core, ramified_model_id, "location")))
  507. location = "/models/" + cast_id2s(new_model)
  508. export_node(new_model, location)
  509. // Manage meta-info
  510. new_model_id = instantiate_node(core, "ModelTransformation", "")
  511. instantiate_attribute(core, new_model_id, "name", name)
  512. instantiate_attribute(core, new_model_id, "location", location)
  513. instantiate_attribute(core, new_model_id, "permissions", "200")
  514. instantiate_link(core, "owner", "", new_model_id, user_id)
  515. instantiate_link(core, "instanceOf", "", new_model_id, ramified_model_id)
  516. while (read_nr_out(source) > 0):
  517. instantiate_link(core, "transformInput", "", new_model_id, set_pop(source))
  518. while (read_nr_out(target) > 0):
  519. instantiate_link(core, "transformOutput", "", new_model_id, set_pop(target))
  520. output("Meta-info correctly set!")
  521. else:
  522. output("Model already exists")
  523. else:
  524. output("Permission denied")
  525. else:
  526. output("No such model")
  527. elif (cmd == "permission_modify"):
  528. String permissions
  529. Integer permission
  530. String model_id
  531. output("Which model do you want to change permissions of?")
  532. model_id = get_model_id(input())
  533. if (model_id != ""):
  534. if (get_relation_to_model(user_id, model_id) == 0):
  535. output("New permissions?")
  536. permissions = input()
  537. Boolean fail
  538. Integer i
  539. i = 0
  540. if (string_len(permissions) != 3):
  541. fail = True
  542. while (bool_and(bool_not(fail), i < 3)):
  543. permission = cast_s2i(string_get(permissions, i))
  544. if (bool_or(permission < 0, permission > 2)):
  545. fail = True
  546. if (bool_not(fail)):
  547. unset_attribute(core, model_id, "permissions")
  548. instantiate_attribute(core, model_id, "permissions", permissions)
  549. else:
  550. output("Permissions must be a string of three characters with each character being a digit between 0 and 2")
  551. else:
  552. output("Permission denied!")
  553. else:
  554. output("No such model!")
  555. elif (cmd == "permission_owner"):
  556. String permissions
  557. String model_id
  558. String user_id
  559. output("Which model do you want to change owner of?")
  560. model_id = get_model_id(input())
  561. if (model_id != ""):
  562. if (get_relation_to_model(user_id, model_id) == 0):
  563. output("New owner?")
  564. user_id = get_user_id(input())
  565. if (user_id != ""):
  566. model_delete_element(core, set_pop(allOutgoingAssociationInstances(core, model_id, "owner")))
  567. instantiate_link(core, "owner", "", model_id, user_id)
  568. else:
  569. output("No such user!")
  570. else:
  571. output("Permission denied!")
  572. else:
  573. output("No such model!")
  574. elif (cmd == "permission_group"):
  575. String permissions
  576. String model_id
  577. String group_id
  578. output("Which model do you want to change permissions of?")
  579. model_id = get_model_id(input())
  580. if (model_id != ""):
  581. if (get_relation_to_model(user_id, model_id) == 0):
  582. output("New group?")
  583. group_id = get_group_id(input())
  584. if (group_id != ""):
  585. model_delete_element(core, set_pop(allOutgoingAssociationInstances(core, model_id, "group")))
  586. instantiate_link(core, "group", "", model_id, group_id)
  587. else:
  588. output("No such group!")
  589. else:
  590. output("Permission denied!")
  591. else:
  592. output("No such model!")
  593. elif (cmd == "group_create"):
  594. // Create a new group and become its owner
  595. String group_id
  596. String name
  597. output("Which group do you want to create?")
  598. name = input()
  599. group_id = get_group_id(name)
  600. if (group_id == ""):
  601. group_id = instantiate_node(core, "Group", "")
  602. instantiate_attribute(core, group_id, "name", name)
  603. instantiate_link(core, "belongsTo", "", user_id, group_id)
  604. instantiate_link(core, "owner", "", group_id, user_id)
  605. output("Group created!")
  606. else:
  607. output("Group already exists")
  608. elif (cmd == "group_delete"):
  609. // Delete an existing group
  610. String group_id
  611. String name
  612. output("Which group do you want to delete?")
  613. name = input()
  614. group_id = get_group_id(name)
  615. if (group_id != ""):
  616. if (allow_group_modify(user_id, group_id)):
  617. model_delete_element(core, group_id)
  618. else:
  619. output("Permission denied")
  620. else:
  621. output("No such group")
  622. elif (cmd == "group_owner_add"):
  623. // Add an owner to your group
  624. String group_id
  625. String other_user_id
  626. output("Which group do you want to add an owner to?")
  627. group_id = get_group_id(input())
  628. if (group_id != ""):
  629. if (allow_group_modify(user_id, group_id)):
  630. output("Which user do you want to make an owner?")
  631. other_user_id = get_user_id(input())
  632. if (other_user_id != ""):
  633. Element overlap
  634. overlap = set_overlap(allIncomingAssociationInstances(core, user_id, "owner"), allOutgoingAssociationInstances(core, group_id, "owner"))
  635. if (read_nr_out(overlap) == 0):
  636. instantiate_link(core, "owner", "", group_id, user_id)
  637. overlap = set_overlap(allOutgoingAssociationInstances(core, user_id, "belongsTo"), allIncomingAssociationInstances(core, group_id, "belongsTo"))
  638. if (read_nr_out(overlap) == 0):
  639. instantiate_link(core, "belongsTo", "", user_id, group_id)
  640. output("New owner added to group!")
  641. else:
  642. output("User is already an owner!")
  643. else:
  644. output("No such user")
  645. else:
  646. output("Permission denied!")
  647. else:
  648. output("No such group")
  649. elif (cmd == "group_owner_delete"):
  650. // Remove an owner from your group
  651. String group_id
  652. String other_user_id
  653. output("Which group do you want to disown someone from?")
  654. group_id = get_group_id(input())
  655. if (group_id != ""):
  656. if (allow_group_modify(user_id, group_id)):
  657. output("Which user do you want to disown?")
  658. other_user_id = get_user_id(input())
  659. if (other_user_id != ""):
  660. Element overlap
  661. overlap = set_overlap(allOutgoingAssociationInstances(core, user_id, "belongsTo"), allIncomingAssociationInstances(core, group_id, "belongsTo"))
  662. if (read_nr_out(overlap) > 0):
  663. overlap = set_overlap(allIncomingAssociationInstances(core, user_id, "owner"), allOutgoingAssociationInstances(core, group_id, "owner"))
  664. if (read_nr_out(overlap) > 0):
  665. model_delete_element(core, set_pop(overlap))
  666. output("Disowned group from user!")
  667. else:
  668. output("User is not even an owner of the group!")
  669. else:
  670. output("User is not even a member of the group!")
  671. else:
  672. output("No such user")
  673. else:
  674. output("Permission denied!")
  675. else:
  676. output("No such group")
  677. elif (cmd == "group_join"):
  678. // Add someone to your group
  679. String group_id
  680. String other_user_id
  681. output("Which group do you want to add someone to?")
  682. group_id = get_group_id(input())
  683. if (group_id != ""):
  684. if (allow_group_modify(user_id, group_id)):
  685. output("Which user do you want to add?")
  686. other_user_id = get_user_id(input())
  687. if (other_user_id != ""):
  688. Element overlap
  689. overlap = set_overlap(allOutgoingAssociationInstances(core, user_id, "belongsTo"), allIncomingAssociationInstances(core, group_id, "belongsTo"))
  690. if (read_nr_out(overlap) == 0):
  691. instantiate_link(core, "belongsTo", "", user_id, group_id)
  692. output("User added to the group!")
  693. else:
  694. output("User is already a member of the group!")
  695. else:
  696. output("No such user")
  697. else:
  698. output("Permission denied!")
  699. else:
  700. output("No such group")
  701. elif (cmd == "group_kick"):
  702. // Remove someone from your group
  703. String group_id
  704. String other_user_id
  705. output("Which group do you want to kick someone from?")
  706. group_id = get_group_id(input())
  707. if (group_id != ""):
  708. if (allow_group_modify(user_id, group_id)):
  709. output("Which user do you want to kick?")
  710. other_user_id = get_user_id(input())
  711. if (other_user_id != ""):
  712. Element overlap
  713. overlap = set_overlap(allOutgoingAssociationInstances(core, user_id, "belongsTo"), allIncomingAssociationInstances(core, group_id, "belongsTo"))
  714. if (read_nr_out(overlap) > 0):
  715. model_delete_element(core, set_pop(overlap))
  716. // Check if user was an owner as well
  717. overlap = set_overlap(allIncomingAssociationInstances(core, user_id, "owner"), allOutgoingAssociationInstances(core, group_id, "owner"))
  718. if (read_nr_out(overlap) > 0):
  719. model_delete_element(core, set_pop(overlap))
  720. output("User kicked!")
  721. else:
  722. output("User is not even a member of the group!")
  723. else:
  724. output("No such user")
  725. else:
  726. output("Permission denied!")
  727. else:
  728. output("No such group")
  729. elif (cmd == "group_list"):
  730. // List all groups you are a member of (and whether you are admin or not!)
  731. Element groups
  732. String group_id
  733. String admin
  734. groups = allAssociationDestinations(core, user_id, "belongsTo")
  735. while (True):
  736. group_id = set_pop(groups)
  737. if (set_in(allOutgoingAssociationInstances(core, group_id, "owner"), user_id)):
  738. admin = " A "
  739. else:
  740. admin = " "
  741. output(string_join(admin, read_attribute(core, group_id, "name")))
  742. elif (cmd == "admin_promote"):
  743. // Promote a user to admin status
  744. if (is_admin(user_id)):
  745. String other_user_id
  746. output("Which user do you want to promote?")
  747. other_user_id = get_user_id(input())
  748. if (other_user_id != ""):
  749. unset_attribute(core, other_user_id, "admin")
  750. instantiate_attribute(core, other_user_id, "admin", True)
  751. output("Permissions granted!")
  752. else:
  753. output("No such user!")
  754. else:
  755. output("Permission denied!")
  756. elif (cmd == "admin_demote"):
  757. // Demote a user to normal status
  758. if (is_admin(user_id)):
  759. String other_user_id
  760. output("Which user do you want to demote?")
  761. other_user_id = get_user_id(input())
  762. if (other_user_id != ""):
  763. unset_attribute(core, other_user_id, "admin")
  764. instantiate_attribute(core, other_user_id, "admin", False)
  765. output("Permissions revoked!")
  766. else:
  767. output("No such user!")
  768. else:
  769. output("Permission denied!")
  770. elif (cmd == "exit"):
  771. // Exit by actually removing the user and decoupling it from all of its models
  772. // Restarting with the same user name will NOT grant you access to anything of the previous user with that same name
  773. do_continue = False
  774. // Delete user from Core Formalism
  775. model_delete_element(core, user_id)
  776. output("Goodbye!")
  777. return !