core_algorithm.alc 35 KB

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