core_algorithm.alc 44 KB

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