core_algorithm.alc 43 KB

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