core_algorithm.alc 77 KB

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