core_algorithm.alc 42 KB

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