core_algorithm.alc 41 KB

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