core_algorithm.alc 43 KB

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