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