core_algorithm.alc 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216
  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_rendered(model_name : String, mapper_name : String):
  894. Element trace_links
  895. String rendered
  896. String trace_link_id
  897. String allowed_rendered
  898. Element trace_links_2
  899. String trace_link_2_id
  900. allowed_rendered = "Success: "
  901. trace_links = allOutgoingAssociationInstances(core, get_entry_id(model_name), "tracability")
  902. while (set_len(trace_links) > 0):
  903. trace_link_id = set_pop(trace_links)
  904. if (value_eq(read_attribute(core, trace_link_id, "type"), "CS_perceptualized")):
  905. rendered = readAssociationDestination(core, trace_link_id)
  906. trace_links_2 = allOutgoingAssociationInstances(core, rendered, "tracability")
  907. while (set_len(trace_links_2) > 0):
  908. trace_link_2_id = set_pop(trace_links_2)
  909. if (value_eq(read_attribute(core, readAssociationDestination(core, trace_link_2_id), "name"), mapper_name)):
  910. if (value_eq(read_attribute(core, trace_link_2_id, "type"), "CS_mapper")):
  911. allowed_rendered = string_join(allowed_rendered, read_attribute(core, rendered, "name")) + "\n"
  912. return allowed_rendered!
  913. String function cmd_model_render(model_name : String, mapper_name : String, rendered_name : String):
  914. String model_ID
  915. String mapper_ID
  916. String tracability_name
  917. Element inputs
  918. Element output_map
  919. Element rendered_model
  920. Element tracability_model
  921. Element result
  922. Element out_links
  923. String link
  924. model_ID = get_entry_id(model_name)
  925. if (model_ID != ""):
  926. mapper_ID = get_entry_id(mapper_name)
  927. if (allow_read(current_user_id, model_ID)):
  928. if (mapper_ID != ""):
  929. if (allow_read(current_user_id, mapper_ID)):
  930. // Everything is fine; start the actual operation
  931. // Find metamodel to render to
  932. tracability_name = "tracability/" + rendered_name
  933. // Take the abstract syntax model and the previously rendered model
  934. inputs = dict_create()
  935. dict_add(inputs, "abstract", model_name)
  936. dict_add(inputs, "rendered", rendered_name)
  937. // Fetch the output types
  938. output_map = dict_create()
  939. out_links = allOutgoingAssociationInstances(core, mapper_ID, "transformOutput")
  940. while(set_len(out_links) > 0):
  941. link = set_pop(out_links)
  942. dict_add(output_map, read_attribute(core, link, "name"), full_name(readAssociationDestination(core, link)))
  943. if (get_entry_id(rendered_name) == ""):
  944. // Instantiate
  945. Element rendered
  946. rendered = get_full_model(get_entry_id(output_map["rendered"]), get_entry_id("formalisms/SimpleClassDiagrams"))
  947. if (element_eq(rendered, read_root())):
  948. return "Rendered metamodel doesn't conform to formalisms/SimpleClassDiagrams"!
  949. rendered_model = instantiate_model(rendered)
  950. model_create(rendered_model, rendered_name, get_entry_id(output_map["rendered"]), "Model")
  951. // Tracability model won't exist either
  952. tracability_model = instantiate_model(get_full_model(get_entry_id("formalisms/Tracability"), get_entry_id("formalisms/SimpleClassDiagrams")))
  953. model_create(tracability_model, tracability_name, get_entry_id("formalisms/Tracability"), "Model")
  954. else:
  955. // Read out tracability model
  956. tracability_model = get_full_model(get_entry_id(tracability_name), get_entry_id("formalisms/Tracability"))
  957. if (element_eq(tracability_model, read_root())):
  958. return "Tracability model not typed by Tracability metamodel: " + tracability_name!
  959. // Do the operation itself!
  960. result = execute_operation(mapper_ID, inputs, tracability_model)
  961. // Overwrite the previous rendered model
  962. model_overwrite(result["rendered"], get_entry_id(rendered_name), get_entry_id(output_map["rendered"]))
  963. // Tracability updated in-place
  964. model_overwrite(tracability_model, get_entry_id(tracability_name), get_entry_id("formalisms/Tracability"))
  965. tracability_model = get_full_model(get_entry_id(tracability_name), get_entry_id("formalisms/Tracability"))
  966. if (element_eq(tracability_model, read_root())):
  967. return "Tracability model not typed by Tracability metamodel: " + tracability_name!
  968. // Link all information in the megamodel
  969. String tr_link
  970. tr_link = instantiate_link(core, "tracability", "", get_entry_id(model_name), get_entry_id(rendered_name))
  971. instantiate_attribute(core, tr_link, "type", "CS_perceptualized")
  972. tr_link = instantiate_link(core, "tracability", "", get_entry_id(rendered_name), get_entry_id(mapper_name))
  973. instantiate_attribute(core, tr_link, "type", "CS_mapper")
  974. // Also output the resulting model
  975. return "Success: " + JSON_print(get_full_model(get_entry_id(rendered_name), get_entry_id(output_map["rendered"])))!
  976. else:
  977. return "Permission denied to model: " + mapper_name!
  978. else:
  979. return "Model not found: " + mapper_name!
  980. else:
  981. return "Permission denied to model: " + model_name!
  982. else:
  983. return "Model not found: " + model_name!
  984. //trace_links = allOutgoingAssociationInstances(core, operation_id, "tracability")
  985. //merged_metamodel_id = ""
  986. //while (set_len(trace_links) > 0):
  987. // trace_link_id = set_pop(trace_links)
  988. // if (value_eq(read_attribute(core, trace_link_id, "type"), "operatesOn")):
  989. // merged_metamodel_id = readAssociationDestination(core, trace_link_id)
  990. String function cmd_transformation_execute(transformation_name : String, source_models : Element, target_models : Element):
  991. // Execute a transformation, whatever type it is
  992. // First we detect the type, so we know how to prepare for invocation
  993. String transformation_id
  994. String exact_type
  995. Element sources
  996. Element targets
  997. String source
  998. String target
  999. Element inputs
  1000. Element outputs
  1001. Element output_map
  1002. Element trace_links
  1003. String target_model_name
  1004. String source_model_name
  1005. String source_model_ID
  1006. Element result
  1007. Element keys
  1008. String key
  1009. String assoc_name
  1010. transformation_id = get_entry_id(transformation_name)
  1011. if (transformation_id != ""):
  1012. if (allow_read(current_user_id, transformation_id)):
  1013. if (is_nominal_instance(core, transformation_id, "Transformation")):
  1014. // Read out source and target links
  1015. inputs = dict_create()
  1016. sources = allOutgoingAssociationInstances(core, transformation_id, "transformInput")
  1017. while (set_len(sources) > 0):
  1018. source = set_pop(sources)
  1019. assoc_name = read_attribute(core, source, "name")
  1020. if (dict_in(source_models, assoc_name)):
  1021. source_model_name = source_models[assoc_name]
  1022. else:
  1023. return "Source model not bound: " + assoc_name!
  1024. source_model_ID = get_entry_id(source_model_name)
  1025. if (source_model_ID != ""):
  1026. if (allow_read(current_user_id, source_model_ID)):
  1027. // Check for conformance to the requested metamodel
  1028. dict_add(inputs, assoc_name, source_model_name)
  1029. continue!
  1030. else:
  1031. return "Permission denied to model: " + source_model_name!
  1032. else:
  1033. return "Model not found: " + source_model_name!
  1034. targets = allOutgoingAssociationInstances(core, transformation_id, "transformOutput")
  1035. outputs = dict_create()
  1036. output_map = dict_create()
  1037. while (set_len(targets) > 0):
  1038. target = set_pop(targets)
  1039. assoc_name = read_attribute(core, target, "name")
  1040. if (dict_in(target_models, assoc_name)):
  1041. target_model_name = target_models[assoc_name]
  1042. else:
  1043. return "Target model not bound: " + assoc_name!
  1044. if (get_entry_id(target_model_name) == ""):
  1045. // Doesn't exist yet, so we can easily create
  1046. if (get_entry_id(get_foldername(target_model_name)) == ""):
  1047. return "Folder not found: " + get_foldername(target_model_name)!
  1048. else:
  1049. if (allow_write(current_user_id, get_entry_id(get_foldername(target_model_name)))):
  1050. dict_add(output_map, assoc_name, full_name(readAssociationDestination(core, target)))
  1051. dict_add(outputs, assoc_name, target_model_name)
  1052. else:
  1053. return "Permission denied to folder: " + get_foldername(target_model_name)!
  1054. else:
  1055. // Already exists, so we need to check for write access
  1056. if (allow_write(current_user_id, get_entry_id(target_model_name))):
  1057. dict_add(output_map, assoc_name, read_attribute(core, readAssociationDestination(core, target), "name"))
  1058. dict_add(outputs, assoc_name, target_model_name)
  1059. else:
  1060. return "Permission denied to model: " + target_model_name!
  1061. if (read_type(core, transformation_id) == "ActionLanguage"):
  1062. output("Success: ready for AL execution")
  1063. elif (read_type(core, transformation_id) == "ManualOperation"):
  1064. output("Success: ready for MANUAL execution")
  1065. else:
  1066. output("Success: ready for MT execution")
  1067. result = execute_operation(transformation_id, inputs, read_root())
  1068. // Now write out the models again
  1069. if (element_eq(result, read_root())):
  1070. // Something went wrong!
  1071. return "Failure"!
  1072. else:
  1073. keys = dict_keys(result)
  1074. while (set_len(keys) > 0):
  1075. key = set_pop(keys)
  1076. if (get_entry_id(outputs[key]) == ""):
  1077. // New model
  1078. model_create(result[key], outputs[key], get_entry_id(output_map[key]), "Model")
  1079. else:
  1080. model_overwrite(result[key], get_entry_id(outputs[key]), get_entry_id(output_map[key]))
  1081. return "Success"!
  1082. else:
  1083. return "Model is not executable: " + transformation_name!
  1084. else:
  1085. return "Permission denied to model: " + transformation_name!
  1086. else:
  1087. return "Model not found: " + transformation_name!
  1088. String function cmd_verify(model_name : String, metamodel_name : String):
  1089. // Check whether a model conforms to its specification (with the selected type mapping)
  1090. String model_id
  1091. String result
  1092. Element inputs
  1093. model_id = get_entry_id(model_name)
  1094. if (model_id != ""):
  1095. if (allow_read(current_user_id, model_id)):
  1096. if (get_entry_id(metamodel_name) != ""):
  1097. if (allow_read(current_user_id, get_entry_id(metamodel_name))):
  1098. Element m
  1099. m = get_full_model(get_entry_id(model_name), get_entry_id(metamodel_name))
  1100. if (element_eq(m, read_root())):
  1101. return "No conformance relation can be found between these models"!
  1102. return string_join("Success: ", conformance_scd(m))!
  1103. else:
  1104. return "Permission denied to model: " + metamodel_name!
  1105. else:
  1106. return "Model not found: " + metamodel_name!
  1107. else:
  1108. return "Permission denied to model: " + model_name!
  1109. else:
  1110. return "Model not found: " + model_name!
  1111. String function cmd_model_overwrite(model_name : String, metamodel_name : String):
  1112. // Overwrites an existing model without changing any metadata
  1113. String model_id
  1114. String type_id
  1115. Element new_model
  1116. model_id = get_entry_id(model_name)
  1117. if (model_id != ""):
  1118. if (allow_write(current_user_id, model_id)):
  1119. type_id = set_pop(allAssociationDestinations(core, model_id, "instanceOf"))
  1120. if (allow_read(current_user_id, type_id)):
  1121. Element mm
  1122. mm = get_full_model(get_entry_id(metamodel_name), get_entry_id("formalisms/SimpleClassDiagrams"))
  1123. if (element_eq(mm, read_root())):
  1124. return "Metamodel does not conform to formalisms/SimpleClassDiagrams: " + metamodel_name!
  1125. output("Waiting for model constructors...")
  1126. new_model = compile_model(input(), mm)
  1127. if (element_eq(new_model, read_root())):
  1128. return "Compilation error"!
  1129. model_overwrite(new_model, model_id, get_entry_id(metamodel_name))
  1130. return "Success"!
  1131. else:
  1132. return string_join("Permission denied to model: ", full_name(type_id))!
  1133. else:
  1134. return "Permission denied to model: " + model_name!
  1135. else:
  1136. return "Model not found: " + model_name!
  1137. String function cmd_model_modify(model_name : String, metamodel_name : String):
  1138. // Model modify operation, which uses the mini_modify.alc operations, though with extensions for access control
  1139. String model_id
  1140. String type_id
  1141. model_id = get_entry_id(model_name)
  1142. if (model_id != ""):
  1143. if (allow_read(current_user_id, model_id)):
  1144. type_id = set_pop(allAssociationDestinations(core, model_id, "instanceOf"))
  1145. if (allow_read(current_user_id, type_id)):
  1146. Element new_model
  1147. new_model = get_full_model(model_id, get_entry_id(metamodel_name))
  1148. if (element_eq(new_model, read_root())):
  1149. return "No conformance relation can be found between these models"!
  1150. modify(new_model, allow_write(current_user_id, model_id))
  1151. if (allow_write(current_user_id, model_id)):
  1152. // Overwrite the modified model
  1153. model_overwrite(new_model, model_id, get_entry_id(metamodel_name))
  1154. return "Success"!
  1155. else:
  1156. return string_join("Permission denied to model: ", full_name(type_id))!
  1157. else:
  1158. return "Permission denied to model: " + model_name!
  1159. else:
  1160. return "Model not found: " + model_name!
  1161. Void function model_delete(model_id : String):
  1162. if (read_type(core, model_id) == "Folder"):
  1163. // Folder, so delete all containing elements as well!
  1164. Element elems
  1165. elems = allAssociationDestinations(core, model_id, "contains")
  1166. while (set_len(elems) > 0):
  1167. model_delete(set_pop(elems))
  1168. // Delete self
  1169. model_delete_element(core, model_id)
  1170. else:
  1171. // Only delete this element
  1172. model_delete_element(core, model_id)
  1173. return!
  1174. String function cmd_model_delete(model_name : String):
  1175. String model_id
  1176. model_id = get_entry_id(model_name)
  1177. if (model_id != ""):
  1178. if (allow_write(current_user_id, model_id)):
  1179. model_delete(model_id)
  1180. return "Success"!
  1181. else:
  1182. return "Permission denied to model: " + model_name!
  1183. else:
  1184. return "Model not found: " + model_name!
  1185. String function cmd_model_list(location : String):
  1186. // List all models
  1187. if (get_entry_id(location) != ""):
  1188. if (allow_read(current_user_id, get_entry_id(location))):
  1189. Element models
  1190. String result
  1191. String m
  1192. result = "Success: "
  1193. models = allAssociationDestinations(core, get_entry_id(location), "contains")
  1194. while (set_len(models) > 0):
  1195. m = set_pop(models)
  1196. if (is_nominal_instance(core, m, "Folder")):
  1197. result = result + (get_filename(read_attribute(core, m, "name")) + "/\n")
  1198. else:
  1199. result = result + (get_filename(read_attribute(core, m, "name")) + "\n")
  1200. return result!
  1201. else:
  1202. return "Permission denied to folder: " + location!
  1203. else:
  1204. return "Location not found: " + location!
  1205. String function cmd_model_list_full(location : String):
  1206. // List all models with full info
  1207. if (get_entry_id(location) != ""):
  1208. if (allow_read(current_user_id, get_entry_id(location))):
  1209. Element models
  1210. String m
  1211. String permissions
  1212. String owner
  1213. String group
  1214. String name
  1215. String type
  1216. String result
  1217. result = "Success: "
  1218. models = allAssociationDestinations(core, get_entry_id(location), "contains")
  1219. while (set_len(models) > 0):
  1220. m = set_pop(models)
  1221. permissions = read_attribute(core, m, "permissions")
  1222. owner = read_attribute(core, set_pop(allAssociationDestinations(core, m, "owner")), "name")
  1223. group = read_attribute(core, set_pop(allAssociationDestinations(core, m, "group")), "name")
  1224. if (is_nominal_instance(core, m, "Folder")):
  1225. name = get_filename(read_attribute(core, m, "name")) + "/"
  1226. else:
  1227. name = get_filename(read_attribute(core, m, "name"))
  1228. result = result + (((((((permissions + " ") + owner) + " ") + group) + " ") + name) + "\n")
  1229. return result!
  1230. else:
  1231. return "Permission denied to folder: " + location!
  1232. else:
  1233. return "Location not found: " + location!
  1234. String function cmd_transformation_add_MANUAL(source_models : Element, target_models : Element, operation_name : String):
  1235. return transformation_add(source_models, target_models, operation_name, "manual")!
  1236. String function cmd_transformation_add_AL(source_models : Element, target_models : Element, operation_name : String):
  1237. return transformation_add(source_models, target_models, operation_name, "actionlanguage")!
  1238. String function transformation_add(source_models : Element, target_models : Element, operation_name : String, operation_type : String):
  1239. // Add a manual transformation model
  1240. String model_id
  1241. Element models
  1242. Element source
  1243. Element target
  1244. String name
  1245. Element all_formalisms
  1246. Element formalism_map
  1247. Element merged_formalism
  1248. String merged_formalism_id
  1249. String source_formalism_id
  1250. String tracability_link
  1251. Element fused
  1252. Element keys
  1253. String key
  1254. Element mm
  1255. source = dict_create()
  1256. target = dict_create()
  1257. all_formalisms = set_create()
  1258. formalism_map = set_create()
  1259. fused = set_create()
  1260. keys = dict_keys(source_models)
  1261. while (set_len(keys) > 0):
  1262. key = set_pop(keys)
  1263. name = source_models[key]
  1264. model_id = get_entry_id(name)
  1265. if (model_id != ""):
  1266. if (allow_read(current_user_id, model_id)):
  1267. if (bool_not(dict_in(source, key))):
  1268. dict_add(source, key, model_id)
  1269. mm = get_full_model(model_id, get_entry_id("formalisms/SimpleClassDiagrams"))
  1270. if (element_eq(mm, read_root())):
  1271. return "Transformation source type not in formalisms/SimpleClassDiagrams hierarchy: " + key!
  1272. if (bool_not(set_in(fused, key))):
  1273. set_add_node(formalism_map, create_tuple(key, mm))
  1274. set_add(fused, key)
  1275. if (bool_not(set_in(all_formalisms, model_id))):
  1276. set_add(all_formalisms, model_id)
  1277. else:
  1278. return "Name already selected for source: " + key!
  1279. else:
  1280. return "Permission denied to model: " + name!
  1281. else:
  1282. return "Model not found: " + name!
  1283. keys = dict_keys(target_models)
  1284. while (set_len(keys) > 0):
  1285. key = set_pop(keys)
  1286. name = target_models[key]
  1287. model_id = get_entry_id(name)
  1288. if (model_id != ""):
  1289. if (allow_read(current_user_id, model_id)):
  1290. if (bool_not(dict_in(target, key))):
  1291. dict_add(target, key, model_id)
  1292. mm = get_full_model(model_id, get_entry_id("formalisms/SimpleClassDiagrams"))
  1293. if (element_eq(mm, read_root())):
  1294. return "Transformation target type not in formalisms/SimpleClassDiagrams hierarchy: " + key!
  1295. if (bool_not(set_in(fused, key))):
  1296. set_add_node(formalism_map, create_tuple(key, mm))
  1297. set_add(fused, key)
  1298. if (bool_not(set_in(all_formalisms, model_id))):
  1299. set_add(all_formalisms, model_id)
  1300. else:
  1301. return "Name already selected for target: " + key!
  1302. else:
  1303. return "Permission denied to model: " + name!
  1304. else:
  1305. return "Model not found: " + name!
  1306. if (get_entry_id(operation_name) == ""):
  1307. // Write out a merged metamodel containing all these models: this is the MM for the manual operation
  1308. // New location is available, so write
  1309. if (bool_not(bool_and(dict_len(source_models) == 0, dict_len(target_models) == 0))):
  1310. merged_formalism = model_fuse(formalism_map)
  1311. modify(merged_formalism, True)
  1312. if (operation_type == "manual"):
  1313. // Finished with all information, now create the model itself!
  1314. Element m
  1315. m = get_full_model(get_entry_id("formalisms/ManualOperation"), get_entry_id("formalisms/SimpleClassDiagrams"))
  1316. if (element_eq(m, read_root())):
  1317. log("Error when opening ManualOperation: no conformance relation found")
  1318. return "Error when opening ManualOperation: no conformance relation found"!
  1319. model_create(instantiate_model(m), operation_name, get_entry_id("formalisms/ManualOperation"), "ManualOperation")
  1320. model_id = get_entry_id(operation_name)
  1321. elif (operation_type == "actionlanguage"):
  1322. // Finished with all information, now create the model itself!
  1323. output("Waiting for code constructors...")
  1324. Element compiled
  1325. compiled = compile_code(input())
  1326. if (element_eq(compiled, read_root())):
  1327. return "Compilation error"!
  1328. add_code_model(get_full_model(get_entry_id("formalisms/ActionLanguage"), get_entry_id("formalisms/SimpleClassDiagrams")), "AL/" + operation_name, compiled)
  1329. model_create(import_node("AL/" + operation_name), operation_name, get_entry_id("formalisms/ActionLanguage"), "ActionLanguage")
  1330. model_id = get_entry_id(operation_name)
  1331. if (bool_not(bool_and(dict_len(source_models) == 0, dict_len(target_models) == 0))):
  1332. model_create(merged_formalism, "merged/" + operation_name, get_entry_id("formalisms/SimpleClassDiagrams"), "Model")
  1333. merged_formalism_id = get_entry_id("merged/" + operation_name)
  1334. // Add tracability links at this level
  1335. while (set_len(all_formalisms) > 0):
  1336. source_formalism_id = set_pop(all_formalisms)
  1337. tracability_link = instantiate_link(core, "tracability", "", merged_formalism_id, source_formalism_id)
  1338. instantiate_attribute(core, tracability_link, "type", "merged")
  1339. tracability_link = instantiate_link(core, "tracability", "", model_id, merged_formalism_id)
  1340. instantiate_attribute(core, tracability_link, "type", "operatesOn")
  1341. // Extend metadata with info on source and target
  1342. String link
  1343. String dst
  1344. keys = dict_keys(source)
  1345. while (set_len(keys) > 0):
  1346. key = set_pop(keys)
  1347. link = instantiate_link(core, "transformInput", "", model_id, source[key])
  1348. instantiate_attribute(core, link, "name", key)
  1349. keys = dict_keys(target)
  1350. while (set_len(keys) > 0):
  1351. key = set_pop(keys)
  1352. link = instantiate_link(core, "transformOutput", "", model_id, target[key])
  1353. instantiate_attribute(core, link, "name", key)
  1354. return "Success"!
  1355. else:
  1356. return "Model exists: " + operation_name!
  1357. String function cmd_transformation_add_MT(source_models : Element, target_models : Element, operation_name : String):
  1358. // Add a model transformation model
  1359. // Just a usual model instantiation, but need to add the source and target links based on user info
  1360. String ramified_metamodel_id
  1361. Element ramified_metamodel
  1362. String model_id
  1363. Element models
  1364. Element links
  1365. String link_id
  1366. Element supported
  1367. Element source
  1368. Element target
  1369. String name
  1370. String new_model_id
  1371. String merged_link_id
  1372. Element links_merged
  1373. String merged_formalism
  1374. Element keys
  1375. String key
  1376. Element mm
  1377. Element to_ramify
  1378. String source_formalism_id
  1379. String merged_formalism_id
  1380. String tracability_link
  1381. source = dict_create()
  1382. target = dict_create()
  1383. to_ramify = set_create()
  1384. keys = dict_keys(source_models)
  1385. while (set_len(keys) > 0):
  1386. key = set_pop(keys)
  1387. name = source_models[key]
  1388. model_id = get_entry_id(name)
  1389. if (model_id != ""):
  1390. if (allow_read(current_user_id, name)):
  1391. // Check whether or not it is formalisms/SimpleClassDiagrams
  1392. if (is_typed_by(model_id, get_entry_id("formalisms/SimpleClassDiagrams"))):
  1393. if (bool_not(dict_in(source, key))):
  1394. dict_add(source, key, model_id)
  1395. mm = get_full_model(model_id, get_entry_id("formalisms/SimpleClassDiagrams"))
  1396. if (element_eq(mm, read_root())):
  1397. return "ModelTransformation source type not in formalisms/SimpleClassDiagrams hierarchy: " + name!
  1398. set_add_node(to_ramify, create_tuple(key, mm))
  1399. else:
  1400. return "Name was already assigned a metamodel: " + key!
  1401. else:
  1402. return "Model not supported for RAMification: " + name!
  1403. else:
  1404. return "Permission denied: " + name!
  1405. else:
  1406. return "Model not found: " + name!
  1407. keys = dict_keys(target_models)
  1408. while (set_len(keys) > 0):
  1409. key = set_pop(keys)
  1410. name = target_models[key]
  1411. model_id = get_entry_id(name)
  1412. if (model_id != ""):
  1413. if (allow_read(current_user_id, name)):
  1414. if (is_typed_by(model_id, get_entry_id("formalisms/SimpleClassDiagrams"))):
  1415. if (bool_not(dict_in(target, key))):
  1416. if (dict_in(source, key)):
  1417. if (value_eq(model_id, source[key])):
  1418. dict_add(target, key, model_id)
  1419. // Don't add to to_ramify, as it is already in there!
  1420. else:
  1421. return "Name in output cannot have different type than input: " + key!
  1422. else:
  1423. dict_add(target, key, model_id)
  1424. mm = get_full_model(model_id, get_entry_id("formalisms/SimpleClassDiagrams"))
  1425. if (element_eq(mm, read_root())):
  1426. return "ModelTransformation target type not in formalisms/SimpleClassDiagrams hierarchy: " + name!
  1427. set_add_node(to_ramify, create_tuple(key, mm))
  1428. else:
  1429. return "Name was already assigned a metamodel: " + key!
  1430. else:
  1431. return "Model not supported for RAMification: " + name!
  1432. else:
  1433. return "Permission denied: " + name!
  1434. else:
  1435. return "Model not found: " + name!
  1436. merged_formalism = model_fuse(to_ramify)
  1437. modify(merged_formalism, True)
  1438. ramified_metamodel = ramify(merged_formalism)
  1439. model_create(ramified_metamodel, "RAMified/" + operation_name, get_entry_id("formalisms/SimpleClassDiagrams"), "Model")
  1440. ramified_metamodel_id = get_entry_id("RAMified/" + operation_name)
  1441. // Now use the RAMified model to create the instance
  1442. if (get_entry_id(operation_name) == ""):
  1443. String new_model
  1444. // Finished with all information, now create the model itself!
  1445. output("Waiting for model constructors...")
  1446. new_model = compile_model(input(), get_full_model(ramified_metamodel_id, get_entry_id("formalisms/SimpleClassDiagrams")))
  1447. if (element_eq(new_model, read_root())):
  1448. return "Compilation error"!
  1449. model_create(new_model, operation_name, ramified_metamodel_id, "ModelTransformation")
  1450. model_id = get_entry_id(operation_name)
  1451. // Write out a merged metamodel containing all these models: this is the MM for the manual operation
  1452. // New location is available, so write
  1453. model_create(merged_formalism, "merged/" + operation_name, get_entry_id("formalisms/SimpleClassDiagrams"), "Model")
  1454. merged_formalism_id = get_entry_id("merged/" + operation_name)
  1455. // Add tracability links at this level
  1456. tracability_link = instantiate_link(core, "tracability", "", model_id, merged_formalism_id)
  1457. instantiate_attribute(core, tracability_link, "type", "operatesOn")
  1458. // Add tracability links at this level
  1459. tracability_link = instantiate_link(core, "tracability", "", merged_formalism_id, ramified_metamodel_id)
  1460. instantiate_attribute(core, tracability_link, "type", "RAMified")
  1461. // Extend metadata with info on source and target
  1462. String link
  1463. String dst
  1464. keys = dict_keys(source)
  1465. while (set_len(keys) > 0):
  1466. key = set_pop(keys)
  1467. dst = source[key]
  1468. link = instantiate_link(core, "transformInput", "", model_id, dst)
  1469. instantiate_attribute(core, link, "name", key)
  1470. keys = dict_keys(target)
  1471. while (set_len(keys) > 0):
  1472. key = set_pop(keys)
  1473. dst = target[key]
  1474. link = instantiate_link(core, "transformOutput", "", model_id, dst)
  1475. instantiate_attribute(core, link, "name", key)
  1476. return "Success"!
  1477. else:
  1478. return "Model exists: " + operation_name!
  1479. String function cmd_permission_modify(model_name : String, permissions : String):
  1480. Integer permission
  1481. String model_id
  1482. model_id = get_entry_id(model_name)
  1483. if (model_id != ""):
  1484. if (get_relation_to_model(current_user_id, model_id) == 0):
  1485. Boolean fail
  1486. Integer i
  1487. i = 0
  1488. if (string_len(permissions) != 3):
  1489. fail = True
  1490. while (i < 3):
  1491. permission = cast_s2i(string_get(permissions, i))
  1492. if (bool_or(permission < 0, permission > 2)):
  1493. fail = True
  1494. break!
  1495. i = i + 1
  1496. if (bool_not(fail)):
  1497. unset_attribute(core, model_id, "permissions")
  1498. instantiate_attribute(core, model_id, "permissions", permissions)
  1499. return "Success"!
  1500. else:
  1501. return "Permission has incorrect format: must be a string of three characters, with each character being a digit between 0 and 2"!
  1502. else:
  1503. return "Permission denied to model: " + model_name!
  1504. else:
  1505. return "Model not found: " + model_name!
  1506. String function cmd_permission_owner(model_name : String, new_user_name : String):
  1507. String model_id
  1508. String new_user_id
  1509. model_id = get_entry_id(model_name)
  1510. if (model_id != ""):
  1511. if (bool_or(get_relation_to_model(current_user_id, model_id) == 0, is_admin(current_user_id))):
  1512. new_user_id = get_user_id(new_user_name)
  1513. if (new_user_id != ""):
  1514. model_delete_element(core, set_pop(allOutgoingAssociationInstances(core, model_id, "owner")))
  1515. instantiate_link(core, "owner", "", model_id, new_user_id)
  1516. return "Success"!
  1517. else:
  1518. return "No such user: " + new_user_name!
  1519. else:
  1520. return "Permission denied to model: " + model_name!
  1521. else:
  1522. return "Model not found: " + model_name!
  1523. String function cmd_permission_group(model_name : String, new_group_name : String):
  1524. String model_id
  1525. String group_id
  1526. model_id = get_entry_id(model_name)
  1527. if (model_id != ""):
  1528. if (bool_or(get_relation_to_model(current_user_id, model_id) == 0, is_admin(current_user_id))):
  1529. group_id = get_group_id(new_group_name)
  1530. if (group_id != ""):
  1531. model_delete_element(core, set_pop(allOutgoingAssociationInstances(core, model_id, "group")))
  1532. instantiate_link(core, "group", "", model_id, group_id)
  1533. return "Success"!
  1534. else:
  1535. return "No such group: " + new_group_name!
  1536. else:
  1537. return "Permission denied to model: " + model_name!
  1538. else:
  1539. return "Model not found: " + model_name!
  1540. String function cmd_group_create(group_name : String):
  1541. // Create a new group and become its owner
  1542. String group_id
  1543. String name
  1544. group_id = get_group_id(group_name)
  1545. if (group_id == ""):
  1546. group_id = instantiate_node(core, "Group", "")
  1547. instantiate_attribute(core, group_id, "name", group_name)
  1548. instantiate_link(core, "belongsTo", "", current_user_id, group_id)
  1549. instantiate_link(core, "owner", "", group_id, current_user_id)
  1550. return "Success"!
  1551. else:
  1552. return "Group exists: " + group_name!
  1553. String function cmd_group_delete(group_name : String):
  1554. // Delete an existing group
  1555. String group_id
  1556. group_id = get_group_id(group_name)
  1557. if (group_id != ""):
  1558. if (allow_group_modify(current_user_id, group_id)):
  1559. model_delete_element(core, group_id)
  1560. return "Success"!
  1561. else:
  1562. return "Permission denied to group: " + group_name!
  1563. else:
  1564. return "Group not found: " + group_name!
  1565. String function cmd_group_owner_add(group_name : String, other_user_name : String):
  1566. // Add an owner to your group
  1567. String group_id
  1568. String other_user_id
  1569. group_id = get_group_id(group_name)
  1570. if (group_id != ""):
  1571. if (allow_group_modify(current_user_id, group_id)):
  1572. other_user_id = get_user_id(other_user_name)
  1573. if (other_user_id != ""):
  1574. if (set_in(allAssociationDestinations(core, group_id, "owner"), other_user_id)):
  1575. instantiate_link(core, "owner", "", group_id, other_user_id)
  1576. if (set_in(allAssociationDestinations(core, other_user_id, "belongsTo"), group_id)):
  1577. instantiate_link(core, "belongsTo", "", other_user_id, group_id)
  1578. return "Success"!
  1579. else:
  1580. return "User is already the owner!"!
  1581. else:
  1582. return "User not found: " + other_user_name!
  1583. else:
  1584. return "Permission denied to group: " + group_name!
  1585. else:
  1586. return "Group not found: " + group_name!
  1587. String function cmd_group_owner_delete(group_name : String, other_user_name : String):
  1588. // Remove an owner from your group
  1589. String group_id
  1590. String other_user_id
  1591. group_id = get_group_id(group_name)
  1592. if (group_id != ""):
  1593. if (allow_group_modify(current_user_id, group_id)):
  1594. other_user_id = get_user_id(other_user_name)
  1595. if (other_user_id != ""):
  1596. if (set_in(allAssociationDestinations(core, other_user_id, "belongsTo"), group_id)):
  1597. if (set_in(allAssociationDestinations(core, group_id, "owner"), other_user_id)):
  1598. Element overlap
  1599. overlap = set_overlap(allIncomingAssociationInstances(core, group_id, "owner"), allOutgoingAssociationInstances(core, other_user_id, "owner"))
  1600. model_delete_element(core, set_pop(overlap))
  1601. return "Success"!
  1602. else:
  1603. return "User is not an owner of the group!"!
  1604. else:
  1605. return "User is not a member of the group!"!
  1606. else:
  1607. return "User not found: " + other_user_name!
  1608. else:
  1609. return "Permission denied to group: " + group_name!
  1610. else:
  1611. return "Group not found: " + group_name!
  1612. String function cmd_group_join(group_name : String, other_user_name : String):
  1613. // Add someone to your group
  1614. String group_id
  1615. String other_user_id
  1616. group_id = get_group_id(group_name)
  1617. if (group_id != ""):
  1618. if (allow_group_modify(current_user_id, group_id)):
  1619. other_user_id = get_user_id(other_user_name)
  1620. if (other_user_id != ""):
  1621. if (set_in(allOutgoingAssociationInstances(core, other_user_id, "belongsTo"), group_id)):
  1622. instantiate_link(core, "belongsTo", "", other_user_id, group_id)
  1623. return "Success"!
  1624. else:
  1625. return "User is already a member of the group!"!
  1626. else:
  1627. return "User not found: " + other_user_name!
  1628. else:
  1629. return "Permission denied to group: " + group_name!
  1630. else:
  1631. return "Group not found: " + group_name!
  1632. String function cmd_group_kick(group_name : String, other_user_name : String):
  1633. // Remove someone from your group
  1634. String group_id
  1635. String other_user_id
  1636. group_id = get_group_id(group_name)
  1637. if (group_id != ""):
  1638. if (allow_group_modify(current_user_id, group_id)):
  1639. other_user_id = get_user_id(other_user_name)
  1640. if (other_user_id != ""):
  1641. Element overlap
  1642. if (set_in(allAssociationDestinations(core, other_user_id, "belongsTo"), group_id)):
  1643. overlap = set_overlap(allIncomingAssociationInstances(core, group_id, "belongsTo"), allOutgoingAssociationInstances(core, other_user_id, "belongsTo"))
  1644. model_delete_element(core,set_pop(overlap))
  1645. // Check if user was an owner as well
  1646. if (set_in(allAssociationDestinations(core, group_id, "owner"), other_user_id)):
  1647. overlap = set_overlap(allIncomingAssociationInstances(core, other_user_id, "owner"), allOutgoingAssociationInstances(core, group_id, "owner"))
  1648. model_delete_element(core, set_pop(overlap))
  1649. return "Success"!
  1650. else:
  1651. return "User is not a member of the group!"!
  1652. else:
  1653. return "User not found: " + other_user_name!
  1654. else:
  1655. return "Permission denied to group: " + group_name!
  1656. else:
  1657. return "Group not found: " + group_name!
  1658. String function cmd_group_list():
  1659. // List all groups you are a member of (and whether you are admin or not!)
  1660. Element groups
  1661. String group_id
  1662. String admin
  1663. String result
  1664. result = ""
  1665. groups = allAssociationDestinations(core, current_user_id, "belongsTo")
  1666. while (set_len(groups) > 0):
  1667. group_id = set_pop(groups)
  1668. if (set_in(allAssociationDestinations(core, group_id, "owner"), current_user_id)):
  1669. admin = " A "
  1670. else:
  1671. admin = " "
  1672. result = result + (string_join(admin, read_attribute(core, group_id, "name")) + "\n")
  1673. return result!
  1674. String function cmd_admin_promote(other_user_name : String):
  1675. // Promote a user to admin status
  1676. if (is_admin(current_user_id)):
  1677. String other_user_id
  1678. other_user_id = get_user_id(other_user_name)
  1679. if (other_user_id != ""):
  1680. unset_attribute(core, other_user_id, "admin")
  1681. instantiate_attribute(core, other_user_id, "admin", True)
  1682. return "Success"!
  1683. else:
  1684. return "User not found: " + other_user_name!
  1685. else:
  1686. return "Permission denied to administrate: " + other_user_name!
  1687. String function cmd_admin_demote(other_user_name : String):
  1688. // Demote a user to normal status
  1689. if (is_admin(current_user_id)):
  1690. String other_user_id
  1691. other_user_id = get_user_id(other_user_name)
  1692. if (other_user_id != ""):
  1693. unset_attribute(core, other_user_id, "admin")
  1694. instantiate_attribute(core, other_user_id, "admin", False)
  1695. return "Success"!
  1696. else:
  1697. return "User not found: " + other_user_name!
  1698. else:
  1699. return "Permission denied to administrate: " + other_user_name!
  1700. String function cmd_service_register(service_name : String):
  1701. // Active a service for this specific user
  1702. if (get_service_id(service_name) == ""):
  1703. String service
  1704. service = instantiate_node(core, "Service", "")
  1705. instantiate_attribute(core, service, "name", service_name)
  1706. instantiate_attribute(core, service, "port", get_taskname())
  1707. output("Success: " + get_taskname())
  1708. // Wait until we can stop the service
  1709. while (cast_v2s(input()) != "\"service_stop\""):
  1710. output("Service is running on this task: stop it by sending 'service_stop'")
  1711. model_delete_element(core, service)
  1712. // Service terminated
  1713. return "Success"!
  1714. else:
  1715. return "Service already exists: " + service_name!
  1716. String function cmd_user_password(user_name : String, new_password : String):
  1717. if (bool_or(current_user_id == get_user_id(user_name), is_admin(current_user_id))):
  1718. if (get_user_id(user_name) != ""):
  1719. instantiate_attribute(core, get_user_id(user_name), "password", hash(new_password))
  1720. return "Success"!
  1721. else:
  1722. return "No such user: " + user_name!
  1723. else:
  1724. return "Permission denied to user: " + user_name!
  1725. String function cmd_transformation_signature(transformation_name : String):
  1726. String model_id
  1727. model_id = get_entry_id(transformation_name)
  1728. if (model_id != ""):
  1729. if (is_nominal_instance(core, model_id, "Transformation")):
  1730. if (allow_read(current_user_id, transformation_name)):
  1731. String result
  1732. String elem
  1733. Element inputs
  1734. Element outputs
  1735. result = "Success: "
  1736. inputs = allOutgoingAssociationInstances(core, model_id, "transformInput")
  1737. while (set_len(inputs) > 0):
  1738. elem = set_pop(inputs)
  1739. result = string_join(string_join(string_join(string_join("I ", read_attribute(core, elem, "name")), " "), full_name(readAssociationDestination(core, elem))), "\n")
  1740. outputs = allOutgoingAssociationInstances(core, model_id, "transformOutput")
  1741. while (set_len(outputs) > 0):
  1742. elem = set_pop(outputs)
  1743. result = string_join(string_join(string_join(string_join("O ", read_attribute(core, elem, "name")), " "), full_name(readAssociationDestination(core, elem))), "\n")
  1744. return result!
  1745. else:
  1746. return "Permission denied to operation: " + transformation_name!
  1747. else:
  1748. return "Model is not an operation: " + transformation_name!
  1749. else:
  1750. return "No such operation: " + transformation_name!
  1751. String function cmd_model_types(model_name : String):
  1752. if (get_entry_id(model_name) != ""):
  1753. String model_id
  1754. model_id = get_entry_id(model_name)
  1755. Element types
  1756. String type
  1757. String result
  1758. result = "Success: "
  1759. types = allAssociationDestinations(core, model_id, "instanceOf")
  1760. while (set_len(types) > 0):
  1761. type = set_pop(types)
  1762. result = string_join(result, full_name(type) + "\n")
  1763. return result!
  1764. else:
  1765. return "No such model: " + model_name!
  1766. String function cmd_folder_create(folder_name : String):
  1767. if (get_entry_id(folder_name) == ""):
  1768. if (element_neq(create_folders(current_user_id, folder_name), read_root())):
  1769. return "Success"!
  1770. else:
  1771. return "Permission denied to folder: " + folder_name!
  1772. else:
  1773. return "Folder alreay exists: " + folder_name!
  1774. String function cmd_conformance_add(model_name : String, metamodel_name : String):
  1775. // Create a new instanceOf relation now
  1776. String instance_of
  1777. instance_of = instantiate_link(core, "instanceOf", "", get_entry_id(model_name), get_entry_id(metamodel_name))
  1778. return "Success"!
  1779. Void function user_function_skip_init(user_id : String):
  1780. String cmd
  1781. String result
  1782. current_user_id = user_id
  1783. caches = dict_create()
  1784. dict_add_fast(caches, "models", dict_create())
  1785. dict_add_fast(caches, "users", dict_create())
  1786. dict_add_fast(caches, "full_name", dict_create())
  1787. // Find root element
  1788. Element elems
  1789. String elem
  1790. elems = allInstances(core, "Folder")
  1791. while (set_len(elems) > 0):
  1792. elem = set_pop(elems)
  1793. if (value_eq(read_attribute(core, elem, "name"), "")):
  1794. if (set_len(allIncomingAssociationInstances(core, elem, "contains")) == 0):
  1795. dict_add_fast(caches, "root", elem)
  1796. break!
  1797. output("Welcome to the Model Management Interface v2.0!")
  1798. output("Use the 'help' command for a list of possible commands")
  1799. while (True):
  1800. cmd = input()
  1801. if (cmd == "help"):
  1802. output(cmd_help())
  1803. elif (cmd == "model_add"):
  1804. output(cmd_model_add(single_input("Model type?"), single_input("Model name?"), single_input("Model textual representation?")))
  1805. elif (cmd == "process_execute"):
  1806. output(cmd_process_execute(single_input("Process to execute?"), single_input("Model prefix to use?")))
  1807. elif (cmd == "transformation_between"):
  1808. output(cmd_transformation_between(single_input("Source type?"), single_input("Target type?")))
  1809. elif (cmd == "model_render"):
  1810. output(cmd_model_render(single_input("Model name?"), single_input("Mapper name?"), single_input("Rendered name?")))
  1811. elif (cmd == "model_rendered"):
  1812. output(cmd_model_rendered(single_input("Model name?"), single_input("Mapper name?")))
  1813. elif (cmd == "transformation_execute"):
  1814. output(cmd_transformation_execute(single_input("Transformation name?"), dict_input("Source models?"), dict_input("Target models?")))
  1815. elif (cmd == "verify"):
  1816. result = cmd_verify(single_input("Model name?"), single_input("Metamodel name?"))
  1817. if (result != ""):
  1818. output(result)
  1819. elif (cmd == "model_overwrite"):
  1820. output(cmd_model_overwrite(single_input("Model name?"), single_input("Metamodel name?")))
  1821. elif (cmd == "model_modify"):
  1822. output(cmd_model_modify(single_input("Model name?"), single_input("Metamodel name?")))
  1823. elif (cmd == "model_delete"):
  1824. output(cmd_model_delete(single_input("Model name?")))
  1825. elif (cmd == "model_list"):
  1826. output(cmd_model_list(single_input("Location?")))
  1827. elif (cmd == "model_list_full"):
  1828. output(cmd_model_list_full(single_input("Location?")))
  1829. elif (cmd == "transformation_add_MANUAL"):
  1830. output(cmd_transformation_add_MANUAL(dict_input("Source model names?"), dict_input("Target model names?"), single_input("Operation name?")))
  1831. elif (cmd == "transformation_add_AL"):
  1832. output(cmd_transformation_add_AL(dict_input("Source model names?"), dict_input("Target model names?"), single_input("Operation name?")))
  1833. elif (cmd == "transformation_add_MT"):
  1834. output(cmd_transformation_add_MT(dict_input("Source model names?"), dict_input("Target models?"), single_input("Operation name?")))
  1835. elif (cmd == "permission_modify"):
  1836. output(cmd_permission_modify(single_input("Model name?"), single_input("Permissions?")))
  1837. elif (cmd == "permission_owner"):
  1838. output(cmd_permission_owner(single_input("Model name?"), single_input("New owning user?")))
  1839. elif (cmd == "permission_group"):
  1840. output(cmd_permission_group(single_input("Model name?"), single_input("New owning group?")))
  1841. elif (cmd == "group_create"):
  1842. output(cmd_group_create(single_input("New group name?")))
  1843. elif (cmd == "group_delete"):
  1844. output(cmd_group_delete(single_input("Group name?")))
  1845. elif (cmd == "group_owner_add"):
  1846. output(cmd_group_owner_add(single_input("Group name?"), single_input("User name?")))
  1847. elif (cmd == "group_owner_delete"):
  1848. output(cmd_group_owner_delete(single_input("Group name?"), single_input("User name?")))
  1849. elif (cmd == "group_join"):
  1850. output(cmd_group_join(single_input("Group name?"), single_input("User name?")))
  1851. elif (cmd == "group_kick"):
  1852. output(cmd_group_kick(single_input("Group name?"), single_input("User name?")))
  1853. elif (cmd == "group_list"):
  1854. output(cmd_group_list())
  1855. elif (cmd == "admin_promote"):
  1856. output(cmd_admin_promote(single_input("User name?")))
  1857. elif (cmd == "admin_demote"):
  1858. output(cmd_admin_demote(single_input("User name?")))
  1859. elif (cmd == "service_register"):
  1860. output(cmd_service_register(single_input("Service name?")))
  1861. elif (cmd == "user_password"):
  1862. output(cmd_user_password(single_input("User name?"), single_input("New password?")))
  1863. elif (cmd == "transformation_read_signature"):
  1864. output(cmd_transformation_signature(single_input("Transformation name?")))
  1865. elif (cmd == "verbose"):
  1866. set_verbose(True)
  1867. elif (cmd == "quiet"):
  1868. set_verbose(False)
  1869. elif (cmd == "self-destruct"):
  1870. // Delete user from Core Formalism
  1871. model_delete_element(core, user_id)
  1872. return !
  1873. elif (cmd == "user_logout"):
  1874. // TODO
  1875. cmd = "FAIL"
  1876. elif (cmd == "exit"):
  1877. // Exit by actually removing the user and decoupling it from all of its models
  1878. // Restarting with the same user name will NOT grant you access to anything of the previous user with that same name
  1879. // as the current user will have been deleted
  1880. return !
  1881. elif (cmd == "folder_create"):
  1882. output(cmd_folder_create(single_input("Folder name?")))
  1883. elif (cmd == "add_conformance"):
  1884. // TODO
  1885. output(cmd_conformance_add(single_input("Model name?"), single_input("Metamodel name?")))
  1886. elif (cmd == "remove_conformance"):
  1887. // TODO
  1888. cmd = "FAIL"
  1889. elif (cmd == "user_name"):
  1890. // TODO
  1891. cmd = "FAIL"
  1892. elif (cmd == "service_poll"):
  1893. // TODO
  1894. cmd = "FAIL"
  1895. elif (cmd == "model_types"):
  1896. output(cmd_model_types(single_input("Model name?")))
  1897. else:
  1898. output("Unknown command: " + cmd)
  1899. // We never get here!
  1900. return !