core_algorithm.alc 42 KB

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