core_algorithm.alc 65 KB

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