core_algorithm.alc 37 KB

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