core_algorithm.alc 43 KB

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