core_algorithm.alc 40 KB

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