core_algorithm.alc 65 KB

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