core_algorithm.alc 44 KB

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