core_algorithm.alc 41 KB

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