core_algorithm.alc 50 KB

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