core_algorithm.alc 81 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288
  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, tracability_name : String):
  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. // Do tracability
  1110. Element tracability_model
  1111. if (tracability_name != ""):
  1112. // Check if exists
  1113. if (get_entry_id(tracability_name) == ""):
  1114. // No, so create
  1115. tracability_model = instantiate_model(get_full_model(get_entry_id("formalisms/Tracability"), get_entry_id("formalisms/SimpleClassDiagrams")))
  1116. model_create(tracability_model, tracability_name, get_entry_id("formalisms/Tracability"), "Model")
  1117. else:
  1118. // Yes, so read out
  1119. tracability_model = get_full_model(get_entry_id(tracability_name), get_entry_id("formalisms/Tracability"))
  1120. else:
  1121. tracability_model = read_root()
  1122. result = execute_operation(transformation_id, inputs, tracability_model)
  1123. // Flush tracability again, just to be sure
  1124. if (tracability_name != ""):
  1125. model_overwrite(tracability_model, get_entry_id(tracability_name), get_entry_id("formalisms/Tracability"))
  1126. // Now write out the models again
  1127. if (element_eq(result, read_root())):
  1128. // Something went wrong!
  1129. return "Failure"!
  1130. else:
  1131. keys = dict_keys(result)
  1132. while (set_len(keys) > 0):
  1133. key = set_pop(keys)
  1134. if (get_entry_id(outputs[key]) == ""):
  1135. // New model
  1136. model_create(result[key], outputs[key], get_entry_id(output_map[key]), "Model")
  1137. else:
  1138. model_overwrite(result[key], get_entry_id(outputs[key]), get_entry_id(output_map[key]))
  1139. return "Success"!
  1140. else:
  1141. return "Model is not executable: " + transformation_name!
  1142. else:
  1143. return "Permission denied to model: " + transformation_name!
  1144. else:
  1145. return "Model not found: " + transformation_name!
  1146. String function cmd_verify(model_name : String, metamodel_name : String):
  1147. // Check whether a model conforms to its specification (with the selected type mapping)
  1148. String model_id
  1149. String result
  1150. Element inputs
  1151. model_id = get_entry_id(model_name)
  1152. if (model_id != ""):
  1153. if (allow_read(current_user_id, model_id)):
  1154. if (get_entry_id(metamodel_name) != ""):
  1155. if (allow_read(current_user_id, get_entry_id(metamodel_name))):
  1156. Element m
  1157. m = get_full_model(get_entry_id(model_name), get_entry_id(metamodel_name))
  1158. if (element_eq(m, read_root())):
  1159. return "No conformance relation can be found between these models"!
  1160. return string_join("Success: ", conformance_scd(m))!
  1161. else:
  1162. return "Permission denied to model: " + metamodel_name!
  1163. else:
  1164. return "Model not found: " + metamodel_name!
  1165. else:
  1166. return "Permission denied to model: " + model_name!
  1167. else:
  1168. return "Model not found: " + model_name!
  1169. String function cmd_model_overwrite(model_name : String, metamodel_name : String):
  1170. // Overwrites an existing model without changing any metadata
  1171. String model_id
  1172. String type_id
  1173. Element new_model
  1174. model_id = get_entry_id(model_name)
  1175. if (model_id != ""):
  1176. if (allow_write(current_user_id, model_id)):
  1177. type_id = set_pop(allAssociationDestinations(core, model_id, "instanceOf"))
  1178. if (allow_read(current_user_id, type_id)):
  1179. Element mm
  1180. mm = get_full_model(get_entry_id(metamodel_name), get_entry_id("formalisms/SimpleClassDiagrams"))
  1181. if (element_eq(mm, read_root())):
  1182. return "Metamodel does not conform to formalisms/SimpleClassDiagrams: " + metamodel_name!
  1183. output("Waiting for model constructors...")
  1184. new_model = compile_model(input(), mm)
  1185. if (element_eq(new_model, read_root())):
  1186. return "Compilation error"!
  1187. model_overwrite(new_model, model_id, get_entry_id(metamodel_name))
  1188. return "Success"!
  1189. else:
  1190. return string_join("Permission denied to model: ", full_name(type_id))!
  1191. else:
  1192. return "Permission denied to model: " + model_name!
  1193. else:
  1194. return "Model not found: " + model_name!
  1195. String function cmd_model_modify(model_name : String, metamodel_name : String):
  1196. // Model modify operation, which uses the mini_modify.alc operations, though with extensions for access control
  1197. String model_id
  1198. String type_id
  1199. model_id = get_entry_id(model_name)
  1200. if (model_id != ""):
  1201. if (allow_read(current_user_id, model_id)):
  1202. type_id = set_pop(allAssociationDestinations(core, model_id, "instanceOf"))
  1203. if (allow_read(current_user_id, type_id)):
  1204. Element new_model
  1205. new_model = get_full_model(model_id, get_entry_id(metamodel_name))
  1206. if (element_eq(new_model, read_root())):
  1207. return "No conformance relation can be found between these models"!
  1208. modify(new_model, allow_write(current_user_id, model_id))
  1209. if (allow_write(current_user_id, model_id)):
  1210. // Overwrite the modified model
  1211. model_overwrite(new_model, model_id, get_entry_id(metamodel_name))
  1212. return "Success"!
  1213. else:
  1214. return string_join("Permission denied to model: ", full_name(type_id))!
  1215. else:
  1216. return "Permission denied to model: " + model_name!
  1217. else:
  1218. return "Model not found: " + model_name!
  1219. Void function model_delete(model_id : String):
  1220. if (read_type(core, model_id) == "Folder"):
  1221. // Folder, so delete all containing elements as well!
  1222. Element elems
  1223. elems = allAssociationDestinations(core, model_id, "contains")
  1224. while (set_len(elems) > 0):
  1225. model_delete(set_pop(elems))
  1226. // Delete self
  1227. model_delete_element(core, model_id)
  1228. else:
  1229. // Only delete this element
  1230. model_delete_element(core, model_id)
  1231. return!
  1232. String function cmd_model_delete(model_name : String):
  1233. String model_id
  1234. model_id = get_entry_id(model_name)
  1235. if (model_id != ""):
  1236. if (allow_write(current_user_id, model_id)):
  1237. model_delete(model_id)
  1238. return "Success"!
  1239. else:
  1240. return "Permission denied to model: " + model_name!
  1241. else:
  1242. return "Model not found: " + model_name!
  1243. String function cmd_model_list(location : String):
  1244. // List all models
  1245. if (get_entry_id(location) != ""):
  1246. if (allow_read(current_user_id, get_entry_id(location))):
  1247. Element models
  1248. String result
  1249. String m
  1250. result = "Success: "
  1251. models = allAssociationDestinations(core, get_entry_id(location), "contains")
  1252. while (set_len(models) > 0):
  1253. m = set_pop(models)
  1254. if (is_nominal_instance(core, m, "Folder")):
  1255. result = result + (get_filename(read_attribute(core, m, "name")) + "/\n")
  1256. else:
  1257. result = result + (get_filename(read_attribute(core, m, "name")) + "\n")
  1258. return result!
  1259. else:
  1260. return "Permission denied to folder: " + location!
  1261. else:
  1262. return "Location not found: " + location!
  1263. String function cmd_model_list_full(location : String):
  1264. // List all models with full info
  1265. if (get_entry_id(location) != ""):
  1266. if (allow_read(current_user_id, get_entry_id(location))):
  1267. Element models
  1268. String m
  1269. String permissions
  1270. String owner
  1271. String group
  1272. String name
  1273. String type
  1274. String result
  1275. result = "Success: "
  1276. models = allAssociationDestinations(core, get_entry_id(location), "contains")
  1277. while (set_len(models) > 0):
  1278. m = set_pop(models)
  1279. permissions = read_attribute(core, m, "permissions")
  1280. owner = read_attribute(core, set_pop(allAssociationDestinations(core, m, "owner")), "name")
  1281. group = read_attribute(core, set_pop(allAssociationDestinations(core, m, "group")), "name")
  1282. if (is_nominal_instance(core, m, "Folder")):
  1283. name = get_filename(read_attribute(core, m, "name")) + "/"
  1284. else:
  1285. name = get_filename(read_attribute(core, m, "name"))
  1286. result = result + permissions + " " + owner + " " + group + " " + name + "\n"
  1287. return result!
  1288. else:
  1289. return "Permission denied to folder: " + location!
  1290. else:
  1291. return "Location not found: " + location!
  1292. String function cmd_transformation_add_MANUAL(source_models : Element, target_models : Element, operation_name : String):
  1293. return transformation_add(source_models, target_models, operation_name, "manual")!
  1294. String function cmd_transformation_add_AL(source_models : Element, target_models : Element, operation_name : String):
  1295. return transformation_add(source_models, target_models, operation_name, "actionlanguage")!
  1296. String function transformation_add(source_models : Element, target_models : Element, operation_name : String, operation_type : String):
  1297. // Add a manual transformation model
  1298. String model_id
  1299. Element models
  1300. Element source
  1301. Element target
  1302. String name
  1303. Element all_formalisms
  1304. Element formalism_map
  1305. Element merged_formalism
  1306. String merged_formalism_id
  1307. String source_formalism_id
  1308. String tracability_link
  1309. Element fused
  1310. Element keys
  1311. String key
  1312. Element mm
  1313. source = dict_create()
  1314. target = dict_create()
  1315. all_formalisms = set_create()
  1316. formalism_map = set_create()
  1317. fused = set_create()
  1318. keys = dict_keys(source_models)
  1319. while (set_len(keys) > 0):
  1320. key = set_pop(keys)
  1321. name = source_models[key]
  1322. model_id = get_entry_id(name)
  1323. if (model_id != ""):
  1324. if (allow_read(current_user_id, model_id)):
  1325. if (bool_not(dict_in(source, key))):
  1326. dict_add(source, key, model_id)
  1327. mm = get_full_model(model_id, get_entry_id("formalisms/SimpleClassDiagrams"))
  1328. if (element_eq(mm, read_root())):
  1329. return "Transformation source type not in formalisms/SimpleClassDiagrams hierarchy: " + key!
  1330. if (bool_not(set_in(fused, key))):
  1331. set_add_node(formalism_map, create_tuple(key, mm))
  1332. set_add(fused, key)
  1333. if (bool_not(set_in(all_formalisms, model_id))):
  1334. set_add(all_formalisms, model_id)
  1335. else:
  1336. return "Name already selected for source: " + key!
  1337. else:
  1338. return "Permission denied to model: " + name!
  1339. else:
  1340. return "Model not found: " + name!
  1341. keys = dict_keys(target_models)
  1342. while (set_len(keys) > 0):
  1343. key = set_pop(keys)
  1344. name = target_models[key]
  1345. model_id = get_entry_id(name)
  1346. if (model_id != ""):
  1347. if (allow_read(current_user_id, model_id)):
  1348. if (bool_not(dict_in(target, key))):
  1349. dict_add(target, key, model_id)
  1350. mm = get_full_model(model_id, get_entry_id("formalisms/SimpleClassDiagrams"))
  1351. if (element_eq(mm, read_root())):
  1352. return "Transformation target type not in formalisms/SimpleClassDiagrams hierarchy: " + key!
  1353. if (bool_not(set_in(fused, key))):
  1354. set_add_node(formalism_map, create_tuple(key, mm))
  1355. set_add(fused, key)
  1356. if (bool_not(set_in(all_formalisms, model_id))):
  1357. set_add(all_formalisms, model_id)
  1358. else:
  1359. return "Name already selected for target: " + key!
  1360. else:
  1361. return "Permission denied to model: " + name!
  1362. else:
  1363. return "Model not found: " + name!
  1364. if (get_entry_id(operation_name) == ""):
  1365. // Write out a merged metamodel containing all these models: this is the MM for the manual operation
  1366. // New location is available, so write
  1367. if (bool_not(bool_and(dict_len(source_models) == 0, dict_len(target_models) == 0))):
  1368. merged_formalism = model_fuse(formalism_map)
  1369. model_create(merged_formalism, "merged/" + operation_name, get_entry_id("formalisms/SimpleClassDiagrams"), "Model")
  1370. modify(merged_formalism, True)
  1371. model_overwrite(merged_formalism, get_entry_id("merged/" + operation_name), get_entry_id("formalisms/SimpleClassDiagrams"))
  1372. if (operation_type == "manual"):
  1373. // Finished with all information, now create the model itself!
  1374. Element m
  1375. m = get_full_model(get_entry_id("formalisms/ManualOperation"), get_entry_id("formalisms/SimpleClassDiagrams"))
  1376. if (element_eq(m, read_root())):
  1377. log("Error when opening ManualOperation: no conformance relation found")
  1378. return "Error when opening ManualOperation: no conformance relation found"!
  1379. model_create(instantiate_model(m), operation_name, get_entry_id("formalisms/ManualOperation"), "ManualOperation")
  1380. model_id = get_entry_id(operation_name)
  1381. elif (operation_type == "actionlanguage"):
  1382. // Finished with all information, now create the model itself!
  1383. output("Waiting for code constructors...")
  1384. Element compiled
  1385. compiled = compile_code(input())
  1386. if (element_eq(compiled, read_root())):
  1387. return "Compilation error"!
  1388. add_code_model(get_full_model(get_entry_id("formalisms/ActionLanguage"), get_entry_id("formalisms/SimpleClassDiagrams")), "AL/" + operation_name, compiled)
  1389. model_create(import_node("AL/" + operation_name), operation_name, get_entry_id("formalisms/ActionLanguage"), "ActionLanguage")
  1390. model_id = get_entry_id(operation_name)
  1391. if (bool_not(bool_and(dict_len(source_models) == 0, dict_len(target_models) == 0))):
  1392. merged_formalism_id = get_entry_id("merged/" + operation_name)
  1393. // Add tracability links at this level
  1394. while (set_len(all_formalisms) > 0):
  1395. source_formalism_id = set_pop(all_formalisms)
  1396. tracability_link = instantiate_link(core, "tracability", "", merged_formalism_id, source_formalism_id)
  1397. instantiate_attribute(core, tracability_link, "type", "merged")
  1398. tracability_link = instantiate_link(core, "tracability", "", model_id, merged_formalism_id)
  1399. instantiate_attribute(core, tracability_link, "type", "operatesOn")
  1400. // Extend metadata with info on source and target
  1401. String link
  1402. String dst
  1403. keys = dict_keys(source)
  1404. while (set_len(keys) > 0):
  1405. key = set_pop(keys)
  1406. log("Add transformInput link for " + model_id + " to " + cast_v2s(source[key]) + " while adding operation " + operation_name)
  1407. link = instantiate_link(core, "transformInput", "", model_id, source[key])
  1408. instantiate_attribute(core, link, "name", key)
  1409. keys = dict_keys(target)
  1410. while (set_len(keys) > 0):
  1411. key = set_pop(keys)
  1412. link = instantiate_link(core, "transformOutput", "", model_id, target[key])
  1413. instantiate_attribute(core, link, "name", key)
  1414. return "Success"!
  1415. else:
  1416. return "Model exists: " + operation_name!
  1417. String function cmd_transformation_add_MT(source_models : Element, target_models : Element, operation_name : String):
  1418. // Add a model transformation model
  1419. // Just a usual model instantiation, but need to add the source and target links based on user info
  1420. String ramified_metamodel_id
  1421. Element ramified_metamodel
  1422. String model_id
  1423. Element models
  1424. Element links
  1425. String link_id
  1426. Element supported
  1427. Element source
  1428. Element target
  1429. String name
  1430. String new_model_id
  1431. String merged_link_id
  1432. Element links_merged
  1433. String merged_formalism
  1434. Element keys
  1435. String key
  1436. Element mm
  1437. Element to_ramify
  1438. String source_formalism_id
  1439. String merged_formalism_id
  1440. String tracability_link
  1441. source = dict_create()
  1442. target = dict_create()
  1443. to_ramify = set_create()
  1444. keys = dict_keys(source_models)
  1445. while (set_len(keys) > 0):
  1446. key = set_pop(keys)
  1447. name = source_models[key]
  1448. model_id = get_entry_id(name)
  1449. if (model_id != ""):
  1450. if (allow_read(current_user_id, name)):
  1451. // Check whether or not it is formalisms/SimpleClassDiagrams
  1452. if (is_typed_by(model_id, get_entry_id("formalisms/SimpleClassDiagrams"))):
  1453. if (bool_not(dict_in(source, key))):
  1454. dict_add(source, key, model_id)
  1455. mm = get_full_model(model_id, get_entry_id("formalisms/SimpleClassDiagrams"))
  1456. if (element_eq(mm, read_root())):
  1457. return "ModelTransformation source type not in formalisms/SimpleClassDiagrams hierarchy: " + name!
  1458. set_add_node(to_ramify, create_tuple(key, mm))
  1459. else:
  1460. return "Name was already assigned a metamodel: " + key!
  1461. else:
  1462. return "Model not supported for RAMification: " + name!
  1463. else:
  1464. return "Permission denied: " + name!
  1465. else:
  1466. return "Model not found: " + name!
  1467. keys = dict_keys(target_models)
  1468. while (set_len(keys) > 0):
  1469. key = set_pop(keys)
  1470. name = target_models[key]
  1471. model_id = get_entry_id(name)
  1472. if (model_id != ""):
  1473. if (allow_read(current_user_id, name)):
  1474. if (is_typed_by(model_id, get_entry_id("formalisms/SimpleClassDiagrams"))):
  1475. if (bool_not(dict_in(target, key))):
  1476. if (dict_in(source, key)):
  1477. if (value_eq(model_id, source[key])):
  1478. dict_add(target, key, model_id)
  1479. // Don't add to to_ramify, as it is already in there!
  1480. else:
  1481. return "Name in output cannot have different type than input: " + key!
  1482. else:
  1483. dict_add(target, key, model_id)
  1484. mm = get_full_model(model_id, get_entry_id("formalisms/SimpleClassDiagrams"))
  1485. if (element_eq(mm, read_root())):
  1486. return "ModelTransformation target type not in formalisms/SimpleClassDiagrams hierarchy: " + name!
  1487. set_add_node(to_ramify, create_tuple(key, mm))
  1488. else:
  1489. return "Name was already assigned a metamodel: " + key!
  1490. else:
  1491. return "Model not supported for RAMification: " + name!
  1492. else:
  1493. return "Permission denied: " + name!
  1494. else:
  1495. return "Model not found: " + name!
  1496. merged_formalism = model_fuse(to_ramify)
  1497. modify(merged_formalism, True)
  1498. ramified_metamodel = ramify(merged_formalism)
  1499. model_create(ramified_metamodel, "RAMified/" + operation_name, get_entry_id("formalisms/SimpleClassDiagrams"), "Model")
  1500. ramified_metamodel_id = get_entry_id("RAMified/" + operation_name)
  1501. // Now use the RAMified model to create the instance
  1502. if (get_entry_id(operation_name) == ""):
  1503. String new_model
  1504. // Finished with all information, now create the model itself!
  1505. output("Waiting for model constructors...")
  1506. new_model = compile_model(input(), get_full_model(ramified_metamodel_id, get_entry_id("formalisms/SimpleClassDiagrams")))
  1507. if (element_eq(new_model, read_root())):
  1508. return "Compilation error"!
  1509. model_create(new_model, operation_name, ramified_metamodel_id, "ModelTransformation")
  1510. model_id = get_entry_id(operation_name)
  1511. // Write out a merged metamodel containing all these models: this is the MM for the manual operation
  1512. // New location is available, so write
  1513. model_create(merged_formalism, "merged/" + operation_name, get_entry_id("formalisms/SimpleClassDiagrams"), "Model")
  1514. merged_formalism_id = get_entry_id("merged/" + operation_name)
  1515. // Add tracability links at this level
  1516. tracability_link = instantiate_link(core, "tracability", "", model_id, merged_formalism_id)
  1517. instantiate_attribute(core, tracability_link, "type", "operatesOn")
  1518. // Add tracability links at this level
  1519. tracability_link = instantiate_link(core, "tracability", "", merged_formalism_id, ramified_metamodel_id)
  1520. instantiate_attribute(core, tracability_link, "type", "RAMified")
  1521. // Extend metadata with info on source and target
  1522. String link
  1523. String dst
  1524. keys = dict_keys(source)
  1525. while (set_len(keys) > 0):
  1526. key = set_pop(keys)
  1527. dst = source[key]
  1528. log("Add transformInput link for " + model_id + " to " + dst + " while adding operation " + operation_name)
  1529. link = instantiate_link(core, "transformInput", "", model_id, dst)
  1530. instantiate_attribute(core, link, "name", key)
  1531. keys = dict_keys(target)
  1532. while (set_len(keys) > 0):
  1533. key = set_pop(keys)
  1534. dst = target[key]
  1535. link = instantiate_link(core, "transformOutput", "", model_id, dst)
  1536. instantiate_attribute(core, link, "name", key)
  1537. return "Success"!
  1538. else:
  1539. return "Model exists: " + operation_name!
  1540. String function cmd_permission_modify(model_name : String, permissions : String):
  1541. Integer permission
  1542. String model_id
  1543. model_id = get_entry_id(model_name)
  1544. if (model_id != ""):
  1545. if (get_relation_to_model(current_user_id, model_id) == 0):
  1546. Boolean fail
  1547. Integer i
  1548. i = 0
  1549. if (string_len(permissions) != 3):
  1550. fail = True
  1551. while (i < 3):
  1552. permission = cast_s2i(string_get(permissions, i))
  1553. if (bool_or(permission < 0, permission > 2)):
  1554. fail = True
  1555. break!
  1556. i = i + 1
  1557. if (bool_not(fail)):
  1558. unset_attribute(core, model_id, "permissions")
  1559. instantiate_attribute(core, model_id, "permissions", permissions)
  1560. return "Success"!
  1561. else:
  1562. return "Permission has incorrect format: must be a string of three characters, with each character being a digit between 0 and 2"!
  1563. else:
  1564. return "Permission denied to model: " + model_name!
  1565. else:
  1566. return "Model not found: " + model_name!
  1567. String function cmd_permission_owner(model_name : String, new_user_name : String):
  1568. String model_id
  1569. String new_user_id
  1570. model_id = get_entry_id(model_name)
  1571. if (model_id != ""):
  1572. if (bool_or(get_relation_to_model(current_user_id, model_id) == 0, is_admin(current_user_id))):
  1573. new_user_id = get_user_id(new_user_name)
  1574. if (new_user_id != ""):
  1575. model_delete_element(core, set_pop(allOutgoingAssociationInstances(core, model_id, "owner")))
  1576. instantiate_link(core, "owner", "", model_id, new_user_id)
  1577. return "Success"!
  1578. else:
  1579. return "No such user: " + new_user_name!
  1580. else:
  1581. return "Permission denied to model: " + model_name!
  1582. else:
  1583. return "Model not found: " + model_name!
  1584. String function cmd_permission_group(model_name : String, new_group_name : String):
  1585. String model_id
  1586. String group_id
  1587. model_id = get_entry_id(model_name)
  1588. if (model_id != ""):
  1589. if (bool_or(get_relation_to_model(current_user_id, model_id) == 0, is_admin(current_user_id))):
  1590. group_id = get_group_id(new_group_name)
  1591. if (group_id != ""):
  1592. model_delete_element(core, set_pop(allOutgoingAssociationInstances(core, model_id, "group")))
  1593. instantiate_link(core, "group", "", model_id, group_id)
  1594. return "Success"!
  1595. else:
  1596. return "No such group: " + new_group_name!
  1597. else:
  1598. return "Permission denied to model: " + model_name!
  1599. else:
  1600. return "Model not found: " + model_name!
  1601. String function cmd_group_create(group_name : String):
  1602. // Create a new group and become its owner
  1603. String group_id
  1604. String name
  1605. group_id = get_group_id(group_name)
  1606. if (group_id == ""):
  1607. group_id = instantiate_node(core, "Group", "")
  1608. instantiate_attribute(core, group_id, "name", group_name)
  1609. instantiate_link(core, "belongsTo", "", current_user_id, group_id)
  1610. instantiate_link(core, "owner", "", group_id, current_user_id)
  1611. return "Success"!
  1612. else:
  1613. return "Group exists: " + group_name!
  1614. String function cmd_group_delete(group_name : String):
  1615. // Delete an existing group
  1616. String group_id
  1617. group_id = get_group_id(group_name)
  1618. if (group_id != ""):
  1619. if (allow_group_modify(current_user_id, group_id)):
  1620. model_delete_element(core, group_id)
  1621. return "Success"!
  1622. else:
  1623. return "Permission denied to group: " + group_name!
  1624. else:
  1625. return "Group not found: " + group_name!
  1626. String function cmd_group_owner_add(group_name : String, other_user_name : String):
  1627. // Add an owner to your group
  1628. String group_id
  1629. String other_user_id
  1630. group_id = get_group_id(group_name)
  1631. if (group_id != ""):
  1632. if (allow_group_modify(current_user_id, group_id)):
  1633. other_user_id = get_user_id(other_user_name)
  1634. if (other_user_id != ""):
  1635. if (set_in(allAssociationDestinations(core, group_id, "owner"), other_user_id)):
  1636. instantiate_link(core, "owner", "", group_id, other_user_id)
  1637. if (set_in(allAssociationDestinations(core, other_user_id, "belongsTo"), group_id)):
  1638. instantiate_link(core, "belongsTo", "", other_user_id, group_id)
  1639. return "Success"!
  1640. else:
  1641. return "User is already the owner!"!
  1642. else:
  1643. return "User not found: " + other_user_name!
  1644. else:
  1645. return "Permission denied to group: " + group_name!
  1646. else:
  1647. return "Group not found: " + group_name!
  1648. String function cmd_group_owner_delete(group_name : String, other_user_name : String):
  1649. // Remove an owner from your group
  1650. String group_id
  1651. String other_user_id
  1652. group_id = get_group_id(group_name)
  1653. if (group_id != ""):
  1654. if (allow_group_modify(current_user_id, group_id)):
  1655. other_user_id = get_user_id(other_user_name)
  1656. if (other_user_id != ""):
  1657. if (set_in(allAssociationDestinations(core, other_user_id, "belongsTo"), group_id)):
  1658. if (set_in(allAssociationDestinations(core, group_id, "owner"), other_user_id)):
  1659. Element overlap
  1660. overlap = set_overlap(allIncomingAssociationInstances(core, group_id, "owner"), allOutgoingAssociationInstances(core, other_user_id, "owner"))
  1661. model_delete_element(core, set_pop(overlap))
  1662. return "Success"!
  1663. else:
  1664. return "User is not an owner of the group!"!
  1665. else:
  1666. return "User is not a member of the group!"!
  1667. else:
  1668. return "User not found: " + other_user_name!
  1669. else:
  1670. return "Permission denied to group: " + group_name!
  1671. else:
  1672. return "Group not found: " + group_name!
  1673. String function cmd_group_join(group_name : String, other_user_name : String):
  1674. // Add someone to your group
  1675. String group_id
  1676. String other_user_id
  1677. group_id = get_group_id(group_name)
  1678. if (group_id != ""):
  1679. if (allow_group_modify(current_user_id, group_id)):
  1680. other_user_id = get_user_id(other_user_name)
  1681. if (other_user_id != ""):
  1682. if (set_in(allOutgoingAssociationInstances(core, other_user_id, "belongsTo"), group_id)):
  1683. instantiate_link(core, "belongsTo", "", other_user_id, group_id)
  1684. return "Success"!
  1685. else:
  1686. return "User is already a member of the group!"!
  1687. else:
  1688. return "User not found: " + other_user_name!
  1689. else:
  1690. return "Permission denied to group: " + group_name!
  1691. else:
  1692. return "Group not found: " + group_name!
  1693. String function cmd_group_kick(group_name : String, other_user_name : String):
  1694. // Remove someone from your group
  1695. String group_id
  1696. String other_user_id
  1697. group_id = get_group_id(group_name)
  1698. if (group_id != ""):
  1699. if (allow_group_modify(current_user_id, group_id)):
  1700. other_user_id = get_user_id(other_user_name)
  1701. if (other_user_id != ""):
  1702. Element overlap
  1703. if (set_in(allAssociationDestinations(core, other_user_id, "belongsTo"), group_id)):
  1704. overlap = set_overlap(allIncomingAssociationInstances(core, group_id, "belongsTo"), allOutgoingAssociationInstances(core, other_user_id, "belongsTo"))
  1705. model_delete_element(core,set_pop(overlap))
  1706. // Check if user was an owner as well
  1707. if (set_in(allAssociationDestinations(core, group_id, "owner"), other_user_id)):
  1708. overlap = set_overlap(allIncomingAssociationInstances(core, other_user_id, "owner"), allOutgoingAssociationInstances(core, group_id, "owner"))
  1709. model_delete_element(core, set_pop(overlap))
  1710. return "Success"!
  1711. else:
  1712. return "User is not a member of the group!"!
  1713. else:
  1714. return "User not found: " + other_user_name!
  1715. else:
  1716. return "Permission denied to group: " + group_name!
  1717. else:
  1718. return "Group not found: " + group_name!
  1719. String function cmd_group_list():
  1720. // List all groups you are a member of (and whether you are admin or not!)
  1721. Element groups
  1722. String group_id
  1723. String admin
  1724. String result
  1725. result = ""
  1726. groups = allAssociationDestinations(core, current_user_id, "belongsTo")
  1727. while (set_len(groups) > 0):
  1728. group_id = set_pop(groups)
  1729. if (set_in(allAssociationDestinations(core, group_id, "owner"), current_user_id)):
  1730. admin = " A "
  1731. else:
  1732. admin = " "
  1733. result = result + (string_join(admin, read_attribute(core, group_id, "name")) + "\n")
  1734. return result!
  1735. String function cmd_admin_promote(other_user_name : String):
  1736. // Promote a user to admin status
  1737. if (is_admin(current_user_id)):
  1738. String other_user_id
  1739. other_user_id = get_user_id(other_user_name)
  1740. if (other_user_id != ""):
  1741. unset_attribute(core, other_user_id, "admin")
  1742. instantiate_attribute(core, other_user_id, "admin", True)
  1743. return "Success"!
  1744. else:
  1745. return "User not found: " + other_user_name!
  1746. else:
  1747. return "Permission denied to administrate: " + other_user_name!
  1748. String function cmd_admin_demote(other_user_name : String):
  1749. // Demote a user to normal status
  1750. if (is_admin(current_user_id)):
  1751. String other_user_id
  1752. other_user_id = get_user_id(other_user_name)
  1753. if (other_user_id != ""):
  1754. unset_attribute(core, other_user_id, "admin")
  1755. instantiate_attribute(core, other_user_id, "admin", False)
  1756. return "Success"!
  1757. else:
  1758. return "User not found: " + other_user_name!
  1759. else:
  1760. return "Permission denied to administrate: " + other_user_name!
  1761. String function cmd_service_register(service_name : String):
  1762. // Active a service for this specific user
  1763. if (get_service_id(service_name) == ""):
  1764. String service
  1765. service = instantiate_node(core, "Service", "")
  1766. instantiate_attribute(core, service, "name", service_name)
  1767. instantiate_attribute(core, service, "port", get_taskname())
  1768. output("Success: " + get_taskname())
  1769. // Wait until we can stop the service
  1770. while (cast_v2s(input()) != "\"service_stop\""):
  1771. output("Service is running on this task: stop it by sending 'service_stop'")
  1772. model_delete_element(core, service)
  1773. // Service terminated
  1774. return "Success"!
  1775. else:
  1776. return "Service already exists: " + service_name!
  1777. String function cmd_user_password(user_name : String, new_password : String):
  1778. if (bool_or(current_user_id == get_user_id(user_name), is_admin(current_user_id))):
  1779. if (get_user_id(user_name) != ""):
  1780. instantiate_attribute(core, get_user_id(user_name), "password", hash(new_password))
  1781. return "Success"!
  1782. else:
  1783. return "No such user: " + user_name!
  1784. else:
  1785. return "Permission denied to user: " + user_name!
  1786. String function cmd_transformation_signature(transformation_name : String):
  1787. String model_id
  1788. model_id = get_entry_id(transformation_name)
  1789. if (model_id != ""):
  1790. if (is_nominal_instance(core, model_id, "Transformation")):
  1791. if (allow_read(current_user_id, transformation_name)):
  1792. String result
  1793. String elem
  1794. Element inputs
  1795. Element outputs
  1796. result = "Success: "
  1797. inputs = allOutgoingAssociationInstances(core, model_id, "transformInput")
  1798. while (set_len(inputs) > 0):
  1799. elem = set_pop(inputs)
  1800. result = string_join(string_join(string_join(string_join(result + "I ", read_attribute(core, elem, "name")), " "), full_name(readAssociationDestination(core, elem))), "\n")
  1801. outputs = allOutgoingAssociationInstances(core, model_id, "transformOutput")
  1802. while (set_len(outputs) > 0):
  1803. elem = set_pop(outputs)
  1804. result = string_join(string_join(string_join(string_join(result + "O ", read_attribute(core, elem, "name")), " "), full_name(readAssociationDestination(core, elem))), "\n")
  1805. return result!
  1806. else:
  1807. return "Permission denied to operation: " + transformation_name!
  1808. else:
  1809. return "Model is not an operation: " + transformation_name!
  1810. else:
  1811. return "No such operation: " + transformation_name!
  1812. String function cmd_model_types(model_name : String):
  1813. if (get_entry_id(model_name) != ""):
  1814. String model_id
  1815. model_id = get_entry_id(model_name)
  1816. Element types
  1817. String type
  1818. String result
  1819. result = "Success: "
  1820. types = allAssociationDestinations(core, model_id, "instanceOf")
  1821. while (set_len(types) > 0):
  1822. type = set_pop(types)
  1823. result = string_join(result, full_name(type) + "\n")
  1824. return result!
  1825. else:
  1826. return "No such model: " + model_name!
  1827. String function cmd_folder_create(folder_name : String):
  1828. if (get_entry_id(folder_name) == ""):
  1829. if (element_neq(create_folders(current_user_id, folder_name), read_root())):
  1830. return "Success"!
  1831. else:
  1832. return "Permission denied to folder: " + folder_name!
  1833. else:
  1834. return "Folder alreay exists: " + folder_name!
  1835. String function cmd_conformance_add(model_name : String, metamodel_name : String):
  1836. // Create a new instanceOf relation now
  1837. String instance_of
  1838. instance_of = instantiate_link(core, "instanceOf", "", get_entry_id(model_name), get_entry_id(metamodel_name))
  1839. return "Success"!
  1840. Void function user_function_skip_init(user_id : String):
  1841. String cmd
  1842. String result
  1843. current_user_id = user_id
  1844. caches = dict_create()
  1845. dict_add_fast(caches, "models", dict_create())
  1846. dict_add_fast(caches, "users", dict_create())
  1847. dict_add_fast(caches, "full_name", dict_create())
  1848. // Find root element
  1849. Element elems
  1850. String elem
  1851. elems = allInstances(core, "Folder")
  1852. while (set_len(elems) > 0):
  1853. elem = set_pop(elems)
  1854. if (value_eq(read_attribute(core, elem, "name"), "")):
  1855. if (set_len(allIncomingAssociationInstances(core, elem, "contains")) == 0):
  1856. dict_add_fast(caches, "root", elem)
  1857. break!
  1858. output("Welcome to the Model Management Interface v2.0!")
  1859. output("Use the 'help' command for a list of possible commands")
  1860. while (True):
  1861. cmd = input()
  1862. if (cmd == "help"):
  1863. output(cmd_help())
  1864. elif (cmd == "model_add"):
  1865. output(cmd_model_add(single_input("Model type?"), single_input("Model name?"), single_input("Model textual representation?")))
  1866. elif (cmd == "process_execute"):
  1867. output(cmd_process_execute(single_input("Process to execute?"), single_input("Model prefix to use?")))
  1868. elif (cmd == "transformation_between"):
  1869. output(cmd_transformation_between(dict_input("Source signature?"), dict_input("Target signature?")))
  1870. elif (cmd == "model_render"):
  1871. output(cmd_model_render(single_input("Model name?"), single_input("Mapper name?"), single_input("Rendered name?")))
  1872. elif (cmd == "model_rendered"):
  1873. output(cmd_model_rendered(single_input("Model name?"), single_input("Mapper name?")))
  1874. elif (cmd == "transformation_execute"):
  1875. output(cmd_transformation_execute(single_input("Transformation name?"), dict_input("Source models?"), dict_input("Target models?"), single_input("Tracability model?")))
  1876. elif (cmd == "verify"):
  1877. result = cmd_verify(single_input("Model name?"), single_input("Metamodel name?"))
  1878. if (result != ""):
  1879. output(result)
  1880. elif (cmd == "model_overwrite"):
  1881. output(cmd_model_overwrite(single_input("Model name?"), single_input("Metamodel name?")))
  1882. elif (cmd == "model_modify"):
  1883. output(cmd_model_modify(single_input("Model name?"), single_input("Metamodel name?")))
  1884. elif (cmd == "model_delete"):
  1885. output(cmd_model_delete(single_input("Model name?")))
  1886. elif (cmd == "model_list"):
  1887. output(cmd_model_list(single_input("Location?")))
  1888. elif (cmd == "model_list_full"):
  1889. output(cmd_model_list_full(single_input("Location?")))
  1890. elif (cmd == "transformation_add_MANUAL"):
  1891. output(cmd_transformation_add_MANUAL(dict_input("Source model names?"), dict_input("Target model names?"), single_input("Operation name?")))
  1892. elif (cmd == "transformation_add_AL"):
  1893. output(cmd_transformation_add_AL(dict_input("Source model names?"), dict_input("Target model names?"), single_input("Operation name?")))
  1894. elif (cmd == "transformation_add_MT"):
  1895. output(cmd_transformation_add_MT(dict_input("Source model names?"), dict_input("Target models?"), single_input("Operation name?")))
  1896. elif (cmd == "permission_modify"):
  1897. output(cmd_permission_modify(single_input("Model name?"), single_input("Permissions?")))
  1898. elif (cmd == "permission_owner"):
  1899. output(cmd_permission_owner(single_input("Model name?"), single_input("New owning user?")))
  1900. elif (cmd == "permission_group"):
  1901. output(cmd_permission_group(single_input("Model name?"), single_input("New owning group?")))
  1902. elif (cmd == "group_create"):
  1903. output(cmd_group_create(single_input("New group name?")))
  1904. elif (cmd == "group_delete"):
  1905. output(cmd_group_delete(single_input("Group name?")))
  1906. elif (cmd == "group_owner_add"):
  1907. output(cmd_group_owner_add(single_input("Group name?"), single_input("User name?")))
  1908. elif (cmd == "group_owner_delete"):
  1909. output(cmd_group_owner_delete(single_input("Group name?"), single_input("User name?")))
  1910. elif (cmd == "group_join"):
  1911. output(cmd_group_join(single_input("Group name?"), single_input("User name?")))
  1912. elif (cmd == "group_kick"):
  1913. output(cmd_group_kick(single_input("Group name?"), single_input("User name?")))
  1914. elif (cmd == "group_list"):
  1915. output(cmd_group_list())
  1916. elif (cmd == "admin_promote"):
  1917. output(cmd_admin_promote(single_input("User name?")))
  1918. elif (cmd == "admin_demote"):
  1919. output(cmd_admin_demote(single_input("User name?")))
  1920. elif (cmd == "service_register"):
  1921. output(cmd_service_register(single_input("Service name?")))
  1922. elif (cmd == "user_password"):
  1923. output(cmd_user_password(single_input("User name?"), single_input("New password?")))
  1924. elif (cmd == "transformation_read_signature"):
  1925. output(cmd_transformation_signature(single_input("Transformation name?")))
  1926. elif (cmd == "verbose"):
  1927. set_verbose(True)
  1928. elif (cmd == "quiet"):
  1929. set_verbose(False)
  1930. elif (cmd == "self-destruct"):
  1931. // Delete user from Core Formalism
  1932. model_delete_element(core, user_id)
  1933. return !
  1934. elif (cmd == "user_logout"):
  1935. // TODO
  1936. cmd = "FAIL"
  1937. elif (cmd == "exit"):
  1938. // Exit by actually removing the user and decoupling it from all of its models
  1939. // Restarting with the same user name will NOT grant you access to anything of the previous user with that same name
  1940. // as the current user will have been deleted
  1941. return !
  1942. elif (cmd == "folder_create"):
  1943. output(cmd_folder_create(single_input("Folder name?")))
  1944. elif (cmd == "add_conformance"):
  1945. // TODO
  1946. output(cmd_conformance_add(single_input("Model name?"), single_input("Metamodel name?")))
  1947. elif (cmd == "remove_conformance"):
  1948. // TODO
  1949. cmd = "FAIL"
  1950. elif (cmd == "user_name"):
  1951. // TODO
  1952. cmd = "FAIL"
  1953. elif (cmd == "service_poll"):
  1954. // TODO
  1955. cmd = "FAIL"
  1956. elif (cmd == "model_types"):
  1957. output(cmd_model_types(single_input("Model name?")))
  1958. else:
  1959. output("Unknown command: " + cmd)
  1960. // We never get here!
  1961. return !