core_algorithm.alc 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072
  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. Element core = ?
  12. Void function main():
  13. // Initialize the Core Formalism
  14. String core_location
  15. String core_model
  16. String core_model_location
  17. String core_formalism_model
  18. String scd_location
  19. String scd_model
  20. String admin_group
  21. String admin_user
  22. scd_location = "models/SimpleClassDiagrams"
  23. core_location = "models/CoreFormalism"
  24. core_model_location = "models/core"
  25. // Create the Model itself and make public
  26. core = instantiate_model(import_node(core_location))
  27. export_node(core_model_location, core)
  28. // Create admin group
  29. admin_group = instantiate_node(core, "Group", "")
  30. instantiate_attribute(core, admin_group, "name", "admin")
  31. // Create admin user
  32. admin_user = instantiate_node(core, "User", "")
  33. output("Desired username for admin user?")
  34. instantiate_attribute(core, admin_user, "name", input())
  35. instantiate_attribute(core, admin_user, "admin", True)
  36. // Create link between admin user and group
  37. instantiate_link(core, "ownedBy", "", admin_group, admin_user)
  38. instantiate_link(core, "belongsTo", "", admin_user, admin_group)
  39. // Add the SimpleClassDiagrams formalism already
  40. scd_model = instantiate_node(core, "Model", "")
  41. instantiate_attribute(core, scd_model, "name", "SimpleClassDiagrams")
  42. instantiate_attribute(core, scd_model, "location", scd_location)
  43. instantiate_attribute(core, scd_model, "permissions", "221")
  44. instantiate_link(core, "instanceOf", "", scd_model, scd_model)
  45. // Make necessary links for the formalism to the owners
  46. instantiate_link(core, "group", "", scd_model, admin_group)
  47. instantiate_link(core, "owner", "", scd_model, admin_user)
  48. // Add the core formalism already
  49. core_formalism_model = instantiate_node(core, "Model", "")
  50. instantiate_attribute(core, core_formalism_model, "name", "CoreFormalism")
  51. instantiate_attribute(core, core_formalism_model, "location", core_location)
  52. instantiate_attribute(core, core_formalism_model, "permissions", "221")
  53. instantiate_link(core, "instanceOf", "", core_formalism_model, scd_model)
  54. // Make necessary links for the formalism to the owners
  55. instantiate_link(core, "group", "", core_formalism_model, admin_group)
  56. instantiate_link(core, "owner", "", core_formalism_model, admin_user)
  57. // Add the core model
  58. core_model = instantiate_node(core, "Model", "")
  59. instantiate_attribute(core, core_model, "name", "core")
  60. instantiate_attribute(core, core_model, "location", core_model_location)
  61. instantiate_attribute(core, core_model, "permissions", "200")
  62. instantiate_link(core, "instanceOf", "", core_model, core_formalism_model)
  63. // Make necessary links for the formalism to the owners
  64. instantiate_link(core, "group", "", core_model, admin_group)
  65. instantiate_link(core, "owner", "", core_model, admin_user)
  66. // Switch all new users to the user_function
  67. // This accesses the bootstrap level, so do not change this unless you know what you are doing
  68. Element root
  69. root = read_root()
  70. dict_delete(root["__hierarchy"], "__IP")
  71. dict_add(root["__hierarchy"], "__IP", user_function)
  72. // Call this for ourselves as well
  73. user_function_skip_init(admin_user)
  74. // Done, so finish up
  75. // Admin user will have been deleted by the user_function as usual
  76. // Note that if there are no admin users left, it will be very difficult to manage, as nobody will have admin permissions!
  77. return !
  78. Integer function get_relation_to_model(user_id : String, model_id : String):
  79. if (set_in(allAssociationDestinations(core, model_id, "owner"), user_id)):
  80. // We are the owner
  81. return 0!
  82. else:
  83. String group_id
  84. group_id = set_pop(allAssociationDestinations(core, model_id, "group"))
  85. if (set_in(allAssociationDestinations(core, user_id, "belongsTo"), group_id)):
  86. // We are in the owning group
  87. return 1!
  88. else:
  89. // We are not related whatsoever
  90. return 2!
  91. Boolean function is_admin(user_id : String):
  92. if (read_attribute(core, user_id, "admin")):
  93. return True!
  94. else:
  95. return False!
  96. Boolean function allow_read(user_id : String, model_id : String):
  97. if (is_admin(user_id)):
  98. // Is admin, so always allow
  99. return True!
  100. else:
  101. // Check permissions
  102. String permission
  103. permission = string_get(read_attribute(core, model_id, "permissions"), get_relation_to_model(user_id, model_id))
  104. if (bool_or(permission == "1", permission == "2")):
  105. return True!
  106. else:
  107. return False!
  108. Boolean function allow_write(user_id : String, model_id : String):
  109. if (is_admin(user_id)):
  110. // Is admin, so always allow
  111. return True!
  112. else:
  113. // Check permissions
  114. String permission
  115. permission = string_get(read_attribute(core, model_id, "permissions"), get_relation_to_model(user_id, model_id))
  116. if (permission == "2"):
  117. return True!
  118. else:
  119. return False!
  120. Boolean function allow_change_metadata(user_id : String, model_id : String):
  121. if (is_admin(user_id)):
  122. // Is admin, so always allow
  123. return True!
  124. else:
  125. if (get_relation_to_model(user_id, model_id) == 0):
  126. // Only owner can chmod
  127. return True!
  128. else:
  129. return False!
  130. Boolean function allow_group_modify(user_id : String, group_id : String):
  131. if (is_admin(user_id)):
  132. // Is admin, so always allow
  133. return True!
  134. else:
  135. if (read_nr_out(set_overlap(allIncomingAssociationInstances(core, user_id, "owner"), allOutgoingAssociationInstances(core, group_id, "owner"))) > 0):
  136. // We are an owner
  137. return True!
  138. else:
  139. return False!
  140. Boolean function check_login(user_id : String):
  141. // TODO
  142. return False!
  143. Element function user_function():
  144. String username
  145. String user_id
  146. output("Log on as which user?")
  147. output("non-existing user will be created")
  148. username = input()
  149. user_id = get_user_id(username)
  150. if (user_id == ""):
  151. // New user
  152. // Add user to Core Formalism
  153. user_id = instantiate_node(core, "User", "")
  154. instantiate_attribute(core, user_id, "name", username)
  155. instantiate_attribute(core, user_id, "admin", False)
  156. // Now call with user created
  157. user_function_skip_init(user_id)
  158. else:
  159. if (check_login(user_id)):
  160. user_function_skip_init(user_id)
  161. // User destroyed already, so just stop execution
  162. return create_node()!
  163. String function get_model_id(name : String):
  164. Element models
  165. String model
  166. models = allInstances(core, "Model")
  167. while (read_nr_out(models) > 0):
  168. model = set_pop(models)
  169. if (value_eq(name, read_attribute(core, model, "name"))):
  170. return model!
  171. return ""!
  172. String function get_user_id(name : String):
  173. Element users
  174. String user
  175. users = allInstances(core, "User")
  176. while (read_nr_out(users) > 0):
  177. user = set_pop(users)
  178. if (value_eq(read_attribute(core, user, "name"), name)):
  179. return user!
  180. return ""!
  181. String function get_group_id(name : String):
  182. Element groups
  183. String group
  184. groups = allInstances(core, "Group")
  185. while (read_nr_out(groups) > 0):
  186. group = set_pop(groups)
  187. if (value_eq(read_attribute(core, group, "name"), name)):
  188. return group!
  189. return ""!
  190. Void function model_create(model : Element, name : String, user_id : String, type_id : String):
  191. String location
  192. String model_id
  193. location = "/models/" + cast_id2s(model)
  194. export_node(location, model)
  195. // Manage meta-info
  196. model_id = instantiate_node(core, "Model", "")
  197. instantiate_attribute(core, model_id, "name", name)
  198. instantiate_attribute(core, model_id, "location", location)
  199. instantiate_attribute(core, model_id, "permissions", "200")
  200. instantiate_link(core, "owner", "", model_id, user_id)
  201. instantiate_link(core, "instanceOf", "", model_id, type_id)
  202. return!
  203. Void function model_overwrite(model : Element, name : String):
  204. String location
  205. String model_id
  206. location = "/models/" + cast_id2s(model)
  207. export_node(location, model)
  208. model_id = get_model_id(name)
  209. // Change location in meta-data
  210. unset_attribute(core, model_id, "location")
  211. instantiate_attribute(core, model_id, "location", location)
  212. return!
  213. Void function user_function_skip_init(user_id : String):
  214. Boolean do_continue
  215. String cmd
  216. do_continue = True
  217. output("Welcome to the Model Management Interface v2.0!")
  218. output("Use the 'help' command for a list of possible commands")
  219. while (do_continue):
  220. output("Ready for command...")
  221. cmd = input()
  222. if (cmd == "help"):
  223. output("Model operations")
  224. output(" model_add -- Add a new model")
  225. output(" model_modify -- Modify an existing model")
  226. output(" model_delete -- [TODO] Delete a model and all related transformations")
  227. output(" model_list -- List all models")
  228. output(" model_list_full -- List all models with full info")
  229. output(" model_overwrite -- Overwrites a model with an uploaded model, leaving all metadata")
  230. output("")
  231. output("Transformation-specific operations")
  232. output(" transformation_add_MT_language -- Create a RAMified metamodel")
  233. output(" transformation_reRAMify -- RAMify a merged metamodel again")
  234. output(" transformation_add_MT -- Initialize a new model transformation")
  235. output(" transformation_add_AL -- [TODO] Initialize a new action language transformation")
  236. output(" transformation_add_EXT -- [TODO] Initialize a new external tool transformation")
  237. output(" transformation_execute -- Execute a transformation on a set of input models")
  238. output("")
  239. output("Model permission operations")
  240. output(" permission_modify -- Change model permissions")
  241. output(" permission_owner -- Change model owner")
  242. output(" permission_group -- Change model group")
  243. output("")
  244. output("Group operations")
  245. output(" group_create -- Create a group")
  246. output(" group_delete -- Delete a group")
  247. output(" group_owner_add -- Add group owner")
  248. output(" group_owner_delete -- Remove group owner")
  249. output(" group_join -- Add someone to your group")
  250. output(" group_kick -- Kick someone from your group")
  251. output(" group_list -- List all groups you are a member of")
  252. output("")
  253. output("Admin operations")
  254. output(" admin_promote -- Promote a user to admin status")
  255. output(" admin_demote -- Demote a user to normal status")
  256. output("")
  257. output("General operations")
  258. output(" account_delete -- Remove current user and revoke all permissions ")
  259. elif (cmd == "model_add"):
  260. // Model addition operation, which uses model upload commands of the compiler
  261. String name
  262. String type
  263. String location
  264. String type_id
  265. Element new_model
  266. String new_model_id
  267. output("Creating new model!")
  268. output("Model type?")
  269. type_id = get_model_id(input())
  270. if (type_id != ""):
  271. // Type exists
  272. if (allow_read(user_id, type_id)):
  273. // And is readable
  274. output("Model name?")
  275. name = input()
  276. if (get_model_id(name) == ""):
  277. // Model doesn't exist yet
  278. output("Waiting for model constructors...")
  279. // TODO Update construct_model call to this interface
  280. new_model = construct_model_raw(import_node(read_attribute(core, type_id, "location")))
  281. model_create(new_model, name, user_id, type_id)
  282. output("Model upload success!")
  283. else:
  284. output("Model with that name already exists!")
  285. else:
  286. output("You are not allowed to read this type model!")
  287. else:
  288. output("Could not find type model!")
  289. elif (cmd == "transformation_execute"):
  290. // Execute a transformation, whatever type it is
  291. // First we detect the type, so we know how to prepare for invocation
  292. String transformation_id
  293. String exact_type
  294. Element sources
  295. Element targets
  296. String source
  297. String target
  298. String name_id
  299. Element inputs
  300. Element outputs
  301. Element trace_links
  302. output("Which transformation do you want to execute?")
  303. transformation_id = get_model_id(input())
  304. if (transformation_id != ""):
  305. if (allow_read(user_id, transformation_id)):
  306. if (is_nominal_instance(core, transformation_id, "Transformation")):
  307. // Read out source and target links
  308. sources = allOutgoingAssociationInstances(core, transformation_id, "transformInput")
  309. inputs = create_node()
  310. while (read_nr_out(source) > 0):
  311. source = set_pop(sources)
  312. output(string_join("Which model to bind for source element ", read_attribute(core, source, "name")))
  313. name_id = get_model_id(input())
  314. if (name_id != ""):
  315. if (allow_read(user_id, name_id)):
  316. dict_add(inputs, read_attribute(core, source, "name"), name_id)
  317. else:
  318. output("Permission denied")
  319. set_add(sources, source)
  320. else:
  321. output("No such model")
  322. set_add(sources, source)
  323. targets = allOutgoingAssociationInstances(core, transformation_id, "transformOutput")
  324. outputs = create_node()
  325. while (read_nr_out(targets) > 0):
  326. target = set_pop(targets)
  327. output(string_join("Which model to create for target element ", read_attribute(core, target, "name")))
  328. name_id = get_model_id(input())
  329. dict_add(outputs, read_attribute(core, target, "name"), name_id)
  330. exact_type = read_type(core, transformation_id)
  331. if (exact_type == "ModelTransformation"):
  332. // Model transformation is always in-place and uses only a single metamodel
  333. // Therefore, we must:
  334. // 1) Create an empty model, instance of merged metamodel
  335. // 2) Merge the different source models and retype
  336. // 3) Perform the transformation on the merged model
  337. // 4) Split the resulting model based on the target formalisms
  338. //
  339. // There is one exception: if the target model is bound to a source model, that model is overwritten
  340. // This allows for some optimizations when it is a simple in-place transformation (skip model copy, join, and split)
  341. // First check for this exception, as it is much faster
  342. Element input_model
  343. Element schedule_model
  344. String trace_link_id
  345. Element merged_model
  346. String merged_metamodel_id
  347. String ramified_metamodel_id
  348. schedule_model = import_node(read_attribute(core, transformation_id, "location"))
  349. if (bool_and(bool_and(read_nr_out(inputs) == 1, read_nr_out(outputs) == 1), set_equality(inputs, outputs))):
  350. // inputs and outputs have the same values and there is only one: keep in-place without additional bookkeeping
  351. input_model = import_node(read_attribute(core, set_pop(inputs), "location"))
  352. transform(input_model, schedule_model)
  353. else:
  354. // Need to fall back to the default approach, which is way slower
  355. // 1) Create empty instance of merged metamodel
  356. ramified_metamodel_id = set_pop(followAssociation(core, transformation_id, "instanceOf"))
  357. trace_links = allOutgoingAssociationInstances(core, ramified_metamodel_id, "tracability")
  358. merged_metamodel_id = ""
  359. while (read_nr_out(trace_links) > 0):
  360. trace_link_id = set_pop(trace_links)
  361. if (value_eq(read_attribute(core, trace_link_id, "type"), "ramified")):
  362. merged_metamodel_id = readAssociationDestination(core, trace_link_id)
  363. if (merged_metamodel_id != ""):
  364. merged_model = instantiate_model(import_node(read_attribute(core, merged_metamodel_id, "location")))
  365. // 2) Merge source models
  366. String key
  367. Element keys
  368. Element input_keys
  369. Element output_keys
  370. input_keys = dict_keys(inputs)
  371. while (read_nr_out(input_keys) > 0):
  372. key = set_pop(input_keys)
  373. model_join(merged_model, import_node(read_attribute(core, inputs[key], "location")), key)
  374. // 3) Transform
  375. transform(merged_model, schedule_model)
  376. // 4) Split in different files depending on type
  377. String desired_metamodel_id
  378. Element split_off_model
  379. output_keys = dict_keys(outputs)
  380. while (read_nr_out(output_keys) > 0):
  381. key = set_pop(output_keys)
  382. desired_metamodel_id = set_pop(followAssociation(core, outputs[key], "instanceOf"))
  383. split_off_model = model_split(merged_model, import_node(read_attribute(core, desired_metamodel_id, "location")), key)
  384. // Check if the destination model already exists
  385. if (get_model_id(outputs[key]) == ""):
  386. // New model
  387. model_create(split_off_model, outputs[key], user_id, desired_metamodel_id)
  388. else:
  389. // Model exists, so we overwrite
  390. model_overwrite(split_off_model, outputs[key])
  391. else:
  392. output("Could not resolve intermediate merged metamodel")
  393. elif (exact_type == "ActionLanguage"):
  394. output("Not Implemented yet!")
  395. else:
  396. output("Did not know how to interpret model of type " + exact_type)
  397. else:
  398. output("Model is not an executable transformation")
  399. else:
  400. output("Permission denied")
  401. else:
  402. output("No such transformation")
  403. elif (cmd == "model_overwrite"):
  404. // Overwrites an existing model without changing any metadata
  405. String model_id
  406. Element new_model
  407. output("Which model to overwrite?")
  408. model_id = get_model_id(input())
  409. if (model_id == ""):
  410. if (allow_write(user_id, model_id)):
  411. if (allow_read(user_id, set_pop(followAssociation(core, model_id, "instanceOf")))):
  412. output("Waiting for model constructors...")
  413. new_model = construct_model_raw(import_node(read_attribute(core, set_pop(followAssociation(core, model_id, "instanceOf")), "location")))
  414. model_overwrite(new_model, model_id)
  415. output("Model overwrite success!")
  416. else:
  417. output("Permission denied")
  418. else:
  419. output("Permission denied")
  420. else:
  421. output("No such model")
  422. elif (cmd == "model_modify"):
  423. // Model modify operation, which uses the mini_modify.alc operations, though with extensions for access control
  424. String model_id
  425. String type_id
  426. output("Which model do you want to modify?")
  427. model_id = get_model_id(input())
  428. if (model_id != ""):
  429. if (allow_read(user_id, model_id)):
  430. type_id = set_pop(allAssociationDestinations(core, model_id, "instanceOf"))
  431. if (allow_read(user_id, type_id)):
  432. modify(import_node(read_attribute(core, model_id, "location")), allow_write(user_id, model_id))
  433. else:
  434. output("You are not allowed to read the type model!")
  435. else:
  436. output("You are not allowed to read this model!")
  437. else:
  438. output("Could not find model!")
  439. elif (cmd == "model_delete"):
  440. // Delete a model and all of its related transformations
  441. String model_id
  442. output("=================================================")
  443. output("WARNING: Deletion is a very destructive operation")
  444. output(" as it also deletes all transformations ")
  445. output(" defined which make use of this model! ")
  446. output("=================================================")
  447. output("")
  448. output("Currently not supported!")
  449. elif (cmd == "model_list"):
  450. // List all models
  451. Element models
  452. String m
  453. models = allInstances(core, "Model")
  454. while (read_nr_out(models) > 0):
  455. m = set_pop(models)
  456. log("at " + cast_e2s(m))
  457. log("Name: " + cast_v2s(read_attribute(core, m, "name")))
  458. log("Instance of " + cast_e2s(set_pop(followAssociation(core, m, "instanceOf"))))
  459. log("Name: " + cast_e2s(read_attribute(core, set_pop(followAssociation(core, m, "instanceOf")), "name")))
  460. output(string_join((string_join(" ", read_attribute(core, m, "name")) + " : "), read_attribute(core, set_pop(followAssociation(core, m, "instanceOf")), "name")))
  461. elif (cmd == "model_list_full"):
  462. // List all models with full info
  463. Element models
  464. String m
  465. String permissions
  466. String owner
  467. String group
  468. String name
  469. String type
  470. String size
  471. models = allInstances(core, "Model")
  472. while (read_nr_out(models) > 0):
  473. m = set_pop(models)
  474. permissions = read_attribute(core, m, "permissions")
  475. owner = read_attribute(core, set_pop(allAssociationDestinations(core, m, "owner")), "name")
  476. group = read_attribute(core, set_pop(allAssociationDestinations(core, m, "group")), "name")
  477. name = read_attribute(core, m, "name")
  478. size = cast_i2s(read_nr_out(dict_read(import_node(read_attribute(core, m, "location")), "model")))
  479. type = read_attribute(core, set_pop(allAssociationDestinations(core, m, "instanceOf")), "name")
  480. output(((((((((((" " + permissions) + " ") + owner) + " ") + group) + " ") + size) + " ") + name) + " : ") + type)
  481. elif (cmd == "transformation_add_MT_language"):
  482. // Create a model transformation language from a set of input and output formalisms
  483. String name
  484. String model_id
  485. Element source
  486. Element target
  487. Element all_formalisms
  488. Element merged_formalism
  489. Element ramified_formalism
  490. String old_type_id
  491. String type_id
  492. String location
  493. String new_model_id
  494. source = create_node()
  495. target = create_node()
  496. old_type_id = ""
  497. // Read involved formalisms
  498. output("Formalisms to include (terminate with empty string)?")
  499. name = input()
  500. while (name != ""):
  501. model_id = get_model_id(name)
  502. if (model_id != ""):
  503. if (allow_read(user_id, model_id)):
  504. type_id = set_pop(allAssociationDestinations(core, model_id, "instanceOf"))
  505. if (bool_or(old_type_id == "", type_id == old_type_id)):
  506. set_add(all_formalisms, create_tuple(name, import_node(read_attribute(core, model_id, "location"))))
  507. old_type_id = type_id
  508. elif (old_type_id != type_id):
  509. // Already have a previous type_id and now another: CLASH
  510. output("Cannot add model as types not compatible with previous models; try again")
  511. else:
  512. output("Model not readable; try again")
  513. else:
  514. output("No such model; try again")
  515. name = input()
  516. // Merge both into a single metamodel
  517. if (read_nr_out(source) > 0):
  518. if (read_nr_out(target) > 0):
  519. output("Name of the RAMified tranformation metamodel?")
  520. name = input()
  521. if (get_model_id(name) == ""):
  522. String merged_formalism_id
  523. String ramified_formalism_id
  524. String source_formalism_id
  525. String tracability_link
  526. // New location is available, so write
  527. merged_formalism = model_fuse(all_formalisms)
  528. location = "/models/" + cast_id2s(merged_formalism)
  529. export_node(location, merged_formalism)
  530. // Manage meta-info
  531. new_model_id = instantiate_node(core, "Model", "")
  532. merged_formalism_id = new_model_id
  533. instantiate_attribute(core, new_model_id, "name", "__merged_" + name)
  534. instantiate_attribute(core, new_model_id, "location", location)
  535. instantiate_attribute(core, new_model_id, "permissions", "200")
  536. instantiate_link(core, "owner", "", new_model_id, user_id)
  537. instantiate_link(core, "instanceOf", "", new_model_id, type_id)
  538. // Add tracability links at this level
  539. while (read_nr_out(all_formalisms) > 0):
  540. source_formalism_id = list_read(set_pop(all_formalisms), 1)
  541. tracability_link = instantiate_link(core, "tracability", "", merged_formalism_id, source_formalism_id)
  542. instantiate_attribute(core, tracability_link, "type", "merged")
  543. // Merge complete, now RAMify!
  544. ramified_formalism = ramify(merged_formalism)
  545. location = "/models/" + cast_id2s(ramified_formalism)
  546. export_node(location, ramified_formalism)
  547. // Manage meta-info
  548. new_model_id = instantiate_node(core, "Model", "")
  549. instantiate_attribute(core, new_model_id, "name", name)
  550. instantiate_attribute(core, new_model_id, "location", location)
  551. instantiate_attribute(core, new_model_id, "permissions", "200")
  552. instantiate_link(core, "owner", "", new_model_id, user_id)
  553. instantiate_link(core, "instanceOf", "", new_model_id, type_id)
  554. // Add tracability link at this level
  555. tracability_link = instantiate_link(core, "tracability", "", ramified_formalism_id, merged_formalism_id)
  556. instantiate_attribute(core, tracability_link, "type", "RAMified")
  557. else:
  558. output("Model already exists!")
  559. else:
  560. output("At least one target formalism is required")
  561. else:
  562. output("At least one source formalism is required")
  563. elif (cmd == "transformation_reRAMify"):
  564. String model_id
  565. String source_id
  566. Element trace_links
  567. String trace_link
  568. String merged_model_id
  569. Element new_model
  570. String location
  571. output("Merged metamodel to RAMify again?")
  572. model_id = get_model_id(input())
  573. if (model_id != ""):
  574. if (allow_write(user_id, model_id)):
  575. // We must be able to write the RAMification model, as we will update it in-place
  576. trace_links = allOutgoingAssociationInstances(core, model_id, "tracability")
  577. merged_model_id = ""
  578. while (read_nr_out(trace_links) > 0):
  579. trace_link = set_pop(trace_links)
  580. if (value_eq(read_attribute(core, trace_link, "type"), "RAMified")):
  581. // Found a RAMification link, so redo it on the original model
  582. merged_model_id = readAssociationDestination(core, trace_link)
  583. if (merged_model_id != ""):
  584. new_model = ramify(core["model"][merged_model_id])
  585. model_overwrite(new_model, model_id)
  586. else:
  587. output("Could not determine original model of RAMified metamodel!")
  588. else:
  589. output("Permission denied")
  590. else:
  591. output("No such model")
  592. elif (cmd == "transformation_add_MT"):
  593. // Add a model transformation model
  594. // Just a usual model instantiation, but need to add the source and target links based on user info
  595. String ramified_metamodel_id
  596. String model_id
  597. Element models
  598. Element links
  599. String link_id
  600. Element supported
  601. Element source
  602. Element target
  603. String name
  604. String new_model_id
  605. source = create_node()
  606. target = create_node()
  607. supported = create_node()
  608. output("RAMified metamodel to use?")
  609. ramified_metamodel_id = get_model_id(input())
  610. if (ramified_metamodel_id != ""):
  611. if (allow_read(user_id, ramified_metamodel_id)):
  612. output("Supported metamodels:")
  613. links = allOutgoingAssociationInstances(core, ramified_metamodel_id, "tracability")
  614. while (read_nr_out(links) > 0):
  615. link_id = set_pop(links)
  616. if (value_eq(read_attribute(core, link_id, "type"), "merged")):
  617. output(string_join(" ", read_attribute(core, readAssociationDestination(core, link_id), "name")))
  618. set_add(supported, readAssociationDestination(core, link_id))
  619. output("")
  620. output("Which ones do you want to use as source (empty string to finish)?")
  621. name = input()
  622. while (name != ""):
  623. model_id = get_model_id(name)
  624. if (model_id != ""):
  625. if (set_in(supported, model_id)):
  626. if (bool_not(set_in(source, model_id))):
  627. set_add(source, model_id)
  628. output("Model added as source")
  629. else:
  630. output("Model already selected as source")
  631. else:
  632. output("Model is not supported by RAMified metamodel!")
  633. else:
  634. output("No such model; try again")
  635. name = input()
  636. output("Which ones do you want to use as target (empty string to finish)?")
  637. name = input()
  638. while (name != ""):
  639. model_id = get_model_id(name)
  640. if (model_id != ""):
  641. if (set_in(supported, model_id)):
  642. if (bool_not(set_in(target, model_id))):
  643. set_add(target, model_id)
  644. output("Model added as target")
  645. else:
  646. output("Model already selected as target")
  647. else:
  648. output("Model is not supported by RAMified metamodel!")
  649. else:
  650. output("No such model; try again")
  651. name = input()
  652. output("Name of new transformation?")
  653. name = input()
  654. if (get_model_id(name) == ""):
  655. String new_model
  656. // Finished with all information, now create the model itself!
  657. new_model = instantiate_model(import_node(read_attribute(core, ramified_metamodel_id, "location")))
  658. model_create(new_model, name, user_id, ramified_metamodel_id)
  659. model_id = get_model_id(name)
  660. // Extend metadata with info on source and target
  661. while (read_nr_out(source) > 0):
  662. instantiate_link(core, "transformInput", "", new_model_id, set_pop(source))
  663. while (read_nr_out(target) > 0):
  664. instantiate_link(core, "transformOutput", "", new_model_id, set_pop(target))
  665. output("Meta-info correctly set!")
  666. else:
  667. output("Model already exists")
  668. else:
  669. output("Permission denied")
  670. else:
  671. output("No such model")
  672. elif (cmd == "permission_modify"):
  673. String permissions
  674. Integer permission
  675. String model_id
  676. output("Which model do you want to change permissions of?")
  677. model_id = get_model_id(input())
  678. if (model_id != ""):
  679. if (get_relation_to_model(user_id, model_id) == 0):
  680. output("New permissions?")
  681. permissions = input()
  682. Boolean fail
  683. Integer i
  684. i = 0
  685. if (string_len(permissions) != 3):
  686. fail = True
  687. while (bool_and(bool_not(fail), i < 3)):
  688. permission = cast_s2i(string_get(permissions, i))
  689. if (bool_or(permission < 0, permission > 2)):
  690. fail = True
  691. if (bool_not(fail)):
  692. unset_attribute(core, model_id, "permissions")
  693. instantiate_attribute(core, model_id, "permissions", permissions)
  694. else:
  695. output("Permissions must be a string of three characters with each character being a digit between 0 and 2")
  696. else:
  697. output("Permission denied!")
  698. else:
  699. output("No such model!")
  700. elif (cmd == "permission_owner"):
  701. String permissions
  702. String model_id
  703. String user_id
  704. output("Which model do you want to change owner of?")
  705. model_id = get_model_id(input())
  706. if (model_id != ""):
  707. if (get_relation_to_model(user_id, model_id) == 0):
  708. output("New owner?")
  709. user_id = get_user_id(input())
  710. if (user_id != ""):
  711. model_delete_element(core, set_pop(allOutgoingAssociationInstances(core, model_id, "owner")))
  712. instantiate_link(core, "owner", "", model_id, user_id)
  713. else:
  714. output("No such user!")
  715. else:
  716. output("Permission denied!")
  717. else:
  718. output("No such model!")
  719. elif (cmd == "permission_group"):
  720. String permissions
  721. String model_id
  722. String group_id
  723. output("Which model do you want to change permissions of?")
  724. model_id = get_model_id(input())
  725. if (model_id != ""):
  726. if (get_relation_to_model(user_id, model_id) == 0):
  727. output("New group?")
  728. group_id = get_group_id(input())
  729. if (group_id != ""):
  730. model_delete_element(core, set_pop(allOutgoingAssociationInstances(core, model_id, "group")))
  731. instantiate_link(core, "group", "", model_id, group_id)
  732. else:
  733. output("No such group!")
  734. else:
  735. output("Permission denied!")
  736. else:
  737. output("No such model!")
  738. elif (cmd == "group_create"):
  739. // Create a new group and become its owner
  740. String group_id
  741. String name
  742. output("Which group do you want to create?")
  743. name = input()
  744. group_id = get_group_id(name)
  745. if (group_id == ""):
  746. group_id = instantiate_node(core, "Group", "")
  747. instantiate_attribute(core, group_id, "name", name)
  748. instantiate_link(core, "belongsTo", "", user_id, group_id)
  749. instantiate_link(core, "owner", "", group_id, user_id)
  750. output("Group created!")
  751. else:
  752. output("Group already exists")
  753. elif (cmd == "group_delete"):
  754. // Delete an existing group
  755. String group_id
  756. String name
  757. output("Which group do you want to delete?")
  758. name = input()
  759. group_id = get_group_id(name)
  760. if (group_id != ""):
  761. if (allow_group_modify(user_id, group_id)):
  762. model_delete_element(core, group_id)
  763. else:
  764. output("Permission denied")
  765. else:
  766. output("No such group")
  767. elif (cmd == "group_owner_add"):
  768. // Add an owner to your group
  769. String group_id
  770. String other_user_id
  771. output("Which group do you want to add an owner to?")
  772. group_id = get_group_id(input())
  773. if (group_id != ""):
  774. if (allow_group_modify(user_id, group_id)):
  775. output("Which user do you want to make an owner?")
  776. other_user_id = get_user_id(input())
  777. if (other_user_id != ""):
  778. Element overlap
  779. overlap = set_overlap(allIncomingAssociationInstances(core, user_id, "owner"), allOutgoingAssociationInstances(core, group_id, "owner"))
  780. if (read_nr_out(overlap) == 0):
  781. instantiate_link(core, "owner", "", group_id, user_id)
  782. overlap = set_overlap(allOutgoingAssociationInstances(core, user_id, "belongsTo"), allIncomingAssociationInstances(core, group_id, "belongsTo"))
  783. if (read_nr_out(overlap) == 0):
  784. instantiate_link(core, "belongsTo", "", user_id, group_id)
  785. output("New owner added to group!")
  786. else:
  787. output("User is already an owner!")
  788. else:
  789. output("No such user")
  790. else:
  791. output("Permission denied!")
  792. else:
  793. output("No such group")
  794. elif (cmd == "group_owner_delete"):
  795. // Remove an owner from your group
  796. String group_id
  797. String other_user_id
  798. output("Which group do you want to disown someone from?")
  799. group_id = get_group_id(input())
  800. if (group_id != ""):
  801. if (allow_group_modify(user_id, group_id)):
  802. output("Which user do you want to disown?")
  803. other_user_id = get_user_id(input())
  804. if (other_user_id != ""):
  805. Element overlap
  806. overlap = set_overlap(allOutgoingAssociationInstances(core, user_id, "belongsTo"), allIncomingAssociationInstances(core, group_id, "belongsTo"))
  807. if (read_nr_out(overlap) > 0):
  808. overlap = set_overlap(allIncomingAssociationInstances(core, user_id, "owner"), allOutgoingAssociationInstances(core, group_id, "owner"))
  809. if (read_nr_out(overlap) > 0):
  810. model_delete_element(core, set_pop(overlap))
  811. output("Disowned group from user!")
  812. else:
  813. output("User is not even an owner of the group!")
  814. else:
  815. output("User is not even a member of the group!")
  816. else:
  817. output("No such user")
  818. else:
  819. output("Permission denied!")
  820. else:
  821. output("No such group")
  822. elif (cmd == "group_join"):
  823. // Add someone to your group
  824. String group_id
  825. String other_user_id
  826. output("Which group do you want to add someone to?")
  827. group_id = get_group_id(input())
  828. if (group_id != ""):
  829. if (allow_group_modify(user_id, group_id)):
  830. output("Which user do you want to add?")
  831. other_user_id = get_user_id(input())
  832. if (other_user_id != ""):
  833. Element overlap
  834. overlap = set_overlap(allOutgoingAssociationInstances(core, user_id, "belongsTo"), allIncomingAssociationInstances(core, group_id, "belongsTo"))
  835. if (read_nr_out(overlap) == 0):
  836. instantiate_link(core, "belongsTo", "", user_id, group_id)
  837. output("User added to the group!")
  838. else:
  839. output("User is already a member of the group!")
  840. else:
  841. output("No such user")
  842. else:
  843. output("Permission denied!")
  844. else:
  845. output("No such group")
  846. elif (cmd == "group_kick"):
  847. // Remove someone from your group
  848. String group_id
  849. String other_user_id
  850. output("Which group do you want to kick someone from?")
  851. group_id = get_group_id(input())
  852. if (group_id != ""):
  853. if (allow_group_modify(user_id, group_id)):
  854. output("Which user do you want to kick?")
  855. other_user_id = get_user_id(input())
  856. if (other_user_id != ""):
  857. Element overlap
  858. overlap = set_overlap(allOutgoingAssociationInstances(core, user_id, "belongsTo"), allIncomingAssociationInstances(core, group_id, "belongsTo"))
  859. if (read_nr_out(overlap) > 0):
  860. model_delete_element(core, set_pop(overlap))
  861. // Check if user was an owner as well
  862. overlap = set_overlap(allIncomingAssociationInstances(core, user_id, "owner"), allOutgoingAssociationInstances(core, group_id, "owner"))
  863. if (read_nr_out(overlap) > 0):
  864. model_delete_element(core, set_pop(overlap))
  865. output("User kicked!")
  866. else:
  867. output("User is not even a member of the group!")
  868. else:
  869. output("No such user")
  870. else:
  871. output("Permission denied!")
  872. else:
  873. output("No such group")
  874. elif (cmd == "group_list"):
  875. // List all groups you are a member of (and whether you are admin or not!)
  876. Element groups
  877. String group_id
  878. String admin
  879. groups = allAssociationDestinations(core, user_id, "belongsTo")
  880. while (True):
  881. group_id = set_pop(groups)
  882. if (set_in(allOutgoingAssociationInstances(core, group_id, "owner"), user_id)):
  883. admin = " A "
  884. else:
  885. admin = " "
  886. output(string_join(admin, read_attribute(core, group_id, "name")))
  887. elif (cmd == "admin_promote"):
  888. // Promote a user to admin status
  889. if (is_admin(user_id)):
  890. String other_user_id
  891. output("Which user do you want to promote?")
  892. other_user_id = get_user_id(input())
  893. if (other_user_id != ""):
  894. unset_attribute(core, other_user_id, "admin")
  895. instantiate_attribute(core, other_user_id, "admin", True)
  896. output("Permissions granted!")
  897. else:
  898. output("No such user!")
  899. else:
  900. output("Permission denied!")
  901. elif (cmd == "admin_demote"):
  902. // Demote a user to normal status
  903. if (is_admin(user_id)):
  904. String other_user_id
  905. output("Which user do you want to demote?")
  906. other_user_id = get_user_id(input())
  907. if (other_user_id != ""):
  908. unset_attribute(core, other_user_id, "admin")
  909. instantiate_attribute(core, other_user_id, "admin", False)
  910. output("Permissions revoked!")
  911. else:
  912. output("No such user!")
  913. else:
  914. output("Permission denied!")
  915. elif (cmd == "exit"):
  916. // Exit by actually removing the user and decoupling it from all of its models
  917. // Restarting with the same user name will NOT grant you access to anything of the previous user with that same name
  918. do_continue = False
  919. // Delete user from Core Formalism
  920. model_delete_element(core, user_id)
  921. output("Goodbye!")
  922. return !