core_algorithm.alc 38 KB

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