core_algorithm.alc 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407
  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 == "model_overwrite"):
  587. // Overwrites an existing model without changing any metadata
  588. String model_id
  589. Element new_model
  590. output("Which model to overwrite?")
  591. model_id = get_model_id(input())
  592. if (model_id != ""):
  593. if (allow_write(user_id, model_id)):
  594. if (allow_read(user_id, set_pop(followAssociation(core, model_id, "instanceOf")))):
  595. output("Waiting for model constructors...")
  596. new_model = construct_model_raw(get_full_model(set_pop(followAssociation(core, model_id, "instanceOf"))))
  597. model_overwrite(new_model, model_id)
  598. output("Model overwrite success!")
  599. else:
  600. output("Permission denied")
  601. else:
  602. output("Permission denied")
  603. else:
  604. output("No such model")
  605. elif (cmd == "model_modify"):
  606. // Model modify operation, which uses the mini_modify.alc operations, though with extensions for access control
  607. String model_id
  608. String type_id
  609. output("Which model do you want to modify?")
  610. model_id = get_model_id(input())
  611. if (model_id != ""):
  612. if (allow_read(user_id, model_id)):
  613. type_id = set_pop(allAssociationDestinations(core, model_id, "instanceOf"))
  614. if (allow_read(user_id, type_id)):
  615. modify(get_full_model(model_id), allow_write(user_id, model_id))
  616. else:
  617. output("Permission denied")
  618. else:
  619. output("Permission denied")
  620. else:
  621. output("Could not find model!")
  622. elif (cmd == "model_delete"):
  623. // Delete a model and all of its related transformations
  624. String model_id
  625. output("=================================================")
  626. output("WARNING: Deletion is a very destructive operation")
  627. output(" as it also deletes all transformations ")
  628. output(" defined which make use of this model! ")
  629. output("=================================================")
  630. output("")
  631. output("Currently not supported!")
  632. elif (cmd == "model_list"):
  633. // List all models
  634. Element models
  635. String m
  636. models = allInstances(core, "Model")
  637. while (read_nr_out(models) > 0):
  638. m = set_pop(models)
  639. output(string_join((string_join(" ", read_attribute(core, m, "name")) + " : "), read_attribute(core, set_pop(followAssociation(core, m, "instanceOf")), "name")))
  640. elif (cmd == "model_list_full"):
  641. // List all models with full info
  642. Element models
  643. String m
  644. String permissions
  645. String owner
  646. String group
  647. String name
  648. String type
  649. models = allInstances(core, "Model")
  650. while (read_nr_out(models) > 0):
  651. m = set_pop(models)
  652. permissions = read_attribute(core, m, "permissions")
  653. owner = read_attribute(core, set_pop(allAssociationDestinations(core, m, "owner")), "name")
  654. group = read_attribute(core, set_pop(allAssociationDestinations(core, m, "group")), "name")
  655. name = read_attribute(core, m, "name")
  656. type = read_attribute(core, set_pop(allAssociationDestinations(core, m, "instanceOf")), "name")
  657. output(((((((((" " + permissions) + " ") + owner) + " ") + group) + " ") + name) + " : ") + type)
  658. elif (cmd == "transformation_add_MT_language"):
  659. // Create a model transformation language from a set of input and output formalisms
  660. String name
  661. String model_id
  662. Element source
  663. Element target
  664. Element all_formalisms
  665. Element merged_formalism
  666. Element ramified_formalism
  667. String old_type_id
  668. String type_id
  669. String location
  670. String new_model_id
  671. old_type_id = ""
  672. log("Adding MT language")
  673. // Read involved formalisms
  674. all_formalisms = create_node()
  675. output("Formalisms to include (terminate with empty string)?")
  676. name = input()
  677. while (name != ""):
  678. model_id = get_model_id(name)
  679. if (model_id != ""):
  680. if (allow_read(user_id, model_id)):
  681. type_id = set_pop(allAssociationDestinations(core, model_id, "instanceOf"))
  682. if (bool_or(old_type_id == "", type_id == old_type_id)):
  683. set_add(all_formalisms, create_tuple(name, get_full_model(model_id)))
  684. old_type_id = type_id
  685. elif (old_type_id != type_id):
  686. // Already have a previous type_id and now another: CLASH
  687. output("Cannot add model as types not compatible with previous models; try again")
  688. else:
  689. output("Model not readable; try again")
  690. else:
  691. output("No such model; try again")
  692. name = input()
  693. // Merge both into a single metamodel
  694. if (read_nr_out(all_formalisms) > 0):
  695. output("Name of the RAMified transformation metamodel?")
  696. name = input()
  697. if (get_model_id(name) == ""):
  698. String merged_formalism_id
  699. String ramified_formalism_id
  700. String source_formalism_id
  701. String tracability_link
  702. // New location is available, so write
  703. log("FUSE")
  704. merged_formalism = model_fuse(set_copy(all_formalisms))
  705. log("Fuse OK")
  706. model_create(merged_formalism, "__merged_" + name, user_id, type_id, "Model")
  707. merged_formalism_id = get_model_id("__merged_" + name)
  708. // Add tracability links at this level
  709. while (read_nr_out(all_formalisms) > 0):
  710. source_formalism_id = get_model_id(list_read(set_pop(all_formalisms), 0))
  711. tracability_link = instantiate_link(core, "tracability", "", merged_formalism_id, source_formalism_id)
  712. instantiate_attribute(core, tracability_link, "type", "merged")
  713. // Merge complete, now RAMify!
  714. log("RAM")
  715. ramified_formalism = ramify(merged_formalism)
  716. log("RAMed")
  717. model_create(ramified_formalism, name, user_id, type_id, "Model")
  718. ramified_formalism_id = get_model_id(name)
  719. // Add tracability link at this level
  720. tracability_link = instantiate_link(core, "tracability", "", ramified_formalism_id, merged_formalism_id)
  721. instantiate_attribute(core, tracability_link, "type", "RAMified")
  722. else:
  723. output("Model already exists!")
  724. else:
  725. output("At least one formalism is required")
  726. elif (cmd == "transformation_RAMify"):
  727. // RAMify a metamodel
  728. String merged_model_id
  729. String target_model_name
  730. String target_model_id
  731. Element target_model
  732. String tracability_link
  733. output("Which metamodel do you want to RAMify?")
  734. merged_model_id = get_model_id(input())
  735. if (merged_model_id != ""):
  736. if (allow_read(user_id, merged_model_id)):
  737. output("Where do you want to store the RAMified metamodel?")
  738. target_model_name = input()
  739. target_model_id = get_model_id(target_model_name)
  740. if (target_model_id == ""):
  741. // New model, so everything is fine
  742. target_model = ramify(get_full_model(merged_model_id))
  743. model_create(target_model, target_model_name, user_id, set_pop(allAssociationDestinations(core, merged_model_id, "instanceOf")), "Model")
  744. target_model_id = get_model_id(target_model_name)
  745. tracability_link = instantiate_link(core, "tracability", "", target_model_id, merged_model_id)
  746. instantiate_attribute(core, tracability_link, "type", "RAMified")
  747. else:
  748. // Existing model, so overwrite
  749. if (allow_write(user_id, target_model_id)):
  750. target_model = ramify(get_full_model(merged_model_id))
  751. model_overwrite(target_model, target_model_id)
  752. model_delete_element(core, set_pop(allOutgoingAssociationInstances(core, target_model_id, "tracability")))
  753. tracability_link = instantiate_link(core, "tracability", "", target_model_id, merged_model_id)
  754. instantiate_attribute(core, tracability_link, "type", "RAMified")
  755. else:
  756. output("Permission denied")
  757. else:
  758. output("Permission denied!")
  759. else:
  760. output("No such model")
  761. elif (cmd == "transformation_add_AL"):
  762. // Add an action language transformation model
  763. output("Which metamodels do you want to use as source for the action code (empty string to finish)?")
  764. String model_id
  765. Element models
  766. Element source
  767. Element target
  768. String name
  769. name = input()
  770. source = create_node()
  771. target = create_node()
  772. while (name != ""):
  773. model_id = get_model_id(name)
  774. if (model_id != ""):
  775. if (bool_not(set_in(source, model_id))):
  776. set_add(source, model_id)
  777. output("Model added as source")
  778. else:
  779. output("Model already selected as source")
  780. else:
  781. output("No such model; try again")
  782. name = input()
  783. output("Which metamodels do you want to use as target for the action code (empty string to finish)?")
  784. name = input()
  785. while (name != ""):
  786. model_id = get_model_id(name)
  787. if (model_id != ""):
  788. if (bool_not(set_in(target, model_id))):
  789. set_add(target, model_id)
  790. output("Model added as target")
  791. else:
  792. output("Model already selected as target")
  793. else:
  794. output("No such model; try again")
  795. name = input()
  796. output("Name of Action Language model?")
  797. name = input()
  798. log("Add AL is fine!")
  799. if (get_model_id(name) == ""):
  800. // Finished with all information, now create the model itself!
  801. output("Waiting for model constructors...")
  802. log("AL model: " + cast_e2s(get_model_id("ActionLanguage")))
  803. log("Location: " + cast_e2s(read_attribute(core, get_model_id("ActionLanguage"), "location")))
  804. log("Imported: " + cast_e2s(import_node(read_attribute(core, get_model_id("ActionLanguage"), "location"))))
  805. add_code_model(get_full_model(get_model_id("ActionLanguage")), "AL/" + name, construct_function())
  806. log("Exported to " + cast_e2s(import_node("AL/" + name)))
  807. model_create(import_node("AL/" + name), name, user_id, get_model_id("ActionLanguage"), "ActionLanguage")
  808. model_id = get_model_id(name)
  809. // Extend metadata with info on source and target
  810. String link
  811. String dst
  812. log("Adding inputs")
  813. while (read_nr_out(source) > 0):
  814. dst = set_pop(source)
  815. log(" for " + dst)
  816. link = instantiate_link(core, "transformInput", "", model_id, dst)
  817. instantiate_attribute(core, link, "name", read_attribute(core, dst, "name"))
  818. log("Adding outputs")
  819. while (read_nr_out(target) > 0):
  820. dst = set_pop(target)
  821. log(" for " + dst)
  822. link = instantiate_link(core, "transformOutput", "", model_id, dst)
  823. instantiate_attribute(core, link, "name", read_attribute(core, dst, "name"))
  824. else:
  825. output("Model already exists")
  826. elif (cmd == "transformation_add_MT"):
  827. // Add a model transformation model
  828. // Just a usual model instantiation, but need to add the source and target links based on user info
  829. String ramified_metamodel_id
  830. String model_id
  831. Element models
  832. Element links
  833. String link_id
  834. Element supported
  835. Element source
  836. Element target
  837. String name
  838. String new_model_id
  839. String merged_link_id
  840. Element links_merged
  841. String merged_metamodel_id
  842. source = create_node()
  843. target = create_node()
  844. supported = create_node()
  845. output("RAMified metamodel to use?")
  846. ramified_metamodel_id = get_model_id(input())
  847. if (ramified_metamodel_id != ""):
  848. if (allow_read(user_id, ramified_metamodel_id)):
  849. output("Supported metamodels:")
  850. links = allOutgoingAssociationInstances(core, ramified_metamodel_id, "tracability")
  851. while (read_nr_out(links) > 0):
  852. link_id = set_pop(links)
  853. merged_metamodel_id = readAssociationDestination(core, link_id)
  854. if (value_eq(read_attribute(core, link_id, "type"), "RAMified")):
  855. links_merged = allOutgoingAssociationInstances(core, merged_metamodel_id, "tracability")
  856. while (read_nr_out(links_merged) > 0):
  857. merged_link_id = set_pop(links_merged)
  858. if (value_eq(read_attribute(core, merged_link_id, "type"), "merged")):
  859. output(string_join(" ", read_attribute(core, readAssociationDestination(core, merged_link_id), "name")))
  860. set_add(supported, readAssociationDestination(core, merged_link_id))
  861. output("")
  862. output("Which ones do you want to use as source (empty string to finish)?")
  863. name = input()
  864. while (name != ""):
  865. model_id = get_model_id(name)
  866. if (model_id != ""):
  867. if (set_in(supported, model_id)):
  868. if (bool_not(set_in(source, model_id))):
  869. set_add(source, model_id)
  870. output("Model added as source")
  871. else:
  872. output("Model already selected as source")
  873. else:
  874. output("Model is not supported by RAMified metamodel!")
  875. else:
  876. output("No such model; try again")
  877. name = input()
  878. output("Which ones do you want to use as target (empty string to finish)?")
  879. name = input()
  880. while (name != ""):
  881. model_id = get_model_id(name)
  882. if (model_id != ""):
  883. if (set_in(supported, model_id)):
  884. if (bool_not(set_in(target, model_id))):
  885. set_add(target, model_id)
  886. output("Model added as target")
  887. else:
  888. output("Model already selected as target")
  889. else:
  890. output("Model is not supported by RAMified metamodel!")
  891. else:
  892. output("No such model; try again")
  893. name = input()
  894. output("Name of new transformation?")
  895. name = input()
  896. if (get_model_id(name) == ""):
  897. String new_model
  898. // Finished with all information, now create the model itself!
  899. output("Waiting for model constructors...")
  900. new_model = construct_model_raw(get_full_model(ramified_metamodel_id))
  901. model_create(new_model, name, user_id, ramified_metamodel_id, "ModelTransformation")
  902. model_id = get_model_id(name)
  903. // Extend metadata with info on source and target
  904. String link
  905. String dst
  906. while (read_nr_out(source) > 0):
  907. dst = set_pop(source)
  908. link = instantiate_link(core, "transformInput", "", model_id, dst)
  909. instantiate_attribute(core, link, "name", read_attribute(core, dst, "name"))
  910. while (read_nr_out(target) > 0):
  911. dst = set_pop(target)
  912. link = instantiate_link(core, "transformOutput", "", model_id, dst)
  913. instantiate_attribute(core, link, "name", read_attribute(core, dst, "name"))
  914. else:
  915. output("Model already exists")
  916. else:
  917. output("Permission denied")
  918. else:
  919. output("No such model")
  920. elif (cmd == "transformation_list"):
  921. // List all models
  922. Element models
  923. String m
  924. String type
  925. models = allInstances(core, "Transformation")
  926. while (read_nr_out(models) > 0):
  927. m = set_pop(models)
  928. 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"))))
  929. elif (cmd == "transformation_list_full"):
  930. // List all models with full info
  931. Element models
  932. String m
  933. String permissions
  934. String owner
  935. String group
  936. String name
  937. String type
  938. models = allInstances(core, "Transformation")
  939. while (read_nr_out(models) > 0):
  940. m = set_pop(models)
  941. permissions = read_attribute(core, m, "permissions")
  942. owner = read_attribute(core, set_pop(allAssociationDestinations(core, m, "owner")), "name")
  943. group = read_attribute(core, set_pop(allAssociationDestinations(core, m, "group")), "name")
  944. name = read_attribute(core, m, "name")
  945. type = read_attribute(core, set_pop(allAssociationDestinations(core, m, "instanceOf")), "name")
  946. output(((((((((" " + permissions) + " ") + owner) + " ") + group) + " ") + ((("[" + read_type(core, m)) + "] ") + name)) + " : ") + type)
  947. elif (cmd == "permission_modify"):
  948. String permissions
  949. Integer permission
  950. String model_id
  951. output("Which model do you want to change permissions of?")
  952. model_id = get_model_id(input())
  953. if (model_id != ""):
  954. if (get_relation_to_model(user_id, model_id) == 0):
  955. output("New permissions?")
  956. permissions = input()
  957. Boolean fail
  958. Integer i
  959. i = 0
  960. if (string_len(permissions) != 3):
  961. fail = True
  962. while (i < 3):
  963. permission = cast_s2i(string_get(permissions, i))
  964. if (bool_or(permission < 0, permission > 2)):
  965. fail = True
  966. break!
  967. i = i + 1
  968. if (bool_not(fail)):
  969. unset_attribute(core, model_id, "permissions")
  970. instantiate_attribute(core, model_id, "permissions", permissions)
  971. else:
  972. output("Permissions must be a string of three characters with each character being a digit between 0 and 2")
  973. else:
  974. output("Permission denied!")
  975. else:
  976. output("No such model!")
  977. elif (cmd == "permission_owner"):
  978. String permissions
  979. String model_id
  980. String user_id
  981. output("Which model do you want to change owner of?")
  982. model_id = get_model_id(input())
  983. if (model_id != ""):
  984. if (bool_or(get_relation_to_model(user_id, model_id) == 0, is_admin(user_id))):
  985. output("New owner?")
  986. user_id = get_user_id(input())
  987. if (user_id != ""):
  988. model_delete_element(core, set_pop(allOutgoingAssociationInstances(core, model_id, "owner")))
  989. instantiate_link(core, "owner", "", model_id, user_id)
  990. else:
  991. output("No such user!")
  992. else:
  993. output("Permission denied!")
  994. else:
  995. output("No such model!")
  996. elif (cmd == "permission_group"):
  997. String permissions
  998. String model_id
  999. String group_id
  1000. output("Which model do you want to change permissions of?")
  1001. model_id = get_model_id(input())
  1002. if (model_id != ""):
  1003. if (bool_or(get_relation_to_model(user_id, model_id) == 0, is_admin(user_id))):
  1004. output("New group?")
  1005. group_id = get_group_id(input())
  1006. if (group_id != ""):
  1007. model_delete_element(core, set_pop(allOutgoingAssociationInstances(core, model_id, "group")))
  1008. instantiate_link(core, "group", "", model_id, group_id)
  1009. else:
  1010. output("No such group!")
  1011. else:
  1012. output("Permission denied!")
  1013. else:
  1014. output("No such model!")
  1015. elif (cmd == "group_create"):
  1016. // Create a new group and become its owner
  1017. String group_id
  1018. String name
  1019. output("Which group do you want to create?")
  1020. name = input()
  1021. group_id = get_group_id(name)
  1022. if (group_id == ""):
  1023. group_id = instantiate_node(core, "Group", "")
  1024. instantiate_attribute(core, group_id, "name", name)
  1025. instantiate_link(core, "belongsTo", "", user_id, group_id)
  1026. instantiate_link(core, "owner", "", group_id, user_id)
  1027. output("Group created!")
  1028. else:
  1029. output("Group already exists")
  1030. elif (cmd == "group_delete"):
  1031. // Delete an existing group
  1032. String group_id
  1033. String name
  1034. output("Which group do you want to delete?")
  1035. name = input()
  1036. group_id = get_group_id(name)
  1037. if (group_id != ""):
  1038. if (allow_group_modify(user_id, group_id)):
  1039. model_delete_element(core, group_id)
  1040. else:
  1041. output("Permission denied")
  1042. else:
  1043. output("No such group")
  1044. elif (cmd == "group_owner_add"):
  1045. // Add an owner to your group
  1046. String group_id
  1047. String other_user_id
  1048. output("Which group do you want to add an owner to?")
  1049. group_id = get_group_id(input())
  1050. if (group_id != ""):
  1051. if (allow_group_modify(user_id, group_id)):
  1052. output("Which user do you want to make an owner?")
  1053. other_user_id = get_user_id(input())
  1054. if (other_user_id != ""):
  1055. Element overlap
  1056. overlap = set_overlap(allIncomingAssociationInstances(core, other_user_id, "owner"), allOutgoingAssociationInstances(core, group_id, "owner"))
  1057. if (read_nr_out(overlap) == 0):
  1058. instantiate_link(core, "owner", "", group_id, other_user_id)
  1059. overlap = set_overlap(allOutgoingAssociationInstances(core, other_user_id, "belongsTo"), allIncomingAssociationInstances(core, group_id, "belongsTo"))
  1060. if (read_nr_out(overlap) == 0):
  1061. instantiate_link(core, "belongsTo", "", other_user_id, group_id)
  1062. output("New owner added to group!")
  1063. else:
  1064. output("User is already an owner!")
  1065. else:
  1066. output("No such user")
  1067. else:
  1068. output("Permission denied!")
  1069. else:
  1070. output("No such group")
  1071. elif (cmd == "group_owner_delete"):
  1072. // Remove an owner from your group
  1073. String group_id
  1074. String other_user_id
  1075. output("Which group do you want to disown someone from?")
  1076. group_id = get_group_id(input())
  1077. if (group_id != ""):
  1078. if (allow_group_modify(user_id, group_id)):
  1079. output("Which user do you want to disown?")
  1080. other_user_id = get_user_id(input())
  1081. if (other_user_id != ""):
  1082. Element overlap
  1083. overlap = set_overlap(allOutgoingAssociationInstances(core, other_user_id, "belongsTo"), allIncomingAssociationInstances(core, group_id, "belongsTo"))
  1084. if (read_nr_out(overlap) > 0):
  1085. overlap = set_overlap(allIncomingAssociationInstances(core, other_user_id, "owner"), allOutgoingAssociationInstances(core, group_id, "owner"))
  1086. if (read_nr_out(overlap) > 0):
  1087. model_delete_element(core, set_pop(overlap))
  1088. output("Disowned group from user!")
  1089. else:
  1090. output("User is not even an owner of the group!")
  1091. else:
  1092. output("User is not even a member of the group!")
  1093. else:
  1094. output("No such user")
  1095. else:
  1096. output("Permission denied!")
  1097. else:
  1098. output("No such group")
  1099. elif (cmd == "group_join"):
  1100. // Add someone to your group
  1101. String group_id
  1102. String other_user_id
  1103. output("Which group do you want to add someone to?")
  1104. group_id = get_group_id(input())
  1105. if (group_id != ""):
  1106. if (allow_group_modify(user_id, group_id)):
  1107. output("Which user do you want to add?")
  1108. other_user_id = get_user_id(input())
  1109. if (other_user_id != ""):
  1110. Element overlap
  1111. overlap = set_overlap(allOutgoingAssociationInstances(core, other_user_id, "belongsTo"), allIncomingAssociationInstances(core, group_id, "belongsTo"))
  1112. if (read_nr_out(overlap) == 0):
  1113. instantiate_link(core, "belongsTo", "", other_user_id, group_id)
  1114. output("User added to the group!")
  1115. else:
  1116. output("User is already a member of the group!")
  1117. else:
  1118. output("No such user")
  1119. else:
  1120. output("Permission denied!")
  1121. else:
  1122. output("No such group")
  1123. elif (cmd == "group_kick"):
  1124. // Remove someone from your group
  1125. String group_id
  1126. String other_user_id
  1127. output("Which group do you want to kick someone from?")
  1128. group_id = get_group_id(input())
  1129. if (group_id != ""):
  1130. if (allow_group_modify(user_id, group_id)):
  1131. output("Which user do you want to kick?")
  1132. other_user_id = get_user_id(input())
  1133. if (other_user_id != ""):
  1134. Element overlap
  1135. overlap = set_overlap(allOutgoingAssociationInstances(core, other_user_id, "belongsTo"), allIncomingAssociationInstances(core, group_id, "belongsTo"))
  1136. if (read_nr_out(overlap) > 0):
  1137. model_delete_element(core, set_pop(overlap))
  1138. // Check if user was an owner as well
  1139. overlap = set_overlap(allIncomingAssociationInstances(core, other_user_id, "owner"), allOutgoingAssociationInstances(core, group_id, "owner"))
  1140. if (read_nr_out(overlap) > 0):
  1141. model_delete_element(core, set_pop(overlap))
  1142. output("User kicked!")
  1143. else:
  1144. output("User is not even a member of the group!")
  1145. else:
  1146. output("No such user")
  1147. else:
  1148. output("Permission denied!")
  1149. else:
  1150. output("No such group")
  1151. elif (cmd == "group_list"):
  1152. // List all groups you are a member of (and whether you are admin or not!)
  1153. Element groups
  1154. String group_id
  1155. String admin
  1156. groups = allAssociationDestinations(core, user_id, "belongsTo")
  1157. while (read_nr_out(groups) > 0):
  1158. group_id = set_pop(groups)
  1159. if (set_in(allOutgoingAssociationInstances(core, group_id, "owner"), user_id)):
  1160. admin = " A "
  1161. else:
  1162. admin = " "
  1163. output(string_join(admin, read_attribute(core, group_id, "name")))
  1164. elif (cmd == "admin_promote"):
  1165. // Promote a user to admin status
  1166. if (is_admin(user_id)):
  1167. String other_user_id
  1168. output("Which user do you want to promote?")
  1169. other_user_id = get_user_id(input())
  1170. if (other_user_id != ""):
  1171. unset_attribute(core, other_user_id, "admin")
  1172. instantiate_attribute(core, other_user_id, "admin", True)
  1173. output("Permissions granted!")
  1174. else:
  1175. output("No such user!")
  1176. else:
  1177. output("Permission denied!")
  1178. elif (cmd == "admin_demote"):
  1179. // Demote a user to normal status
  1180. if (is_admin(user_id)):
  1181. String other_user_id
  1182. output("Which user do you want to demote?")
  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", False)
  1187. output("Permissions revoked!")
  1188. else:
  1189. output("No such user!")
  1190. else:
  1191. output("Permission denied!")
  1192. elif (cmd == "self-destruct"):
  1193. // Delete user from Core Formalism
  1194. model_delete_element(core, user_id)
  1195. return !
  1196. elif (cmd == "exit"):
  1197. // Exit by actually removing the user and decoupling it from all of its models
  1198. // Restarting with the same user name will NOT grant you access to anything of the previous user with that same name
  1199. // as the current user will have been deleted
  1200. return !
  1201. else:
  1202. output("Unknown command: " + cmd)
  1203. // We never get here!
  1204. return !