core_algorithm.alc 77 KB

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