core_algorithm.alc 42 KB

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