core_algorithm.alc 73 KB

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