core_algorithm.alc 62 KB

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