core_algorithm.alc 38 KB

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