core_algorithm.alc 76 KB

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