core_algorithm.alc 94 KB

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