core_algorithm.alc 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199
  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. String password
  53. while (True):
  54. output("Desired password for admin user?")
  55. password = hash(input())
  56. output("Please repeat the password")
  57. if (password == hash(input())):
  58. output("Passwords match!")
  59. instantiate_attribute(core, admin_user, "password", password)
  60. break!
  61. else:
  62. output("Not the same password, please try again!")
  63. // Create link between admin user and group
  64. instantiate_link(core, "ownedBy", "", admin_group, admin_user)
  65. instantiate_link(core, "belongsTo", "", admin_user, admin_group)
  66. // Add the SimpleClassDiagrams formalism already
  67. scd_model = instantiate_node(core, "Model", "")
  68. instantiate_attribute(core, scd_model, "name", "SimpleClassDiagrams")
  69. instantiate_attribute(core, scd_model, "location", scd_location)
  70. instantiate_attribute(core, scd_model, "permissions", "221")
  71. instantiate_link(core, "instanceOf", "", scd_model, scd_model)
  72. // Make necessary links for the formalism to the owners
  73. instantiate_link(core, "group", "", scd_model, admin_group)
  74. instantiate_link(core, "owner", "", scd_model, admin_user)
  75. // Add the core formalism already
  76. core_formalism_model = instantiate_node(core, "Model", "")
  77. instantiate_attribute(core, core_formalism_model, "name", "CoreFormalism")
  78. instantiate_attribute(core, core_formalism_model, "location", core_location)
  79. instantiate_attribute(core, core_formalism_model, "permissions", "221")
  80. instantiate_link(core, "instanceOf", "", core_formalism_model, scd_model)
  81. // Make necessary links for the formalism to the owners
  82. instantiate_link(core, "group", "", core_formalism_model, admin_group)
  83. instantiate_link(core, "owner", "", core_formalism_model, admin_user)
  84. // Add the core model
  85. core_model = instantiate_node(core, "Model", "")
  86. instantiate_attribute(core, core_model, "name", "core")
  87. instantiate_attribute(core, core_model, "location", core_model_location)
  88. instantiate_attribute(core, core_model, "permissions", "200")
  89. instantiate_link(core, "instanceOf", "", core_model, core_formalism_model)
  90. // Make necessary links for the formalism to the owners
  91. instantiate_link(core, "group", "", core_model, admin_group)
  92. instantiate_link(core, "owner", "", core_model, admin_user)
  93. // Call this for ourselves as well
  94. log("MvC is ready!")
  95. user_function_skip_init(admin_user)
  96. // Done, so finish up
  97. // Admin user will have been deleted by the user_function as usual
  98. // Note that if there are no admin users left, it will be very difficult to manage, as nobody will have admin permissions!
  99. return !
  100. Integer function get_relation_to_model(user_id : String, model_id : String):
  101. if (set_in(allAssociationDestinations(core, model_id, "owner"), user_id)):
  102. // We are the owner
  103. return 0!
  104. else:
  105. String group_id
  106. group_id = set_pop(allAssociationDestinations(core, model_id, "group"))
  107. if (set_in(allAssociationDestinations(core, user_id, "belongsTo"), group_id)):
  108. // We are in the owning group
  109. return 1!
  110. else:
  111. // We are not related whatsoever
  112. return 2!
  113. Boolean function is_admin(user_id : String):
  114. if (read_attribute(core, user_id, "admin")):
  115. return True!
  116. else:
  117. return False!
  118. Boolean function allow_read(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 (bool_or(permission == "1", permission == "2")):
  127. return True!
  128. else:
  129. return False!
  130. Boolean function allow_write(user_id : String, model_id : String):
  131. if (is_admin(user_id)):
  132. // Is admin, so always allow
  133. return True!
  134. else:
  135. // Check permissions
  136. String permission
  137. permission = string_get(read_attribute(core, model_id, "permissions"), get_relation_to_model(user_id, model_id))
  138. if (permission == "2"):
  139. return True!
  140. else:
  141. return False!
  142. Boolean function allow_change_metadata(user_id : String, model_id : String):
  143. if (is_admin(user_id)):
  144. // Is admin, so always allow
  145. return True!
  146. else:
  147. if (get_relation_to_model(user_id, model_id) == 0):
  148. // Only owner can chmod
  149. return True!
  150. else:
  151. return False!
  152. Boolean function allow_group_modify(user_id : String, group_id : String):
  153. if (is_admin(user_id)):
  154. // Is admin, so always allow
  155. return True!
  156. else:
  157. if (read_nr_out(set_overlap(allIncomingAssociationInstances(core, user_id, "owner"), allOutgoingAssociationInstances(core, group_id, "owner"))) > 0):
  158. // We are an owner
  159. return True!
  160. else:
  161. return False!
  162. Boolean function check_login(user_id : String):
  163. String password
  164. String stored_password
  165. stored_password = read_attribute(core, user_id, "password")
  166. output("Password for existing user?")
  167. password = hash(input())
  168. return password == stored_password!
  169. Element function extract_ftg(user_id : String):
  170. // Extract your personal FTG, showing only the readable models that can be opened
  171. // This function does the actual projection of transformations
  172. // TODO: Only keep transformations satisfying some properties!
  173. return create_node()!
  174. Element function user_function():
  175. String username
  176. String user_id
  177. String password
  178. // Load in all global variables, as this code is hotloaded!
  179. Element root
  180. root = read_root()
  181. root = root["__hierarchy"]["objects"]
  182. exec(root["bootstrap/modelling.alc"]["initializers"])
  183. exec(root["bootstrap/library.alc"]["initializers"])
  184. exec(root["bootstrap/constructors.alc"]["initializers"])
  185. exec(root["bootstrap/object_operations.alc"]["initializers"])
  186. exec(root["core/mini_modify.alc"]["initializers"])
  187. exec(root["bootstrap/model_management.alc"]["initializers"])
  188. exec(root["bootstrap/ramify.alc"]["initializers"])
  189. exec(root["bootstrap/transform.alc"]["initializers"])
  190. exec(root["bootstrap/conformance_scd.alc"]["initializers"])
  191. exec(root["core/core_algorithm.alc"]["initializers"])
  192. core = import_node("models/core")
  193. output("Log on as which user?")
  194. username = input()
  195. user_id = get_user_id(username)
  196. if (user_id == ""):
  197. // New user
  198. // Add user to Core Formalism
  199. user_id = instantiate_node(core, "User", "")
  200. instantiate_attribute(core, user_id, "name", username)
  201. instantiate_attribute(core, user_id, "admin", False)
  202. while (True):
  203. output("This is a new user: please give password!")
  204. password = hash(input())
  205. output("Please repeat the password")
  206. if (password == hash(input())):
  207. output("Passwords match!")
  208. output("User created")
  209. instantiate_attribute(core, user_id, "password", password)
  210. break!
  211. else:
  212. output("Not the same password, please try again!")
  213. // Now call with user created
  214. user_function_skip_init(user_id)
  215. else:
  216. while (bool_not(check_login(user_id))):
  217. output("Wrong password! Try again")
  218. user_function_skip_init(user_id)
  219. // User destroyed already, so just stop execution
  220. // TODO return a fresh node as otherwise the compiler doesn't take this
  221. return create_node()!
  222. String function get_model_id(name : String):
  223. Element models
  224. String model
  225. models = allInstances(core, "Model")
  226. while (read_nr_out(models) > 0):
  227. model = set_pop(models)
  228. if (value_eq(name, read_attribute(core, model, "name"))):
  229. return model!
  230. return ""!
  231. String function get_user_id(name : String):
  232. Element users
  233. String user
  234. users = allInstances(core, "User")
  235. while (read_nr_out(users) > 0):
  236. user = set_pop(users)
  237. if (value_eq(read_attribute(core, user, "name"), name)):
  238. return user!
  239. return ""!
  240. String function get_group_id(name : String):
  241. Element groups
  242. String group
  243. groups = allInstances(core, "Group")
  244. while (read_nr_out(groups) > 0):
  245. group = set_pop(groups)
  246. if (value_eq(read_attribute(core, group, "name"), name)):
  247. return group!
  248. return ""!
  249. Void function model_create(model : Element, name : String, user_id : String, type_id : String, kind : String):
  250. String location
  251. String model_id
  252. location = "/models/" + cast_id2s(model)
  253. export_node(location, model)
  254. // Manage meta-info
  255. model_id = instantiate_node(core, kind, "")
  256. instantiate_attribute(core, model_id, "name", name)
  257. instantiate_attribute(core, model_id, "location", location)
  258. instantiate_attribute(core, model_id, "permissions", "200")
  259. instantiate_link(core, "owner", "", model_id, user_id)
  260. instantiate_link(core, "group", "", model_id, get_group_id("nobody"))
  261. instantiate_link(core, "instanceOf", "", model_id, type_id)
  262. return!
  263. Void function model_overwrite(model : Element, model_id : String):
  264. String location
  265. location = "/models/" + cast_id2s(model)
  266. export_node(location, model)
  267. // Change location in meta-data
  268. unset_attribute(core, model_id, "location")
  269. instantiate_attribute(core, model_id, "location", location)
  270. return!
  271. Void function user_function_skip_init(user_id : String):
  272. String cmd
  273. output("Welcome to the Model Management Interface v2.0!")
  274. output("Use the 'help' command for a list of possible commands")
  275. while (True):
  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 (i < 3):
  792. permission = cast_s2i(string_get(permissions, i))
  793. if (bool_or(permission < 0, permission > 2)):
  794. fail = True
  795. break!
  796. i = i + 1
  797. if (bool_not(fail)):
  798. unset_attribute(core, model_id, "permissions")
  799. instantiate_attribute(core, model_id, "permissions", permissions)
  800. else:
  801. output("Permissions must be a string of three characters with each character being a digit between 0 and 2")
  802. else:
  803. output("Permission denied!")
  804. else:
  805. output("No such model!")
  806. elif (cmd == "permission_owner"):
  807. String permissions
  808. String model_id
  809. String user_id
  810. output("Which model do you want to change owner of?")
  811. model_id = get_model_id(input())
  812. if (model_id != ""):
  813. if (bool_or(get_relation_to_model(user_id, model_id) == 0, is_admin(user_id))):
  814. output("New owner?")
  815. user_id = get_user_id(input())
  816. if (user_id != ""):
  817. model_delete_element(core, set_pop(allOutgoingAssociationInstances(core, model_id, "owner")))
  818. instantiate_link(core, "owner", "", model_id, user_id)
  819. else:
  820. output("No such user!")
  821. else:
  822. output("Permission denied!")
  823. else:
  824. output("No such model!")
  825. elif (cmd == "permission_group"):
  826. String permissions
  827. String model_id
  828. String group_id
  829. output("Which model do you want to change permissions of?")
  830. model_id = get_model_id(input())
  831. if (model_id != ""):
  832. if (bool_or(get_relation_to_model(user_id, model_id) == 0, is_admin(user_id))):
  833. output("New group?")
  834. group_id = get_group_id(input())
  835. if (group_id != ""):
  836. model_delete_element(core, set_pop(allOutgoingAssociationInstances(core, model_id, "group")))
  837. instantiate_link(core, "group", "", model_id, group_id)
  838. else:
  839. output("No such group!")
  840. else:
  841. output("Permission denied!")
  842. else:
  843. output("No such model!")
  844. elif (cmd == "group_create"):
  845. // Create a new group and become its owner
  846. String group_id
  847. String name
  848. output("Which group do you want to create?")
  849. name = input()
  850. group_id = get_group_id(name)
  851. if (group_id == ""):
  852. group_id = instantiate_node(core, "Group", "")
  853. instantiate_attribute(core, group_id, "name", name)
  854. instantiate_link(core, "belongsTo", "", user_id, group_id)
  855. instantiate_link(core, "owner", "", group_id, user_id)
  856. output("Group created!")
  857. else:
  858. output("Group already exists")
  859. elif (cmd == "group_delete"):
  860. // Delete an existing group
  861. String group_id
  862. String name
  863. output("Which group do you want to delete?")
  864. name = input()
  865. group_id = get_group_id(name)
  866. if (group_id != ""):
  867. if (allow_group_modify(user_id, group_id)):
  868. model_delete_element(core, group_id)
  869. else:
  870. output("Permission denied")
  871. else:
  872. output("No such group")
  873. elif (cmd == "group_owner_add"):
  874. // Add an owner to your group
  875. String group_id
  876. String other_user_id
  877. output("Which group do you want to add an owner to?")
  878. group_id = get_group_id(input())
  879. if (group_id != ""):
  880. if (allow_group_modify(user_id, group_id)):
  881. output("Which user do you want to make an owner?")
  882. other_user_id = get_user_id(input())
  883. if (other_user_id != ""):
  884. Element overlap
  885. overlap = set_overlap(allIncomingAssociationInstances(core, other_user_id, "owner"), allOutgoingAssociationInstances(core, group_id, "owner"))
  886. if (read_nr_out(overlap) == 0):
  887. instantiate_link(core, "owner", "", group_id, other_user_id)
  888. overlap = set_overlap(allOutgoingAssociationInstances(core, other_user_id, "belongsTo"), allIncomingAssociationInstances(core, group_id, "belongsTo"))
  889. if (read_nr_out(overlap) == 0):
  890. instantiate_link(core, "belongsTo", "", other_user_id, group_id)
  891. output("New owner added to group!")
  892. else:
  893. output("User is already an owner!")
  894. else:
  895. output("No such user")
  896. else:
  897. output("Permission denied!")
  898. else:
  899. output("No such group")
  900. elif (cmd == "group_owner_delete"):
  901. // Remove an owner from your group
  902. String group_id
  903. String other_user_id
  904. output("Which group do you want to disown someone from?")
  905. group_id = get_group_id(input())
  906. if (group_id != ""):
  907. if (allow_group_modify(user_id, group_id)):
  908. output("Which user do you want to disown?")
  909. other_user_id = get_user_id(input())
  910. if (other_user_id != ""):
  911. Element overlap
  912. overlap = set_overlap(allOutgoingAssociationInstances(core, other_user_id, "belongsTo"), allIncomingAssociationInstances(core, group_id, "belongsTo"))
  913. if (read_nr_out(overlap) > 0):
  914. overlap = set_overlap(allIncomingAssociationInstances(core, other_user_id, "owner"), allOutgoingAssociationInstances(core, group_id, "owner"))
  915. if (read_nr_out(overlap) > 0):
  916. model_delete_element(core, set_pop(overlap))
  917. output("Disowned group from user!")
  918. else:
  919. output("User is not even an owner of the group!")
  920. else:
  921. output("User is not even a member of the group!")
  922. else:
  923. output("No such user")
  924. else:
  925. output("Permission denied!")
  926. else:
  927. output("No such group")
  928. elif (cmd == "group_join"):
  929. // Add someone to your group
  930. String group_id
  931. String other_user_id
  932. output("Which group do you want to add someone to?")
  933. group_id = get_group_id(input())
  934. if (group_id != ""):
  935. if (allow_group_modify(user_id, group_id)):
  936. output("Which user do you want to add?")
  937. other_user_id = get_user_id(input())
  938. if (other_user_id != ""):
  939. Element overlap
  940. overlap = set_overlap(allOutgoingAssociationInstances(core, other_user_id, "belongsTo"), allIncomingAssociationInstances(core, group_id, "belongsTo"))
  941. log("Size of overlap: " + cast_v2s(read_nr_out(overlap)))
  942. log("Overlap: " + set_to_string(overlap))
  943. if (read_nr_out(overlap) == 0):
  944. instantiate_link(core, "belongsTo", "", other_user_id, group_id)
  945. output("User added to the group!")
  946. else:
  947. output("User is already a member of the group!")
  948. else:
  949. output("No such user")
  950. else:
  951. output("Permission denied!")
  952. else:
  953. output("No such group")
  954. elif (cmd == "group_kick"):
  955. // Remove someone from your group
  956. String group_id
  957. String other_user_id
  958. output("Which group do you want to kick someone from?")
  959. group_id = get_group_id(input())
  960. if (group_id != ""):
  961. if (allow_group_modify(user_id, group_id)):
  962. output("Which user do you want to kick?")
  963. other_user_id = get_user_id(input())
  964. if (other_user_id != ""):
  965. Element overlap
  966. overlap = set_overlap(allOutgoingAssociationInstances(core, other_user_id, "belongsTo"), allIncomingAssociationInstances(core, group_id, "belongsTo"))
  967. if (read_nr_out(overlap) > 0):
  968. model_delete_element(core, set_pop(overlap))
  969. // Check if user was an owner as well
  970. overlap = set_overlap(allIncomingAssociationInstances(core, other_user_id, "owner"), allOutgoingAssociationInstances(core, group_id, "owner"))
  971. if (read_nr_out(overlap) > 0):
  972. model_delete_element(core, set_pop(overlap))
  973. output("User kicked!")
  974. else:
  975. output("User is not even a member of the group!")
  976. else:
  977. output("No such user")
  978. else:
  979. output("Permission denied!")
  980. else:
  981. output("No such group")
  982. elif (cmd == "group_list"):
  983. // List all groups you are a member of (and whether you are admin or not!)
  984. Element groups
  985. String group_id
  986. String admin
  987. groups = allAssociationDestinations(core, user_id, "belongsTo")
  988. while (read_nr_out(groups) > 0):
  989. group_id = set_pop(groups)
  990. if (set_in(allOutgoingAssociationInstances(core, group_id, "owner"), user_id)):
  991. admin = " A "
  992. else:
  993. admin = " "
  994. output(string_join(admin, read_attribute(core, group_id, "name")))
  995. elif (cmd == "admin_promote"):
  996. // Promote a user to admin status
  997. if (is_admin(user_id)):
  998. String other_user_id
  999. output("Which user do you want to promote?")
  1000. other_user_id = get_user_id(input())
  1001. if (other_user_id != ""):
  1002. unset_attribute(core, other_user_id, "admin")
  1003. instantiate_attribute(core, other_user_id, "admin", True)
  1004. output("Permissions granted!")
  1005. else:
  1006. output("No such user!")
  1007. else:
  1008. output("Permission denied!")
  1009. elif (cmd == "admin_demote"):
  1010. // Demote a user to normal status
  1011. if (is_admin(user_id)):
  1012. String other_user_id
  1013. output("Which user do you want to demote?")
  1014. other_user_id = get_user_id(input())
  1015. if (other_user_id != ""):
  1016. unset_attribute(core, other_user_id, "admin")
  1017. instantiate_attribute(core, other_user_id, "admin", False)
  1018. output("Permissions revoked!")
  1019. else:
  1020. output("No such user!")
  1021. else:
  1022. output("Permission denied!")
  1023. elif (cmd == "self-destruct"):
  1024. // Delete user from Core Formalism
  1025. model_delete_element(core, user_id)
  1026. return !
  1027. elif (cmd == "exit"):
  1028. // Exit by actually removing the user and decoupling it from all of its models
  1029. // Restarting with the same user name will NOT grant you access to anything of the previous user with that same name
  1030. // as the current user will have been deleted
  1031. return !
  1032. else:
  1033. output("Unknown command: " + cmd)
  1034. // We never get here!
  1035. return !