core_algorithm.alc 50 KB

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