core_algorithm.alc 49 KB

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