core_algorithm.alc 43 KB

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