core_algorithm.alc 44 KB

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