core_algorithm.alc 53 KB

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