core_algorithm.alc 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233
  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. include "utils.alh"
  13. include "conformance_finding.alh"
  14. include "typing.alh"
  15. include "compiler.alh"
  16. String core_model_location = "models/core"
  17. Element core = ?
  18. String current_user_id
  19. Element caches
  20. String function get_foldername(name : String):
  21. Element result
  22. result = string_split(name, "/")
  23. list_pop_final(result)
  24. if (list_len(result) == 0):
  25. return ""!
  26. // 'result' now contains a list of entries which we have to join with the seperator
  27. String str
  28. str = list_pop(result, 0)
  29. while (list_len(result) > 0):
  30. str = string_join(str + "/", list_pop(result, 0))
  31. return str!
  32. String function get_filename(name : String):
  33. return list_pop_final(string_split(name, "/"))!
  34. String function full_name(model_id : String):
  35. return read_attribute(core, model_id, "name")!
  36. Void function initialize_core():
  37. // TODO make this more flexible by putting it in the bootstrap in a similar way as other models
  38. add_code_model(import_node("models/ActionLanguage"), "models/Conformance_MV", wrap_conformance)
  39. return !
  40. Boolean function is_typed_by(model_id : String, metamodel_id : String):
  41. return (set_len(get_instanceOf_links(model_id, metamodel_id)) > 0)!
  42. Element function get_instanceOf_links(model_id : String, metamodel_id : String):
  43. Element result
  44. Element options
  45. String elem
  46. result = set_create()
  47. options = allOutgoingAssociationInstances(core, model_id, "instanceOf")
  48. while (set_len(options) > 0):
  49. elem = set_pop(options)
  50. if (value_eq(readAssociationDestination(core, elem), metamodel_id)):
  51. set_add(result, elem)
  52. return result!
  53. String function get_instanceOf_link(model_id : String, metamodel_id : String):
  54. Element all_links
  55. String choice
  56. all_links = get_instanceOf_links(model_id, metamodel_id)
  57. if (set_len(all_links) > 1):
  58. log("WARNING: multiple instanceOf relations were detected for this model; picking one at random!")
  59. elif (set_len(all_links) == 0):
  60. log("No types found!")
  61. log("Source model: " + cast_v2s(read_attribute(core, model_id, "name")))
  62. log("Target meta-model: " + cast_v2s(read_attribute(core, metamodel_id, "name")))
  63. return read_root()!
  64. choice = set_pop(all_links)
  65. return choice!
  66. Element function get_full_model(model_id : String, metamodel_id : String):
  67. // TODO we are restricted to using formalisms/SimpleClassDiagrams as M3 level, through this code!
  68. Element m
  69. Element all_links
  70. String choice
  71. choice = get_instanceOf_link(model_id, metamodel_id)
  72. m = dict_create()
  73. dict_add(m, "model", import_node(read_attribute(core, model_id, "location")))
  74. // TODO for now this is restricted to the fixed semantics
  75. // dict_add(m, "semantics", set_pop(allAssociationDestinations(core, choice, "semantics")))
  76. dict_add(m, "semantics", get_entry_id("models/conformance_mv"))
  77. if (metamodel_id == model_id):
  78. // Found the meta-circular level, so we can stop!
  79. dict_add(m, "metamodel", m)
  80. else:
  81. Element mm
  82. mm = get_full_model(metamodel_id, get_entry_id("formalisms/SimpleClassDiagrams"))
  83. if (element_eq(mm, read_root())):
  84. return read_root()!
  85. else:
  86. dict_add(m, "metamodel", mm)
  87. if (element_neq(choice, read_root())):
  88. Element types
  89. types = allAssociationDestinations(core, choice, "typing")
  90. if (set_len(types) == 1):
  91. // Add the preferred original type mapping
  92. set_type_mapping(m, import_node(read_attribute(core, set_pop(types), "location")))
  93. else:
  94. // Start from scratch
  95. new_type_mapping(m)
  96. else:
  97. // Start from scratch
  98. new_type_mapping(m)
  99. if (find_type_mapping(m)):
  100. // TODO store the found type model somewhere
  101. return m!
  102. else:
  103. log("No type mapping could be deduced!")
  104. log("Error for " + cast_v2s(read_attribute(core, model_id, "name")))
  105. log(" and type " + cast_v2s(read_attribute(core, metamodel_id, "name")))
  106. return read_root()!
  107. Integer function get_relation_to_model(user_id : String, model_id : String):
  108. if (set_in(allAssociationDestinations(core, model_id, "owner"), user_id)):
  109. // We are the owner
  110. return 0!
  111. else:
  112. String group_id
  113. group_id = set_pop(allAssociationDestinations(core, model_id, "group"))
  114. if (set_in(allAssociationDestinations(core, user_id, "belongsTo"), group_id)):
  115. // We are in the owning group
  116. return 1!
  117. else:
  118. // We are not related whatsoever
  119. return 2!
  120. Boolean function is_admin(user_id : String):
  121. if (read_attribute(core, user_id, "admin")):
  122. return True!
  123. else:
  124. return False!
  125. Boolean function allow_read(user_id : String, model_id : String):
  126. if (is_admin(user_id)):
  127. // Is admin, so always allow
  128. return True!
  129. else:
  130. // Check permissions
  131. String permission
  132. permission = string_get(read_attribute(core, model_id, "permissions"), get_relation_to_model(user_id, model_id))
  133. if (bool_or(permission == "1", permission == "2")):
  134. return True!
  135. else:
  136. return False!
  137. Boolean function allow_write(user_id : String, model_id : String):
  138. if (is_admin(user_id)):
  139. // Is admin, so always allow
  140. return True!
  141. else:
  142. // Check permissions
  143. String permission
  144. permission = string_get(read_attribute(core, model_id, "permissions"), get_relation_to_model(user_id, model_id))
  145. if (permission == "2"):
  146. return True!
  147. else:
  148. return False!
  149. Boolean function allow_change_metadata(user_id : String, model_id : String):
  150. if (is_admin(user_id)):
  151. // Is admin, so always allow
  152. return True!
  153. else:
  154. if (get_relation_to_model(user_id, model_id) == 0):
  155. // Only owner can chmod
  156. return True!
  157. else:
  158. return False!
  159. Boolean function allow_group_modify(user_id : String, group_id : String):
  160. if (is_admin(user_id)):
  161. // Is admin, so always allow
  162. return True!
  163. else:
  164. if (set_in(allAssociationDestinations(core, group_id, "owner"), user_id)):
  165. // We are an owner
  166. return True!
  167. else:
  168. return False!
  169. Boolean function check_login(user_id : String):
  170. String password
  171. String stored_password
  172. stored_password = read_attribute(core, user_id, "password")
  173. output("Password for existing user?")
  174. password = hash(input())
  175. return password == stored_password!
  176. Void function new_task():
  177. String username
  178. String user_id
  179. String password
  180. // Load in a hard-reference to the previously created model
  181. core = import_node(core_model_location)
  182. while (True):
  183. output("Log on as which user?")
  184. username = input()
  185. user_id = get_user_id(username)
  186. if (user_id == ""):
  187. // New user
  188. // Add user to Core Formalism
  189. user_id = instantiate_node(core, "User", "")
  190. instantiate_attribute(core, user_id, "name", username)
  191. instantiate_attribute(core, user_id, "admin", False)
  192. output("This is a new user: please give password!")
  193. password = hash(input())
  194. output("Please repeat the password")
  195. if (password == hash(input())):
  196. output("Passwords match!")
  197. instantiate_attribute(core, user_id, "password", password)
  198. break!
  199. else:
  200. output("Not the same password!")
  201. else:
  202. if (check_login(user_id)):
  203. break!
  204. else:
  205. output("Wrong password!")
  206. user_function_skip_init(user_id)
  207. // User destroyed already, so just stop execution
  208. return!
  209. String function get_entry_id(name : String):
  210. Element hierarchy_split
  211. String current
  212. Integer i
  213. Element elems
  214. String current_part
  215. String elem
  216. Boolean found
  217. if (name == ""):
  218. return caches["root"]!
  219. if (dict_in(caches["models"], name)):
  220. if (dict_in(core, caches["models"])):
  221. if (full_name(caches["models"][name]) == name):
  222. return caches["models"][name]!
  223. current = get_entry_id(get_foldername(name))
  224. if (current == ""):
  225. return ""!
  226. if (current_part != ""):
  227. elems = allAssociationDestinations(core, current, "contains")
  228. while (set_len(elems) > 0):
  229. elem = set_pop(elems)
  230. dict_overwrite(caches["models"], read_attribute(core, elem, "name"), elem)
  231. if (value_eq(name, read_attribute(core, elem, "name"))):
  232. return elem!
  233. else:
  234. return current!
  235. return ""!
  236. String function get_service_id(name : String):
  237. core = import_node(core_model_location)
  238. Element services
  239. String service
  240. services = allInstances(core, "Service")
  241. while (set_len(services) > 0):
  242. service = set_pop(services)
  243. if (value_eq(read_attribute(core, service, "name"), name)):
  244. return service!
  245. return ""!
  246. String function get_user_id(name : String):
  247. Element users
  248. String user
  249. users = allInstances(core, "User")
  250. while (set_len(users) > 0):
  251. user = set_pop(users)
  252. if (value_eq(read_attribute(core, user, "name"), name)):
  253. return user!
  254. return ""!
  255. String function get_group_id(name : String):
  256. Element groups
  257. String group
  258. groups = allInstances(core, "Group")
  259. while (set_len(groups) > 0):
  260. group = set_pop(groups)
  261. if (value_eq(read_attribute(core, group, "name"), name)):
  262. return group!
  263. return ""!
  264. String function create_folders(user_id : String, folder_name : String):
  265. Element hierarchy
  266. Integer i
  267. String prev
  268. String elem
  269. String new_entry
  270. String cummul
  271. if (get_entry_id(folder_name) != ""):
  272. // Folders already exist, so don't iterate
  273. return get_entry_id(folder_name)!
  274. i = 0
  275. hierarchy = string_split(folder_name, "/")
  276. prev = get_entry_id("")
  277. cummul = ""
  278. // Go through folders first
  279. while (i < list_len(hierarchy)):
  280. elem = list_read(hierarchy, i)
  281. if (cummul == ""):
  282. cummul = elem
  283. else:
  284. cummul = string_join(cummul + "/", elem)
  285. if (get_entry_id(cummul) == ""):
  286. if (allow_write(user_id, prev)):
  287. // Element does not exist yet!
  288. new_entry = instantiate_node(core, "Folder", "")
  289. instantiate_attribute(core, new_entry, "name", cummul)
  290. instantiate_attribute(core, new_entry, "permissions", "200")
  291. instantiate_link(core, "contains", "", prev, new_entry)
  292. instantiate_link(core, "group", "", new_entry, get_group_id("nobody"))
  293. instantiate_link(core, "owner", "", new_entry, user_id)
  294. else:
  295. return read_root()!
  296. prev = get_entry_id(cummul)
  297. i = i + 1
  298. return prev!
  299. String function store_entry(model_id : String, full_name : String, user_id : String):
  300. String prev
  301. if (get_entry_id(full_name) != ""):
  302. // Delete any old models
  303. model_delete(get_entry_id(full_name))
  304. prev = create_folders(user_id, get_foldername(full_name))
  305. instantiate_link(core, "contains", "", prev, model_id)
  306. return full_name!
  307. String function export_typing(model : Element, name : String):
  308. String result
  309. result = instantiate_node(core, "TypeMapping", "")
  310. name = "type mappings/" + name
  311. // Create type mapping model
  312. String location
  313. location = "type mappings/" + cast_id2s(get_type_mapping(model))
  314. export_node(location, get_type_mapping(model))
  315. String instance_of
  316. name = store_entry(result, name, get_user_id("admin"))
  317. instantiate_attribute(core, result, "name", name)
  318. instantiate_attribute(core, result, "location", location)
  319. instantiate_attribute(core, result, "permissions", "200")
  320. instantiate_link(core, "owner", "", result, current_user_id)
  321. instantiate_link(core, "group", "", result, get_group_id("nobody"))
  322. instance_of = instantiate_link(core, "instanceOf", "", result, get_entry_id("formalisms/TypeMapping"))
  323. instantiate_link(core, "semantics", "", instance_of, get_entry_id("models/conformance_mv"))
  324. return result!
  325. Void function model_create(model : Element, name : String, type_id : String, kind : String):
  326. String location
  327. String model_id
  328. String instance_of
  329. // Create model itself
  330. location = "models/" + cast_id2s(model)
  331. export_node(location, model["model"])
  332. model_id = instantiate_node(core, kind, "")
  333. instantiate_attribute(core, model_id, "name", store_entry(model_id, name, current_user_id))
  334. instantiate_attribute(core, model_id, "location", location)
  335. instantiate_attribute(core, model_id, "permissions", "200")
  336. instantiate_link(core, "owner", "", model_id, current_user_id)
  337. instantiate_link(core, "group", "", model_id, get_group_id("nobody"))
  338. instance_of = instantiate_link(core, "instanceOf", "", model_id, type_id)
  339. instantiate_link(core, "semantics", "", instance_of, get_entry_id("models/conformance_mv"))
  340. // Create type mapping model
  341. instantiate_link(core, "typing", "", instance_of, export_typing(model, name))
  342. return!
  343. Void function model_overwrite(model : Element, model_id : String, metamodel_id : String):
  344. // TODO this should be more elegant than just hiding the old elements
  345. String location
  346. String instanceOf_link
  347. location = "models/" + cast_id2s(model)
  348. export_node(location, model["model"])
  349. // Change location in meta-data
  350. unset_attribute(core, model_id, "location")
  351. instantiate_attribute(core, model_id, "location", location)
  352. // Update the instanceOf relation of the context in which we are working
  353. String choice
  354. choice = get_instanceOf_link(model_id, metamodel_id)
  355. if (element_neq(choice, read_root())):
  356. // There was a link, so we remove it
  357. // First remove the type mapping it referred to
  358. Element tl
  359. tl = allAssociationDestinations(core, choice, "typing")
  360. while (set_len(tl) > 0):
  361. model_delete_element(core, set_pop(tl))
  362. // Now delete the element itself
  363. model_delete_element(core, choice)
  364. // Create a new instanceOf relation now
  365. String instance_of
  366. instance_of = instantiate_link(core, "instanceOf", "", model_id, metamodel_id)
  367. instantiate_link(core, "semantics", "", instance_of, get_entry_id("models/conformance_mv"))
  368. instantiate_link(core, "typing", "", instance_of, export_typing(model, full_name(model_id)))
  369. return!
  370. Boolean function check_conformance(model_id : String):
  371. // TODO check if it actually conforms, considering that instanceOf link
  372. // --> in-depth check
  373. return True!
  374. Boolean function pm_finished(worklist : Element, pm : String):
  375. Element finished
  376. Integer cnt
  377. Integer i
  378. // Check if any of the "finish" elements are in the worklist
  379. // If so, we can already finish, and therefore will stop immediately
  380. finished = allInstances(pm, "Finish")
  381. worklist = set_copy(worklist)
  382. while (set_len(worklist) > 0):
  383. // Check each finished element individually
  384. if (set_in(finished, list_read(set_pop(worklist), 0))):
  385. return True!
  386. return False!
  387. Element function merge_models(models_dict : Element, operation_name : String):
  388. core = import_node(core_model_location)
  389. // 0) Find operation signature
  390. Element input_metamodels
  391. Element iter
  392. String edge
  393. String operation_id
  394. operation_id = get_entry_id(operation_name)
  395. input_metamodels = dict_create()
  396. iter = allOutgoingAssociationInstances(core, operation_id, "transformInput")
  397. while (set_len(iter) > 0):
  398. edge = set_pop(iter)
  399. dict_add(input_metamodels, read_attribute(core, edge, "name"), read_attribute(core, readAssociationDestination(core, edge), "name"))
  400. String merged_metamodel_id
  401. String trace_link_id
  402. Element trace_links
  403. // 1) Find merged metamodel
  404. trace_links = allOutgoingAssociationInstances(core, operation_id, "tracability")
  405. merged_metamodel_id = ""
  406. while (set_len(trace_links) > 0):
  407. trace_link_id = set_pop(trace_links)
  408. if (value_eq(read_attribute(core, trace_link_id, "type"), "operatesOn")):
  409. merged_metamodel_id = readAssociationDestination(core, trace_link_id)
  410. // 2) Merge source models
  411. if (merged_metamodel_id != ""):
  412. Element model_tuples
  413. Element keys
  414. String key
  415. Element mm
  416. model_tuples = set_create()
  417. keys = dict_keys(models_dict)
  418. while (set_len(keys) > 0):
  419. key = set_pop(keys)
  420. mm = get_full_model(get_entry_id(models_dict[key]), get_entry_id(input_metamodels[key]))
  421. if (element_eq(mm, read_root())):
  422. log("Signature mismatch in operation for tag " + key)
  423. output("Signature mismatch in operation for tag " + key)
  424. return read_root()!
  425. set_add_node(model_tuples, create_tuple(key, mm))
  426. Element merged_metamodel
  427. merged_metamodel = get_full_model(merged_metamodel_id, get_entry_id("SimpleClassDiagrams"))
  428. if (element_eq(merged_metamodel, read_root())):
  429. log("Merged metamodel in operation is not of type SimpleClassDiagrams")
  430. output("Merged metamodel in operation is not of type SimpleClassDiagrams")
  431. return read_root()!
  432. return model_join(model_tuples, merged_metamodel, read_root())!
  433. else:
  434. return read_root()!
  435. Element function split_model(model : Element, metamodels_dict : Element):
  436. core = import_node(core_model_location)
  437. Element model_tuples
  438. Element keys
  439. String key
  440. Element mm
  441. model_tuples = set_create()
  442. keys = dict_keys(metamodels_dict)
  443. while (set_len(keys) > 0):
  444. key = set_pop(keys)
  445. mm = get_full_model(get_entry_id(metamodels_dict[key]), get_entry_id("SimpleClassDiagrams"))
  446. if (element_eq(mm, read_root())):
  447. log("Output metamodel cannot be interpreted using SimpleClassDiagrams: " + key)
  448. output("Output metamodel cannot be interpreted using SimpleClassDiagrams: " + key)
  449. return read_root()!
  450. set_add_node(model_tuples, create_tuple(key, mm))
  451. return model_split(model, model_tuples, read_root())!
  452. Element function get_model(model_name : String, metamodel_name : String):
  453. core = import_node(core_model_location)
  454. return get_full_model(get_entry_id(model_name), get_entry_id(metamodel_name))!
  455. Void function store_model(model_name : String, metamodel_name : String, model : Element):
  456. core = import_node(core_model_location)
  457. if (get_entry_id(model_name) == ""):
  458. // New model
  459. model_create(model, model_name, get_entry_id(metamodel_name), "Model")
  460. else:
  461. model_overwrite(model, get_entry_id(model_name), get_entry_id(metamodel_name))
  462. return!
  463. String function get_ramified_metamodel(model_name : String):
  464. core = import_node(core_model_location)
  465. String operation_id
  466. Element trace_links
  467. String merged_metamodel_id
  468. String trace_link_id
  469. String ramified_metamodel_id
  470. operation_id = get_entry_id(model_name)
  471. trace_links = allOutgoingAssociationInstances(core, operation_id, "tracability")
  472. merged_metamodel_id = ""
  473. while (set_len(trace_links) > 0):
  474. trace_link_id = set_pop(trace_links)
  475. if (value_eq(read_attribute(core, trace_link_id, "type"), "operatesOn")):
  476. merged_metamodel_id = readAssociationDestination(core, trace_link_id)
  477. trace_links = allOutgoingAssociationInstances(core, merged_metamodel_id, "tracability")
  478. ramified_metamodel_id = ""
  479. while (set_len(trace_links) > 0):
  480. trace_link_id = set_pop(trace_links)
  481. if (value_eq(read_attribute(core, trace_link_id, "type"), "RAMified")):
  482. ramified_metamodel_id = readAssociationDestination(core, trace_link_id)
  483. return read_attribute(core, ramified_metamodel_id, "name")!
  484. return read_root()!
  485. Element function execute_operation(operation_id : String, input_models : Element, tracability_model : Element):
  486. // Operations are always in-place and uses only a single metamodel
  487. // Therefore, we must:
  488. // 1) Find merged metamodel
  489. // 2) Merge the different source models and retype
  490. // 3) Perform the operation on the merged model
  491. // 4) Split the resulting model based on the target formalisms; if operation successful
  492. Element input_model
  493. String trace_link_id
  494. Element merged_model
  495. String merged_metamodel_id
  496. String ramified_metamodel_id
  497. Boolean result
  498. String exact_type
  499. Element trace_links
  500. String model_ID
  501. String key
  502. Element keys
  503. Element input_keys
  504. Element output_keys
  505. Element model_tuples
  506. String metamodel_name
  507. Element metamodel
  508. String metamodel_ID
  509. Boolean tracability
  510. // 0) Find the signature of the operation
  511. Element input_metamodels
  512. Element output_metamodels
  513. Element iter
  514. String edge
  515. input_metamodels = dict_create()
  516. iter = allOutgoingAssociationInstances(core, operation_id, "transformInput")
  517. while (set_len(iter) > 0):
  518. edge = set_pop(iter)
  519. dict_add(input_metamodels, read_attribute(core, edge, "name"), full_name(readAssociationDestination(core, edge)))
  520. output_metamodels = dict_create()
  521. iter = allOutgoingAssociationInstances(core, operation_id, "transformOutput")
  522. while (set_len(iter) > 0):
  523. edge = set_pop(iter)
  524. dict_add(output_metamodels, read_attribute(core, edge, "name"), full_name(readAssociationDestination(core, edge)))
  525. // 1) Find merged metamodel
  526. exact_type = read_type(core, operation_id)
  527. trace_links = allOutgoingAssociationInstances(core, operation_id, "tracability")
  528. merged_metamodel_id = ""
  529. while (set_len(trace_links) > 0):
  530. trace_link_id = set_pop(trace_links)
  531. if (value_eq(read_attribute(core, trace_link_id, "type"), "operatesOn")):
  532. merged_metamodel_id = readAssociationDestination(core, trace_link_id)
  533. // 2) Merge source models
  534. if (merged_metamodel_id != ""):
  535. model_tuples = set_create()
  536. keys = dict_keys(input_models)
  537. while (set_len(keys) > 0):
  538. key = set_pop(keys)
  539. Element mm
  540. mm = get_full_model(get_entry_id(input_models[key]), get_entry_id(input_metamodels[key]))
  541. if (element_eq(mm, read_root())):
  542. log("Signature mismatch in operation for tag " + key)
  543. output("Signature mismatch in operation for tag " + key)
  544. return read_root()!
  545. set_add_node(model_tuples, create_tuple(key, mm))
  546. Element merged_metamodel
  547. merged_metamodel = get_full_model(merged_metamodel_id, get_entry_id("formalisms/SimpleClassDiagrams"))
  548. if (element_eq(merged_metamodel, read_root())):
  549. log("Merged metamodel in operation is not of type formalisms/SimpleClassDiagrams")
  550. output("Merged metamodel in operation is not of type formalisms/SimpleClassDiagrams")
  551. return read_root()!
  552. merged_model = model_join(model_tuples, merged_metamodel, tracability_model)
  553. else:
  554. if (bool_and(dict_len(input_models) == 1, dict_len(output_metamodels) == 0)):
  555. // Just skip the merge...
  556. Element m
  557. m = get_full_model(get_entry_id(input_models[set_pop(dict_keys(input_models))]), get_entry_id(input_metamodels[set_pop(dict_keys(input_metamodels))]))
  558. if (element_eq(m, read_root())):
  559. log("Signature mismatch in operation for tag " + cast_v2s(set_pop(dict_keys(input_models))))
  560. output("Signature mismatch in operation for tag " + cast_v2s(set_pop(dict_keys(input_models))))
  561. return read_root()!
  562. merged_model = model_copy(m)
  563. elif (bool_and(dict_len(input_models) == 0, dict_len(output_metamodels) == 0)):
  564. merged_model = read_root()
  565. else:
  566. log("Could not resolve intermediate merged metamodel")
  567. output("Could not resolve intermediate merged metamodel")
  568. return read_root()!
  569. // 3) Transform
  570. if (exact_type == "ModelTransformation"):
  571. trace_links = allOutgoingAssociationInstances(core, merged_metamodel_id, "tracability")
  572. ramified_metamodel_id = ""
  573. while (set_len(trace_links) > 0):
  574. trace_link_id = set_pop(trace_links)
  575. if (value_eq(read_attribute(core, trace_link_id, "type"), "RAMified")):
  576. ramified_metamodel_id = readAssociationDestination(core, trace_link_id)
  577. Element operation
  578. operation = get_full_model(operation_id, ramified_metamodel_id)
  579. if (element_eq(operation, read_root())):
  580. log("Operation could not be typed by specified RAMified metamodel!")
  581. output("Operation could not be typed by specified RAMified metamodel!")
  582. return read_root()!
  583. result = transform(merged_model, operation)
  584. elif (exact_type == "ManualOperation"):
  585. output("Please perform manual operation " + cast_v2s(full_name(operation_id)))
  586. modify(merged_model, True)
  587. result = True
  588. elif (exact_type == "ActionLanguage"):
  589. Element func
  590. Element al
  591. al = get_full_model(operation_id, get_entry_id("formalisms/ActionLanguage"))
  592. if (element_eq(al, read_root())):
  593. log("Action Language operation not typed by ActionLanguage metamodel!")
  594. output("Action Language operation not typed by ActionLanguage metamodel!")
  595. return read_root()!
  596. func = get_func_AL_model(al)
  597. result = func(merged_model)
  598. else:
  599. log("Unknown type of operation: " + exact_type)
  600. output("Unknown type of operation: " + exact_type)
  601. return read_root()!
  602. // 4) Split in different models depending on type
  603. if (element_neq(tracability_model, read_root())):
  604. tracability = True
  605. else:
  606. tracability = False
  607. if (result):
  608. model_tuples = set_create()
  609. keys = dict_keys(output_metamodels)
  610. while (set_len(keys) > 0):
  611. key = set_pop(keys)
  612. Element mm
  613. mm = get_full_model(get_entry_id(output_metamodels[key]), get_entry_id("formalisms/SimpleClassDiagrams"))
  614. if (element_eq(mm, read_root())):
  615. log("Output metamodel cannot be interpreted using formalisms/SimpleClassDiagrams: " + key)
  616. output("Output metamodel cannot be interpreted using formalisms/SimpleClassDiagrams: " + key)
  617. return read_root()!
  618. set_add_node(model_tuples, create_tuple(key, mm))
  619. result = model_split(merged_model, model_tuples, tracability)
  620. if (tracability):
  621. Element new_tracability_model
  622. new_tracability_model = result["__tracability"]
  623. dict_overwrite(tracability_model, "model", new_tracability_model["model"])
  624. set_type_mapping(tracability_model, get_type_mapping(new_tracability_model))
  625. dict_overwrite(tracability_model, "metamodel", new_tracability_model["metamodel"])
  626. dict_delete(result, "__tracability")
  627. return result!
  628. else:
  629. log("Negative result of execution")
  630. return read_root()!
  631. Boolean function enact_action(pm : Element, element : String, prefix : String):
  632. Boolean result
  633. String transformation_id
  634. Element lst
  635. String elem
  636. Element inputs
  637. Element outputs
  638. String type_name
  639. String exact_type
  640. Element trace_links
  641. Element output_mms
  642. Element consumes_link
  643. String name
  644. String value
  645. String elem_name
  646. Element keys
  647. String key
  648. String consume
  649. String produce
  650. Element output_map
  651. inputs = dict_create()
  652. outputs = dict_create()
  653. output_map = dict_create()
  654. // Read out the referenced element from the MvC
  655. transformation_id = get_entry_id(read_attribute(pm, element, "name"))
  656. // Find all input model names
  657. lst = allOutgoingAssociationInstances(pm, element, "Consumes")
  658. while (set_len(lst) > 0):
  659. consume = set_pop(lst)
  660. value = read_attribute(pm, readAssociationDestination(pm, consume), "name")
  661. dict_add(inputs, read_attribute(pm, consume, "name"), prefix + value)
  662. // Find all output model names and their metamodel
  663. lst = allOutgoingAssociationInstances(pm, element, "Produces")
  664. while (set_len(lst) > 0):
  665. produce = set_pop(lst)
  666. elem = readAssociationDestination(pm, produce)
  667. type_name = read_attribute(pm, elem, "type")
  668. elem_name = read_attribute(pm, elem, "name")
  669. dict_add(outputs, read_attribute(pm, produce, "name"), type_name)
  670. dict_add(output_map, read_attribute(pm, produce, "name"), prefix + elem_name)
  671. if read_type(core, transformation_id) == "ActionLanguage":
  672. log(string_join("Enacting ActionLanguage: ", read_attribute(pm, element, "name")))
  673. output(string_join("Enacting ActionLanguage: ", read_attribute(pm, element, "name")))
  674. elif read_type(core, transformation_id) == "ManualOperation":
  675. log(string_join("Enacting ManualOperation: ", read_attribute(pm, element, "name")))
  676. output(string_join("Enacting ManualOperation: ", read_attribute(pm, element, "name")))
  677. else:
  678. log(string_join("Enacting ModelTransformation: ", read_attribute(pm, element, "name")))
  679. output(string_join("Enacting ModelTransformation: ", read_attribute(pm, element, "name")))
  680. result = execute_operation(transformation_id, inputs, read_root())
  681. if (element_eq(result, read_root())):
  682. // Something went wrong!
  683. return False!
  684. else:
  685. keys = dict_keys(result)
  686. while (set_len(keys) > 0):
  687. key = set_pop(keys)
  688. if (get_entry_id(output_map[key]) == ""):
  689. // New model
  690. model_create(result[key], output_map[key], get_entry_id(outputs[key]), "Model")
  691. else:
  692. model_overwrite(result[key], get_entry_id(output_map[key]), get_entry_id(outputs[key]))
  693. return True!
  694. Void function enact_PM(pm : Element, prefix : String):
  695. Element worklist
  696. String element
  697. String type
  698. Boolean result
  699. Element tuple
  700. Element counters
  701. Element join_nodes
  702. output("Success")
  703. // Initialize Join counters
  704. counters = dict_create()
  705. join_nodes = allInstances(pm, "Join")
  706. while (set_len(join_nodes) > 0):
  707. dict_add(counters, set_pop(join_nodes), 0)
  708. // Create the worklist with the Start instance as first element
  709. worklist = set_create()
  710. set_add_node(worklist, create_tuple(set_pop(allInstances(pm, "Start")), True))
  711. while (bool_not(pm_finished(worklist, pm))):
  712. // Pop a random element from the list and execute it
  713. tuple = set_pop(worklist)
  714. element = tuple[0]
  715. result = tuple[1]
  716. // Find the type (to see what to do with it)
  717. // this does not yet yield the type of transformation, if it is an Execution
  718. type = read_type(pm, element)
  719. if (type == "Start"):
  720. // Initial node, just progress to the next elements
  721. // Nothing to do here though, as we have just started
  722. result = True
  723. elif (type == "Finish"):
  724. // Should be impossible, as we would have ended...
  725. result = result
  726. elif (type == "Fork"):
  727. result = result
  728. elif (type == "Join"):
  729. // Only do this if all dependencies are fullfilled
  730. // So add to the counter of this Join
  731. dict_overwrite(counters, element, integer_addition(counters[element], 1))
  732. // Now check whether we have enough tokens to execute the Join itself
  733. Integer required
  734. Integer got
  735. required = set_len(allIncomingAssociationInstances(pm, element, "Next")) + set_len(allIncomingAssociationInstances(pm, element, "Else"))
  736. got = counters[element]
  737. if (got == required):
  738. // Reset counter to 0
  739. dict_overwrite(counters, element, 0)
  740. // And continue
  741. else:
  742. // We haven't gotten all yet, so we wait (i.e., continue without adding Next link to worklist)
  743. continue!
  744. elif (type == "Exec"):
  745. // Execute a transformation
  746. // This the difficult part!
  747. result = enact_action(pm, element, prefix)
  748. output("Success")
  749. elif (type == "Decision"):
  750. // If the previous result is true, we add the normal one, otherwise the false one
  751. // in this context, that means that if it is false, we should add it manually to the list, and then continue the simulation loop
  752. if (bool_not(result)):
  753. // Apparently it is False, so map this to the "Else" branch
  754. set_add_node(worklist, create_tuple(set_pop(allAssociationDestinations(pm, element, "Else")), True))
  755. continue!
  756. else:
  757. // Apparently it is True, so map this to the "Then" branch
  758. set_add_node(worklist, create_tuple(set_pop(allAssociationDestinations(pm, element, "Then")), True))
  759. continue!
  760. // We have finished the execution, so add all outgoing edges to the worklist
  761. Element all_next
  762. all_next = allAssociationDestinations(pm, element, "Next")
  763. String next
  764. while (set_len(all_next) > 0):
  765. next = set_pop(all_next)
  766. set_add_node(worklist, create_tuple(next, result))
  767. // Reached a finish element, so stop
  768. return !
  769. String function cmd_help():
  770. String result
  771. result = ""
  772. result = result + ("Model operations\n")
  773. result = result + (" model_add -- Add a new model\n")
  774. result = result + (" model_modify -- Modify an existing model\n")
  775. result = result + (" model_list -- List all models\n")
  776. result = result + (" model_list_full -- List all models with full info\n")
  777. result = result + (" model_overwrite -- Overwrites a model with an uploaded model, leaving all metadata\n")
  778. result = result + (" model_render -- Render a given model with a specified mapper\n")
  779. result = result + (" verify -- Check whether a model conforms to its metamodel\n")
  780. result = result + ("\n")
  781. result = result + ("Transformation-specific operations\n")
  782. result = result + (" transformation_add_MT -- Initialize a new model transformation\n")
  783. result = result + (" transformation_add_AL -- Initialize a new action language transformation\n")
  784. result = result + (" transformation_add_MANUAL -- Initialize a new manual transformation\n")
  785. result = result + (" transformation_execute -- Execute a transformation on a set of input models\n")
  786. result = result + (" transformation_list -- List all model transformations\n")
  787. result = result + (" transformation_list_full -- List all model transformations with permissions\n")
  788. result = result + (" transformation_detail -- List transformation details\n")
  789. result = result + (" transformation_between -- List all transformations between two metamodels\n")
  790. result = result + ("\n")
  791. result = result + ("Process operations\n")
  792. result = result + (" process_execute -- Execute a process model\n")
  793. result = result + ("\n")
  794. result = result + ("Model permission operations\n")
  795. result = result + (" permission_modify -- Change model permissions\n")
  796. result = result + (" permission_owner -- Change model owner\n")
  797. result = result + (" permission_group -- Change model group\n")
  798. result = result + ("\n")
  799. result = result + ("Group operations\n")
  800. result = result + (" group_create -- Create a group\n")
  801. result = result + (" group_delete -- Delete a group\n")
  802. result = result + (" group_owner_add -- Add group owner\n")
  803. result = result + (" group_owner_delete -- Remove group owner\n")
  804. result = result + (" group_join -- Add someone to your group\n")
  805. result = result + (" group_kick -- Kick someone from your group\n")
  806. result = result + (" group_list -- List all groups you are a member of\n")
  807. result = result + ("\n")
  808. if (is_admin(current_user_id)):
  809. result = result + ("Admin operations\n")
  810. result = result + (" admin_promote -- Promote a user to admin status\n")
  811. result = result + (" admin_demote -- Demote a user to normal status\n")
  812. result = result + ("\n")
  813. result = result + ("General operations\n")
  814. result = result + (" self-destruct -- Remove current user and revoke all permissions \n")
  815. result = result + (" exit -- Kill the current task, while retaining user\n")
  816. return result!
  817. String function cmd_model_add(type : String, name : String, code : String):
  818. // Model addition operation, which uses model upload commands of the compiler
  819. String location
  820. String type_id
  821. Element new_model
  822. String new_model_id
  823. type_id = get_entry_id(type)
  824. if (type_id != ""):
  825. // Type exists
  826. if (allow_read(current_user_id, type_id)):
  827. // And is readable
  828. if (get_entry_id(get_foldername(name)) != ""):
  829. // Folder doesn't exist yet!
  830. if (allow_write(current_user_id, get_entry_id(get_foldername(name)))):
  831. // Folder is writable
  832. if (get_entry_id(name) == ""):
  833. // Model doesn't exist yet
  834. Element mm
  835. mm = get_full_model(type_id, get_entry_id("formalisms/SimpleClassDiagrams"))
  836. if (element_eq(mm, read_root())):
  837. return "Type is not typed by formalisms/SimpleClassDiagrams: " + type!
  838. new_model = compile_model(code, mm)
  839. if (element_eq(new_model, read_root())):
  840. return "Compilation error"!
  841. model_create(new_model, name, type_id, "Model")
  842. return "Success"!
  843. else:
  844. return "Model exists: " + name!
  845. else:
  846. return "Permission denied to folder: " + get_foldername(name)!
  847. else:
  848. return "No such folder: " + get_foldername(name)!
  849. else:
  850. return "Permission denied to model: " + type!
  851. else:
  852. return "Model not found: " + type!
  853. String function cmd_process_execute(process : String, prefix : String):
  854. // Execute a process model until it reaches termination
  855. String process_id
  856. process_id = get_entry_id(process)
  857. if (process_id != ""):
  858. if (allow_read(current_user_id, process_id)):
  859. Element pm
  860. pm = get_full_model(process_id, get_entry_id("formalisms/ProcessModel"))
  861. if (element_eq(pm, read_root())):
  862. return "Specified model cannot be interpreted as a ProcessModel: " + process!
  863. enact_PM(pm, prefix)
  864. return "Success"!
  865. else:
  866. return "Permission denied to model: " + process!
  867. else:
  868. return "Model not found: " + process!
  869. String function cmd_transformation_between(source_name : String, target_name : String):
  870. String source_id
  871. String target_id
  872. Element onSource
  873. Element onTarget
  874. Element result
  875. String transformation
  876. source_id = get_entry_id(source_name)
  877. if (source_id != ""):
  878. target_id = get_entry_id(target_name)
  879. if (target_id != ""):
  880. onSource = allAssociationOrigins(core, source_id, "transformInput")
  881. onTarget = allAssociationOrigins(core, target_id, "transformOutput")
  882. result = set_overlap(onSource, onTarget)
  883. String r
  884. r = "Success: "
  885. while (set_len(result) > 0):
  886. transformation = set_pop(result)
  887. if (allow_read(current_user_id, transformation)):
  888. r = r + string_join(full_name(transformation), "\n")
  889. return r!
  890. else:
  891. return "Model not found: " + target_name!
  892. else:
  893. return "Model not found: " + source_name!
  894. String function cmd_model_rendered(model_name : String, mapper_name : String):
  895. Element trace_links
  896. String rendered
  897. String trace_link_id
  898. String allowed_rendered
  899. Element trace_links_2
  900. String trace_link_2_id
  901. allowed_rendered = "Success: "
  902. trace_links = allOutgoingAssociationInstances(core, get_entry_id(model_name), "tracability")
  903. while (set_len(trace_links) > 0):
  904. trace_link_id = set_pop(trace_links)
  905. if (value_eq(read_attribute(core, trace_link_id, "type"), "CS_perceptualized")):
  906. rendered = readAssociationDestination(core, trace_link_id)
  907. trace_links_2 = allOutgoingAssociationInstances(core, rendered, "tracability")
  908. while (set_len(trace_links_2) > 0):
  909. trace_link_2_id = set_pop(trace_links_2)
  910. if (value_eq(read_attribute(core, readAssociationDestination(core, trace_link_2_id), "name"), mapper_name)):
  911. if (value_eq(read_attribute(core, trace_link_2_id, "type"), "CS_mapper")):
  912. allowed_rendered = string_join(allowed_rendered, read_attribute(core, rendered, "name")) + "\n"
  913. return allowed_rendered!
  914. String function cmd_model_render(model_name : String, mapper_name : String, rendered_name : String):
  915. String model_ID
  916. String mapper_ID
  917. String tracability_name
  918. Element inputs
  919. Element output_map
  920. Element rendered_model
  921. Element tracability_model
  922. Element result
  923. Element out_links
  924. String link
  925. String ID_rendered_M
  926. String ID_rendered_MM
  927. String ID_SCD
  928. String ID_tracability_MM
  929. String ID_tracability_M
  930. model_ID = get_entry_id(model_name)
  931. if (model_ID != ""):
  932. mapper_ID = get_entry_id(mapper_name)
  933. if (allow_read(current_user_id, model_ID)):
  934. if (mapper_ID != ""):
  935. if (allow_read(current_user_id, mapper_ID)):
  936. // Everything is fine; start the actual operation
  937. // Find metamodel to render to
  938. tracability_name = "tracability/" + rendered_name
  939. // Take the abstract syntax model and the previously rendered model
  940. inputs = dict_create()
  941. dict_add(inputs, "abstract", model_name)
  942. dict_add(inputs, "rendered", rendered_name)
  943. // Fetch the output types
  944. output_map = dict_create()
  945. out_links = allOutgoingAssociationInstances(core, mapper_ID, "transformOutput")
  946. while(set_len(out_links) > 0):
  947. link = set_pop(out_links)
  948. dict_add(output_map, read_attribute(core, link, "name"), full_name(readAssociationDestination(core, link)))
  949. ID_rendered_MM = get_entry_id(output_map["rendered"])
  950. ID_SCD = get_entry_id("formalisms/SimpleClassDiagrams")
  951. ID_tracability_MM = get_entry_id("formalisms/Tracability")
  952. ID_tracability_M = get_entry_id(tracability_name)
  953. ID_rendered_M = get_entry_id(rendered_name)
  954. if (ID_rendered_M == ""):
  955. // Instantiate
  956. Element rendered
  957. rendered = get_full_model(ID_rendered_MM, ID_SCD)
  958. if (element_eq(rendered, read_root())):
  959. return "Rendered metamodel doesn't conform to formalisms/SimpleClassDiagrams"!
  960. rendered_model = instantiate_model(rendered)
  961. model_create(rendered_model, rendered_name, ID_rendered_MM, "Model")
  962. // Tracability model won't exist either
  963. tracability_model = instantiate_model(get_full_model(ID_tracability_MM, ID_SCD))
  964. model_create(tracability_model, tracability_name, ID_tracability_MM, "Model")
  965. ID_rendered_M = get_entry_id(rendered_name)
  966. ID_tracability_M = get_entry_id(tracability_name)
  967. else:
  968. // Read out tracability model
  969. tracability_model = get_full_model(ID_tracability_M, ID_tracability_MM)
  970. if (element_eq(tracability_model, read_root())):
  971. return "Tracability model not typed by Tracability metamodel: " + tracability_name!
  972. // Do the operation itself!
  973. result = execute_operation(mapper_ID, inputs, tracability_model)
  974. // Overwrite the previous rendered model
  975. model_overwrite(result["rendered"], ID_rendered_M, ID_rendered_MM)
  976. // Tracability updated in-place
  977. model_overwrite(tracability_model, ID_tracability_M, ID_tracability_MM)
  978. if (element_eq(tracability_model, read_root())):
  979. return "Tracability model not typed by Tracability metamodel: " + tracability_name!
  980. // Link all information in the megamodel
  981. String tr_link
  982. tr_link = instantiate_link(core, "tracability", "", get_entry_id(model_name), ID_rendered_M)
  983. instantiate_attribute(core, tr_link, "type", "CS_perceptualized")
  984. tr_link = instantiate_link(core, "tracability", "", ID_rendered_M, get_entry_id(mapper_name))
  985. instantiate_attribute(core, tr_link, "type", "CS_mapper")
  986. // Output the resulting model
  987. String value
  988. value = JSON_print(result["rendered"])
  989. return ("Success: " + value) !
  990. else:
  991. return "Permission denied to model: " + mapper_name!
  992. else:
  993. return "Model not found: " + mapper_name!
  994. else:
  995. return "Permission denied to model: " + model_name!
  996. else:
  997. return "Model not found: " + model_name!
  998. //trace_links = allOutgoingAssociationInstances(core, operation_id, "tracability")
  999. //merged_metamodel_id = ""
  1000. //while (set_len(trace_links) > 0):
  1001. // trace_link_id = set_pop(trace_links)
  1002. // if (value_eq(read_attribute(core, trace_link_id, "type"), "operatesOn")):
  1003. // merged_metamodel_id = readAssociationDestination(core, trace_link_id)
  1004. String function cmd_transformation_execute(transformation_name : String, source_models : Element, target_models : Element):
  1005. // Execute a transformation, whatever type it is
  1006. // First we detect the type, so we know how to prepare for invocation
  1007. String transformation_id
  1008. String exact_type
  1009. Element sources
  1010. Element targets
  1011. String source
  1012. String target
  1013. Element inputs
  1014. Element outputs
  1015. Element output_map
  1016. Element trace_links
  1017. String target_model_name
  1018. String source_model_name
  1019. String source_model_ID
  1020. Element result
  1021. Element keys
  1022. String key
  1023. String assoc_name
  1024. transformation_id = get_entry_id(transformation_name)
  1025. if (transformation_id != ""):
  1026. if (allow_read(current_user_id, transformation_id)):
  1027. if (is_nominal_instance(core, transformation_id, "Transformation")):
  1028. // Read out source and target links
  1029. inputs = dict_create()
  1030. sources = allOutgoingAssociationInstances(core, transformation_id, "transformInput")
  1031. while (set_len(sources) > 0):
  1032. source = set_pop(sources)
  1033. assoc_name = read_attribute(core, source, "name")
  1034. if (dict_in(source_models, assoc_name)):
  1035. source_model_name = source_models[assoc_name]
  1036. else:
  1037. return "Source model not bound: " + assoc_name!
  1038. source_model_ID = get_entry_id(source_model_name)
  1039. if (source_model_ID != ""):
  1040. if (allow_read(current_user_id, source_model_ID)):
  1041. // Check for conformance to the requested metamodel
  1042. dict_add(inputs, assoc_name, source_model_name)
  1043. continue!
  1044. else:
  1045. return "Permission denied to model: " + source_model_name!
  1046. else:
  1047. return "Model not found: " + source_model_name!
  1048. targets = allOutgoingAssociationInstances(core, transformation_id, "transformOutput")
  1049. outputs = dict_create()
  1050. output_map = dict_create()
  1051. while (set_len(targets) > 0):
  1052. target = set_pop(targets)
  1053. assoc_name = read_attribute(core, target, "name")
  1054. if (dict_in(target_models, assoc_name)):
  1055. target_model_name = target_models[assoc_name]
  1056. else:
  1057. return "Target model not bound: " + assoc_name!
  1058. if (get_entry_id(target_model_name) == ""):
  1059. // Doesn't exist yet, so we can easily create
  1060. if (get_entry_id(get_foldername(target_model_name)) == ""):
  1061. return "Folder not found: " + get_foldername(target_model_name)!
  1062. else:
  1063. if (allow_write(current_user_id, get_entry_id(get_foldername(target_model_name)))):
  1064. dict_add(output_map, assoc_name, full_name(readAssociationDestination(core, target)))
  1065. dict_add(outputs, assoc_name, target_model_name)
  1066. else:
  1067. return "Permission denied to folder: " + get_foldername(target_model_name)!
  1068. else:
  1069. // Already exists, so we need to check for write access
  1070. if (allow_write(current_user_id, get_entry_id(target_model_name))):
  1071. dict_add(output_map, assoc_name, read_attribute(core, readAssociationDestination(core, target), "name"))
  1072. dict_add(outputs, assoc_name, target_model_name)
  1073. else:
  1074. return "Permission denied to model: " + target_model_name!
  1075. if (read_type(core, transformation_id) == "ActionLanguage"):
  1076. output("Success: ready for AL execution")
  1077. elif (read_type(core, transformation_id) == "ManualOperation"):
  1078. output("Success: ready for MANUAL execution")
  1079. else:
  1080. output("Success: ready for MT execution")
  1081. result = execute_operation(transformation_id, inputs, read_root())
  1082. // Now write out the models again
  1083. if (element_eq(result, read_root())):
  1084. // Something went wrong!
  1085. return "Failure"!
  1086. else:
  1087. keys = dict_keys(result)
  1088. while (set_len(keys) > 0):
  1089. key = set_pop(keys)
  1090. if (get_entry_id(outputs[key]) == ""):
  1091. // New model
  1092. model_create(result[key], outputs[key], get_entry_id(output_map[key]), "Model")
  1093. else:
  1094. model_overwrite(result[key], get_entry_id(outputs[key]), get_entry_id(output_map[key]))
  1095. return "Success"!
  1096. else:
  1097. return "Model is not executable: " + transformation_name!
  1098. else:
  1099. return "Permission denied to model: " + transformation_name!
  1100. else:
  1101. return "Model not found: " + transformation_name!
  1102. String function cmd_verify(model_name : String, metamodel_name : String):
  1103. // Check whether a model conforms to its specification (with the selected type mapping)
  1104. String model_id
  1105. String result
  1106. Element inputs
  1107. model_id = get_entry_id(model_name)
  1108. if (model_id != ""):
  1109. if (allow_read(current_user_id, model_id)):
  1110. if (get_entry_id(metamodel_name) != ""):
  1111. if (allow_read(current_user_id, get_entry_id(metamodel_name))):
  1112. Element m
  1113. m = get_full_model(get_entry_id(model_name), get_entry_id(metamodel_name))
  1114. if (element_eq(m, read_root())):
  1115. return "No conformance relation can be found between these models"!
  1116. return string_join("Success: ", conformance_scd(m))!
  1117. else:
  1118. return "Permission denied to model: " + metamodel_name!
  1119. else:
  1120. return "Model not found: " + metamodel_name!
  1121. else:
  1122. return "Permission denied to model: " + model_name!
  1123. else:
  1124. return "Model not found: " + model_name!
  1125. String function cmd_model_overwrite(model_name : String, metamodel_name : String):
  1126. // Overwrites an existing model without changing any metadata
  1127. String model_id
  1128. String type_id
  1129. Element new_model
  1130. model_id = get_entry_id(model_name)
  1131. if (model_id != ""):
  1132. if (allow_write(current_user_id, model_id)):
  1133. type_id = set_pop(allAssociationDestinations(core, model_id, "instanceOf"))
  1134. if (allow_read(current_user_id, type_id)):
  1135. Element mm
  1136. mm = get_full_model(get_entry_id(metamodel_name), get_entry_id("formalisms/SimpleClassDiagrams"))
  1137. if (element_eq(mm, read_root())):
  1138. return "Metamodel does not conform to formalisms/SimpleClassDiagrams: " + metamodel_name!
  1139. output("Waiting for model constructors...")
  1140. new_model = compile_model(input(), mm)
  1141. if (element_eq(new_model, read_root())):
  1142. return "Compilation error"!
  1143. model_overwrite(new_model, model_id, get_entry_id(metamodel_name))
  1144. return "Success"!
  1145. else:
  1146. return string_join("Permission denied to model: ", full_name(type_id))!
  1147. else:
  1148. return "Permission denied to model: " + model_name!
  1149. else:
  1150. return "Model not found: " + model_name!
  1151. String function cmd_model_modify(model_name : String, metamodel_name : String):
  1152. // Model modify operation, which uses the mini_modify.alc operations, though with extensions for access control
  1153. String model_id
  1154. String type_id
  1155. model_id = get_entry_id(model_name)
  1156. if (model_id != ""):
  1157. if (allow_read(current_user_id, model_id)):
  1158. type_id = set_pop(allAssociationDestinations(core, model_id, "instanceOf"))
  1159. if (allow_read(current_user_id, type_id)):
  1160. Element new_model
  1161. new_model = get_full_model(model_id, get_entry_id(metamodel_name))
  1162. if (element_eq(new_model, read_root())):
  1163. return "No conformance relation can be found between these models"!
  1164. modify(new_model, allow_write(current_user_id, model_id))
  1165. if (allow_write(current_user_id, model_id)):
  1166. // Overwrite the modified model
  1167. model_overwrite(new_model, model_id, get_entry_id(metamodel_name))
  1168. return "Success"!
  1169. else:
  1170. return string_join("Permission denied to model: ", full_name(type_id))!
  1171. else:
  1172. return "Permission denied to model: " + model_name!
  1173. else:
  1174. return "Model not found: " + model_name!
  1175. Void function model_delete(model_id : String):
  1176. if (read_type(core, model_id) == "Folder"):
  1177. // Folder, so delete all containing elements as well!
  1178. Element elems
  1179. elems = allAssociationDestinations(core, model_id, "contains")
  1180. while (set_len(elems) > 0):
  1181. model_delete(set_pop(elems))
  1182. // Delete self
  1183. model_delete_element(core, model_id)
  1184. else:
  1185. // Only delete this element
  1186. model_delete_element(core, model_id)
  1187. return!
  1188. String function cmd_model_delete(model_name : String):
  1189. String model_id
  1190. model_id = get_entry_id(model_name)
  1191. if (model_id != ""):
  1192. if (allow_write(current_user_id, model_id)):
  1193. model_delete(model_id)
  1194. return "Success"!
  1195. else:
  1196. return "Permission denied to model: " + model_name!
  1197. else:
  1198. return "Model not found: " + model_name!
  1199. String function cmd_model_list(location : String):
  1200. // List all models
  1201. if (get_entry_id(location) != ""):
  1202. if (allow_read(current_user_id, get_entry_id(location))):
  1203. Element models
  1204. String result
  1205. String m
  1206. result = "Success: "
  1207. models = allAssociationDestinations(core, get_entry_id(location), "contains")
  1208. while (set_len(models) > 0):
  1209. m = set_pop(models)
  1210. if (is_nominal_instance(core, m, "Folder")):
  1211. result = result + (get_filename(read_attribute(core, m, "name")) + "/\n")
  1212. else:
  1213. result = result + (get_filename(read_attribute(core, m, "name")) + "\n")
  1214. return result!
  1215. else:
  1216. return "Permission denied to folder: " + location!
  1217. else:
  1218. return "Location not found: " + location!
  1219. String function cmd_model_list_full(location : String):
  1220. // List all models with full info
  1221. if (get_entry_id(location) != ""):
  1222. if (allow_read(current_user_id, get_entry_id(location))):
  1223. Element models
  1224. String m
  1225. String permissions
  1226. String owner
  1227. String group
  1228. String name
  1229. String type
  1230. String result
  1231. result = "Success: "
  1232. models = allAssociationDestinations(core, get_entry_id(location), "contains")
  1233. while (set_len(models) > 0):
  1234. m = set_pop(models)
  1235. permissions = read_attribute(core, m, "permissions")
  1236. owner = read_attribute(core, set_pop(allAssociationDestinations(core, m, "owner")), "name")
  1237. group = read_attribute(core, set_pop(allAssociationDestinations(core, m, "group")), "name")
  1238. if (is_nominal_instance(core, m, "Folder")):
  1239. name = get_filename(read_attribute(core, m, "name")) + "/"
  1240. else:
  1241. name = get_filename(read_attribute(core, m, "name"))
  1242. result = result + permissions + " " + owner + " " + group + " " + name + "\n"
  1243. return result!
  1244. else:
  1245. return "Permission denied to folder: " + location!
  1246. else:
  1247. return "Location not found: " + location!
  1248. String function cmd_transformation_add_MANUAL(source_models : Element, target_models : Element, operation_name : String):
  1249. return transformation_add(source_models, target_models, operation_name, "manual")!
  1250. String function cmd_transformation_add_AL(source_models : Element, target_models : Element, operation_name : String):
  1251. return transformation_add(source_models, target_models, operation_name, "actionlanguage")!
  1252. String function transformation_add(source_models : Element, target_models : Element, operation_name : String, operation_type : String):
  1253. // Add a manual transformation model
  1254. String model_id
  1255. Element models
  1256. Element source
  1257. Element target
  1258. String name
  1259. Element all_formalisms
  1260. Element formalism_map
  1261. Element merged_formalism
  1262. String merged_formalism_id
  1263. String source_formalism_id
  1264. String tracability_link
  1265. Element fused
  1266. Element keys
  1267. String key
  1268. Element mm
  1269. source = dict_create()
  1270. target = dict_create()
  1271. all_formalisms = set_create()
  1272. formalism_map = set_create()
  1273. fused = set_create()
  1274. keys = dict_keys(source_models)
  1275. while (set_len(keys) > 0):
  1276. key = set_pop(keys)
  1277. name = source_models[key]
  1278. model_id = get_entry_id(name)
  1279. if (model_id != ""):
  1280. if (allow_read(current_user_id, model_id)):
  1281. if (bool_not(dict_in(source, key))):
  1282. dict_add(source, key, model_id)
  1283. mm = get_full_model(model_id, get_entry_id("formalisms/SimpleClassDiagrams"))
  1284. if (element_eq(mm, read_root())):
  1285. return "Transformation source type not in formalisms/SimpleClassDiagrams hierarchy: " + key!
  1286. if (bool_not(set_in(fused, key))):
  1287. set_add_node(formalism_map, create_tuple(key, mm))
  1288. set_add(fused, key)
  1289. if (bool_not(set_in(all_formalisms, model_id))):
  1290. set_add(all_formalisms, model_id)
  1291. else:
  1292. return "Name already selected for source: " + key!
  1293. else:
  1294. return "Permission denied to model: " + name!
  1295. else:
  1296. return "Model not found: " + name!
  1297. keys = dict_keys(target_models)
  1298. while (set_len(keys) > 0):
  1299. key = set_pop(keys)
  1300. name = target_models[key]
  1301. model_id = get_entry_id(name)
  1302. if (model_id != ""):
  1303. if (allow_read(current_user_id, model_id)):
  1304. if (bool_not(dict_in(target, key))):
  1305. dict_add(target, key, model_id)
  1306. mm = get_full_model(model_id, get_entry_id("formalisms/SimpleClassDiagrams"))
  1307. if (element_eq(mm, read_root())):
  1308. return "Transformation target type not in formalisms/SimpleClassDiagrams hierarchy: " + key!
  1309. if (bool_not(set_in(fused, key))):
  1310. set_add_node(formalism_map, create_tuple(key, mm))
  1311. set_add(fused, key)
  1312. if (bool_not(set_in(all_formalisms, model_id))):
  1313. set_add(all_formalisms, model_id)
  1314. else:
  1315. return "Name already selected for target: " + key!
  1316. else:
  1317. return "Permission denied to model: " + name!
  1318. else:
  1319. return "Model not found: " + name!
  1320. if (get_entry_id(operation_name) == ""):
  1321. // Write out a merged metamodel containing all these models: this is the MM for the manual operation
  1322. // New location is available, so write
  1323. if (bool_not(bool_and(dict_len(source_models) == 0, dict_len(target_models) == 0))):
  1324. merged_formalism = model_fuse(formalism_map)
  1325. modify(merged_formalism, True)
  1326. if (operation_type == "manual"):
  1327. // Finished with all information, now create the model itself!
  1328. Element m
  1329. m = get_full_model(get_entry_id("formalisms/ManualOperation"), get_entry_id("formalisms/SimpleClassDiagrams"))
  1330. if (element_eq(m, read_root())):
  1331. log("Error when opening ManualOperation: no conformance relation found")
  1332. return "Error when opening ManualOperation: no conformance relation found"!
  1333. model_create(instantiate_model(m), operation_name, get_entry_id("formalisms/ManualOperation"), "ManualOperation")
  1334. model_id = get_entry_id(operation_name)
  1335. elif (operation_type == "actionlanguage"):
  1336. // Finished with all information, now create the model itself!
  1337. output("Waiting for code constructors...")
  1338. Element compiled
  1339. compiled = compile_code(input())
  1340. if (element_eq(compiled, read_root())):
  1341. return "Compilation error"!
  1342. add_code_model(get_full_model(get_entry_id("formalisms/ActionLanguage"), get_entry_id("formalisms/SimpleClassDiagrams")), "AL/" + operation_name, compiled)
  1343. model_create(import_node("AL/" + operation_name), operation_name, get_entry_id("formalisms/ActionLanguage"), "ActionLanguage")
  1344. model_id = get_entry_id(operation_name)
  1345. if (bool_not(bool_and(dict_len(source_models) == 0, dict_len(target_models) == 0))):
  1346. model_create(merged_formalism, "merged/" + operation_name, get_entry_id("formalisms/SimpleClassDiagrams"), "Model")
  1347. merged_formalism_id = get_entry_id("merged/" + operation_name)
  1348. // Add tracability links at this level
  1349. while (set_len(all_formalisms) > 0):
  1350. source_formalism_id = set_pop(all_formalisms)
  1351. tracability_link = instantiate_link(core, "tracability", "", merged_formalism_id, source_formalism_id)
  1352. instantiate_attribute(core, tracability_link, "type", "merged")
  1353. tracability_link = instantiate_link(core, "tracability", "", model_id, merged_formalism_id)
  1354. instantiate_attribute(core, tracability_link, "type", "operatesOn")
  1355. // Extend metadata with info on source and target
  1356. String link
  1357. String dst
  1358. keys = dict_keys(source)
  1359. while (set_len(keys) > 0):
  1360. key = set_pop(keys)
  1361. link = instantiate_link(core, "transformInput", "", model_id, source[key])
  1362. instantiate_attribute(core, link, "name", key)
  1363. keys = dict_keys(target)
  1364. while (set_len(keys) > 0):
  1365. key = set_pop(keys)
  1366. link = instantiate_link(core, "transformOutput", "", model_id, target[key])
  1367. instantiate_attribute(core, link, "name", key)
  1368. return "Success"!
  1369. else:
  1370. return "Model exists: " + operation_name!
  1371. String function cmd_transformation_add_MT(source_models : Element, target_models : Element, operation_name : String):
  1372. // Add a model transformation model
  1373. // Just a usual model instantiation, but need to add the source and target links based on user info
  1374. String ramified_metamodel_id
  1375. Element ramified_metamodel
  1376. String model_id
  1377. Element models
  1378. Element links
  1379. String link_id
  1380. Element supported
  1381. Element source
  1382. Element target
  1383. String name
  1384. String new_model_id
  1385. String merged_link_id
  1386. Element links_merged
  1387. String merged_formalism
  1388. Element keys
  1389. String key
  1390. Element mm
  1391. Element to_ramify
  1392. String source_formalism_id
  1393. String merged_formalism_id
  1394. String tracability_link
  1395. source = dict_create()
  1396. target = dict_create()
  1397. to_ramify = set_create()
  1398. keys = dict_keys(source_models)
  1399. while (set_len(keys) > 0):
  1400. key = set_pop(keys)
  1401. name = source_models[key]
  1402. model_id = get_entry_id(name)
  1403. if (model_id != ""):
  1404. if (allow_read(current_user_id, name)):
  1405. // Check whether or not it is formalisms/SimpleClassDiagrams
  1406. if (is_typed_by(model_id, get_entry_id("formalisms/SimpleClassDiagrams"))):
  1407. if (bool_not(dict_in(source, key))):
  1408. dict_add(source, key, model_id)
  1409. mm = get_full_model(model_id, get_entry_id("formalisms/SimpleClassDiagrams"))
  1410. if (element_eq(mm, read_root())):
  1411. return "ModelTransformation source type not in formalisms/SimpleClassDiagrams hierarchy: " + name!
  1412. set_add_node(to_ramify, create_tuple(key, mm))
  1413. else:
  1414. return "Name was already assigned a metamodel: " + key!
  1415. else:
  1416. return "Model not supported for RAMification: " + name!
  1417. else:
  1418. return "Permission denied: " + name!
  1419. else:
  1420. return "Model not found: " + name!
  1421. keys = dict_keys(target_models)
  1422. while (set_len(keys) > 0):
  1423. key = set_pop(keys)
  1424. name = target_models[key]
  1425. model_id = get_entry_id(name)
  1426. if (model_id != ""):
  1427. if (allow_read(current_user_id, name)):
  1428. if (is_typed_by(model_id, get_entry_id("formalisms/SimpleClassDiagrams"))):
  1429. if (bool_not(dict_in(target, key))):
  1430. if (dict_in(source, key)):
  1431. if (value_eq(model_id, source[key])):
  1432. dict_add(target, key, model_id)
  1433. // Don't add to to_ramify, as it is already in there!
  1434. else:
  1435. return "Name in output cannot have different type than input: " + key!
  1436. else:
  1437. dict_add(target, key, model_id)
  1438. mm = get_full_model(model_id, get_entry_id("formalisms/SimpleClassDiagrams"))
  1439. if (element_eq(mm, read_root())):
  1440. return "ModelTransformation target type not in formalisms/SimpleClassDiagrams hierarchy: " + name!
  1441. set_add_node(to_ramify, create_tuple(key, mm))
  1442. else:
  1443. return "Name was already assigned a metamodel: " + key!
  1444. else:
  1445. return "Model not supported for RAMification: " + name!
  1446. else:
  1447. return "Permission denied: " + name!
  1448. else:
  1449. return "Model not found: " + name!
  1450. merged_formalism = model_fuse(to_ramify)
  1451. modify(merged_formalism, True)
  1452. ramified_metamodel = ramify(merged_formalism)
  1453. model_create(ramified_metamodel, "RAMified/" + operation_name, get_entry_id("formalisms/SimpleClassDiagrams"), "Model")
  1454. ramified_metamodel_id = get_entry_id("RAMified/" + operation_name)
  1455. // Now use the RAMified model to create the instance
  1456. if (get_entry_id(operation_name) == ""):
  1457. String new_model
  1458. // Finished with all information, now create the model itself!
  1459. output("Waiting for model constructors...")
  1460. new_model = compile_model(input(), get_full_model(ramified_metamodel_id, get_entry_id("formalisms/SimpleClassDiagrams")))
  1461. if (element_eq(new_model, read_root())):
  1462. return "Compilation error"!
  1463. model_create(new_model, operation_name, ramified_metamodel_id, "ModelTransformation")
  1464. model_id = get_entry_id(operation_name)
  1465. // Write out a merged metamodel containing all these models: this is the MM for the manual operation
  1466. // New location is available, so write
  1467. model_create(merged_formalism, "merged/" + operation_name, get_entry_id("formalisms/SimpleClassDiagrams"), "Model")
  1468. merged_formalism_id = get_entry_id("merged/" + operation_name)
  1469. // Add tracability links at this level
  1470. tracability_link = instantiate_link(core, "tracability", "", model_id, merged_formalism_id)
  1471. instantiate_attribute(core, tracability_link, "type", "operatesOn")
  1472. // Add tracability links at this level
  1473. tracability_link = instantiate_link(core, "tracability", "", merged_formalism_id, ramified_metamodel_id)
  1474. instantiate_attribute(core, tracability_link, "type", "RAMified")
  1475. // Extend metadata with info on source and target
  1476. String link
  1477. String dst
  1478. keys = dict_keys(source)
  1479. while (set_len(keys) > 0):
  1480. key = set_pop(keys)
  1481. dst = source[key]
  1482. link = instantiate_link(core, "transformInput", "", model_id, dst)
  1483. instantiate_attribute(core, link, "name", key)
  1484. keys = dict_keys(target)
  1485. while (set_len(keys) > 0):
  1486. key = set_pop(keys)
  1487. dst = target[key]
  1488. link = instantiate_link(core, "transformOutput", "", model_id, dst)
  1489. instantiate_attribute(core, link, "name", key)
  1490. return "Success"!
  1491. else:
  1492. return "Model exists: " + operation_name!
  1493. String function cmd_permission_modify(model_name : String, permissions : String):
  1494. Integer permission
  1495. String model_id
  1496. model_id = get_entry_id(model_name)
  1497. if (model_id != ""):
  1498. if (get_relation_to_model(current_user_id, model_id) == 0):
  1499. Boolean fail
  1500. Integer i
  1501. i = 0
  1502. if (string_len(permissions) != 3):
  1503. fail = True
  1504. while (i < 3):
  1505. permission = cast_s2i(string_get(permissions, i))
  1506. if (bool_or(permission < 0, permission > 2)):
  1507. fail = True
  1508. break!
  1509. i = i + 1
  1510. if (bool_not(fail)):
  1511. unset_attribute(core, model_id, "permissions")
  1512. instantiate_attribute(core, model_id, "permissions", permissions)
  1513. return "Success"!
  1514. else:
  1515. return "Permission has incorrect format: must be a string of three characters, with each character being a digit between 0 and 2"!
  1516. else:
  1517. return "Permission denied to model: " + model_name!
  1518. else:
  1519. return "Model not found: " + model_name!
  1520. String function cmd_permission_owner(model_name : String, new_user_name : String):
  1521. String model_id
  1522. String new_user_id
  1523. model_id = get_entry_id(model_name)
  1524. if (model_id != ""):
  1525. if (bool_or(get_relation_to_model(current_user_id, model_id) == 0, is_admin(current_user_id))):
  1526. new_user_id = get_user_id(new_user_name)
  1527. if (new_user_id != ""):
  1528. model_delete_element(core, set_pop(allOutgoingAssociationInstances(core, model_id, "owner")))
  1529. instantiate_link(core, "owner", "", model_id, new_user_id)
  1530. return "Success"!
  1531. else:
  1532. return "No such user: " + new_user_name!
  1533. else:
  1534. return "Permission denied to model: " + model_name!
  1535. else:
  1536. return "Model not found: " + model_name!
  1537. String function cmd_permission_group(model_name : String, new_group_name : String):
  1538. String model_id
  1539. String group_id
  1540. model_id = get_entry_id(model_name)
  1541. if (model_id != ""):
  1542. if (bool_or(get_relation_to_model(current_user_id, model_id) == 0, is_admin(current_user_id))):
  1543. group_id = get_group_id(new_group_name)
  1544. if (group_id != ""):
  1545. model_delete_element(core, set_pop(allOutgoingAssociationInstances(core, model_id, "group")))
  1546. instantiate_link(core, "group", "", model_id, group_id)
  1547. return "Success"!
  1548. else:
  1549. return "No such group: " + new_group_name!
  1550. else:
  1551. return "Permission denied to model: " + model_name!
  1552. else:
  1553. return "Model not found: " + model_name!
  1554. String function cmd_group_create(group_name : String):
  1555. // Create a new group and become its owner
  1556. String group_id
  1557. String name
  1558. group_id = get_group_id(group_name)
  1559. if (group_id == ""):
  1560. group_id = instantiate_node(core, "Group", "")
  1561. instantiate_attribute(core, group_id, "name", group_name)
  1562. instantiate_link(core, "belongsTo", "", current_user_id, group_id)
  1563. instantiate_link(core, "owner", "", group_id, current_user_id)
  1564. return "Success"!
  1565. else:
  1566. return "Group exists: " + group_name!
  1567. String function cmd_group_delete(group_name : String):
  1568. // Delete an existing group
  1569. String group_id
  1570. group_id = get_group_id(group_name)
  1571. if (group_id != ""):
  1572. if (allow_group_modify(current_user_id, group_id)):
  1573. model_delete_element(core, group_id)
  1574. return "Success"!
  1575. else:
  1576. return "Permission denied to group: " + group_name!
  1577. else:
  1578. return "Group not found: " + group_name!
  1579. String function cmd_group_owner_add(group_name : String, other_user_name : String):
  1580. // Add an owner to your group
  1581. String group_id
  1582. String other_user_id
  1583. group_id = get_group_id(group_name)
  1584. if (group_id != ""):
  1585. if (allow_group_modify(current_user_id, group_id)):
  1586. other_user_id = get_user_id(other_user_name)
  1587. if (other_user_id != ""):
  1588. if (set_in(allAssociationDestinations(core, group_id, "owner"), other_user_id)):
  1589. instantiate_link(core, "owner", "", group_id, other_user_id)
  1590. if (set_in(allAssociationDestinations(core, other_user_id, "belongsTo"), group_id)):
  1591. instantiate_link(core, "belongsTo", "", other_user_id, group_id)
  1592. return "Success"!
  1593. else:
  1594. return "User is already the owner!"!
  1595. else:
  1596. return "User not found: " + other_user_name!
  1597. else:
  1598. return "Permission denied to group: " + group_name!
  1599. else:
  1600. return "Group not found: " + group_name!
  1601. String function cmd_group_owner_delete(group_name : String, other_user_name : String):
  1602. // Remove an owner from your group
  1603. String group_id
  1604. String other_user_id
  1605. group_id = get_group_id(group_name)
  1606. if (group_id != ""):
  1607. if (allow_group_modify(current_user_id, group_id)):
  1608. other_user_id = get_user_id(other_user_name)
  1609. if (other_user_id != ""):
  1610. if (set_in(allAssociationDestinations(core, other_user_id, "belongsTo"), group_id)):
  1611. if (set_in(allAssociationDestinations(core, group_id, "owner"), other_user_id)):
  1612. Element overlap
  1613. overlap = set_overlap(allIncomingAssociationInstances(core, group_id, "owner"), allOutgoingAssociationInstances(core, other_user_id, "owner"))
  1614. model_delete_element(core, set_pop(overlap))
  1615. return "Success"!
  1616. else:
  1617. return "User is not an owner of the group!"!
  1618. else:
  1619. return "User is not a member of the group!"!
  1620. else:
  1621. return "User not found: " + other_user_name!
  1622. else:
  1623. return "Permission denied to group: " + group_name!
  1624. else:
  1625. return "Group not found: " + group_name!
  1626. String function cmd_group_join(group_name : String, other_user_name : String):
  1627. // Add someone to your group
  1628. String group_id
  1629. String other_user_id
  1630. group_id = get_group_id(group_name)
  1631. if (group_id != ""):
  1632. if (allow_group_modify(current_user_id, group_id)):
  1633. other_user_id = get_user_id(other_user_name)
  1634. if (other_user_id != ""):
  1635. if (set_in(allOutgoingAssociationInstances(core, other_user_id, "belongsTo"), group_id)):
  1636. instantiate_link(core, "belongsTo", "", other_user_id, group_id)
  1637. return "Success"!
  1638. else:
  1639. return "User is already a member of the group!"!
  1640. else:
  1641. return "User not found: " + other_user_name!
  1642. else:
  1643. return "Permission denied to group: " + group_name!
  1644. else:
  1645. return "Group not found: " + group_name!
  1646. String function cmd_group_kick(group_name : String, other_user_name : String):
  1647. // Remove someone from your group
  1648. String group_id
  1649. String other_user_id
  1650. group_id = get_group_id(group_name)
  1651. if (group_id != ""):
  1652. if (allow_group_modify(current_user_id, group_id)):
  1653. other_user_id = get_user_id(other_user_name)
  1654. if (other_user_id != ""):
  1655. Element overlap
  1656. if (set_in(allAssociationDestinations(core, other_user_id, "belongsTo"), group_id)):
  1657. overlap = set_overlap(allIncomingAssociationInstances(core, group_id, "belongsTo"), allOutgoingAssociationInstances(core, other_user_id, "belongsTo"))
  1658. model_delete_element(core,set_pop(overlap))
  1659. // Check if user was an owner as well
  1660. if (set_in(allAssociationDestinations(core, group_id, "owner"), other_user_id)):
  1661. overlap = set_overlap(allIncomingAssociationInstances(core, other_user_id, "owner"), allOutgoingAssociationInstances(core, group_id, "owner"))
  1662. model_delete_element(core, set_pop(overlap))
  1663. return "Success"!
  1664. else:
  1665. return "User is not a member of the group!"!
  1666. else:
  1667. return "User not found: " + other_user_name!
  1668. else:
  1669. return "Permission denied to group: " + group_name!
  1670. else:
  1671. return "Group not found: " + group_name!
  1672. String function cmd_group_list():
  1673. // List all groups you are a member of (and whether you are admin or not!)
  1674. Element groups
  1675. String group_id
  1676. String admin
  1677. String result
  1678. result = ""
  1679. groups = allAssociationDestinations(core, current_user_id, "belongsTo")
  1680. while (set_len(groups) > 0):
  1681. group_id = set_pop(groups)
  1682. if (set_in(allAssociationDestinations(core, group_id, "owner"), current_user_id)):
  1683. admin = " A "
  1684. else:
  1685. admin = " "
  1686. result = result + (string_join(admin, read_attribute(core, group_id, "name")) + "\n")
  1687. return result!
  1688. String function cmd_admin_promote(other_user_name : String):
  1689. // Promote a user to admin status
  1690. if (is_admin(current_user_id)):
  1691. String other_user_id
  1692. other_user_id = get_user_id(other_user_name)
  1693. if (other_user_id != ""):
  1694. unset_attribute(core, other_user_id, "admin")
  1695. instantiate_attribute(core, other_user_id, "admin", True)
  1696. return "Success"!
  1697. else:
  1698. return "User not found: " + other_user_name!
  1699. else:
  1700. return "Permission denied to administrate: " + other_user_name!
  1701. String function cmd_admin_demote(other_user_name : String):
  1702. // Demote a user to normal status
  1703. if (is_admin(current_user_id)):
  1704. String other_user_id
  1705. other_user_id = get_user_id(other_user_name)
  1706. if (other_user_id != ""):
  1707. unset_attribute(core, other_user_id, "admin")
  1708. instantiate_attribute(core, other_user_id, "admin", False)
  1709. return "Success"!
  1710. else:
  1711. return "User not found: " + other_user_name!
  1712. else:
  1713. return "Permission denied to administrate: " + other_user_name!
  1714. String function cmd_service_register(service_name : String):
  1715. // Active a service for this specific user
  1716. if (get_service_id(service_name) == ""):
  1717. String service
  1718. service = instantiate_node(core, "Service", "")
  1719. instantiate_attribute(core, service, "name", service_name)
  1720. instantiate_attribute(core, service, "port", get_taskname())
  1721. output("Success: " + get_taskname())
  1722. // Wait until we can stop the service
  1723. while (cast_v2s(input()) != "\"service_stop\""):
  1724. output("Service is running on this task: stop it by sending 'service_stop'")
  1725. model_delete_element(core, service)
  1726. // Service terminated
  1727. return "Success"!
  1728. else:
  1729. return "Service already exists: " + service_name!
  1730. String function cmd_user_password(user_name : String, new_password : String):
  1731. if (bool_or(current_user_id == get_user_id(user_name), is_admin(current_user_id))):
  1732. if (get_user_id(user_name) != ""):
  1733. instantiate_attribute(core, get_user_id(user_name), "password", hash(new_password))
  1734. return "Success"!
  1735. else:
  1736. return "No such user: " + user_name!
  1737. else:
  1738. return "Permission denied to user: " + user_name!
  1739. String function cmd_transformation_signature(transformation_name : String):
  1740. String model_id
  1741. model_id = get_entry_id(transformation_name)
  1742. if (model_id != ""):
  1743. if (is_nominal_instance(core, model_id, "Transformation")):
  1744. if (allow_read(current_user_id, transformation_name)):
  1745. String result
  1746. String elem
  1747. Element inputs
  1748. Element outputs
  1749. result = "Success: "
  1750. inputs = allOutgoingAssociationInstances(core, model_id, "transformInput")
  1751. while (set_len(inputs) > 0):
  1752. elem = set_pop(inputs)
  1753. result = string_join(string_join(string_join(string_join("I ", read_attribute(core, elem, "name")), " "), full_name(readAssociationDestination(core, elem))), "\n")
  1754. outputs = allOutgoingAssociationInstances(core, model_id, "transformOutput")
  1755. while (set_len(outputs) > 0):
  1756. elem = set_pop(outputs)
  1757. result = string_join(string_join(string_join(string_join("O ", read_attribute(core, elem, "name")), " "), full_name(readAssociationDestination(core, elem))), "\n")
  1758. return result!
  1759. else:
  1760. return "Permission denied to operation: " + transformation_name!
  1761. else:
  1762. return "Model is not an operation: " + transformation_name!
  1763. else:
  1764. return "No such operation: " + transformation_name!
  1765. String function cmd_model_types(model_name : String):
  1766. if (get_entry_id(model_name) != ""):
  1767. String model_id
  1768. model_id = get_entry_id(model_name)
  1769. Element types
  1770. String type
  1771. String result
  1772. result = "Success: "
  1773. types = allAssociationDestinations(core, model_id, "instanceOf")
  1774. while (set_len(types) > 0):
  1775. type = set_pop(types)
  1776. result = string_join(result, full_name(type) + "\n")
  1777. return result!
  1778. else:
  1779. return "No such model: " + model_name!
  1780. String function cmd_folder_create(folder_name : String):
  1781. if (get_entry_id(folder_name) == ""):
  1782. if (element_neq(create_folders(current_user_id, folder_name), read_root())):
  1783. return "Success"!
  1784. else:
  1785. return "Permission denied to folder: " + folder_name!
  1786. else:
  1787. return "Folder alreay exists: " + folder_name!
  1788. String function cmd_conformance_add(model_name : String, metamodel_name : String):
  1789. // Create a new instanceOf relation now
  1790. String instance_of
  1791. instance_of = instantiate_link(core, "instanceOf", "", get_entry_id(model_name), get_entry_id(metamodel_name))
  1792. return "Success"!
  1793. Void function user_function_skip_init(user_id : String):
  1794. String cmd
  1795. String result
  1796. current_user_id = user_id
  1797. caches = dict_create()
  1798. dict_add_fast(caches, "models", dict_create())
  1799. dict_add_fast(caches, "users", dict_create())
  1800. dict_add_fast(caches, "full_name", dict_create())
  1801. // Find root element
  1802. Element elems
  1803. String elem
  1804. elems = allInstances(core, "Folder")
  1805. while (set_len(elems) > 0):
  1806. elem = set_pop(elems)
  1807. if (value_eq(read_attribute(core, elem, "name"), "")):
  1808. if (set_len(allIncomingAssociationInstances(core, elem, "contains")) == 0):
  1809. dict_add_fast(caches, "root", elem)
  1810. break!
  1811. output("Welcome to the Model Management Interface v2.0!")
  1812. output("Use the 'help' command for a list of possible commands")
  1813. while (True):
  1814. cmd = input()
  1815. if (cmd == "help"):
  1816. output(cmd_help())
  1817. elif (cmd == "model_add"):
  1818. output(cmd_model_add(single_input("Model type?"), single_input("Model name?"), single_input("Model textual representation?")))
  1819. elif (cmd == "process_execute"):
  1820. output(cmd_process_execute(single_input("Process to execute?"), single_input("Model prefix to use?")))
  1821. elif (cmd == "transformation_between"):
  1822. output(cmd_transformation_between(single_input("Source type?"), single_input("Target type?")))
  1823. elif (cmd == "model_render"):
  1824. output(cmd_model_render(single_input("Model name?"), single_input("Mapper name?"), single_input("Rendered name?")))
  1825. elif (cmd == "model_rendered"):
  1826. output(cmd_model_rendered(single_input("Model name?"), single_input("Mapper name?")))
  1827. elif (cmd == "transformation_execute"):
  1828. output(cmd_transformation_execute(single_input("Transformation name?"), dict_input("Source models?"), dict_input("Target models?")))
  1829. elif (cmd == "verify"):
  1830. result = cmd_verify(single_input("Model name?"), single_input("Metamodel name?"))
  1831. if (result != ""):
  1832. output(result)
  1833. elif (cmd == "model_overwrite"):
  1834. output(cmd_model_overwrite(single_input("Model name?"), single_input("Metamodel name?")))
  1835. elif (cmd == "model_modify"):
  1836. output(cmd_model_modify(single_input("Model name?"), single_input("Metamodel name?")))
  1837. elif (cmd == "model_delete"):
  1838. output(cmd_model_delete(single_input("Model name?")))
  1839. elif (cmd == "model_list"):
  1840. output(cmd_model_list(single_input("Location?")))
  1841. elif (cmd == "model_list_full"):
  1842. output(cmd_model_list_full(single_input("Location?")))
  1843. elif (cmd == "transformation_add_MANUAL"):
  1844. output(cmd_transformation_add_MANUAL(dict_input("Source model names?"), dict_input("Target model names?"), single_input("Operation name?")))
  1845. elif (cmd == "transformation_add_AL"):
  1846. output(cmd_transformation_add_AL(dict_input("Source model names?"), dict_input("Target model names?"), single_input("Operation name?")))
  1847. elif (cmd == "transformation_add_MT"):
  1848. output(cmd_transformation_add_MT(dict_input("Source model names?"), dict_input("Target models?"), single_input("Operation name?")))
  1849. elif (cmd == "permission_modify"):
  1850. output(cmd_permission_modify(single_input("Model name?"), single_input("Permissions?")))
  1851. elif (cmd == "permission_owner"):
  1852. output(cmd_permission_owner(single_input("Model name?"), single_input("New owning user?")))
  1853. elif (cmd == "permission_group"):
  1854. output(cmd_permission_group(single_input("Model name?"), single_input("New owning group?")))
  1855. elif (cmd == "group_create"):
  1856. output(cmd_group_create(single_input("New group name?")))
  1857. elif (cmd == "group_delete"):
  1858. output(cmd_group_delete(single_input("Group name?")))
  1859. elif (cmd == "group_owner_add"):
  1860. output(cmd_group_owner_add(single_input("Group name?"), single_input("User name?")))
  1861. elif (cmd == "group_owner_delete"):
  1862. output(cmd_group_owner_delete(single_input("Group name?"), single_input("User name?")))
  1863. elif (cmd == "group_join"):
  1864. output(cmd_group_join(single_input("Group name?"), single_input("User name?")))
  1865. elif (cmd == "group_kick"):
  1866. output(cmd_group_kick(single_input("Group name?"), single_input("User name?")))
  1867. elif (cmd == "group_list"):
  1868. output(cmd_group_list())
  1869. elif (cmd == "admin_promote"):
  1870. output(cmd_admin_promote(single_input("User name?")))
  1871. elif (cmd == "admin_demote"):
  1872. output(cmd_admin_demote(single_input("User name?")))
  1873. elif (cmd == "service_register"):
  1874. output(cmd_service_register(single_input("Service name?")))
  1875. elif (cmd == "user_password"):
  1876. output(cmd_user_password(single_input("User name?"), single_input("New password?")))
  1877. elif (cmd == "transformation_read_signature"):
  1878. output(cmd_transformation_signature(single_input("Transformation name?")))
  1879. elif (cmd == "verbose"):
  1880. set_verbose(True)
  1881. elif (cmd == "quiet"):
  1882. set_verbose(False)
  1883. elif (cmd == "self-destruct"):
  1884. // Delete user from Core Formalism
  1885. model_delete_element(core, user_id)
  1886. return !
  1887. elif (cmd == "user_logout"):
  1888. // TODO
  1889. cmd = "FAIL"
  1890. elif (cmd == "exit"):
  1891. // Exit by actually removing the user and decoupling it from all of its models
  1892. // Restarting with the same user name will NOT grant you access to anything of the previous user with that same name
  1893. // as the current user will have been deleted
  1894. return !
  1895. elif (cmd == "folder_create"):
  1896. output(cmd_folder_create(single_input("Folder name?")))
  1897. elif (cmd == "add_conformance"):
  1898. // TODO
  1899. output(cmd_conformance_add(single_input("Model name?"), single_input("Metamodel name?")))
  1900. elif (cmd == "remove_conformance"):
  1901. // TODO
  1902. cmd = "FAIL"
  1903. elif (cmd == "user_name"):
  1904. // TODO
  1905. cmd = "FAIL"
  1906. elif (cmd == "service_poll"):
  1907. // TODO
  1908. cmd = "FAIL"
  1909. elif (cmd == "model_types"):
  1910. output(cmd_model_types(single_input("Model name?")))
  1911. else:
  1912. output("Unknown command: " + cmd)
  1913. // We never get here!
  1914. return !