core_algorithm.alc 44 KB

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