core_algorithm.alc 76 KB

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