core_algorithm.alc 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804
  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 function JSON_print(model : Element):
  16. String result
  17. Element keys_m
  18. String v_m
  19. String type
  20. String attr_key
  21. Element attr_keys
  22. result = "["
  23. keys_m = dict_keys(model["model"])
  24. while (read_nr_out(keys_m) > 0):
  25. v_m = set_pop(keys_m)
  26. type = read_type(model["metamodel"], read_type(model, v_m))
  27. if (bool_or(type == "Class", type == "Association")):
  28. result = result + "{"
  29. result = (((result + "\"id\": \"") + v_m) + "\"") + ","
  30. result = (((result + "\"type\": \"") + read_type(model, v_m)) + "\"") + ","
  31. if (type == "Association"):
  32. result = ((result + "\"__source\": \"") + reverseKeyLookup(model["model"], read_edge_src(model["model"][v_m]))) + "\","
  33. result = ((result + "\"__target\": \"") + reverseKeyLookup(model["model"], read_edge_dst(model["model"][v_m]))) + "\","
  34. // Has attributes
  35. attr_keys = dict_keys(getAttributeList(model, v_m))
  36. while (0 < read_nr_out(attr_keys)):
  37. attr_key = set_pop(attr_keys)
  38. result = ((((result + "\"") + attr_key) + "\": ") + cast_v2s(read_attribute(model, v_m, attr_key))) + ","
  39. result = result + "}"
  40. if (read_nr_out(keys_m) > 0):
  41. result = result + ","
  42. result = result + "]"
  43. return result!
  44. Void function main():
  45. // Initialize the Core Formalism
  46. String core_model
  47. String core_formalism_model
  48. String scd_model
  49. Element al_model
  50. String admin_group
  51. String admin_user
  52. String nobody_group
  53. String instance_of
  54. String core_formalism
  55. Element scd
  56. Element al
  57. Element pm
  58. Element tracability
  59. String pm_model
  60. String tracability_model
  61. String coreM_model_location
  62. String pm_location
  63. String scd_location
  64. String al_location
  65. String tracability_location
  66. coreM_model_location = "models/core"
  67. pm_location = "models/ProcessModel"
  68. scd_location = "models/SimpleClassDiagrams"
  69. al_location = "models/ActionLanguage"
  70. tracability_location = "models/Tracability"
  71. scd = import_node(scd_location)
  72. al = import_node(al_location)
  73. pm = import_node(pm_location)
  74. tracability = import_node(tracability_location)
  75. // Create the Model itself and make public
  76. core_formalism = import_node(core_location)
  77. core = instantiate_model(core_formalism)
  78. export_node(core_model_location, core)
  79. export_node(coreM_model_location, core["model"])
  80. // Switch all new users to the user_function
  81. // This accesses the bootstrap level, so do not change this unless you know what you are doing
  82. Element root
  83. Element prev
  84. root = read_root()
  85. root = root["__hierarchy"]["__IP"]
  86. while (value_neq(root, !call)):
  87. prev = root
  88. root = root["next"]
  89. dict_delete(prev, "next")
  90. dict_add(prev, "next", user_function["body"])
  91. // Create admin group
  92. admin_group = instantiate_node(core, "Group", "")
  93. instantiate_attribute(core, admin_group, "name", "admin")
  94. // Create nobody group
  95. nobody_group = instantiate_node(core, "Group", "")
  96. instantiate_attribute(core, nobody_group, "name", "nobody")
  97. // Create admin user
  98. admin_user = instantiate_node(core, "User", "")
  99. output("Desired username for admin user?")
  100. instantiate_attribute(core, admin_user, "name", input())
  101. instantiate_attribute(core, admin_user, "admin", True)
  102. String password
  103. while (True):
  104. output("Desired password for admin user?")
  105. password = hash(input())
  106. output("Please repeat the password")
  107. if (password == hash(input())):
  108. output("Passwords match!")
  109. instantiate_attribute(core, admin_user, "password", password)
  110. break!
  111. else:
  112. output("Not the same password, please try again!")
  113. // Create link between admin user and group
  114. instantiate_link(core, "ownedBy", "", admin_group, admin_user)
  115. instantiate_link(core, "belongsTo", "", admin_user, admin_group)
  116. // Add the SimpleClassDiagrams formalism already
  117. scd_model = instantiate_node(core, "Model", "")
  118. instantiate_attribute(core, scd_model, "name", "SimpleClassDiagrams")
  119. instantiate_attribute(core, scd_model, "location", scd_location + "/model")
  120. instantiate_attribute(core, scd_model, "permissions", "221")
  121. instance_of = instantiate_link(core, "instanceOf", "", scd_model, scd_model)
  122. instantiate_attribute(core, instance_of, "type_mapping", scd["type_mapping"])
  123. // Make necessary links for the formalism to the owners
  124. instantiate_link(core, "group", "", scd_model, admin_group)
  125. instantiate_link(core, "owner", "", scd_model, admin_user)
  126. // Add the Tracability formalism
  127. tracability_model = instantiate_node(core, "Model", "")
  128. instantiate_attribute(core, tracability_model, "name", "Tracability")
  129. instantiate_attribute(core, tracability_model, "location", tracability_location + "/model")
  130. instantiate_attribute(core, tracability_model, "permissions", "221")
  131. instance_of = instantiate_link(core, "instanceOf", "", tracability_model, scd_model)
  132. instantiate_attribute(core, instance_of, "type_mapping", tracability["type_mapping"])
  133. // Add the user/group links
  134. instantiate_link(core, "group", "", tracability_model, admin_group)
  135. instantiate_link(core, "owner", "", tracability_model, admin_user)
  136. // Add the Process Model formalism
  137. pm_model = instantiate_node(core, "Model", "")
  138. instantiate_attribute(core, pm_model, "name", "ProcessModel")
  139. instantiate_attribute(core, pm_model, "location", pm_location + "/model")
  140. instantiate_attribute(core, pm_model, "permissions", "221")
  141. instance_of = instantiate_link(core, "instanceOf", "", pm_model, scd_model)
  142. instantiate_attribute(core, instance_of, "type_mapping", pm["type_mapping"])
  143. // Make necessary links for the formalism to the owners
  144. instantiate_link(core, "group", "", pm_model, admin_group)
  145. instantiate_link(core, "owner", "", pm_model, admin_user)
  146. // Add the Action Language formalism
  147. al_model = instantiate_node(core, "Model", "")
  148. instantiate_attribute(core, al_model, "name", "ActionLanguage")
  149. instantiate_attribute(core, al_model, "location", al_location + "/model")
  150. instantiate_attribute(core, al_model, "permissions", "221")
  151. instance_of = instantiate_link(core, "instanceOf", "", al_model, scd_model)
  152. instantiate_attribute(core, instance_of, "type_mapping", al["type_mapping"])
  153. // Make necessary links for the formalism to the owners
  154. instantiate_link(core, "group", "", al_model, admin_group)
  155. instantiate_link(core, "owner", "", al_model, admin_user)
  156. // Add the Manual Operation formalism
  157. Element manual
  158. String manual_model
  159. manual = instantiate_model(scd)
  160. manual_model = instantiate_node(core, "Model", "")
  161. export_node("models/ManualOperation", manual)
  162. instantiate_attribute(core, manual_model, "name", "ManualOperation")
  163. instantiate_attribute(core, manual_model, "location", "models/ManualOperation/model")
  164. instantiate_attribute(core, manual_model, "permissions", "221")
  165. instance_of = instantiate_link(core, "instanceOf", "", manual_model, scd_model)
  166. instantiate_attribute(core, instance_of, "type_mapping", create_node())
  167. // Make necessary links for the formalism to the owners
  168. instantiate_link(core, "group", "", manual_model, admin_group)
  169. instantiate_link(core, "owner", "", manual_model, admin_user)
  170. // Add the core formalism already
  171. core_formalism_model = instantiate_node(core, "Model", "")
  172. instantiate_attribute(core, core_formalism_model, "name", "CoreFormalism")
  173. instantiate_attribute(core, core_formalism_model, "location", core_location + "/model")
  174. instantiate_attribute(core, core_formalism_model, "permissions", "221")
  175. instance_of = instantiate_link(core, "instanceOf", "", core_formalism_model, scd_model)
  176. instantiate_attribute(core, instance_of, "type_mapping", core_formalism["type_mapping"])
  177. // Make necessary links for the formalism to the owners
  178. instantiate_link(core, "group", "", core_formalism_model, admin_group)
  179. instantiate_link(core, "owner", "", core_formalism_model, admin_user)
  180. // Add the core model
  181. core_model = instantiate_node(core, "Model", "")
  182. instantiate_attribute(core, core_model, "name", "core")
  183. instantiate_attribute(core, core_model, "location", coreM_model_location)
  184. instantiate_attribute(core, core_model, "permissions", "200")
  185. instance_of = instantiate_link(core, "instanceOf", "", core_model, core_formalism_model)
  186. instantiate_attribute(core, instance_of, "type_mapping", core["type_mapping"])
  187. // Make necessary links for the formalism to the owners
  188. instantiate_link(core, "group", "", core_model, admin_group)
  189. instantiate_link(core, "owner", "", core_model, admin_user)
  190. // Call this for ourselves as well
  191. log("MvC is ready!")
  192. user_function_skip_init(admin_user)
  193. // Done, so finish up
  194. // Admin user will have been deleted by the user_function as usual
  195. // Note that if there are no admin users left, it will be very difficult to manage, as nobody will have admin permissions!
  196. return !
  197. String function get_instanceOf_link(model_id : String):
  198. Element all_links
  199. String choice
  200. all_links = allOutgoingAssociationInstances(core, model_id, "instanceOf")
  201. if (read_nr_out(all_links) > 1):
  202. log("WARNING: multiple instanceOf relations were detected for this model; picking one at random!")
  203. elif (read_nr_out(all_links) == 0):
  204. log("ERROR: untyped model!")
  205. choice = set_pop(allOutgoingAssociationInstances(core, model_id, "instanceOf"))
  206. return choice!
  207. Element function get_full_model(model_id : String):
  208. Element m
  209. Element all_links
  210. String choice
  211. choice = get_instanceOf_link(model_id)
  212. m = create_node()
  213. dict_add(m, "model", import_node(read_attribute(core, model_id, "location")))
  214. dict_add(m, "type_mapping", read_attribute(core, choice, "type_mapping"))
  215. if (readAssociationDestination(core, choice) == model_id):
  216. // Found the meta-circular level, so we can stop!
  217. dict_add(m, "metamodel", m)
  218. else:
  219. dict_add(m, "metamodel", get_full_model(readAssociationDestination(core, choice)))
  220. return m!
  221. Integer function get_relation_to_model(user_id : String, model_id : String):
  222. if (set_in(allAssociationDestinations(core, model_id, "owner"), user_id)):
  223. // We are the owner
  224. return 0!
  225. else:
  226. String group_id
  227. group_id = set_pop(allAssociationDestinations(core, model_id, "group"))
  228. if (set_in(allAssociationDestinations(core, user_id, "belongsTo"), group_id)):
  229. // We are in the owning group
  230. return 1!
  231. else:
  232. // We are not related whatsoever
  233. return 2!
  234. Boolean function is_admin(user_id : String):
  235. if (read_attribute(core, user_id, "admin")):
  236. return True!
  237. else:
  238. return False!
  239. Boolean function allow_read(user_id : String, model_id : String):
  240. if (is_admin(user_id)):
  241. // Is admin, so always allow
  242. return True!
  243. else:
  244. // Check permissions
  245. String permission
  246. permission = string_get(read_attribute(core, model_id, "permissions"), get_relation_to_model(user_id, model_id))
  247. if (bool_or(permission == "1", permission == "2")):
  248. return True!
  249. else:
  250. return False!
  251. Boolean function allow_write(user_id : String, model_id : String):
  252. if (is_admin(user_id)):
  253. // Is admin, so always allow
  254. return True!
  255. else:
  256. // Check permissions
  257. String permission
  258. permission = string_get(read_attribute(core, model_id, "permissions"), get_relation_to_model(user_id, model_id))
  259. if (permission == "2"):
  260. return True!
  261. else:
  262. return False!
  263. Boolean function allow_change_metadata(user_id : String, model_id : String):
  264. if (is_admin(user_id)):
  265. // Is admin, so always allow
  266. return True!
  267. else:
  268. if (get_relation_to_model(user_id, model_id) == 0):
  269. // Only owner can chmod
  270. return True!
  271. else:
  272. return False!
  273. Boolean function allow_group_modify(user_id : String, group_id : String):
  274. if (is_admin(user_id)):
  275. // Is admin, so always allow
  276. return True!
  277. else:
  278. if (read_nr_out(set_overlap(allIncomingAssociationInstances(core, user_id, "owner"), allOutgoingAssociationInstances(core, group_id, "owner"))) > 0):
  279. // We are an owner
  280. return True!
  281. else:
  282. return False!
  283. Boolean function check_login(user_id : String):
  284. String password
  285. String stored_password
  286. stored_password = read_attribute(core, user_id, "password")
  287. output("Password for existing user?")
  288. password = hash(input())
  289. return password == stored_password!
  290. Element function extract_ftg(user_id : String):
  291. // Extract your personal FTG, showing only the readable models that can be opened
  292. // This function does the actual projection of transformations
  293. // TODO: Only keep transformations satisfying some properties!
  294. return create_node()!
  295. Element function user_function():
  296. String username
  297. String user_id
  298. String password
  299. // Load in all global variables, as this code is hotloaded!
  300. Element root
  301. root = read_root()
  302. root = root["__hierarchy"]["objects"]
  303. exec(root["bootstrap/modelling.alc"]["initializers"])
  304. exec(root["bootstrap/library.alc"]["initializers"])
  305. exec(root["bootstrap/constructors.alc"]["initializers"])
  306. exec(root["bootstrap/object_operations.alc"]["initializers"])
  307. exec(root["core/mini_modify.alc"]["initializers"])
  308. exec(root["bootstrap/model_management.alc"]["initializers"])
  309. exec(root["bootstrap/ramify.alc"]["initializers"])
  310. exec(root["bootstrap/transform.alc"]["initializers"])
  311. exec(root["bootstrap/conformance_scd.alc"]["initializers"])
  312. exec(root["core/core_algorithm.alc"]["initializers"])
  313. // Load in a hard-reference to the previously created model
  314. core = import_node(core_model_location)
  315. while (True):
  316. output("Log on as which user?")
  317. username = input()
  318. user_id = get_user_id(username)
  319. if (user_id == ""):
  320. // New user
  321. // Add user to Core Formalism
  322. user_id = instantiate_node(core, "User", "")
  323. instantiate_attribute(core, user_id, "name", username)
  324. instantiate_attribute(core, user_id, "admin", False)
  325. output("This is a new user: please give password!")
  326. password = hash(input())
  327. output("Please repeat the password")
  328. if (password == hash(input())):
  329. output("Passwords match!")
  330. instantiate_attribute(core, user_id, "password", password)
  331. break!
  332. else:
  333. output("Not the same password!")
  334. else:
  335. if (check_login(user_id)):
  336. break!
  337. else:
  338. output("Wrong password!")
  339. user_function_skip_init(user_id)
  340. // User destroyed already, so just stop execution
  341. // TODO return a fresh node as otherwise the compiler doesn't take this
  342. return create_node()!
  343. String function get_model_id(name : String):
  344. Element models
  345. String model
  346. models = allInstances(core, "Model")
  347. while (read_nr_out(models) > 0):
  348. model = set_pop(models)
  349. if (value_eq(name, read_attribute(core, model, "name"))):
  350. return model!
  351. return ""!
  352. String function get_user_id(name : String):
  353. Element users
  354. String user
  355. users = allInstances(core, "User")
  356. while (read_nr_out(users) > 0):
  357. user = set_pop(users)
  358. if (value_eq(read_attribute(core, user, "name"), name)):
  359. return user!
  360. return ""!
  361. String function get_group_id(name : String):
  362. Element groups
  363. String group
  364. groups = allInstances(core, "Group")
  365. while (read_nr_out(groups) > 0):
  366. group = set_pop(groups)
  367. if (value_eq(read_attribute(core, group, "name"), name)):
  368. return group!
  369. return ""!
  370. Void function model_create(model : Element, name : String, user_id : String, type_id : String, kind : String):
  371. String location
  372. String model_id
  373. String instance_of
  374. location = "models/" + cast_id2s(model)
  375. export_node(location, model["model"])
  376. // Manage meta-info
  377. model_id = instantiate_node(core, kind, "")
  378. instantiate_attribute(core, model_id, "name", name)
  379. instantiate_attribute(core, model_id, "location", location)
  380. instantiate_attribute(core, model_id, "permissions", "200")
  381. instantiate_link(core, "owner", "", model_id, user_id)
  382. instantiate_link(core, "group", "", model_id, get_group_id("nobody"))
  383. instance_of = instantiate_link(core, "instanceOf", "", model_id, type_id)
  384. instantiate_attribute(core, instance_of, "type_mapping", model["type_mapping"])
  385. return!
  386. Void function model_overwrite(model : Element, model_id : String):
  387. String location
  388. String instanceOf_link
  389. location = "models/" + cast_id2s(model)
  390. export_node(location, model["model"])
  391. // Change location in meta-data
  392. unset_attribute(core, model_id, "location")
  393. instantiate_attribute(core, model_id, "location", location)
  394. instanceOf_link = get_instanceOf_link(model_id)
  395. unset_attribute(core, instanceOf_link, "type_mapping")
  396. instantiate_attribute(core, instanceOf_link, "type_mapping", model["type_mapping"])
  397. return!
  398. Boolean function check_is_typed_by(model_id : String, metamodel_id : String):
  399. // TODO check if there is actually an instanceOf link between them
  400. // --> quick check!
  401. return True!
  402. Boolean function check_conformance(model_id : String):
  403. // TODO check if it actually conforms, considering that instanceOf link
  404. // --> in-depth check
  405. return True!
  406. Boolean function pm_finished(worklist : Element, pm : String):
  407. Element finished
  408. Integer cnt
  409. Integer i
  410. // Check if any of the "finish" elements are in the worklist
  411. // If so, we can already finish, and therefore will stop immediately
  412. finished = allInstances(pm, "Finish")
  413. i = 0
  414. cnt = read_nr_out(finished)
  415. while (i < cnt):
  416. // Check each finished element individually
  417. if (set_in(finished, dict_read(read_edge_dst(read_out(worklist, i)), 0))):
  418. return True!
  419. i = i + 1
  420. return False!
  421. Element function execute_operation(operation_id : String, input_models : Element, output_metamodels : Element, tracability_model : Element):
  422. // Operations are always in-place and uses only a single metamodel
  423. // Therefore, we must:
  424. // 1) Find merged metamodel
  425. // 2) Merge the different source models and retype
  426. // 3) Perform the operation on the merged model
  427. // 4) Split the resulting model based on the target formalisms; if operation successful
  428. Element input_model
  429. String trace_link_id
  430. Element merged_model
  431. String merged_metamodel_id
  432. String ramified_metamodel_id
  433. Boolean result
  434. String exact_type
  435. Element trace_links
  436. String linktype
  437. String model_ID
  438. String key
  439. Element keys
  440. Element input_keys
  441. Element output_keys
  442. Element model_tuples
  443. String metamodel_name
  444. Element metamodel
  445. String metamodel_ID
  446. Boolean tracability
  447. // 1) Find merged metamodel
  448. exact_type = read_type(core, operation_id)
  449. if (exact_type == "ModelTransformation"):
  450. linktype = "RAMified"
  451. ramified_metamodel_id = set_pop(allAssociationDestinations(core, operation_id, "instanceOf"))
  452. trace_links = allOutgoingAssociationInstances(core, ramified_metamodel_id, "tracability")
  453. elif (exact_type == "ManualOperation"):
  454. linktype = "operatesOn"
  455. trace_links = allOutgoingAssociationInstances(core, operation_id, "tracability")
  456. elif (exact_type == "ActionLanguage"):
  457. linktype = "operatesOn"
  458. trace_links = allOutgoingAssociationInstances(core, operation_id, "tracability")
  459. else:
  460. // Don't know how to execute this operation!
  461. return read_root()!
  462. merged_metamodel_id = ""
  463. while (read_nr_out(trace_links) > 0):
  464. trace_link_id = set_pop(trace_links)
  465. if (value_eq(read_attribute(core, trace_link_id, "type"), linktype)):
  466. merged_metamodel_id = readAssociationDestination(core, trace_link_id)
  467. if (merged_metamodel_id != ""):
  468. // 2) Merge source models
  469. model_tuples = create_node()
  470. while (read_nr_out(input_models) > 0):
  471. model_ID = get_model_id(set_pop(input_models))
  472. input_model = get_full_model(model_ID)
  473. metamodel_name = read_attribute(core, set_pop(allAssociationDestinations(core, model_ID, "instanceOf")), "name")
  474. set_add(model_tuples, create_tuple(metamodel_name, input_model))
  475. merged_model = model_join(model_tuples, get_full_model(merged_metamodel_id), read_root())
  476. // 3) Transform
  477. if (exact_type == "ModelTransformation"):
  478. result = transform(merged_model, get_full_model(operation_id))
  479. elif (exact_type == "ManualOperation"):
  480. output("Please perform manual operation " + cast_v2s(read_attribute(core, operation_id, "name")))
  481. modify(merged_model, True)
  482. result = True
  483. elif (exact_type == "ActionLanguage"):
  484. Element func
  485. func = get_func_AL_model(get_full_model(operation_id))
  486. result = func(merged_model)
  487. else:
  488. log("ERROR")
  489. // 4) Split in different models depending on type
  490. if (element_neq(tracability_model, read_root())):
  491. tracability = True
  492. else:
  493. tracability = False
  494. if (result):
  495. model_tuples = create_node()
  496. while (read_nr_out(output_metamodels) > 0):
  497. metamodel_ID = get_model_id(set_pop(output_metamodels))
  498. metamodel_name = read_attribute(core, metamodel_ID, "name")
  499. metamodel = get_full_model(metamodel_ID)
  500. set_add(model_tuples, create_tuple(metamodel_name, metamodel))
  501. result = model_split(merged_model, model_tuples, tracability)
  502. if (tracability):
  503. Element new_tracability_model
  504. new_tracability_model = result["__tracability"]
  505. dict_overwrite(tracability_model, "model", new_tracability_model["model"])
  506. dict_overwrite(tracability_model, "type_mapping", new_tracability_model["type_mapping"])
  507. dict_overwrite(tracability_model, "metamodel", new_tracability_model["metamodel"])
  508. dict_delete(result, "__tracability")
  509. return result!
  510. else:
  511. return read_root()!
  512. else:
  513. output("Could not resolve intermediate merged metamodel")
  514. return read_root()!
  515. Boolean function enact_action(pm : Element, element : String, prefix : String, user_id : String):
  516. Boolean result
  517. String transformation_id
  518. Element lst
  519. String elem
  520. Element inputs
  521. Element outputs
  522. String type_name
  523. String exact_type
  524. Element trace_links
  525. Element output_mms
  526. Element consumes_link
  527. String name
  528. String value
  529. String elem_name
  530. Element keys
  531. String key
  532. inputs = create_node()
  533. outputs = create_node()
  534. // Read out the referenced element from the MvC
  535. transformation_id = get_model_id(read_attribute(pm, element, "name"))
  536. // Find all input model names
  537. lst = allOutgoingAssociationInstances(pm, element, "Consumes")
  538. while (read_nr_out(lst) > 0):
  539. value = read_attribute(pm, readAssociationDestination(pm, set_pop(lst)), "name")
  540. set_add(inputs, prefix + value)
  541. // Find all output model names and their metamodel
  542. lst = allAssociationDestinations(pm, element, "Produces")
  543. while (read_nr_out(lst) > 0):
  544. elem = set_pop(lst)
  545. type_name = read_attribute(pm, elem, "type")
  546. elem_name = read_attribute(pm, elem, "name")
  547. dict_add(outputs, type_name, prefix + elem_name)
  548. result = execute_operation(transformation_id, inputs, dict_keys(outputs), read_root())
  549. if (element_eq(result, read_root())):
  550. // Something went wrong!
  551. return False!
  552. else:
  553. keys = dict_keys(result)
  554. while (read_nr_out(keys) > 0):
  555. key = set_pop(keys)
  556. if (get_model_id(outputs[key]) == ""):
  557. // New model
  558. model_create(result[key], outputs[key], user_id, get_model_id(key), "Model")
  559. else:
  560. model_overwrite(result[key], get_model_id(outputs[key]))
  561. return True!
  562. Void function enact_PM(pm : Element, prefix : String, user_id : String):
  563. Element worklist
  564. String element
  565. String start
  566. String type
  567. Boolean result
  568. Element tuple
  569. Element counters
  570. Element join_nodes
  571. // Initialize Join counters
  572. counters = create_node()
  573. join_nodes = allInstances(pm, "Join")
  574. while (read_nr_out(join_nodes) > 0):
  575. dict_add(counters, set_pop(join_nodes), 0)
  576. // Create the worklist with the Start instance as first element
  577. worklist = create_node()
  578. set_add(worklist, create_tuple(set_pop(allInstances(pm, "Start")), True))
  579. while (bool_not(pm_finished(worklist, pm))):
  580. // Pop a random element from the list and execute it
  581. tuple = set_pop(worklist)
  582. element = tuple[0]
  583. result = tuple[1]
  584. log("Enacting " + cast_v2s(element))
  585. // Find the type (to see what to do with it)
  586. // this does not yet yield the type of transformation, if it is an Execution
  587. type = read_type(pm, element)
  588. if (type == "Start"):
  589. // Initial node, just progress to the next elements
  590. // Nothing to do here though, as we have just started
  591. result = True
  592. elif (type == "Finish"):
  593. // Should be impossible, as we would have ended...
  594. result = result
  595. elif (type == "Fork"):
  596. result = result
  597. elif (type == "Join"):
  598. // Only do this if all dependencies are fullfilled
  599. // So add to the counter of this Join
  600. dict_overwrite(counters, element, integer_addition(counters[element], 1))
  601. // Now check whether we have enough tokens to execute the Join itself
  602. Integer required
  603. Integer got
  604. required = read_nr_out(allIncomingAssociationInstances(pm, element, "Next")) + read_nr_out(allIncomingAssociationInstances(pm, element, "Else"))
  605. got = counters[element]
  606. if (got == required):
  607. // Reset counter to 0
  608. dict_overwrite(counters, element, 0)
  609. // And continue
  610. else:
  611. // We haven't gotten all yet, so we wait (i.e., continue without adding Next link to worklist)
  612. continue!
  613. elif (type == "Exec"):
  614. // Execute a transformation
  615. // This the difficult part!
  616. result = enact_action(pm, element, prefix, user_id)
  617. elif (type == "Decision"):
  618. // If the previous result is true, we add the normal one, otherwise the false one
  619. // in this context, that means that if it is false, we should add it manually to the list, and then continue the simulation loop
  620. if (bool_not(result)):
  621. // Apparently it is False, so map this to the "Else" branch
  622. set_add(worklist, create_tuple(set_pop(allAssociationDestinations(pm, element, "Else")), True))
  623. continue!
  624. else:
  625. // Apparently it is True, so map this to the "Then" branch
  626. set_add(worklist, create_tuple(set_pop(allAssociationDestinations(pm, element, "Then")), True))
  627. continue!
  628. // We have finished the execution, so add all outgoing edges to the worklist
  629. Element all_next
  630. all_next = allAssociationDestinations(pm, element, "Next")
  631. String next
  632. while (read_nr_out(all_next) > 0):
  633. next = set_pop(all_next)
  634. set_add(worklist, create_tuple(next, result))
  635. // Reached a finish element, so stop
  636. return !
  637. Void function user_function_skip_init(user_id : String):
  638. String cmd
  639. output("Welcome to the Model Management Interface v2.0!")
  640. output("Use the 'help' command for a list of possible commands")
  641. while (True):
  642. output("Ready for command...")
  643. cmd = input()
  644. if (cmd == "help"):
  645. output("Model operations")
  646. output(" model_add -- Add a new model")
  647. output(" model_modify -- Modify an existing model")
  648. output(" model_list -- List all models")
  649. output(" model_list_full -- List all models with full info")
  650. output(" model_overwrite -- Overwrites a model with an uploaded model, leaving all metadata")
  651. output(" model_render -- Render a given model with a specified mapper")
  652. output(" verify -- Check whether a model conforms to its metamodel")
  653. output("")
  654. output("Transformation-specific operations")
  655. output(" transformation_add_MT_language -- Create a RAMified metamodel of a set of models")
  656. output(" transformation_add_MT -- Initialize a new model transformation")
  657. output(" transformation_add_AL -- Initialize a new action language transformation")
  658. output(" transformation_add_MANUAL -- Initialize a new manual transformation")
  659. output(" transformation_execute -- Execute a transformation on a set of input models")
  660. output(" transformation_list -- List all model transformations")
  661. output(" transformation_list_full -- List all model transformations with permissions")
  662. output(" transformation_detail -- List transformation details")
  663. output(" transformation_RAMify -- RAMify a metamodel (again)")
  664. output("")
  665. output("Process operations")
  666. output(" process_execute -- Execute a process model")
  667. output("")
  668. output("Model permission operations")
  669. output(" permission_modify -- Change model permissions")
  670. output(" permission_owner -- Change model owner")
  671. output(" permission_group -- Change model group")
  672. output("")
  673. output("Group operations")
  674. output(" group_create -- Create a group")
  675. output(" group_delete -- Delete a group")
  676. output(" group_owner_add -- Add group owner")
  677. output(" group_owner_delete -- Remove group owner")
  678. output(" group_join -- Add someone to your group")
  679. output(" group_kick -- Kick someone from your group")
  680. output(" group_list -- List all groups you are a member of")
  681. output("")
  682. output("Admin operations")
  683. output(" admin_promote -- Promote a user to admin status")
  684. output(" admin_demote -- Demote a user to normal status")
  685. output("")
  686. output("General operations")
  687. output(" self-destruct -- Remove current user and revoke all permissions ")
  688. output(" exit -- Kill the current task, while retaining user")
  689. elif (cmd == "model_add"):
  690. // Model addition operation, which uses model upload commands of the compiler
  691. String name
  692. String type
  693. String location
  694. String type_id
  695. Element new_model
  696. String new_model_id
  697. output("Creating new model!")
  698. output("Model type?")
  699. type = input()
  700. type_id = get_model_id(type)
  701. if (type_id != ""):
  702. // Type exists
  703. if (allow_read(user_id, type_id)):
  704. // And is readable
  705. output("Model name?")
  706. name = input()
  707. if (get_model_id(name) == ""):
  708. // Model doesn't exist yet
  709. output("Waiting for model constructors...")
  710. new_model = construct_model_raw(get_full_model(type_id))
  711. model_create(new_model, name, user_id, type_id, "Model")
  712. output("Model upload success!")
  713. else:
  714. output("Model with that name already exists!")
  715. else:
  716. output("Permission denied")
  717. else:
  718. output("Could not find type model: " + type)
  719. elif (cmd == "process_execute"):
  720. // Execute a process model until it reaches termination
  721. String process_id
  722. output("Which process model do you want to execute?")
  723. process_id = get_model_id(input())
  724. if (process_id != ""):
  725. if (allow_read(user_id, process_id)):
  726. output("Model prefix to use?")
  727. enact_PM(get_full_model(process_id), input(), user_id)
  728. else:
  729. output("Permission denied")
  730. else:
  731. output("No such model")
  732. elif (cmd == "model_render"):
  733. String model_name
  734. String model_ID
  735. String mapper_name
  736. String mapper_ID
  737. String rendered_name
  738. String tracability_name
  739. String type_ID
  740. Element inputs
  741. Element outputs
  742. Element rendered_model
  743. Element tracability_model
  744. Element result
  745. output("Model to render?")
  746. model_name = input()
  747. model_ID = get_model_id(model_name)
  748. if (model_ID != ""):
  749. output("Mapper to use?")
  750. mapper_name = input()
  751. mapper_ID = get_model_id(mapper_name)
  752. if (allow_read(user_id, model_ID)):
  753. if (mapper_ID != ""):
  754. if (allow_read(user_id, mapper_ID)):
  755. // Everything is fine; start the actual operation
  756. // Find metamodel to render to
  757. rendered_name = (("__RENDERED_" + model_name) + "__") + mapper_name
  758. tracability_name = (("__TRACABILITY_" + model_name) + "__") + mapper_name
  759. // Take the abstract syntax model and the previously rendered model
  760. inputs = create_node()
  761. set_add(inputs, model_name)
  762. set_add(inputs, rendered_name)
  763. // Generate a new rendered model only (no write to original model!)
  764. outputs = create_node()
  765. type_ID = set_pop(allAssociationDestinations(core, mapper_ID, "transformOutput"))
  766. set_add(outputs, read_attribute(core, type_ID, "name"))
  767. // Rendered model doesn't exist yet, so create first
  768. if (get_model_id(rendered_name) == ""):
  769. // Rendered model doesn't exist yet, so create first!
  770. rendered_model = instantiate_model(get_full_model(type_ID))
  771. model_create(rendered_model, rendered_name, user_id, type_ID, "Model")
  772. // Tracability model won't exist either
  773. tracability_model = instantiate_model(get_full_model(get_model_id("Tracability")))
  774. model_create(tracability_model, tracability_name, user_id, get_model_id("Tracability"), "Model")
  775. else:
  776. // Read out tracability model
  777. tracability_model = get_full_model(get_model_id(tracability_name))
  778. // Do the operation itself!
  779. result = execute_operation(mapper_ID, inputs, outputs, tracability_model)
  780. // Overwrite the previous rendered model; tracability updated in-place
  781. model_overwrite(result[read_attribute(core, type_ID, "name")], get_model_id(rendered_name))
  782. // Also output the resulting model
  783. output("Mapping success")
  784. output(JSON_print(get_full_model(get_model_id(rendered_name))))
  785. else:
  786. output("Permission denied")
  787. else:
  788. output("No such mapper")
  789. else:
  790. output("Permission denied")
  791. else:
  792. output("No such model")
  793. elif (cmd == "transformation_execute"):
  794. // Execute a transformation, whatever type it is
  795. // First we detect the type, so we know how to prepare for invocation
  796. String transformation_id
  797. String exact_type
  798. Element sources
  799. Element targets
  800. String source
  801. String target
  802. String name_id
  803. Element inputs
  804. Element outputs
  805. Element trace_links
  806. String target_model_name
  807. String source_model_name
  808. String source_model_ID
  809. Element result
  810. Element keys
  811. String key
  812. output("Which transformation do you want to execute?")
  813. transformation_id = input()
  814. transformation_id = get_model_id(transformation_id)
  815. if (transformation_id != ""):
  816. if (allow_read(user_id, transformation_id)):
  817. if (is_nominal_instance(core, transformation_id, "Transformation")):
  818. // Read out source and target links
  819. sources = allOutgoingAssociationInstances(core, transformation_id, "transformInput")
  820. inputs = create_node()
  821. while (read_nr_out(sources) > 0):
  822. source = set_pop(sources)
  823. output(string_join("Which model to bind for source type ", read_attribute(core, readAssociationDestination(core, source), "name")))
  824. source_model_name = input()
  825. source_model_ID = get_model_id(source_model_name)
  826. if (name_id != ""):
  827. if (allow_read(user_id, name_id)):
  828. // Check for conformance to the requested metamodel
  829. if (check_is_typed_by(source_model_ID, source)):
  830. if (check_conformance(source_model_ID)):
  831. set_add(inputs, source_model_name)
  832. continue!
  833. else:
  834. output("Model has correct type but does not conform completely!")
  835. else:
  836. output("Model has different type!")
  837. else:
  838. output("Permission denied")
  839. else:
  840. output("No such model")
  841. // Retry this model type
  842. set_add(sources, source)
  843. targets = allOutgoingAssociationInstances(core, transformation_id, "transformOutput")
  844. outputs = create_node()
  845. while (read_nr_out(targets) > 0):
  846. target = set_pop(targets)
  847. output(string_join("Which model to create for target type ", read_attribute(core, readAssociationDestination(core, target), "name")))
  848. target_model_name = input()
  849. if (get_model_id(target_model_name) == ""):
  850. // Doesn't exist yet, so we can easily create
  851. dict_add(outputs, read_attribute(core, readAssociationDestination(core, target), "name"), target_model_name)
  852. else:
  853. // Already exists, so we need to check for write access
  854. if (allow_write(user_id, get_model_id(target_model_name))):
  855. dict_add(outputs, read_attribute(core, readAssociationDestination(core, target), "name"), target_model_name)
  856. else:
  857. output("Permission denied; try again")
  858. set_add(targets, target)
  859. result = execute_operation(transformation_id, inputs, dict_keys(outputs), read_root())
  860. // Now write out the models again
  861. if (element_eq(result, read_root())):
  862. // Something went wrong!
  863. log("Transformation failed; discarding changes")
  864. else:
  865. keys = dict_keys(result)
  866. while (read_nr_out(keys) > 0):
  867. key = set_pop(keys)
  868. if (get_model_id(outputs[key]) == ""):
  869. // New model
  870. model_create(result[key], outputs[key], user_id, get_model_id(key), "Model")
  871. else:
  872. model_overwrite(result[key], get_model_id(outputs[key]))
  873. else:
  874. output("Model is not an executable transformation")
  875. else:
  876. output("Permission denied")
  877. else:
  878. output("No such transformation")
  879. elif (cmd == "verify"):
  880. // Check whether a model conforms to its specification (with the selected type mapping)
  881. String model_id
  882. String result
  883. output("Which model to verify?")
  884. model_id = get_model_id(input())
  885. if (model_id != ""):
  886. if (allow_read(user_id, model_id)):
  887. output("Verifying model...")
  888. result = conformance_scd(get_full_model(model_id))
  889. output(result)
  890. else:
  891. output("Permission denied")
  892. else:
  893. output("No such model")
  894. elif (cmd == "model_overwrite"):
  895. // Overwrites an existing model without changing any metadata
  896. String model_id
  897. Element new_model
  898. output("Which model to overwrite?")
  899. model_id = get_model_id(input())
  900. if (model_id != ""):
  901. if (allow_write(user_id, model_id)):
  902. if (allow_read(user_id, set_pop(allAssociationDestinations(core, model_id, "instanceOf")))):
  903. output("Waiting for model constructors...")
  904. new_model = construct_model_raw(get_full_model(set_pop(allAssociationDestinations(core, model_id, "instanceOf"))))
  905. model_overwrite(new_model, model_id)
  906. output("Model overwrite success!")
  907. else:
  908. output("Permission denied")
  909. else:
  910. output("Permission denied")
  911. else:
  912. output("No such model")
  913. elif (cmd == "model_modify"):
  914. // Model modify operation, which uses the mini_modify.alc operations, though with extensions for access control
  915. String model_id
  916. String type_id
  917. output("Which model do you want to modify?")
  918. model_id = get_model_id(input())
  919. if (model_id != ""):
  920. if (allow_read(user_id, model_id)):
  921. type_id = set_pop(allAssociationDestinations(core, model_id, "instanceOf"))
  922. if (allow_read(user_id, type_id)):
  923. modify(get_full_model(model_id), allow_write(user_id, model_id))
  924. else:
  925. output("Permission denied")
  926. else:
  927. output("Permission denied")
  928. else:
  929. output("Could not find model!")
  930. elif (cmd == "model_delete"):
  931. // Delete a model and all of its related transformations
  932. String model_id
  933. output("=================================================")
  934. output("WARNING: Deletion is a very destructive operation")
  935. output(" as it also deletes all transformations ")
  936. output(" defined which make use of this model! ")
  937. output("=================================================")
  938. output("")
  939. output("Currently not supported!")
  940. elif (cmd == "model_list"):
  941. // List all models
  942. Element models
  943. String m
  944. models = allInstances(core, "Model")
  945. while (read_nr_out(models) > 0):
  946. m = set_pop(models)
  947. output(string_join((string_join(" ", read_attribute(core, m, "name")) + " : "), read_attribute(core, set_pop(allAssociationDestinations(core, m, "instanceOf")), "name")))
  948. elif (cmd == "model_list_full"):
  949. // List all models with full info
  950. Element models
  951. String m
  952. String permissions
  953. String owner
  954. String group
  955. String name
  956. String type
  957. models = allInstances(core, "Model")
  958. while (read_nr_out(models) > 0):
  959. m = set_pop(models)
  960. permissions = read_attribute(core, m, "permissions")
  961. owner = read_attribute(core, set_pop(allAssociationDestinations(core, m, "owner")), "name")
  962. group = read_attribute(core, set_pop(allAssociationDestinations(core, m, "group")), "name")
  963. name = read_attribute(core, m, "name")
  964. type = read_attribute(core, set_pop(allAssociationDestinations(core, m, "instanceOf")), "name")
  965. output(((((((((" " + permissions) + " ") + owner) + " ") + group) + " ") + name) + " : ") + type)
  966. elif (cmd == "transformation_add_MT_language"):
  967. // Create a model transformation language from a set of input and output formalisms
  968. String name
  969. String model_id
  970. Element source
  971. Element target
  972. Element all_formalisms
  973. Element merged_formalism
  974. Element ramified_formalism
  975. String old_type_id
  976. String type_id
  977. String location
  978. String new_model_id
  979. old_type_id = ""
  980. // Read involved formalisms
  981. all_formalisms = create_node()
  982. output("Formalisms to include (terminate with empty string)?")
  983. name = input()
  984. while (name != ""):
  985. model_id = get_model_id(name)
  986. if (model_id != ""):
  987. if (allow_read(user_id, model_id)):
  988. type_id = set_pop(allAssociationDestinations(core, model_id, "instanceOf"))
  989. if (bool_or(old_type_id == "", type_id == old_type_id)):
  990. set_add(all_formalisms, create_tuple(name, get_full_model(model_id)))
  991. old_type_id = type_id
  992. elif (old_type_id != type_id):
  993. // Already have a previous type_id and now another: CLASH
  994. output("Cannot add model as types not compatible with previous models; try again")
  995. else:
  996. output("Model not readable; try again")
  997. else:
  998. output("No such model; try again")
  999. name = input()
  1000. // Merge all into a single metamodel
  1001. if (read_nr_out(all_formalisms) > 0):
  1002. output("Name of the RAMified transformation metamodel?")
  1003. name = input()
  1004. if (get_model_id(name) == ""):
  1005. String merged_formalism_id
  1006. String ramified_formalism_id
  1007. String source_formalism_id
  1008. String tracability_link
  1009. // New location is available, so write
  1010. merged_formalism = model_fuse(set_copy(all_formalisms))
  1011. model_create(merged_formalism, "__merged_" + name, user_id, type_id, "Model")
  1012. merged_formalism_id = get_model_id("__merged_" + name)
  1013. // Add tracability links at this level
  1014. while (read_nr_out(all_formalisms) > 0):
  1015. source_formalism_id = get_model_id(list_read(set_pop(all_formalisms), 0))
  1016. tracability_link = instantiate_link(core, "tracability", "", merged_formalism_id, source_formalism_id)
  1017. instantiate_attribute(core, tracability_link, "type", "merged")
  1018. // Merge complete, now RAMify!
  1019. ramified_formalism = ramify(merged_formalism)
  1020. model_create(ramified_formalism, name, user_id, type_id, "Model")
  1021. ramified_formalism_id = get_model_id(name)
  1022. // Add tracability link at this level
  1023. tracability_link = instantiate_link(core, "tracability", "", ramified_formalism_id, merged_formalism_id)
  1024. instantiate_attribute(core, tracability_link, "type", "RAMified")
  1025. else:
  1026. output("Model already exists!")
  1027. else:
  1028. output("At least one formalism is required")
  1029. elif (cmd == "transformation_RAMify"):
  1030. // RAMify a metamodel
  1031. String merged_model_id
  1032. String target_model_name
  1033. String target_model_id
  1034. Element target_model
  1035. String tracability_link
  1036. output("Which metamodel do you want to RAMify?")
  1037. merged_model_id = get_model_id(input())
  1038. if (merged_model_id != ""):
  1039. if (allow_read(user_id, merged_model_id)):
  1040. output("Where do you want to store the RAMified metamodel?")
  1041. target_model_name = input()
  1042. target_model_id = get_model_id(target_model_name)
  1043. if (target_model_id == ""):
  1044. // New model, so everything is fine
  1045. target_model = ramify(get_full_model(merged_model_id))
  1046. model_create(target_model, target_model_name, user_id, set_pop(allAssociationDestinations(core, merged_model_id, "instanceOf")), "Model")
  1047. target_model_id = get_model_id(target_model_name)
  1048. tracability_link = instantiate_link(core, "tracability", "", target_model_id, merged_model_id)
  1049. instantiate_attribute(core, tracability_link, "type", "RAMified")
  1050. else:
  1051. // Existing model, so overwrite
  1052. if (allow_write(user_id, target_model_id)):
  1053. target_model = ramify(get_full_model(merged_model_id))
  1054. model_overwrite(target_model, target_model_id)
  1055. model_delete_element(core, set_pop(allOutgoingAssociationInstances(core, target_model_id, "tracability")))
  1056. tracability_link = instantiate_link(core, "tracability", "", target_model_id, merged_model_id)
  1057. instantiate_attribute(core, tracability_link, "type", "RAMified")
  1058. else:
  1059. output("Permission denied")
  1060. else:
  1061. output("Permission denied!")
  1062. else:
  1063. output("No such model")
  1064. elif (bool_or(cmd == "transformation_add_MANUAL", cmd == "transformation_add_AL")):
  1065. // Add a manual transformation model
  1066. String model_id
  1067. Element models
  1068. Element source
  1069. Element target
  1070. String name
  1071. Element all_formalisms
  1072. Element merged_formalism
  1073. String merged_formalism_id
  1074. String source_formalism_id
  1075. String tracability_link
  1076. String type_id
  1077. String old_type_id
  1078. source = create_node()
  1079. target = create_node()
  1080. all_formalisms = create_node()
  1081. type_id = ""
  1082. old_type_id = ""
  1083. output("Which metamodels do you want to use as source for the operation (empty string to finish)?")
  1084. name = input()
  1085. while (name != ""):
  1086. model_id = get_model_id(name)
  1087. if (model_id != ""):
  1088. if (allow_read(user_id, model_id)):
  1089. if (bool_not(set_in(source, model_id))):
  1090. type_id = set_pop(allAssociationDestinations(core, model_id, "instanceOf"))
  1091. if (bool_or(old_type_id == "", type_id == old_type_id)):
  1092. old_type_id = type_id
  1093. set_add(source, model_id)
  1094. set_add(all_formalisms, create_tuple(name, get_full_model(model_id)))
  1095. elif (old_type_id != type_id):
  1096. // Already have a previous type_id and now another: CLASH
  1097. output("Cannot add model as types not compatible with previous models; try again")
  1098. else:
  1099. output("Model already selected as source")
  1100. else:
  1101. output("Permission denied")
  1102. else:
  1103. output("No such model; try again")
  1104. name = input()
  1105. output("Which metamodels do you want to use as target for the operation (empty string to finish)?")
  1106. name = input()
  1107. while (name != ""):
  1108. model_id = get_model_id(name)
  1109. if (model_id != ""):
  1110. if (allow_read(user_id, model_id)):
  1111. if (bool_not(set_in(target, model_id))):
  1112. type_id = set_pop(allAssociationDestinations(core, model_id, "instanceOf"))
  1113. if (bool_or(old_type_id == "", type_id == old_type_id)):
  1114. old_type_id = type_id
  1115. set_add(target, model_id)
  1116. set_add(all_formalisms, create_tuple(name, get_full_model(model_id)))
  1117. elif (old_type_id != type_id):
  1118. // Already have a previous type_id and now another: CLASH
  1119. output("Cannot add model as types not compatible with previous models; try again")
  1120. else:
  1121. output("Model already selected as target")
  1122. else:
  1123. output("Permission denied")
  1124. else:
  1125. output("No such model; try again")
  1126. name = input()
  1127. output("Name of operation model?")
  1128. name = input()
  1129. if (get_model_id(name) == ""):
  1130. if (cmd == "transformation_add_MANUAL"):
  1131. // Finished with all information, now create the model itself!
  1132. model_create(instantiate_model(get_full_model(get_model_id("ManualOperation"))), name, user_id, get_model_id("ManualOperation"), "ManualOperation")
  1133. model_id = get_model_id(name)
  1134. elif (cmd == "transformation_add_AL"):
  1135. // Finished with all information, now create the model itself!
  1136. output("Waiting for model constructors...")
  1137. add_code_model(get_full_model(get_model_id("ActionLanguage")), "AL/" + name, construct_function())
  1138. model_create(import_node("AL/" + name), name, user_id, get_model_id("ActionLanguage"), "ActionLanguage")
  1139. model_id = get_model_id(name)
  1140. // Write out a merged metamodel containing all these models: this is the MM for the manual operation
  1141. // New location is available, so write
  1142. merged_formalism = model_fuse(set_copy(all_formalisms))
  1143. model_create(merged_formalism, "__merged_" + name, user_id, type_id, "Model")
  1144. merged_formalism_id = get_model_id("__merged_" + name)
  1145. // Add tracability links at this level
  1146. while (read_nr_out(all_formalisms) > 0):
  1147. source_formalism_id = get_model_id(list_read(set_pop(all_formalisms), 0))
  1148. tracability_link = instantiate_link(core, "tracability", "", merged_formalism_id, source_formalism_id)
  1149. instantiate_attribute(core, tracability_link, "type", "merged")
  1150. tracability_link = instantiate_link(core, "tracability", "", model_id, merged_formalism_id)
  1151. instantiate_attribute(core, tracability_link, "type", "operatesOn")
  1152. // Extend metadata with info on source and target
  1153. String link
  1154. String dst
  1155. while (read_nr_out(source) > 0):
  1156. dst = set_pop(source)
  1157. link = instantiate_link(core, "transformInput", "", model_id, dst)
  1158. while (read_nr_out(target) > 0):
  1159. dst = set_pop(target)
  1160. link = instantiate_link(core, "transformOutput", "", model_id, dst)
  1161. else:
  1162. output("Model already exists")
  1163. elif (cmd == "transformation_add_MT"):
  1164. // Add a model transformation model
  1165. // Just a usual model instantiation, but need to add the source and target links based on user info
  1166. String ramified_metamodel_id
  1167. String model_id
  1168. Element models
  1169. Element links
  1170. String link_id
  1171. Element supported
  1172. Element source
  1173. Element target
  1174. String name
  1175. String new_model_id
  1176. String merged_link_id
  1177. Element links_merged
  1178. String merged_metamodel_id
  1179. source = create_node()
  1180. target = create_node()
  1181. supported = create_node()
  1182. output("RAMified metamodel to use?")
  1183. ramified_metamodel_id = get_model_id(input())
  1184. if (ramified_metamodel_id != ""):
  1185. if (allow_read(user_id, ramified_metamodel_id)):
  1186. output("Supported metamodels:")
  1187. links = allOutgoingAssociationInstances(core, ramified_metamodel_id, "tracability")
  1188. while (read_nr_out(links) > 0):
  1189. link_id = set_pop(links)
  1190. merged_metamodel_id = readAssociationDestination(core, link_id)
  1191. if (value_eq(read_attribute(core, link_id, "type"), "RAMified")):
  1192. links_merged = allOutgoingAssociationInstances(core, merged_metamodel_id, "tracability")
  1193. while (read_nr_out(links_merged) > 0):
  1194. merged_link_id = set_pop(links_merged)
  1195. if (value_eq(read_attribute(core, merged_link_id, "type"), "merged")):
  1196. output(string_join(" ", read_attribute(core, readAssociationDestination(core, merged_link_id), "name")))
  1197. set_add(supported, readAssociationDestination(core, merged_link_id))
  1198. output("Which metamodels do you want to use as source for the operation (empty string to finish)?")
  1199. name = input()
  1200. while (name != ""):
  1201. model_id = get_model_id(name)
  1202. if (model_id != ""):
  1203. if (set_in(supported, model_id)):
  1204. if (bool_not(set_in(source, model_id))):
  1205. set_add(source, model_id)
  1206. else:
  1207. output("Model already selected as source")
  1208. else:
  1209. output("Model is not supported by RAMified metamodel!")
  1210. else:
  1211. output("No such model; try again")
  1212. name = input()
  1213. output("Which metamodels do you want to use as target for the operation (empty string to finish)?")
  1214. name = input()
  1215. while (name != ""):
  1216. model_id = get_model_id(name)
  1217. if (model_id != ""):
  1218. if (set_in(supported, model_id)):
  1219. if (bool_not(set_in(target, model_id))):
  1220. set_add(target, model_id)
  1221. else:
  1222. output("Model already selected as target")
  1223. else:
  1224. output("Model is not supported by RAMified metamodel!")
  1225. else:
  1226. output("No such model; try again")
  1227. name = input()
  1228. output("Name of new operation?")
  1229. name = input()
  1230. if (get_model_id(name) == ""):
  1231. String new_model
  1232. // Finished with all information, now create the model itself!
  1233. output("Waiting for model constructors...")
  1234. new_model = construct_model_raw(get_full_model(ramified_metamodel_id))
  1235. model_create(new_model, name, user_id, ramified_metamodel_id, "ModelTransformation")
  1236. model_id = get_model_id(name)
  1237. // Extend metadata with info on source and target
  1238. String link
  1239. String dst
  1240. while (read_nr_out(source) > 0):
  1241. dst = set_pop(source)
  1242. link = instantiate_link(core, "transformInput", "", model_id, dst)
  1243. while (read_nr_out(target) > 0):
  1244. dst = set_pop(target)
  1245. link = instantiate_link(core, "transformOutput", "", model_id, dst)
  1246. else:
  1247. output("Model already exists")
  1248. else:
  1249. output("Permission denied")
  1250. else:
  1251. output("No such model")
  1252. elif (cmd == "transformation_list"):
  1253. // List all models
  1254. Element models
  1255. String m
  1256. String type
  1257. models = allInstances(core, "Transformation")
  1258. while (read_nr_out(models) > 0):
  1259. m = set_pop(models)
  1260. output(string_join(("[" + read_type(core, m)) + "]", string_join((string_join(" ", read_attribute(core, m, "name")) + " : "), read_attribute(core, set_pop(allAssociationDestinations(core, m, "instanceOf")), "name"))))
  1261. elif (cmd == "transformation_list_full"):
  1262. // List all models with full info
  1263. Element models
  1264. String m
  1265. String permissions
  1266. String owner
  1267. String group
  1268. String name
  1269. String type
  1270. models = allInstances(core, "Transformation")
  1271. while (read_nr_out(models) > 0):
  1272. m = set_pop(models)
  1273. permissions = read_attribute(core, m, "permissions")
  1274. owner = read_attribute(core, set_pop(allAssociationDestinations(core, m, "owner")), "name")
  1275. group = read_attribute(core, set_pop(allAssociationDestinations(core, m, "group")), "name")
  1276. name = read_attribute(core, m, "name")
  1277. type = read_attribute(core, set_pop(allAssociationDestinations(core, m, "instanceOf")), "name")
  1278. output(((((((((" " + permissions) + " ") + owner) + " ") + group) + " ") + ((("[" + read_type(core, m)) + "] ") + name)) + " : ") + type)
  1279. elif (cmd == "permission_modify"):
  1280. String permissions
  1281. Integer permission
  1282. String model_id
  1283. output("Which model do you want to change permissions of?")
  1284. model_id = get_model_id(input())
  1285. if (model_id != ""):
  1286. if (get_relation_to_model(user_id, model_id) == 0):
  1287. output("New permissions?")
  1288. permissions = input()
  1289. Boolean fail
  1290. Integer i
  1291. i = 0
  1292. if (string_len(permissions) != 3):
  1293. fail = True
  1294. while (i < 3):
  1295. permission = cast_s2i(string_get(permissions, i))
  1296. if (bool_or(permission < 0, permission > 2)):
  1297. fail = True
  1298. break!
  1299. i = i + 1
  1300. if (bool_not(fail)):
  1301. unset_attribute(core, model_id, "permissions")
  1302. instantiate_attribute(core, model_id, "permissions", permissions)
  1303. else:
  1304. output("Permissions must be a string of three characters with each character being a digit between 0 and 2")
  1305. else:
  1306. output("Permission denied!")
  1307. else:
  1308. output("No such model!")
  1309. elif (cmd == "permission_owner"):
  1310. String permissions
  1311. String model_id
  1312. String user_id
  1313. output("Which model do you want to change owner of?")
  1314. model_id = get_model_id(input())
  1315. if (model_id != ""):
  1316. if (bool_or(get_relation_to_model(user_id, model_id) == 0, is_admin(user_id))):
  1317. output("New owner?")
  1318. user_id = get_user_id(input())
  1319. if (user_id != ""):
  1320. model_delete_element(core, set_pop(allOutgoingAssociationInstances(core, model_id, "owner")))
  1321. instantiate_link(core, "owner", "", model_id, user_id)
  1322. else:
  1323. output("No such user!")
  1324. else:
  1325. output("Permission denied!")
  1326. else:
  1327. output("No such model!")
  1328. elif (cmd == "permission_group"):
  1329. String permissions
  1330. String model_id
  1331. String group_id
  1332. output("Which model do you want to change permissions of?")
  1333. model_id = get_model_id(input())
  1334. if (model_id != ""):
  1335. if (bool_or(get_relation_to_model(user_id, model_id) == 0, is_admin(user_id))):
  1336. output("New group?")
  1337. group_id = get_group_id(input())
  1338. if (group_id != ""):
  1339. model_delete_element(core, set_pop(allOutgoingAssociationInstances(core, model_id, "group")))
  1340. instantiate_link(core, "group", "", model_id, group_id)
  1341. else:
  1342. output("No such group!")
  1343. else:
  1344. output("Permission denied!")
  1345. else:
  1346. output("No such model!")
  1347. elif (cmd == "group_create"):
  1348. // Create a new group and become its owner
  1349. String group_id
  1350. String name
  1351. output("Which group do you want to create?")
  1352. name = input()
  1353. group_id = get_group_id(name)
  1354. if (group_id == ""):
  1355. group_id = instantiate_node(core, "Group", "")
  1356. instantiate_attribute(core, group_id, "name", name)
  1357. instantiate_link(core, "belongsTo", "", user_id, group_id)
  1358. instantiate_link(core, "owner", "", group_id, user_id)
  1359. output("Group created!")
  1360. else:
  1361. output("Group already exists")
  1362. elif (cmd == "group_delete"):
  1363. // Delete an existing group
  1364. String group_id
  1365. String name
  1366. output("Which group do you want to delete?")
  1367. name = input()
  1368. group_id = get_group_id(name)
  1369. if (group_id != ""):
  1370. if (allow_group_modify(user_id, group_id)):
  1371. model_delete_element(core, group_id)
  1372. else:
  1373. output("Permission denied")
  1374. else:
  1375. output("No such group")
  1376. elif (cmd == "group_owner_add"):
  1377. // Add an owner to your group
  1378. String group_id
  1379. String other_user_id
  1380. output("Which group do you want to add an owner to?")
  1381. group_id = get_group_id(input())
  1382. if (group_id != ""):
  1383. if (allow_group_modify(user_id, group_id)):
  1384. output("Which user do you want to make an owner?")
  1385. other_user_id = get_user_id(input())
  1386. if (other_user_id != ""):
  1387. Element overlap
  1388. overlap = set_overlap(allIncomingAssociationInstances(core, other_user_id, "owner"), allOutgoingAssociationInstances(core, group_id, "owner"))
  1389. if (read_nr_out(overlap) == 0):
  1390. instantiate_link(core, "owner", "", group_id, other_user_id)
  1391. overlap = set_overlap(allOutgoingAssociationInstances(core, other_user_id, "belongsTo"), allIncomingAssociationInstances(core, group_id, "belongsTo"))
  1392. if (read_nr_out(overlap) == 0):
  1393. instantiate_link(core, "belongsTo", "", other_user_id, group_id)
  1394. output("New owner added to group!")
  1395. else:
  1396. output("User is already an owner!")
  1397. else:
  1398. output("No such user")
  1399. else:
  1400. output("Permission denied!")
  1401. else:
  1402. output("No such group")
  1403. elif (cmd == "group_owner_delete"):
  1404. // Remove an owner from your group
  1405. String group_id
  1406. String other_user_id
  1407. output("Which group do you want to disown someone from?")
  1408. group_id = get_group_id(input())
  1409. if (group_id != ""):
  1410. if (allow_group_modify(user_id, group_id)):
  1411. output("Which user do you want to disown?")
  1412. other_user_id = get_user_id(input())
  1413. if (other_user_id != ""):
  1414. Element overlap
  1415. overlap = set_overlap(allOutgoingAssociationInstances(core, other_user_id, "belongsTo"), allIncomingAssociationInstances(core, group_id, "belongsTo"))
  1416. if (read_nr_out(overlap) > 0):
  1417. overlap = set_overlap(allIncomingAssociationInstances(core, other_user_id, "owner"), allOutgoingAssociationInstances(core, group_id, "owner"))
  1418. if (read_nr_out(overlap) > 0):
  1419. model_delete_element(core, set_pop(overlap))
  1420. output("Disowned group from user!")
  1421. else:
  1422. output("User is not even an owner of the group!")
  1423. else:
  1424. output("User is not even a member of the group!")
  1425. else:
  1426. output("No such user")
  1427. else:
  1428. output("Permission denied!")
  1429. else:
  1430. output("No such group")
  1431. elif (cmd == "group_join"):
  1432. // Add someone to your group
  1433. String group_id
  1434. String other_user_id
  1435. output("Which group do you want to add someone to?")
  1436. group_id = get_group_id(input())
  1437. if (group_id != ""):
  1438. if (allow_group_modify(user_id, group_id)):
  1439. output("Which user do you want to add?")
  1440. other_user_id = get_user_id(input())
  1441. if (other_user_id != ""):
  1442. Element overlap
  1443. overlap = set_overlap(allOutgoingAssociationInstances(core, other_user_id, "belongsTo"), allIncomingAssociationInstances(core, group_id, "belongsTo"))
  1444. if (read_nr_out(overlap) == 0):
  1445. instantiate_link(core, "belongsTo", "", other_user_id, group_id)
  1446. output("User added to the group!")
  1447. else:
  1448. output("User is already a member of the group!")
  1449. else:
  1450. output("No such user")
  1451. else:
  1452. output("Permission denied!")
  1453. else:
  1454. output("No such group")
  1455. elif (cmd == "group_kick"):
  1456. // Remove someone from your group
  1457. String group_id
  1458. String other_user_id
  1459. output("Which group do you want to kick someone from?")
  1460. group_id = get_group_id(input())
  1461. if (group_id != ""):
  1462. if (allow_group_modify(user_id, group_id)):
  1463. output("Which user do you want to kick?")
  1464. other_user_id = get_user_id(input())
  1465. if (other_user_id != ""):
  1466. Element overlap
  1467. overlap = set_overlap(allOutgoingAssociationInstances(core, other_user_id, "belongsTo"), allIncomingAssociationInstances(core, group_id, "belongsTo"))
  1468. if (read_nr_out(overlap) > 0):
  1469. model_delete_element(core, set_pop(overlap))
  1470. // Check if user was an owner as well
  1471. overlap = set_overlap(allIncomingAssociationInstances(core, other_user_id, "owner"), allOutgoingAssociationInstances(core, group_id, "owner"))
  1472. if (read_nr_out(overlap) > 0):
  1473. model_delete_element(core, set_pop(overlap))
  1474. output("User kicked!")
  1475. else:
  1476. output("User is not even a member of the group!")
  1477. else:
  1478. output("No such user")
  1479. else:
  1480. output("Permission denied!")
  1481. else:
  1482. output("No such group")
  1483. elif (cmd == "group_list"):
  1484. // List all groups you are a member of (and whether you are admin or not!)
  1485. Element groups
  1486. String group_id
  1487. String admin
  1488. groups = allAssociationDestinations(core, user_id, "belongsTo")
  1489. while (read_nr_out(groups) > 0):
  1490. group_id = set_pop(groups)
  1491. if (set_in(allOutgoingAssociationInstances(core, group_id, "owner"), user_id)):
  1492. admin = " A "
  1493. else:
  1494. admin = " "
  1495. output(string_join(admin, read_attribute(core, group_id, "name")))
  1496. elif (cmd == "admin_promote"):
  1497. // Promote a user to admin status
  1498. if (is_admin(user_id)):
  1499. String other_user_id
  1500. output("Which user do you want to promote?")
  1501. other_user_id = get_user_id(input())
  1502. if (other_user_id != ""):
  1503. unset_attribute(core, other_user_id, "admin")
  1504. instantiate_attribute(core, other_user_id, "admin", True)
  1505. output("Permissions granted!")
  1506. else:
  1507. output("No such user!")
  1508. else:
  1509. output("Permission denied!")
  1510. elif (cmd == "admin_demote"):
  1511. // Demote a user to normal status
  1512. if (is_admin(user_id)):
  1513. String other_user_id
  1514. output("Which user do you want to demote?")
  1515. other_user_id = get_user_id(input())
  1516. if (other_user_id != ""):
  1517. unset_attribute(core, other_user_id, "admin")
  1518. instantiate_attribute(core, other_user_id, "admin", False)
  1519. output("Permissions revoked!")
  1520. else:
  1521. output("No such user!")
  1522. else:
  1523. output("Permission denied!")
  1524. elif (cmd == "self-destruct"):
  1525. // Delete user from Core Formalism
  1526. model_delete_element(core, user_id)
  1527. return !
  1528. elif (cmd == "exit"):
  1529. // Exit by actually removing the user and decoupling it from all of its models
  1530. // Restarting with the same user name will NOT grant you access to anything of the previous user with that same name
  1531. // as the current user will have been deleted
  1532. return !
  1533. else:
  1534. output("Unknown command: " + cmd)
  1535. // We never get here!
  1536. return !