core_algorithm.alc 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396
  1. include "modelling.alh"
  2. include "library.alh"
  3. include "primitives.alh"
  4. include "constructors.alh"
  5. include "object_operations.alh"
  6. include "mini_modify.alh"
  7. include "model_management.alh"
  8. include "ramify.alh"
  9. include "conformance_scd.alh"
  10. include "transform.alh"
  11. include "metamodels.alh"
  12. Element core = ?
  13. String core_location = "models/CoreFormalism"
  14. String core_model_location = "core"
  15. String coreM_model_location = "models/core"
  16. Void function main():
  17. // Initialize the Core Formalism
  18. String core_model
  19. String core_formalism_model
  20. String scd_model
  21. Element al_model
  22. String admin_group
  23. String admin_user
  24. String nobody_group
  25. String instance_of
  26. String core_formalism
  27. Element scd
  28. Element al
  29. String scd_location
  30. String al_location
  31. scd_location = "models/SimpleClassDiagrams"
  32. scd = import_node(scd_location)
  33. al_location = "models/ActionLanguage"
  34. al = import_node(al_location)
  35. // Create the Model itself and make public
  36. core_formalism = import_node(core_location)
  37. core = instantiate_model(core_formalism)
  38. export_node(core_model_location, core)
  39. export_node(coreM_model_location, core["model"])
  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. Element prev
  44. root = read_root()
  45. root = root["__hierarchy"]["__IP"]
  46. while (value_neq(root, !call)):
  47. prev = root
  48. root = root["next"]
  49. dict_delete(prev, "next")
  50. dict_add(prev, "next", user_function["body"])
  51. // Create admin group
  52. admin_group = instantiate_node(core, "Group", "")
  53. instantiate_attribute(core, admin_group, "name", "admin")
  54. // Create nobody group
  55. nobody_group = instantiate_node(core, "Group", "")
  56. instantiate_attribute(core, nobody_group, "name", "nobody")
  57. // Create admin user
  58. admin_user = instantiate_node(core, "User", "")
  59. output("Desired username for admin user?")
  60. instantiate_attribute(core, admin_user, "name", input())
  61. instantiate_attribute(core, admin_user, "admin", True)
  62. String password
  63. while (True):
  64. output("Desired password for admin user?")
  65. password = hash(input())
  66. output("Please repeat the password")
  67. if (password == hash(input())):
  68. output("Passwords match!")
  69. instantiate_attribute(core, admin_user, "password", password)
  70. break!
  71. else:
  72. output("Not the same password, please try again!")
  73. // Create link between admin user and group
  74. instantiate_link(core, "ownedBy", "", admin_group, admin_user)
  75. instantiate_link(core, "belongsTo", "", admin_user, admin_group)
  76. // Add the SimpleClassDiagrams formalism already
  77. scd_model = instantiate_node(core, "Model", "")
  78. instantiate_attribute(core, scd_model, "name", "SimpleClassDiagrams")
  79. instantiate_attribute(core, scd_model, "location", scd_location + "/model")
  80. instantiate_attribute(core, scd_model, "permissions", "221")
  81. instance_of = instantiate_link(core, "instanceOf", "", scd_model, scd_model)
  82. instantiate_attribute(core, instance_of, "type_mapping", scd["type_mapping"])
  83. // Make necessary links for the formalism to the owners
  84. instantiate_link(core, "group", "", scd_model, admin_group)
  85. instantiate_link(core, "owner", "", scd_model, admin_user)
  86. // Add the Action Language formalism
  87. al_model = instantiate_node(core, "Model", "")
  88. instantiate_attribute(core, al_model, "name", "ActionLanguage")
  89. instantiate_attribute(core, al_model, "location", al_location + "/model")
  90. instantiate_attribute(core, al_model, "permissions", "221")
  91. instance_of = instantiate_link(core, "instanceOf", "", al_model, scd_model)
  92. instantiate_attribute(core, instance_of, "type_mapping", al["type_mapping"])
  93. // Make necessary links for the formalism to the owners
  94. instantiate_link(core, "group", "", al_model, admin_group)
  95. instantiate_link(core, "owner", "", al_model, admin_user)
  96. // Add the core formalism already
  97. core_formalism_model = instantiate_node(core, "Model", "")
  98. instantiate_attribute(core, core_formalism_model, "name", "CoreFormalism")
  99. instantiate_attribute(core, core_formalism_model, "location", core_location + "/model")
  100. instantiate_attribute(core, core_formalism_model, "permissions", "221")
  101. instance_of = instantiate_link(core, "instanceOf", "", core_formalism_model, scd_model)
  102. instantiate_attribute(core, instance_of, "type_mapping", core_formalism["type_mapping"])
  103. // Make necessary links for the formalism to the owners
  104. instantiate_link(core, "group", "", core_formalism_model, admin_group)
  105. instantiate_link(core, "owner", "", core_formalism_model, admin_user)
  106. // Add the core model
  107. core_model = instantiate_node(core, "Model", "")
  108. instantiate_attribute(core, core_model, "name", "core")
  109. instantiate_attribute(core, core_model, "location", coreM_model_location)
  110. instantiate_attribute(core, core_model, "permissions", "200")
  111. instance_of = instantiate_link(core, "instanceOf", "", core_model, core_formalism_model)
  112. instantiate_attribute(core, instance_of, "type_mapping", core["type_mapping"])
  113. // Make necessary links for the formalism to the owners
  114. instantiate_link(core, "group", "", core_model, admin_group)
  115. instantiate_link(core, "owner", "", core_model, admin_user)
  116. // Call this for ourselves as well
  117. log("MvC is ready!")
  118. user_function_skip_init(admin_user)
  119. // Done, so finish up
  120. // Admin user will have been deleted by the user_function as usual
  121. // Note that if there are no admin users left, it will be very difficult to manage, as nobody will have admin permissions!
  122. return !
  123. String function get_instanceOf_link(model_id : String):
  124. Element all_links
  125. String choice
  126. all_links = allOutgoingAssociationInstances(core, model_id, "instanceOf")
  127. if (read_nr_out(all_links) > 1):
  128. log("WARNING: multiple instanceOf relations were detected for this model; picking one at random!")
  129. elif (read_nr_out(all_links) == 0):
  130. log("ERROR: untyped model!")
  131. choice = set_pop(allOutgoingAssociationInstances(core, model_id, "instanceOf"))
  132. return choice!
  133. Element function get_full_model(model_id : String):
  134. Element m
  135. Element all_links
  136. String choice
  137. choice = get_instanceOf_link(model_id)
  138. m = create_node()
  139. dict_add(m, "model", import_node(read_attribute(core, model_id, "location")))
  140. dict_add(m, "type_mapping", read_attribute(core, choice, "type_mapping"))
  141. if (readAssociationDestination(core, choice) == model_id):
  142. // Found the meta-circular level, so we can stop!
  143. dict_add(m, "metamodel", m)
  144. else:
  145. dict_add(m, "metamodel", get_full_model(readAssociationDestination(core, choice)))
  146. return m!
  147. Integer function get_relation_to_model(user_id : String, model_id : String):
  148. if (set_in(allAssociationDestinations(core, model_id, "owner"), user_id)):
  149. // We are the owner
  150. return 0!
  151. else:
  152. String group_id
  153. group_id = set_pop(allAssociationDestinations(core, model_id, "group"))
  154. if (set_in(allAssociationDestinations(core, user_id, "belongsTo"), group_id)):
  155. // We are in the owning group
  156. return 1!
  157. else:
  158. // We are not related whatsoever
  159. return 2!
  160. Boolean function is_admin(user_id : String):
  161. if (read_attribute(core, user_id, "admin")):
  162. return True!
  163. else:
  164. return False!
  165. Boolean function allow_read(user_id : String, model_id : String):
  166. if (is_admin(user_id)):
  167. // Is admin, so always allow
  168. return True!
  169. else:
  170. // Check permissions
  171. String permission
  172. permission = string_get(read_attribute(core, model_id, "permissions"), get_relation_to_model(user_id, model_id))
  173. if (bool_or(permission == "1", permission == "2")):
  174. return True!
  175. else:
  176. return False!
  177. Boolean function allow_write(user_id : String, model_id : String):
  178. if (is_admin(user_id)):
  179. // Is admin, so always allow
  180. return True!
  181. else:
  182. // Check permissions
  183. String permission
  184. permission = string_get(read_attribute(core, model_id, "permissions"), get_relation_to_model(user_id, model_id))
  185. if (permission == "2"):
  186. return True!
  187. else:
  188. return False!
  189. Boolean function allow_change_metadata(user_id : String, model_id : String):
  190. if (is_admin(user_id)):
  191. // Is admin, so always allow
  192. return True!
  193. else:
  194. if (get_relation_to_model(user_id, model_id) == 0):
  195. // Only owner can chmod
  196. return True!
  197. else:
  198. return False!
  199. Boolean function allow_group_modify(user_id : String, group_id : String):
  200. if (is_admin(user_id)):
  201. // Is admin, so always allow
  202. return True!
  203. else:
  204. if (read_nr_out(set_overlap(allIncomingAssociationInstances(core, user_id, "owner"), allOutgoingAssociationInstances(core, group_id, "owner"))) > 0):
  205. // We are an owner
  206. return True!
  207. else:
  208. return False!
  209. Boolean function check_login(user_id : String):
  210. String password
  211. String stored_password
  212. stored_password = read_attribute(core, user_id, "password")
  213. output("Password for existing user?")
  214. password = hash(input())
  215. return password == stored_password!
  216. Element function extract_ftg(user_id : String):
  217. // Extract your personal FTG, showing only the readable models that can be opened
  218. // This function does the actual projection of transformations
  219. // TODO: Only keep transformations satisfying some properties!
  220. return create_node()!
  221. Element function user_function():
  222. String username
  223. String user_id
  224. String password
  225. // Load in all global variables, as this code is hotloaded!
  226. Element root
  227. root = read_root()
  228. root = root["__hierarchy"]["objects"]
  229. exec(root["bootstrap/modelling.alc"]["initializers"])
  230. exec(root["bootstrap/library.alc"]["initializers"])
  231. exec(root["bootstrap/constructors.alc"]["initializers"])
  232. exec(root["bootstrap/object_operations.alc"]["initializers"])
  233. exec(root["core/mini_modify.alc"]["initializers"])
  234. exec(root["bootstrap/model_management.alc"]["initializers"])
  235. exec(root["bootstrap/ramify.alc"]["initializers"])
  236. exec(root["bootstrap/transform.alc"]["initializers"])
  237. exec(root["bootstrap/conformance_scd.alc"]["initializers"])
  238. exec(root["core/core_algorithm.alc"]["initializers"])
  239. // Load in a hard-reference to the previously created model
  240. core = import_node(core_model_location)
  241. output("Log on as which user?")
  242. username = input()
  243. user_id = get_user_id(username)
  244. if (user_id == ""):
  245. // New user
  246. // Add user to Core Formalism
  247. user_id = instantiate_node(core, "User", "")
  248. instantiate_attribute(core, user_id, "name", username)
  249. instantiate_attribute(core, user_id, "admin", False)
  250. while (True):
  251. output("This is a new user: please give password!")
  252. password = hash(input())
  253. output("Please repeat the password")
  254. if (password == hash(input())):
  255. output("Passwords match!")
  256. output("User created")
  257. instantiate_attribute(core, user_id, "password", password)
  258. break!
  259. else:
  260. output("Not the same password, please try again!")
  261. // Now call with user created
  262. user_function_skip_init(user_id)
  263. else:
  264. while (bool_not(check_login(user_id))):
  265. output("Wrong password! Try again")
  266. user_function_skip_init(user_id)
  267. // User destroyed already, so just stop execution
  268. // TODO return a fresh node as otherwise the compiler doesn't take this
  269. return create_node()!
  270. String function get_model_id(name : String):
  271. Element models
  272. String model
  273. models = allInstances(core, "Model")
  274. while (read_nr_out(models) > 0):
  275. model = set_pop(models)
  276. if (value_eq(name, read_attribute(core, model, "name"))):
  277. return model!
  278. return ""!
  279. String function get_user_id(name : String):
  280. Element users
  281. String user
  282. users = allInstances(core, "User")
  283. while (read_nr_out(users) > 0):
  284. user = set_pop(users)
  285. if (value_eq(read_attribute(core, user, "name"), name)):
  286. return user!
  287. return ""!
  288. String function get_group_id(name : String):
  289. Element groups
  290. String group
  291. groups = allInstances(core, "Group")
  292. while (read_nr_out(groups) > 0):
  293. group = set_pop(groups)
  294. if (value_eq(read_attribute(core, group, "name"), name)):
  295. return group!
  296. return ""!
  297. Void function model_create(model : Element, name : String, user_id : String, type_id : String, kind : String):
  298. String location
  299. String model_id
  300. String instance_of
  301. location = "/models/" + cast_id2s(model)
  302. export_node(location, model["model"])
  303. // Manage meta-info
  304. model_id = instantiate_node(core, kind, "")
  305. instantiate_attribute(core, model_id, "name", name)
  306. instantiate_attribute(core, model_id, "location", location)
  307. instantiate_attribute(core, model_id, "permissions", "200")
  308. instantiate_link(core, "owner", "", model_id, user_id)
  309. instantiate_link(core, "group", "", model_id, get_group_id("nobody"))
  310. instance_of = instantiate_link(core, "instanceOf", "", model_id, type_id)
  311. instantiate_attribute(core, instance_of, "type_mapping", model["type_mapping"])
  312. return!
  313. Void function model_overwrite(model : Element, model_id : String):
  314. String location
  315. String instanceOf_link
  316. location = "/models/" + cast_id2s(model)
  317. export_node(location, model["model"])
  318. // Change location in meta-data
  319. unset_attribute(core, model_id, "location")
  320. instantiate_attribute(core, model_id, "location", location)
  321. instanceOf_link = get_instanceOf_link(model_id)
  322. unset_attribute(core, instanceOf_link, "type_mapping")
  323. instantiate_attribute(core, instanceOf_link, "type_mapping", model["type_mapping"])
  324. return!
  325. Boolean function check_is_typed_by(model_id : String, metamodel_id : String):
  326. // TODO check if there is actually an instanceOf link between them
  327. // --> quick check!
  328. return True!
  329. Boolean function check_conformance(model_id : String):
  330. // TODO check if it actually conforms, considering that instanceOf link
  331. // --> in-depth check
  332. return True!
  333. Void function user_function_skip_init(user_id : String):
  334. String cmd
  335. output("Welcome to the Model Management Interface v2.0!")
  336. output("Use the 'help' command for a list of possible commands")
  337. while (True):
  338. output("Ready for command...")
  339. cmd = input()
  340. if (cmd == "help"):
  341. output("Model operations")
  342. output(" model_add -- Add a new model")
  343. output(" model_modify -- Modify an existing model")
  344. output(" model_delete -- [TODO] Delete a model and all related transformations")
  345. output(" model_list -- List all models")
  346. output(" model_list_full -- List all models with full info")
  347. output(" model_overwrite -- Overwrites a model with an uploaded model, leaving all metadata")
  348. output("")
  349. output("Transformation-specific operations")
  350. output(" transformation_add_MT_language -- Create a RAMified metamodel of a set of models")
  351. output(" transformation_add_MT -- Initialize a new model transformation")
  352. output(" transformation_add_AL -- [TODO] Initialize a new action language transformation")
  353. output(" transformation_add_EXT -- [TODO] Initialize a new external tool transformation")
  354. output(" transformation_add_MANUAL -- [TODO] Initialize a new manual transformation")
  355. output(" transformation_execute -- Execute a transformation on a set of input models")
  356. output(" transformation_list -- List all model transformations")
  357. output(" transformation_list_full -- List all model transformations with permissions")
  358. output(" transformation_detail -- List transformation details")
  359. output(" transformation_RAMify -- RAMify a metamodel (again)")
  360. output("")
  361. output("Model permission operations")
  362. output(" permission_modify -- Change model permissions")
  363. output(" permission_owner -- Change model owner")
  364. output(" permission_group -- Change model group")
  365. output("")
  366. output("Group operations")
  367. output(" group_create -- Create a group")
  368. output(" group_delete -- Delete a group")
  369. output(" group_owner_add -- Add group owner")
  370. output(" group_owner_delete -- Remove group owner")
  371. output(" group_join -- Add someone to your group")
  372. output(" group_kick -- Kick someone from your group")
  373. output(" group_list -- List all groups you are a member of")
  374. output("")
  375. output("Admin operations")
  376. output(" admin_promote -- Promote a user to admin status")
  377. output(" admin_demote -- Demote a user to normal status")
  378. output("")
  379. output("General operations")
  380. output(" self-destruct -- Remove current user and revoke all permissions ")
  381. output(" exit -- Kill the current task, while retaining user")
  382. elif (cmd == "model_add"):
  383. // Model addition operation, which uses model upload commands of the compiler
  384. String name
  385. String type
  386. String location
  387. String type_id
  388. Element new_model
  389. String new_model_id
  390. output("Creating new model!")
  391. output("Model type?")
  392. type_id = get_model_id(input())
  393. if (type_id != ""):
  394. // Type exists
  395. if (allow_read(user_id, type_id)):
  396. // And is readable
  397. output("Model name?")
  398. name = input()
  399. if (get_model_id(name) == ""):
  400. // Model doesn't exist yet
  401. output("Waiting for model constructors...")
  402. new_model = construct_model_raw(get_full_model(type_id))
  403. model_create(new_model, name, user_id, type_id, "Model")
  404. output("Model upload success!")
  405. else:
  406. output("Model with that name already exists!")
  407. else:
  408. output("Permission denied")
  409. else:
  410. output("Could not find type model!")
  411. elif (cmd == "transformation_execute"):
  412. // Execute a transformation, whatever type it is
  413. // First we detect the type, so we know how to prepare for invocation
  414. String transformation_id
  415. String exact_type
  416. Element sources
  417. Element targets
  418. String source
  419. String target
  420. String name_id
  421. Element inputs
  422. Element outputs
  423. Element trace_links
  424. String target_model_name
  425. String source_model_name
  426. output("Which transformation do you want to execute?")
  427. transformation_id = get_model_id(input())
  428. if (transformation_id != ""):
  429. if (allow_read(user_id, transformation_id)):
  430. if (is_nominal_instance(core, transformation_id, "Transformation")):
  431. // Read out source and target links
  432. sources = allOutgoingAssociationInstances(core, transformation_id, "transformInput")
  433. inputs = create_node()
  434. while (read_nr_out(sources) > 0):
  435. source = set_pop(sources)
  436. output(string_join("Which model to bind for source element ", read_attribute(core, source, "name")))
  437. source_model_name = input()
  438. name_id = get_model_id(source_model_name)
  439. if (name_id != ""):
  440. if (allow_read(user_id, name_id)):
  441. // Check for conformance to the specified metamodel!
  442. Element specified_model
  443. // TODO Maybe find out which conformance relation to use, as there might be multiple!
  444. if (check_is_typed_by(name_id, source)):
  445. if (check_conformance(name_id)):
  446. dict_add(inputs, read_attribute(core, source, "name"), source_model_name)
  447. else:
  448. output("Model has correct type but does not conform completely!")
  449. set_add(sources, source)
  450. else:
  451. output("Model has different type!")
  452. set_add(sources, source)
  453. else:
  454. output("Permission denied")
  455. set_add(sources, source)
  456. else:
  457. output("No such model")
  458. set_add(sources, source)
  459. targets = allOutgoingAssociationInstances(core, transformation_id, "transformOutput")
  460. outputs = create_node()
  461. while (read_nr_out(targets) > 0):
  462. target = set_pop(targets)
  463. output(string_join("Which model to create for target element ", read_attribute(core, target, "name")))
  464. target_model_name = input()
  465. if (get_model_id(target_model_name) == ""):
  466. // Doesn't exist yet, so we can easily create
  467. dict_add(outputs, read_attribute(core, target, "name"), target_model_name)
  468. else:
  469. // Already exists, so we need to check for write access
  470. if (allow_write(user_id, get_model_id(target_model_name))):
  471. dict_add(outputs, read_attribute(core, target, "name"), target_model_name)
  472. else:
  473. output("Permission denied; try again")
  474. exact_type = read_type(core, transformation_id)
  475. if (exact_type == "ModelTransformation"):
  476. // Model transformation is always in-place and uses only a single metamodel
  477. // Therefore, we must:
  478. // 1) Create an empty model, instance of merged metamodel
  479. // 2) Merge the different source models and retype
  480. // 3) Perform the transformation on the merged model
  481. // 4) Split the resulting model based on the target formalisms
  482. //
  483. // There is one exception: if the target model is bound to a source model, that model is overwritten
  484. // This allows for some optimizations when it is a simple in-place transformation (skip model copy, join, and split)
  485. // First check for this exception, as it is much faster
  486. Element input_model
  487. Element schedule_model
  488. String trace_link_id
  489. Element merged_model
  490. String merged_metamodel_id
  491. String ramified_metamodel_id
  492. Boolean result
  493. schedule_model = get_full_model(transformation_id)
  494. // 1) Create empty instance of merged metamodel
  495. ramified_metamodel_id = set_pop(followAssociation(core, transformation_id, "instanceOf"))
  496. trace_links = allOutgoingAssociationInstances(core, ramified_metamodel_id, "tracability")
  497. merged_metamodel_id = ""
  498. while (read_nr_out(trace_links) > 0):
  499. trace_link_id = set_pop(trace_links)
  500. if (value_eq(read_attribute(core, trace_link_id, "type"), "RAMified")):
  501. merged_metamodel_id = readAssociationDestination(core, trace_link_id)
  502. if (merged_metamodel_id != ""):
  503. merged_model = instantiate_model(get_full_model(merged_metamodel_id))
  504. // 2) Merge source models
  505. String key
  506. Element keys
  507. Element input_keys
  508. Element output_keys
  509. input_keys = dict_keys(inputs)
  510. while (read_nr_out(input_keys) > 0):
  511. key = set_pop(input_keys)
  512. model_join(merged_model, get_full_model(get_model_id(inputs[key])), key + "/")
  513. // 3) Transform
  514. log("EXECUTE TRANSFORMATION " + cast_e2s(read_attribute(core, transformation_id, "name")))
  515. result = transform(merged_model, schedule_model)
  516. output("Transformation executed with result: " + cast_v2s(result))
  517. // 4) Split in different files depending on type
  518. String desired_metamodel_id
  519. Element split_off_model
  520. output_keys = dict_keys(outputs)
  521. while (read_nr_out(output_keys) > 0):
  522. key = set_pop(output_keys)
  523. desired_metamodel_id = get_model_id(key)
  524. split_off_model = model_split(merged_model, get_full_model(desired_metamodel_id), key + "/")
  525. // Check if the destination model already exists
  526. if (get_model_id(outputs[key]) == ""):
  527. // New model
  528. model_create(split_off_model, outputs[key], user_id, desired_metamodel_id, "Model")
  529. else:
  530. // Model exists, so we overwrite
  531. model_overwrite(split_off_model, get_model_id(outputs[key]))
  532. else:
  533. output("Could not resolve intermediate merged metamodel")
  534. elif (exact_type == "ActionLanguage"):
  535. Element action_model
  536. Element dictionary
  537. Element input_keys
  538. Element output_keys
  539. Element result
  540. String key
  541. Element func
  542. output("Action Language execution starts!")
  543. log("Getting full model for transformation")
  544. action_model = get_full_model(transformation_id)
  545. log("Got model: " + cast_e2s(action_model))
  546. // 1) Group source models in dictionary
  547. // --> This is just the "inputs" variable
  548. log("Create inputs")
  549. // 2) Execute action language model
  550. func = get_func_AL_model(action_model)
  551. log("Ready to execute: " + cast_e2s(func))
  552. result = func(inputs)
  553. log("Result: " + cast_e2s(result))
  554. // 3) Split output dictionary back to seperate models
  555. output_keys = dict_keys(outputs)
  556. while (read_nr_out(output_keys) > 0):
  557. key = set_pop(output_keys)
  558. log("Splitting " + key)
  559. // Check if the destination model already exists
  560. if (get_model_id(outputs[key]) == ""):
  561. // New model
  562. model_create(result[key], outputs[key], user_id, get_model_id(key), "Model")
  563. else:
  564. // Model exists, so we overwrite
  565. model_overwrite(result[key], get_model_id(outputs[key]))
  566. log("Finished")
  567. else:
  568. output("Did not know how to interpret model of type " + exact_type)
  569. else:
  570. output("Model is not an executable transformation")
  571. else:
  572. output("Permission denied")
  573. else:
  574. output("No such transformation")
  575. elif (cmd == "model_overwrite"):
  576. // Overwrites an existing model without changing any metadata
  577. String model_id
  578. Element new_model
  579. output("Which model to overwrite?")
  580. model_id = get_model_id(input())
  581. if (model_id != ""):
  582. if (allow_write(user_id, model_id)):
  583. if (allow_read(user_id, set_pop(followAssociation(core, model_id, "instanceOf")))):
  584. output("Waiting for model constructors...")
  585. new_model = construct_model_raw(get_full_model(set_pop(followAssociation(core, model_id, "instanceOf"))))
  586. model_overwrite(new_model, model_id)
  587. output("Model overwrite success!")
  588. else:
  589. output("Permission denied")
  590. else:
  591. output("Permission denied")
  592. else:
  593. output("No such model")
  594. elif (cmd == "model_modify"):
  595. // Model modify operation, which uses the mini_modify.alc operations, though with extensions for access control
  596. String model_id
  597. String type_id
  598. output("Which model do you want to modify?")
  599. model_id = get_model_id(input())
  600. if (model_id != ""):
  601. if (allow_read(user_id, model_id)):
  602. type_id = set_pop(allAssociationDestinations(core, model_id, "instanceOf"))
  603. if (allow_read(user_id, type_id)):
  604. modify(get_full_model(model_id), allow_write(user_id, model_id))
  605. else:
  606. output("Permission denied")
  607. else:
  608. output("Permission denied")
  609. else:
  610. output("Could not find model!")
  611. elif (cmd == "model_delete"):
  612. // Delete a model and all of its related transformations
  613. String model_id
  614. output("=================================================")
  615. output("WARNING: Deletion is a very destructive operation")
  616. output(" as it also deletes all transformations ")
  617. output(" defined which make use of this model! ")
  618. output("=================================================")
  619. output("")
  620. output("Currently not supported!")
  621. elif (cmd == "model_list"):
  622. // List all models
  623. Element models
  624. String m
  625. models = allInstances(core, "Model")
  626. while (read_nr_out(models) > 0):
  627. m = set_pop(models)
  628. output(string_join((string_join(" ", read_attribute(core, m, "name")) + " : "), read_attribute(core, set_pop(followAssociation(core, m, "instanceOf")), "name")))
  629. elif (cmd == "model_list_full"):
  630. // List all models with full info
  631. Element models
  632. String m
  633. String permissions
  634. String owner
  635. String group
  636. String name
  637. String type
  638. models = allInstances(core, "Model")
  639. while (read_nr_out(models) > 0):
  640. m = set_pop(models)
  641. permissions = read_attribute(core, m, "permissions")
  642. owner = read_attribute(core, set_pop(allAssociationDestinations(core, m, "owner")), "name")
  643. group = read_attribute(core, set_pop(allAssociationDestinations(core, m, "group")), "name")
  644. name = read_attribute(core, m, "name")
  645. type = read_attribute(core, set_pop(allAssociationDestinations(core, m, "instanceOf")), "name")
  646. output(((((((((" " + permissions) + " ") + owner) + " ") + group) + " ") + name) + " : ") + type)
  647. elif (cmd == "transformation_add_MT_language"):
  648. // Create a model transformation language from a set of input and output formalisms
  649. String name
  650. String model_id
  651. Element source
  652. Element target
  653. Element all_formalisms
  654. Element merged_formalism
  655. Element ramified_formalism
  656. String old_type_id
  657. String type_id
  658. String location
  659. String new_model_id
  660. old_type_id = ""
  661. log("Adding MT language")
  662. // Read involved formalisms
  663. all_formalisms = create_node()
  664. output("Formalisms to include (terminate with empty string)?")
  665. name = input()
  666. while (name != ""):
  667. model_id = get_model_id(name)
  668. if (model_id != ""):
  669. if (allow_read(user_id, model_id)):
  670. type_id = set_pop(allAssociationDestinations(core, model_id, "instanceOf"))
  671. if (bool_or(old_type_id == "", type_id == old_type_id)):
  672. set_add(all_formalisms, create_tuple(name, get_full_model(model_id)))
  673. old_type_id = type_id
  674. elif (old_type_id != type_id):
  675. // Already have a previous type_id and now another: CLASH
  676. output("Cannot add model as types not compatible with previous models; try again")
  677. else:
  678. output("Model not readable; try again")
  679. else:
  680. output("No such model; try again")
  681. name = input()
  682. // Merge both into a single metamodel
  683. if (read_nr_out(all_formalisms) > 0):
  684. output("Name of the RAMified transformation metamodel?")
  685. name = input()
  686. if (get_model_id(name) == ""):
  687. String merged_formalism_id
  688. String ramified_formalism_id
  689. String source_formalism_id
  690. String tracability_link
  691. // New location is available, so write
  692. log("FUSE")
  693. merged_formalism = model_fuse(set_copy(all_formalisms))
  694. log("Fuse OK")
  695. model_create(merged_formalism, "__merged_" + name, user_id, type_id, "Model")
  696. merged_formalism_id = get_model_id("__merged_" + name)
  697. // Add tracability links at this level
  698. while (read_nr_out(all_formalisms) > 0):
  699. source_formalism_id = get_model_id(list_read(set_pop(all_formalisms), 0))
  700. tracability_link = instantiate_link(core, "tracability", "", merged_formalism_id, source_formalism_id)
  701. instantiate_attribute(core, tracability_link, "type", "merged")
  702. // Merge complete, now RAMify!
  703. log("RAM")
  704. ramified_formalism = ramify(merged_formalism)
  705. log("RAMed")
  706. model_create(ramified_formalism, name, user_id, type_id, "Model")
  707. ramified_formalism_id = get_model_id(name)
  708. // Add tracability link at this level
  709. tracability_link = instantiate_link(core, "tracability", "", ramified_formalism_id, merged_formalism_id)
  710. instantiate_attribute(core, tracability_link, "type", "RAMified")
  711. else:
  712. output("Model already exists!")
  713. else:
  714. output("At least one formalism is required")
  715. elif (cmd == "transformation_RAMify"):
  716. // RAMify a metamodel
  717. String merged_model_id
  718. String target_model_name
  719. String target_model_id
  720. Element target_model
  721. String tracability_link
  722. output("Which metamodel do you want to RAMify?")
  723. merged_model_id = get_model_id(input())
  724. if (merged_model_id != ""):
  725. if (allow_read(user_id, merged_model_id)):
  726. output("Where do you want to store the RAMified metamodel?")
  727. target_model_name = input()
  728. target_model_id = get_model_id(target_model_name)
  729. if (target_model_id == ""):
  730. // New model, so everything is fine
  731. target_model = ramify(get_full_model(merged_model_id))
  732. model_create(target_model, target_model_name, user_id, set_pop(allAssociationDestinations(core, merged_model_id, "instanceOf")), "Model")
  733. target_model_id = get_model_id(target_model_name)
  734. tracability_link = instantiate_link(core, "tracability", "", target_model_id, merged_model_id)
  735. instantiate_attribute(core, tracability_link, "type", "RAMified")
  736. else:
  737. // Existing model, so overwrite
  738. if (allow_write(user_id, target_model_id)):
  739. target_model = ramify(get_full_model(merged_model_id))
  740. model_overwrite(target_model, target_model_id)
  741. model_delete_element(core, set_pop(allOutgoingAssociationInstances(core, target_model_id, "tracability")))
  742. tracability_link = instantiate_link(core, "tracability", "", target_model_id, merged_model_id)
  743. instantiate_attribute(core, tracability_link, "type", "RAMified")
  744. else:
  745. output("Permission denied")
  746. else:
  747. output("Permission denied!")
  748. else:
  749. output("No such model")
  750. elif (cmd == "transformation_add_AL"):
  751. // Add an action language transformation model
  752. output("Which metamodels do you want to use as source for the action code (empty string to finish)?")
  753. String model_id
  754. Element models
  755. Element source
  756. Element target
  757. String name
  758. name = input()
  759. source = create_node()
  760. target = create_node()
  761. while (name != ""):
  762. model_id = get_model_id(name)
  763. if (model_id != ""):
  764. if (bool_not(set_in(source, model_id))):
  765. set_add(source, model_id)
  766. output("Model added as source")
  767. else:
  768. output("Model already selected as source")
  769. else:
  770. output("No such model; try again")
  771. name = input()
  772. output("Which metamodels do you want to use as target for the action code (empty string to finish)?")
  773. name = input()
  774. while (name != ""):
  775. model_id = get_model_id(name)
  776. if (model_id != ""):
  777. if (bool_not(set_in(target, model_id))):
  778. set_add(target, model_id)
  779. output("Model added as target")
  780. else:
  781. output("Model already selected as target")
  782. else:
  783. output("No such model; try again")
  784. name = input()
  785. output("Name of Action Language model?")
  786. name = input()
  787. log("Add AL is fine!")
  788. if (get_model_id(name) == ""):
  789. // Finished with all information, now create the model itself!
  790. output("Waiting for model constructors...")
  791. log("AL model: " + cast_e2s(get_model_id("ActionLanguage")))
  792. log("Location: " + cast_e2s(read_attribute(core, get_model_id("ActionLanguage"), "location")))
  793. log("Imported: " + cast_e2s(import_node(read_attribute(core, get_model_id("ActionLanguage"), "location"))))
  794. add_code_model(import_node(read_attribute(core, get_model_id("ActionLanguage"), "location")), "AL/" + name, construct_function())
  795. log("Exported to " + cast_e2s(import_node("AL/" + name)))
  796. model_create(import_node("AL/" + name), name, user_id, get_model_id("ActionLanguage"), "ActionLanguage")
  797. model_id = get_model_id(name)
  798. // Extend metadata with info on source and target
  799. String link
  800. String dst
  801. log("Adding inputs")
  802. while (read_nr_out(source) > 0):
  803. dst = set_pop(source)
  804. log(" for " + dst)
  805. link = instantiate_link(core, "transformInput", "", model_id, dst)
  806. instantiate_attribute(core, link, "name", read_attribute(core, dst, "name"))
  807. log("Adding outputs")
  808. while (read_nr_out(target) > 0):
  809. dst = set_pop(target)
  810. log(" for " + dst)
  811. link = instantiate_link(core, "transformOutput", "", model_id, dst)
  812. instantiate_attribute(core, link, "name", read_attribute(core, dst, "name"))
  813. else:
  814. output("Model already exists")
  815. elif (cmd == "transformation_add_MT"):
  816. // Add a model transformation model
  817. // Just a usual model instantiation, but need to add the source and target links based on user info
  818. String ramified_metamodel_id
  819. String model_id
  820. Element models
  821. Element links
  822. String link_id
  823. Element supported
  824. Element source
  825. Element target
  826. String name
  827. String new_model_id
  828. String merged_link_id
  829. Element links_merged
  830. String merged_metamodel_id
  831. source = create_node()
  832. target = create_node()
  833. supported = create_node()
  834. output("RAMified metamodel to use?")
  835. ramified_metamodel_id = get_model_id(input())
  836. if (ramified_metamodel_id != ""):
  837. if (allow_read(user_id, ramified_metamodel_id)):
  838. output("Supported metamodels:")
  839. links = allOutgoingAssociationInstances(core, ramified_metamodel_id, "tracability")
  840. while (read_nr_out(links) > 0):
  841. link_id = set_pop(links)
  842. merged_metamodel_id = readAssociationDestination(core, link_id)
  843. if (value_eq(read_attribute(core, link_id, "type"), "RAMified")):
  844. links_merged = allOutgoingAssociationInstances(core, merged_metamodel_id, "tracability")
  845. while (read_nr_out(links_merged) > 0):
  846. merged_link_id = set_pop(links_merged)
  847. if (value_eq(read_attribute(core, merged_link_id, "type"), "merged")):
  848. output(string_join(" ", read_attribute(core, readAssociationDestination(core, merged_link_id), "name")))
  849. set_add(supported, readAssociationDestination(core, merged_link_id))
  850. output("")
  851. output("Which ones do you want to use as source (empty string to finish)?")
  852. name = input()
  853. while (name != ""):
  854. model_id = get_model_id(name)
  855. if (model_id != ""):
  856. if (set_in(supported, model_id)):
  857. if (bool_not(set_in(source, model_id))):
  858. set_add(source, model_id)
  859. output("Model added as source")
  860. else:
  861. output("Model already selected as source")
  862. else:
  863. output("Model is not supported by RAMified metamodel!")
  864. else:
  865. output("No such model; try again")
  866. name = input()
  867. output("Which ones do you want to use as target (empty string to finish)?")
  868. name = input()
  869. while (name != ""):
  870. model_id = get_model_id(name)
  871. if (model_id != ""):
  872. if (set_in(supported, model_id)):
  873. if (bool_not(set_in(target, model_id))):
  874. set_add(target, model_id)
  875. output("Model added as target")
  876. else:
  877. output("Model already selected as target")
  878. else:
  879. output("Model is not supported by RAMified metamodel!")
  880. else:
  881. output("No such model; try again")
  882. name = input()
  883. output("Name of new transformation?")
  884. name = input()
  885. if (get_model_id(name) == ""):
  886. String new_model
  887. // Finished with all information, now create the model itself!
  888. output("Waiting for model constructors...")
  889. new_model = construct_model_raw(get_full_model(ramified_metamodel_id))
  890. model_create(new_model, name, user_id, ramified_metamodel_id, "ModelTransformation")
  891. model_id = get_model_id(name)
  892. // Extend metadata with info on source and target
  893. String link
  894. String dst
  895. while (read_nr_out(source) > 0):
  896. dst = set_pop(source)
  897. link = instantiate_link(core, "transformInput", "", model_id, dst)
  898. instantiate_attribute(core, link, "name", read_attribute(core, dst, "name"))
  899. while (read_nr_out(target) > 0):
  900. dst = set_pop(target)
  901. link = instantiate_link(core, "transformOutput", "", model_id, dst)
  902. instantiate_attribute(core, link, "name", read_attribute(core, dst, "name"))
  903. else:
  904. output("Model already exists")
  905. else:
  906. output("Permission denied")
  907. else:
  908. output("No such model")
  909. elif (cmd == "transformation_list"):
  910. // List all models
  911. Element models
  912. String m
  913. String type
  914. models = allInstances(core, "Transformation")
  915. while (read_nr_out(models) > 0):
  916. m = set_pop(models)
  917. output(string_join(("[" + read_type(core, m)) + "]", string_join((string_join(" ", read_attribute(core, m, "name")) + " : "), read_attribute(core, set_pop(followAssociation(core, m, "instanceOf")), "name"))))
  918. elif (cmd == "transformation_list_full"):
  919. // List all models with full info
  920. Element models
  921. String m
  922. String permissions
  923. String owner
  924. String group
  925. String name
  926. String type
  927. models = allInstances(core, "Transformation")
  928. while (read_nr_out(models) > 0):
  929. m = set_pop(models)
  930. permissions = read_attribute(core, m, "permissions")
  931. owner = read_attribute(core, set_pop(allAssociationDestinations(core, m, "owner")), "name")
  932. group = read_attribute(core, set_pop(allAssociationDestinations(core, m, "group")), "name")
  933. name = read_attribute(core, m, "name")
  934. type = read_attribute(core, set_pop(allAssociationDestinations(core, m, "instanceOf")), "name")
  935. output(((((((((" " + permissions) + " ") + owner) + " ") + group) + " ") + ((("[" + read_type(core, m)) + "] ") + name)) + " : ") + type)
  936. elif (cmd == "permission_modify"):
  937. String permissions
  938. Integer permission
  939. String model_id
  940. output("Which model do you want to change permissions of?")
  941. model_id = get_model_id(input())
  942. if (model_id != ""):
  943. if (get_relation_to_model(user_id, model_id) == 0):
  944. output("New permissions?")
  945. permissions = input()
  946. Boolean fail
  947. Integer i
  948. i = 0
  949. if (string_len(permissions) != 3):
  950. fail = True
  951. while (i < 3):
  952. permission = cast_s2i(string_get(permissions, i))
  953. if (bool_or(permission < 0, permission > 2)):
  954. fail = True
  955. break!
  956. i = i + 1
  957. if (bool_not(fail)):
  958. unset_attribute(core, model_id, "permissions")
  959. instantiate_attribute(core, model_id, "permissions", permissions)
  960. else:
  961. output("Permissions must be a string of three characters with each character being a digit between 0 and 2")
  962. else:
  963. output("Permission denied!")
  964. else:
  965. output("No such model!")
  966. elif (cmd == "permission_owner"):
  967. String permissions
  968. String model_id
  969. String user_id
  970. output("Which model do you want to change owner of?")
  971. model_id = get_model_id(input())
  972. if (model_id != ""):
  973. if (bool_or(get_relation_to_model(user_id, model_id) == 0, is_admin(user_id))):
  974. output("New owner?")
  975. user_id = get_user_id(input())
  976. if (user_id != ""):
  977. model_delete_element(core, set_pop(allOutgoingAssociationInstances(core, model_id, "owner")))
  978. instantiate_link(core, "owner", "", model_id, user_id)
  979. else:
  980. output("No such user!")
  981. else:
  982. output("Permission denied!")
  983. else:
  984. output("No such model!")
  985. elif (cmd == "permission_group"):
  986. String permissions
  987. String model_id
  988. String group_id
  989. output("Which model do you want to change permissions of?")
  990. model_id = get_model_id(input())
  991. if (model_id != ""):
  992. if (bool_or(get_relation_to_model(user_id, model_id) == 0, is_admin(user_id))):
  993. output("New group?")
  994. group_id = get_group_id(input())
  995. if (group_id != ""):
  996. model_delete_element(core, set_pop(allOutgoingAssociationInstances(core, model_id, "group")))
  997. instantiate_link(core, "group", "", model_id, group_id)
  998. else:
  999. output("No such group!")
  1000. else:
  1001. output("Permission denied!")
  1002. else:
  1003. output("No such model!")
  1004. elif (cmd == "group_create"):
  1005. // Create a new group and become its owner
  1006. String group_id
  1007. String name
  1008. output("Which group do you want to create?")
  1009. name = input()
  1010. group_id = get_group_id(name)
  1011. if (group_id == ""):
  1012. group_id = instantiate_node(core, "Group", "")
  1013. instantiate_attribute(core, group_id, "name", name)
  1014. instantiate_link(core, "belongsTo", "", user_id, group_id)
  1015. instantiate_link(core, "owner", "", group_id, user_id)
  1016. output("Group created!")
  1017. else:
  1018. output("Group already exists")
  1019. elif (cmd == "group_delete"):
  1020. // Delete an existing group
  1021. String group_id
  1022. String name
  1023. output("Which group do you want to delete?")
  1024. name = input()
  1025. group_id = get_group_id(name)
  1026. if (group_id != ""):
  1027. if (allow_group_modify(user_id, group_id)):
  1028. model_delete_element(core, group_id)
  1029. else:
  1030. output("Permission denied")
  1031. else:
  1032. output("No such group")
  1033. elif (cmd == "group_owner_add"):
  1034. // Add an owner to your group
  1035. String group_id
  1036. String other_user_id
  1037. output("Which group do you want to add an owner to?")
  1038. group_id = get_group_id(input())
  1039. if (group_id != ""):
  1040. if (allow_group_modify(user_id, group_id)):
  1041. output("Which user do you want to make an owner?")
  1042. other_user_id = get_user_id(input())
  1043. if (other_user_id != ""):
  1044. Element overlap
  1045. overlap = set_overlap(allIncomingAssociationInstances(core, other_user_id, "owner"), allOutgoingAssociationInstances(core, group_id, "owner"))
  1046. if (read_nr_out(overlap) == 0):
  1047. instantiate_link(core, "owner", "", group_id, other_user_id)
  1048. overlap = set_overlap(allOutgoingAssociationInstances(core, other_user_id, "belongsTo"), allIncomingAssociationInstances(core, group_id, "belongsTo"))
  1049. if (read_nr_out(overlap) == 0):
  1050. instantiate_link(core, "belongsTo", "", other_user_id, group_id)
  1051. output("New owner added to group!")
  1052. else:
  1053. output("User is already an owner!")
  1054. else:
  1055. output("No such user")
  1056. else:
  1057. output("Permission denied!")
  1058. else:
  1059. output("No such group")
  1060. elif (cmd == "group_owner_delete"):
  1061. // Remove an owner from your group
  1062. String group_id
  1063. String other_user_id
  1064. output("Which group do you want to disown someone from?")
  1065. group_id = get_group_id(input())
  1066. if (group_id != ""):
  1067. if (allow_group_modify(user_id, group_id)):
  1068. output("Which user do you want to disown?")
  1069. other_user_id = get_user_id(input())
  1070. if (other_user_id != ""):
  1071. Element overlap
  1072. overlap = set_overlap(allOutgoingAssociationInstances(core, other_user_id, "belongsTo"), allIncomingAssociationInstances(core, group_id, "belongsTo"))
  1073. if (read_nr_out(overlap) > 0):
  1074. overlap = set_overlap(allIncomingAssociationInstances(core, other_user_id, "owner"), allOutgoingAssociationInstances(core, group_id, "owner"))
  1075. if (read_nr_out(overlap) > 0):
  1076. model_delete_element(core, set_pop(overlap))
  1077. output("Disowned group from user!")
  1078. else:
  1079. output("User is not even an owner of the group!")
  1080. else:
  1081. output("User is not even a member of the group!")
  1082. else:
  1083. output("No such user")
  1084. else:
  1085. output("Permission denied!")
  1086. else:
  1087. output("No such group")
  1088. elif (cmd == "group_join"):
  1089. // Add someone to your group
  1090. String group_id
  1091. String other_user_id
  1092. output("Which group do you want to add someone to?")
  1093. group_id = get_group_id(input())
  1094. if (group_id != ""):
  1095. if (allow_group_modify(user_id, group_id)):
  1096. output("Which user do you want to add?")
  1097. other_user_id = get_user_id(input())
  1098. if (other_user_id != ""):
  1099. Element overlap
  1100. overlap = set_overlap(allOutgoingAssociationInstances(core, other_user_id, "belongsTo"), allIncomingAssociationInstances(core, group_id, "belongsTo"))
  1101. if (read_nr_out(overlap) == 0):
  1102. instantiate_link(core, "belongsTo", "", other_user_id, group_id)
  1103. output("User added to the group!")
  1104. else:
  1105. output("User is already a member of the group!")
  1106. else:
  1107. output("No such user")
  1108. else:
  1109. output("Permission denied!")
  1110. else:
  1111. output("No such group")
  1112. elif (cmd == "group_kick"):
  1113. // Remove someone from your group
  1114. String group_id
  1115. String other_user_id
  1116. output("Which group do you want to kick someone from?")
  1117. group_id = get_group_id(input())
  1118. if (group_id != ""):
  1119. if (allow_group_modify(user_id, group_id)):
  1120. output("Which user do you want to kick?")
  1121. other_user_id = get_user_id(input())
  1122. if (other_user_id != ""):
  1123. Element overlap
  1124. overlap = set_overlap(allOutgoingAssociationInstances(core, other_user_id, "belongsTo"), allIncomingAssociationInstances(core, group_id, "belongsTo"))
  1125. if (read_nr_out(overlap) > 0):
  1126. model_delete_element(core, set_pop(overlap))
  1127. // Check if user was an owner as well
  1128. overlap = set_overlap(allIncomingAssociationInstances(core, other_user_id, "owner"), allOutgoingAssociationInstances(core, group_id, "owner"))
  1129. if (read_nr_out(overlap) > 0):
  1130. model_delete_element(core, set_pop(overlap))
  1131. output("User kicked!")
  1132. else:
  1133. output("User is not even a member of the group!")
  1134. else:
  1135. output("No such user")
  1136. else:
  1137. output("Permission denied!")
  1138. else:
  1139. output("No such group")
  1140. elif (cmd == "group_list"):
  1141. // List all groups you are a member of (and whether you are admin or not!)
  1142. Element groups
  1143. String group_id
  1144. String admin
  1145. groups = allAssociationDestinations(core, user_id, "belongsTo")
  1146. while (read_nr_out(groups) > 0):
  1147. group_id = set_pop(groups)
  1148. if (set_in(allOutgoingAssociationInstances(core, group_id, "owner"), user_id)):
  1149. admin = " A "
  1150. else:
  1151. admin = " "
  1152. output(string_join(admin, read_attribute(core, group_id, "name")))
  1153. elif (cmd == "admin_promote"):
  1154. // Promote a user to admin status
  1155. if (is_admin(user_id)):
  1156. String other_user_id
  1157. output("Which user do you want to promote?")
  1158. other_user_id = get_user_id(input())
  1159. if (other_user_id != ""):
  1160. unset_attribute(core, other_user_id, "admin")
  1161. instantiate_attribute(core, other_user_id, "admin", True)
  1162. output("Permissions granted!")
  1163. else:
  1164. output("No such user!")
  1165. else:
  1166. output("Permission denied!")
  1167. elif (cmd == "admin_demote"):
  1168. // Demote a user to normal status
  1169. if (is_admin(user_id)):
  1170. String other_user_id
  1171. output("Which user do you want to demote?")
  1172. other_user_id = get_user_id(input())
  1173. if (other_user_id != ""):
  1174. unset_attribute(core, other_user_id, "admin")
  1175. instantiate_attribute(core, other_user_id, "admin", False)
  1176. output("Permissions revoked!")
  1177. else:
  1178. output("No such user!")
  1179. else:
  1180. output("Permission denied!")
  1181. elif (cmd == "self-destruct"):
  1182. // Delete user from Core Formalism
  1183. model_delete_element(core, user_id)
  1184. return !
  1185. elif (cmd == "exit"):
  1186. // Exit by actually removing the user and decoupling it from all of its models
  1187. // Restarting with the same user name will NOT grant you access to anything of the previous user with that same name
  1188. // as the current user will have been deleted
  1189. return !
  1190. else:
  1191. output("Unknown command: " + cmd)
  1192. // We never get here!
  1193. return !