core_algorithm.alc 80 KB

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