core_algorithm.alc 42 KB

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