core_algorithm.alc 94 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681
  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. include "random.alh"
  17. include "io.alh"
  18. String core_model_location = "models/core"
  19. Element core = ?
  20. String current_user_id
  21. Element caches
  22. Element pm_tasks = ?
  23. String function get_foldername(name : String):
  24. Element result
  25. result = string_split(name, "/")
  26. list_pop_final(result)
  27. if (list_len(result) == 0):
  28. return ""!
  29. // 'result' now contains a list of entries which we have to join with the seperator
  30. String str
  31. str = list_pop_final(result)
  32. while (list_len(result) > 0):
  33. str = cast_string(list_pop_final(result)) + "/" + str
  34. return str!
  35. String function get_filename(name : String):
  36. return list_pop_final(string_split(name, "/"))!
  37. String function full_name(model_id : String):
  38. if (dict_in(core["model"], model_id)):
  39. return read_attribute(core, model_id, "name")!
  40. else:
  41. return read_root()!
  42. Void function initialize_core():
  43. // TODO make this more flexible by putting it in the bootstrap in a similar way as other models
  44. return !
  45. Boolean function is_typed_by(model_id : String, metamodel_id : String):
  46. return (set_len(get_instanceOf_links(model_id, metamodel_id)) > 0)!
  47. Element function get_instanceOf_links(model_id : String, metamodel_id : String):
  48. Element result
  49. Element options
  50. String elem
  51. result = set_create()
  52. options = allOutgoingAssociationInstances(core, model_id, "instanceOf")
  53. while (set_len(options) > 0):
  54. elem = set_pop(options)
  55. if (value_eq(readAssociationDestination(core, elem), metamodel_id)):
  56. set_add(result, elem)
  57. return result!
  58. String function get_instanceOf_link(model_id : String, metamodel_id : String):
  59. Element all_links
  60. String choice
  61. all_links = get_instanceOf_links(model_id, metamodel_id)
  62. if (set_len(all_links) > 1):
  63. log("WARNING: multiple instanceOf relations were detected for this model; picking one at random!")
  64. elif (set_len(all_links) == 0):
  65. //log("No types found!")
  66. //log("Source model: " + cast_value(read_attribute(core, model_id, "name")))
  67. //log("Target meta-model: " + cast_value(read_attribute(core, metamodel_id, "name")))
  68. return read_root()!
  69. choice = set_pop(all_links)
  70. return choice!
  71. Element function get_full_model(model_id : String, metamodel_id : String):
  72. // TODO we are restricted to using formalisms/SimpleClassDiagrams as M3 level, through this code!
  73. Element m
  74. Element all_links
  75. String choice
  76. choice = get_instanceOf_link(model_id, metamodel_id)
  77. m = dict_create()
  78. dict_add(m, "model", import_node(read_attribute(core, model_id, "location")))
  79. // TODO for now this is restricted to the fixed semantics
  80. // dict_add(m, "semantics", anAssociationDestination(core, choice, "semantics"))
  81. //dict_add(m, "semantics", get_entry_id("models/conformance_mv"))
  82. if (metamodel_id == model_id):
  83. // Found the meta-circular level, so we can stop!
  84. dict_add(m, "metamodel", m)
  85. else:
  86. Element mm
  87. mm = get_full_model(metamodel_id, get_entry_id("formalisms/SimpleClassDiagrams"))
  88. if (element_eq(mm, read_root())):
  89. log("Error finding type mapping...")
  90. return read_root()!
  91. else:
  92. dict_add(m, "metamodel", mm)
  93. if (element_neq(choice, read_root())):
  94. Element types
  95. types = allAssociationDestinations(core, choice, "typing")
  96. if (set_len(types) == 1):
  97. // Add the preferred original type mapping
  98. set_type_mapping(m, import_node(read_attribute(core, set_pop(types), "location")))
  99. else:
  100. // Start from scratch
  101. new_type_mapping(m)
  102. else:
  103. // Start from scratch
  104. new_type_mapping(m)
  105. if (find_type_mapping(m)):
  106. // TODO store the found type model somewhere
  107. return m!
  108. else:
  109. log("No type mapping could be deduced!")
  110. log("Error for " + cast_value(read_attribute(core, model_id, "name")))
  111. log(" and type " + cast_value(read_attribute(core, metamodel_id, "name")))
  112. return read_root()!
  113. Integer function get_relation_to_model(user_id : String, model_id : String):
  114. if (set_in(allAssociationDestinations(core, model_id, "owner"), user_id)):
  115. // We are the owner
  116. return 0!
  117. else:
  118. String group_id
  119. group_id = anAssociationDestination(core, model_id, "group")
  120. if (set_in(allAssociationDestinations(core, user_id, "belongsTo"), group_id)):
  121. // We are in the owning group
  122. return 1!
  123. else:
  124. // We are not related whatsoever
  125. return 2!
  126. Boolean function is_admin(user_id : String):
  127. if (read_attribute(core, user_id, "admin")):
  128. return True!
  129. else:
  130. return False!
  131. Boolean function allow_execute(user_id : String, model_id : String):
  132. return allow_read(user_id, model_id)!
  133. Boolean function allow_read(user_id : String, model_id : String):
  134. if (is_admin(user_id)):
  135. // Is admin, so always allow
  136. return True!
  137. else:
  138. // Check permissions
  139. String permission
  140. permission = string_get(read_attribute(core, model_id, "permissions"), get_relation_to_model(user_id, model_id))
  141. if (bool_or(permission == "1", permission == "2")):
  142. return True!
  143. else:
  144. return False!
  145. Boolean function allow_write(user_id : String, model_id : String):
  146. if (is_admin(user_id)):
  147. // Is admin, so always allow
  148. return True!
  149. else:
  150. // Check permissions
  151. String permission
  152. permission = string_get(read_attribute(core, model_id, "permissions"), get_relation_to_model(user_id, model_id))
  153. if (permission == "2"):
  154. return True!
  155. else:
  156. return False!
  157. Boolean function allow_change_metadata(user_id : String, model_id : String):
  158. if (is_admin(user_id)):
  159. // Is admin, so always allow
  160. return True!
  161. else:
  162. if (get_relation_to_model(user_id, model_id) == 0):
  163. // Only owner can chmod
  164. return True!
  165. else:
  166. return False!
  167. Boolean function allow_group_modify(user_id : String, group_id : String):
  168. if (is_admin(user_id)):
  169. // Is admin, so always allow
  170. return True!
  171. else:
  172. if (set_in(allAssociationDestinations(core, group_id, "owner"), user_id)):
  173. // We are an owner
  174. return True!
  175. else:
  176. return False!
  177. Boolean function check_login(user_id : String):
  178. String password
  179. String stored_password
  180. stored_password = read_attribute(core, user_id, "password")
  181. output("Password for existing user?")
  182. password = hash(input())
  183. return password == stored_password!
  184. String function get_entry_id(name : String):
  185. Element hierarchy_split
  186. String current
  187. Integer i
  188. Element elems
  189. String elem
  190. Boolean found
  191. String folder_name
  192. if (name == ""):
  193. return caches["root"]!
  194. if (string_get(name, string_len(name) - 1) == "/"):
  195. name = string_substr(name, 0, string_len(name) - 1)
  196. name = string_replace(name, "~", "users/" + cast_string(read_attribute(core, current_user_id, "name")))
  197. if (dict_in(caches["models"], name)):
  198. if (dict_in(core, caches["models"])):
  199. if (full_name(caches["models"][name]) == name):
  200. return caches["models"][name]!
  201. folder_name = get_foldername(name)
  202. current = get_entry_id(folder_name)
  203. if (current == ""):
  204. return ""!
  205. elems = allAssociationDestinations(core, current, "contains")
  206. while (set_len(elems) > 0):
  207. elem = set_pop(elems)
  208. dict_overwrite(caches["models"], read_attribute(core, elem, "name"), elem)
  209. if (value_eq(name, read_attribute(core, elem, "name"))):
  210. return elem!
  211. return ""!
  212. String function get_service_id(name : String):
  213. core = import_node(core_model_location)
  214. Element services
  215. String service
  216. services = allInstances(core, "Service")
  217. while (set_len(services) > 0):
  218. service = set_pop(services)
  219. if (value_eq(read_attribute(core, service, "name"), name)):
  220. return service!
  221. return ""!
  222. String function get_user_id(name : String):
  223. Element users
  224. String user
  225. users = allInstances(core, "User")
  226. while (set_len(users) > 0):
  227. user = set_pop(users)
  228. if (value_eq(read_attribute(core, user, "name"), name)):
  229. return user!
  230. return ""!
  231. String function get_group_id(name : String):
  232. Element groups
  233. String group
  234. groups = allInstances(core, "Group")
  235. while (set_len(groups) > 0):
  236. group = set_pop(groups)
  237. if (value_eq(read_attribute(core, group, "name"), name)):
  238. return group!
  239. return ""!
  240. String function create_folders(user_id : String, folder_name : String, permissions : Boolean):
  241. Element hierarchy
  242. Integer i
  243. String prev
  244. String elem
  245. String new_entry
  246. String cummul
  247. if (get_entry_id(folder_name) != ""):
  248. // Folders already exist, so don't iterate
  249. return get_entry_id(folder_name)!
  250. i = 0
  251. folder_name = string_replace(folder_name, "~", "users/" + cast_string(read_attribute(core, current_user_id, "name")))
  252. hierarchy = string_split(folder_name, "/")
  253. prev = get_entry_id("")
  254. cummul = ""
  255. // Go through folders first
  256. while (i < list_len(hierarchy)):
  257. elem = list_read(hierarchy, i)
  258. if (cummul == ""):
  259. cummul = elem
  260. else:
  261. cummul = string_join(cummul + "/", elem)
  262. if (get_entry_id(cummul) == ""):
  263. if (bool_or(permissions, allow_write(current_user_id, prev))):
  264. // Element does not exist yet!
  265. new_entry = instantiate_node(core, "Folder", "")
  266. instantiate_attribute(core, new_entry, "name", cummul)
  267. instantiate_attribute(core, new_entry, "permissions", "200")
  268. instantiate_link(core, "contains", "", prev, new_entry)
  269. instantiate_link(core, "group", "", new_entry, get_group_id("nobody"))
  270. instantiate_link(core, "owner", "", new_entry, user_id)
  271. else:
  272. log("Not allowed to write to " + cast_string(read_attribute(core, prev, "name")))
  273. return read_root()!
  274. prev = get_entry_id(cummul)
  275. i = i + 1
  276. return prev!
  277. String function store_entry(model_id : String, full_name : String, user_id : String):
  278. String prev
  279. full_name = string_replace(full_name, "~", "users/" + cast_string(read_attribute(core, current_user_id, "name")))
  280. if (get_entry_id(full_name) != ""):
  281. // Delete any old models
  282. model_delete(get_entry_id(full_name))
  283. prev = create_folders(user_id, get_foldername(full_name), True)
  284. instantiate_link(core, "contains", "", prev, model_id)
  285. return full_name!
  286. String function export_typing(model : Element, name : String):
  287. String result
  288. result = instantiate_node(core, "TypeMapping", "")
  289. // Create type mapping model
  290. String location
  291. location = "type mappings/" + cast_id(get_type_mapping(model))
  292. export_node(location, get_type_mapping(model))
  293. name = location
  294. String instance_of
  295. name = store_entry(result, name, current_user_id)
  296. instantiate_attribute(core, result, "name", name)
  297. instantiate_attribute(core, result, "location", location)
  298. instantiate_attribute(core, result, "permissions", "200")
  299. instantiate_link(core, "owner", "", result, current_user_id)
  300. instantiate_link(core, "group", "", result, get_group_id("nobody"))
  301. instance_of = instantiate_link(core, "instanceOf", "", result, get_entry_id("formalisms/TypeMapping"))
  302. //instantiate_link(core, "semantics", "", instance_of, get_entry_id("models/conformance_mv"))
  303. return result!
  304. Void function model_create(model : Element, name : String, type_id : String, kind : String):
  305. if (get_entry_id(name) != ""):
  306. model_overwrite(model, get_entry_id(name), type_id)
  307. else:
  308. String location
  309. String model_id
  310. String instance_of
  311. // Create model itself
  312. location = "models/" + cast_id(model)
  313. export_node(location, model["model"])
  314. model_id = instantiate_node(core, kind, "")
  315. instantiate_attribute(core, model_id, "name", store_entry(model_id, name, current_user_id))
  316. instantiate_attribute(core, model_id, "location", location)
  317. instantiate_attribute(core, model_id, "permissions", "200")
  318. instantiate_link(core, "owner", "", model_id, current_user_id)
  319. instantiate_link(core, "group", "", model_id, get_group_id("nobody"))
  320. instance_of = instantiate_link(core, "instanceOf", "", model_id, type_id)
  321. //instantiate_link(core, "semantics", "", instance_of, get_entry_id("models/conformance_mv"))
  322. // Create type mapping model
  323. instantiate_link(core, "typing", "", instance_of, export_typing(model, name))
  324. return!
  325. Void function model_overwrite(model : Element, model_id : String, metamodel_id : String):
  326. // TODO this should be more elegant than just hiding the old elements
  327. String location
  328. String instanceOf_link
  329. location = "models/" + cast_id(model)
  330. export_node(location, model["model"])
  331. // Change location in meta-data
  332. unset_attribute(core, model_id, "location")
  333. instantiate_attribute(core, model_id, "location", location)
  334. // Update the instanceOf relation of the context in which we are working
  335. String choice
  336. choice = get_instanceOf_link(model_id, metamodel_id)
  337. if (element_neq(choice, read_root())):
  338. // There was a link, so we remove it
  339. // First remove the type mapping it referred to
  340. Element tl
  341. tl = allAssociationDestinations(core, choice, "typing")
  342. while (set_len(tl) > 0):
  343. model_delete_element(core, set_pop(tl))
  344. // Now delete the element itself
  345. model_delete_element(core, choice)
  346. // Create a new instanceOf relation now
  347. String instance_of
  348. instance_of = instantiate_link(core, "instanceOf", "", model_id, metamodel_id)
  349. //instantiate_link(core, "semantics", "", instance_of, get_entry_id("models/conformance_mv"))
  350. instantiate_link(core, "typing", "", instance_of, export_typing(model, full_name(model_id)))
  351. return!
  352. Element function merge_models(models_dict : Element, operation_name : String):
  353. core = import_node(core_model_location)
  354. // 0) Find operation signature
  355. Element input_metamodels
  356. Element iter
  357. String edge
  358. String operation_id
  359. operation_id = get_entry_id(operation_name)
  360. input_metamodels = dict_create()
  361. iter = allOutgoingAssociationInstances(core, operation_id, "transformInput")
  362. while (set_len(iter) > 0):
  363. edge = set_pop(iter)
  364. dict_add(input_metamodels, read_attribute(core, edge, "name"), read_attribute(core, readAssociationDestination(core, edge), "name"))
  365. String merged_metamodel_id
  366. String trace_link_id
  367. Element trace_links
  368. // 1) Find merged metamodel
  369. trace_links = allOutgoingAssociationInstances(core, operation_id, "tracability")
  370. merged_metamodel_id = ""
  371. while (set_len(trace_links) > 0):
  372. trace_link_id = set_pop(trace_links)
  373. if (value_eq(read_attribute(core, trace_link_id, "type"), "operatesOn")):
  374. merged_metamodel_id = readAssociationDestination(core, trace_link_id)
  375. // 2) Merge source models
  376. if (merged_metamodel_id != ""):
  377. Element model_tuples
  378. Element keys
  379. String key
  380. Element mm
  381. model_tuples = set_create()
  382. keys = dict_keys(models_dict)
  383. while (set_len(keys) > 0):
  384. key = set_pop(keys)
  385. mm = get_full_model(get_entry_id(models_dict[key]), get_entry_id(input_metamodels[key]))
  386. if (element_eq(mm, read_root())):
  387. log("Signature mismatch in operation for tag " + key)
  388. output("Signature mismatch in operation for tag " + key)
  389. return read_root()!
  390. set_add_node(model_tuples, create_tuple(key, mm))
  391. Element merged_metamodel
  392. merged_metamodel = get_full_model(merged_metamodel_id, get_entry_id("SimpleClassDiagrams"))
  393. if (element_eq(merged_metamodel, read_root())):
  394. log("Merged metamodel in operation is not of type SimpleClassDiagrams")
  395. output("Type cannot be typed as formalisms/SimpleClassDiagrams: 'merged'")
  396. return read_root()!
  397. return model_join(model_tuples, merged_metamodel, read_root())!
  398. else:
  399. return read_root()!
  400. Element function split_model(model : Element, metamodels_dict : Element):
  401. core = import_node(core_model_location)
  402. Element model_tuples
  403. Element keys
  404. String key
  405. Element mm
  406. model_tuples = set_create()
  407. keys = dict_keys(metamodels_dict)
  408. while (set_len(keys) > 0):
  409. key = set_pop(keys)
  410. mm = get_full_model(get_entry_id(metamodels_dict[key]), get_entry_id("SimpleClassDiagrams"))
  411. if (element_eq(mm, read_root())):
  412. log("Output metamodel cannot be interpreted using SimpleClassDiagrams: " + key)
  413. output("Type cannot be typed as formalisms/SimpleClassDiagrams: " + key)
  414. return read_root()!
  415. set_add_node(model_tuples, create_tuple(key, mm))
  416. return model_split(model, model_tuples, read_root())!
  417. Boolean function check_conformance(model_id : String):
  418. // TODO check if it actually conforms, considering that instanceOf link
  419. // --> in-depth check
  420. return True!
  421. Element function get_model(model_name : String, metamodel_name : String):
  422. core = import_node(core_model_location)
  423. return get_full_model(get_entry_id(model_name), get_entry_id(metamodel_name))!
  424. Void function store_model(model_name : String, metamodel_name : String, model : Element):
  425. core = import_node(core_model_location)
  426. model_create(model, model_name, get_entry_id(metamodel_name), "Model")
  427. return!
  428. String function get_ramified_metamodel(model_name : String):
  429. core = import_node(core_model_location)
  430. String operation_id
  431. Element trace_links
  432. String merged_metamodel_id
  433. String trace_link_id
  434. String ramified_metamodel_id
  435. operation_id = get_entry_id(model_name)
  436. trace_links = allOutgoingAssociationInstances(core, operation_id, "tracability")
  437. merged_metamodel_id = ""
  438. while (set_len(trace_links) > 0):
  439. trace_link_id = set_pop(trace_links)
  440. if (value_eq(read_attribute(core, trace_link_id, "type"), "operatesOn")):
  441. merged_metamodel_id = readAssociationDestination(core, trace_link_id)
  442. trace_links = allOutgoingAssociationInstances(core, merged_metamodel_id, "tracability")
  443. ramified_metamodel_id = ""
  444. while (set_len(trace_links) > 0):
  445. trace_link_id = set_pop(trace_links)
  446. if (value_eq(read_attribute(core, trace_link_id, "type"), "RAMified")):
  447. ramified_metamodel_id = readAssociationDestination(core, trace_link_id)
  448. return read_attribute(core, ramified_metamodel_id, "name")!
  449. return read_root()!
  450. Element function execute_operation(operation_id : String, input_models : Element, tracability_model : Element):
  451. // Operations are always in-place and uses only a single metamodel
  452. // Therefore, we must:
  453. // 1) Find merged metamodel
  454. // 2) Merge the different source models and retype
  455. // 3) Perform the operation on the merged model
  456. // 4) Split the resulting model based on the target formalisms; if operation successful
  457. Element input_model
  458. String trace_link_id
  459. Element merged_model
  460. String merged_metamodel_id
  461. String ramified_metamodel_id
  462. Boolean result
  463. String exact_type
  464. Element trace_links
  465. String model_ID
  466. String key
  467. Element keys
  468. Element input_keys
  469. Element output_keys
  470. Element model_tuples
  471. String metamodel_name
  472. Element metamodel
  473. String metamodel_ID
  474. Boolean tracability
  475. // 0) Find the signature of the operation
  476. Element input_metamodels
  477. Element output_metamodels
  478. Element iter
  479. String edge
  480. input_metamodels = dict_create()
  481. iter = allOutgoingAssociationInstances(core, operation_id, "transformInput")
  482. while (set_len(iter) > 0):
  483. edge = set_pop(iter)
  484. dict_add(input_metamodels, read_attribute(core, edge, "name"), full_name(readAssociationDestination(core, edge)))
  485. output_metamodels = dict_create()
  486. iter = allOutgoingAssociationInstances(core, operation_id, "transformOutput")
  487. while (set_len(iter) > 0):
  488. edge = set_pop(iter)
  489. dict_add(output_metamodels, read_attribute(core, edge, "name"), full_name(readAssociationDestination(core, edge)))
  490. // 1) Find merged metamodel
  491. exact_type = read_type(core, operation_id)
  492. trace_links = allOutgoingAssociationInstances(core, operation_id, "tracability")
  493. merged_metamodel_id = ""
  494. while (set_len(trace_links) > 0):
  495. trace_link_id = set_pop(trace_links)
  496. if (value_eq(read_attribute(core, trace_link_id, "type"), "operatesOn")):
  497. merged_metamodel_id = readAssociationDestination(core, trace_link_id)
  498. // 2) Merge source models
  499. if (merged_metamodel_id != ""):
  500. model_tuples = set_create()
  501. keys = dict_keys(input_models)
  502. while (set_len(keys) > 0):
  503. key = set_pop(keys)
  504. Element mm
  505. if (get_entry_id(input_models[key]) == ""):
  506. // Input model doesn't exist yet, so create
  507. cmd_model_add(input_metamodels[key], input_models[key], "")
  508. mm = get_full_model(get_entry_id(input_models[key]), get_entry_id(input_metamodels[key]))
  509. if (element_eq(mm, read_root())):
  510. return "Signature mismatch in operation for tag: " + key!
  511. set_add_node(model_tuples, create_tuple(key, mm))
  512. Element merged_metamodel
  513. merged_metamodel = get_full_model(merged_metamodel_id, get_entry_id("formalisms/SimpleClassDiagrams"))
  514. if (element_eq(merged_metamodel, read_root())):
  515. return "Type cannot be typed as formalisms/SimpleClassDiagrams: 'merged'"!
  516. merged_model = model_join(model_tuples, merged_metamodel, tracability_model)
  517. else:
  518. if (bool_and(dict_len(input_models) == 1, dict_len(output_metamodels) == 0)):
  519. // Just skip the merge...
  520. Element m
  521. 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))]))
  522. if (element_eq(m, read_root())):
  523. return "Signature mismatch in operation for tag: " + cast_value(set_pop(dict_keys(input_models)))!
  524. merged_model = model_copy(m)
  525. elif (bool_and(dict_len(input_models) == 0, dict_len(output_metamodels) == 0)):
  526. merged_model = read_root()
  527. else:
  528. return "Could not resolve intermediate merged metamodel"!
  529. // 3) Transform
  530. if (exact_type == "ModelTransformation"):
  531. trace_links = allOutgoingAssociationInstances(core, merged_metamodel_id, "tracability")
  532. ramified_metamodel_id = ""
  533. while (set_len(trace_links) > 0):
  534. trace_link_id = set_pop(trace_links)
  535. if (value_eq(read_attribute(core, trace_link_id, "type"), "RAMified")):
  536. ramified_metamodel_id = readAssociationDestination(core, trace_link_id)
  537. Element operation
  538. operation = get_full_model(operation_id, ramified_metamodel_id)
  539. if (element_eq(operation, read_root())):
  540. return "Operation could not be typed by specified RAMified metamodel!"!
  541. result = transform(merged_model, operation)
  542. elif (exact_type == "ManualOperation"):
  543. output("Please perform manual operation " + cast_value(full_name(operation_id)))
  544. log("Performing manual")
  545. if (element_neq(merged_model, read_root())):
  546. String model_name
  547. model_name = ""
  548. while (get_entry_id(model_name) != ""):
  549. model_name = "tmp/" + random_string(20)
  550. model_create(merged_model, model_name, merged_metamodel_id, "Model")
  551. // We want to modify, so modify
  552. do_spawn_modify(model_name, True)
  553. merged_model = get_full_model(get_entry_id(model_name), merged_metamodel_id)
  554. //model_delete(get_entry_id(model_name))
  555. result = True
  556. else:
  557. output("Nothing to modify")
  558. return True!
  559. elif (exact_type == "ActionLanguage"):
  560. Element func
  561. Element al
  562. al = get_full_model(operation_id, get_entry_id("formalisms/ActionLanguage"))
  563. if (element_eq(al, read_root())):
  564. return "Action Language operation not typed by ActionLanguage metamodel!"!
  565. func = get_func_AL_model(al)
  566. result = func(merged_model)
  567. else:
  568. return "Unknown type of operation: " + exact_type!
  569. // 4) Split in different models depending on type
  570. if (element_neq(tracability_model, read_root())):
  571. tracability = True
  572. else:
  573. tracability = False
  574. if (result):
  575. model_tuples = set_create()
  576. keys = dict_keys(output_metamodels)
  577. while (set_len(keys) > 0):
  578. key = set_pop(keys)
  579. Element mm
  580. mm = get_full_model(get_entry_id(output_metamodels[key]), get_entry_id("formalisms/SimpleClassDiagrams"))
  581. if (element_eq(mm, read_root())):
  582. return "Type cannot be typed as formalisms/SimpleClassDiagrams: " + key!
  583. set_add_node(model_tuples, create_tuple(key, mm))
  584. result = model_split(merged_model, model_tuples, tracability)
  585. if (tracability):
  586. Element new_tracability_model
  587. new_tracability_model = result["__tracability"]
  588. dict_overwrite(tracability_model, "model", new_tracability_model["model"])
  589. set_type_mapping(tracability_model, get_type_mapping(new_tracability_model))
  590. dict_overwrite(tracability_model, "metamodel", new_tracability_model["metamodel"])
  591. dict_delete(result, "__tracability")
  592. return result!
  593. else:
  594. return "Failure"!
  595. Boolean function enact_action(pm : Element, element : String, mapping : Element):
  596. Boolean result
  597. String transformation_id
  598. Element lst
  599. String elem
  600. Element inputs
  601. Element outputs
  602. String type_name
  603. String exact_type
  604. Element trace_links
  605. Element output_mms
  606. Element consumes_link
  607. String name
  608. String elem_name
  609. Element keys
  610. String key
  611. String consume
  612. String produce
  613. Element input_traceability_model
  614. String output_traceability_name
  615. String output_traceability_type
  616. Element output_map
  617. inputs = dict_create()
  618. outputs = dict_create()
  619. output_map = dict_create()
  620. input_traceability_model = read_root()
  621. output_traceability_name = read_root()
  622. output_traceability_type = read_root()
  623. // Read out the referenced element from the MvC
  624. transformation_id = get_entry_id(read_attribute(pm, element, "name"))
  625. // Find all input model names
  626. lst = allOutgoingAssociationInstances(pm, element, "Consumes")
  627. while (set_len(lst) > 0):
  628. consume = set_pop(lst)
  629. elem = readAssociationDestination(pm, consume)
  630. type_name = read_attribute(pm, elem, "type")
  631. elem_name = read_attribute(pm, elem, "name")
  632. if (cast_string(read_attribute(pm, consume, "name")) == "__traceability"):
  633. input_traceability_model = model_copy(get_full_model(get_entry_id(mapping[elem_name]), get_entry_id(type_name)))
  634. output_traceability_type = type_name
  635. else:
  636. dict_add(inputs, read_attribute(pm, consume, "name"), mapping[elem_name])
  637. // Find all output model names and their metamodel
  638. lst = allOutgoingAssociationInstances(pm, element, "Produces")
  639. while (set_len(lst) > 0):
  640. produce = set_pop(lst)
  641. elem = readAssociationDestination(pm, produce)
  642. type_name = read_attribute(pm, elem, "type")
  643. elem_name = read_attribute(pm, elem, "name")
  644. if (cast_string(read_attribute(pm, produce, "name")) == "__traceability"):
  645. output_traceability_name = mapping[elem_name]
  646. else:
  647. dict_add(outputs, read_attribute(pm, produce, "name"), type_name)
  648. dict_add(output_map, read_attribute(pm, produce, "name"), mapping[elem_name])
  649. if read_type(core, transformation_id) == "ActionLanguage":
  650. log(string_join("Enacting ActionLanguage: ", read_attribute(pm, element, "name")))
  651. output("Success: ready for AL execution")
  652. elif read_type(core, transformation_id) == "ManualOperation":
  653. log(string_join("Enacting ManualOperation: ", read_attribute(pm, element, "name")))
  654. output("Success: ready for MANUAL execution")
  655. else:
  656. log(string_join("Enacting ModelTransformation: ", read_attribute(pm, element, "name")))
  657. output("Success: ready for MT execution")
  658. result = execute_operation(transformation_id, inputs, input_traceability_model)
  659. log(string_join("Finished: ", read_attribute(pm, element, "name")))
  660. if (is_physical_string(result)):
  661. // Something went wrong!
  662. output("Failure")
  663. return False!
  664. else:
  665. // Check if we have to store traceability information
  666. if (element_neq(output_traceability_name, read_root())):
  667. // Yes, so store it
  668. // Note that we have overwritten the previous input traceability model, which is why it was a copy
  669. model_overwrite(input_traceability_model, get_entry_id(output_traceability_name), get_entry_id(output_traceability_type))
  670. keys = dict_keys(outputs)
  671. while (set_len(keys) > 0):
  672. key = set_pop(keys)
  673. model_create(result[key], output_map[key], get_entry_id(outputs[key]), "Model")
  674. output("Success")
  675. return True!
  676. Element function PM_signature(pm : Element):
  677. Element all_data
  678. Element result
  679. String entry
  680. result = dict_create()
  681. all_data = allInstances(pm, "Data")
  682. while (set_len(all_data) > 0):
  683. entry = set_pop(all_data)
  684. dict_add(result, cast_string(read_attribute(pm, entry, "name")), cast_string(read_attribute(pm, entry, "type")))
  685. return result!
  686. Void function enact_PM_activity(activity_to_task : Element, task_to_result : Element, pm : Element, element : String, mapping : Element):
  687. Boolean result
  688. pm_tasks = activity_to_task
  689. set_add(activity_to_task[element], get_taskname())
  690. result = enact_action(pm, element, mapping)
  691. dict_add_fast(task_to_result, get_taskname(), result)
  692. while (other_has_output(get_taskname())):
  693. sleep(1)
  694. return!
  695. Void function enact_PM(pm : Element, mapping : Element):
  696. Element worklist
  697. String element
  698. String type
  699. Boolean result
  700. Element tuple
  701. Element counters
  702. Element join_nodes
  703. Element exec_nodes
  704. Element keys
  705. String key
  706. output("Success")
  707. // For all entries in the signature, not in the mapping, we add a mock location
  708. Element signature
  709. String mock_location
  710. Element mock_locations
  711. mock_locations = set_create()
  712. signature = PM_signature(pm)
  713. keys = dict_keys(signature)
  714. while (set_len(keys) > 0):
  715. key = set_pop(keys)
  716. if (bool_not(dict_in(mapping, key))):
  717. // Add mock location
  718. mock_location = ""
  719. while (get_entry_id(mock_location) != ""):
  720. mock_location = "tmp/" + random_string(10)
  721. dict_add(mapping, key, mock_location)
  722. cmd_model_add(signature[key], mapping[key], "")
  723. set_add(mock_locations, mock_location)
  724. // Initialize Join counters
  725. counters = dict_create()
  726. join_nodes = allInstances(pm, "Join")
  727. while (set_len(join_nodes) > 0):
  728. dict_add(counters, set_pop(join_nodes), 0)
  729. // Initialize activity to task dictionary with empty sets
  730. Element activity_to_task
  731. Element task_to_activity
  732. Element task_to_result
  733. activity_to_task = dict_create()
  734. task_to_activity = dict_create()
  735. task_to_result = dict_create()
  736. exec_nodes = allInstances(pm, "Exec")
  737. while (set_len(exec_nodes) > 0):
  738. dict_add(activity_to_task, set_pop(exec_nodes), set_create())
  739. // Create the worklist with the Start instance as first element
  740. worklist = list_create()
  741. list_append(worklist, set_pop(allInstances(pm, "Start")))
  742. // Keep on iterating until we reach finish
  743. while (True):
  744. // Check if there are PM elements to expand
  745. if (list_len(worklist) > 0):
  746. // There is something we can do, so do it!
  747. element = list_pop_final(worklist)
  748. // Find the type (to see what to do with it)
  749. // this does not yet yield the type of transformation, if it is an Execution
  750. type = read_type(pm, element)
  751. // Some types have nothing to do, such as start and fork
  752. // Therefore, they are not mentioned in the following conditional
  753. if (type == "Finish"):
  754. // We have finished, so terminate
  755. break!
  756. elif (type == "Join"):
  757. // Only do this if all dependencies are fullfilled
  758. // So add to the counter of this Join
  759. dict_overwrite(counters, element, integer_addition(counters[element], 1))
  760. // Now check whether we have enough tokens to execute the Join itself
  761. Integer required
  762. Integer got
  763. required = set_len(allIncomingAssociationInstances(pm, element, "Next")) + set_len(allIncomingAssociationInstances(pm, element, "Else")) + set_len(allIncomingAssociationInstances(pm, element, "Then"))
  764. got = counters[element]
  765. if (got == required):
  766. // Reset counter to 0
  767. dict_overwrite(counters, element, 0)
  768. // And continue
  769. else:
  770. // We haven't gotten all yet, so we wait (i.e., continue without adding Next link to worklist)
  771. continue!
  772. elif (type == "Exec"):
  773. // Execute a transformation
  774. // This the difficult part!
  775. Element args
  776. String taskname
  777. args = list_create()
  778. list_append(args, activity_to_task)
  779. list_append(args, task_to_result)
  780. list_append(args, pm)
  781. list_append(args, element)
  782. list_append(args, mapping)
  783. taskname = spawn(enact_PM_activity, args)
  784. output(taskname + " : " + cast_string(read_attribute(pm, element, "name")))
  785. dict_add(task_to_activity, taskname, element)
  786. continue!
  787. // We have finished the execution, so add all outgoing edges to the worklist
  788. Element all_next
  789. all_next = allAssociationDestinations(pm, element, "Next")
  790. String next
  791. while (set_len(all_next) > 0):
  792. next = set_pop(all_next)
  793. list_append(worklist, next)
  794. else:
  795. // No new tasks to spawn, so go and check on all running activities
  796. if (dict_len(task_to_result) > 0):
  797. // There are outputs!
  798. Element keys
  799. String task
  800. keys = dict_keys(task_to_result)
  801. while (set_len(keys) > 0):
  802. task = set_pop(keys)
  803. result = task_to_result[task]
  804. Element all_next
  805. all_next = allAssociationDestinations(pm, task_to_activity[task], "Next")
  806. String next
  807. while (set_len(all_next) > 0):
  808. next = set_pop(all_next)
  809. if (read_type(pm, next) == "Decision"):
  810. // Got decision node, so expand immediately
  811. if (result):
  812. list_append(worklist, anAssociationDestination(pm, next, "Then"))
  813. else:
  814. list_append(worklist, anAssociationDestination(pm, next, "Else"))
  815. else:
  816. // Other node, so just append for further processing
  817. list_append(worklist, next)
  818. set_remove(activity_to_task[task_to_activity[task]], task)
  819. dict_delete(task_to_result, task)
  820. dict_delete(task_to_activity, task)
  821. else:
  822. // No finished activities either, so we sleep for some time
  823. sleep(0.1)
  824. // Remove all mock locations again
  825. while (set_len(mock_locations) > 0):
  826. model_delete(get_entry_id(set_pop(mock_locations)))
  827. return !
  828. String function cmd_AL_text(location : String):
  829. if location == "":
  830. return "Success: \n"!
  831. else:
  832. if (element_eq(import_node(location + "/__text"), read_root())):
  833. return "Unknown location: " + cast_string(location)!
  834. else:
  835. return "Success: " + cast_string(import_node(location + "/__text"))!
  836. String function cmd_help():
  837. String result
  838. result = ""
  839. result = result + ("Model operations\n")
  840. result = result + (" model_add -- Add a new model\n")
  841. result = result + (" model_move -- Move a model to a different location\n")
  842. result = result + (" model_modify -- Modify an existing model\n")
  843. result = result + (" model_list -- List all models\n")
  844. result = result + (" model_list_full -- List all models with full info\n")
  845. result = result + (" model_overwrite -- Overwrites a model with an uploaded model, leaving all metadata\n")
  846. result = result + (" model_render -- Render a given model with a specified mapper\n")
  847. result = result + (" verify -- Check whether a model conforms to its metamodel\n")
  848. result = result + ("\n")
  849. result = result + ("Transformation-specific operations\n")
  850. result = result + (" transformation_add_MT -- Initialize a new model transformation\n")
  851. result = result + (" transformation_add_AL -- Initialize a new action language transformation\n")
  852. result = result + (" transformation_add_MANUAL -- Initialize a new manual transformation\n")
  853. result = result + (" transformation_execute -- Execute a transformation on a set of input models\n")
  854. result = result + (" transformation_list -- List all model transformations\n")
  855. result = result + (" transformation_list_full -- List all model transformations with permissions\n")
  856. result = result + (" transformation_detail -- List transformation details\n")
  857. result = result + (" transformation_between -- List all transformations between two metamodels\n")
  858. result = result + ("\n")
  859. result = result + ("Process operations\n")
  860. result = result + (" process_execute -- Execute a process model\n")
  861. result = result + ("\n")
  862. result = result + ("Model permission operations\n")
  863. result = result + (" permission_modify -- Change model permissions\n")
  864. result = result + (" permission_owner -- Change model owner\n")
  865. result = result + (" permission_group -- Change model group\n")
  866. result = result + ("\n")
  867. result = result + ("Group operations\n")
  868. result = result + (" group_create -- Create a group\n")
  869. result = result + (" group_delete -- Delete a group\n")
  870. result = result + (" group_owner_add -- Add group owner\n")
  871. result = result + (" group_owner_delete -- Remove group owner\n")
  872. result = result + (" group_join -- Add someone to your group\n")
  873. result = result + (" group_kick -- Kick someone from your group\n")
  874. result = result + (" group_list -- List all groups you are a member of\n")
  875. result = result + ("\n")
  876. if (is_admin(current_user_id)):
  877. result = result + ("Admin operations\n")
  878. result = result + (" admin_promote -- Promote a user to admin status\n")
  879. result = result + (" admin_demote -- Demote a user to normal status\n")
  880. result = result + ("\n")
  881. result = result + ("General operations\n")
  882. result = result + (" exit -- Kill the current task, while retaining user\n")
  883. return result!
  884. String function cmd_model_move(source : String, target : String):
  885. String source_id
  886. source_id = get_entry_id(source)
  887. if (source_id != ""):
  888. if (allow_write(current_user_id, source_id)):
  889. // Create folders on path to the target
  890. if (element_neq(create_folders(current_user_id, get_foldername(target), False), read_root())):
  891. if (allow_write(current_user_id, get_entry_id(get_foldername(target)))):
  892. // Change location, first the name
  893. instantiate_attribute(core, source_id, "name", target)
  894. // Now the folder links
  895. Element links
  896. links = allIncomingAssociationInstances(core, source_id, "contains")
  897. while (set_len(links) > 0):
  898. model_delete_element(core, set_pop(links))
  899. instantiate_link(core, "contains", "", get_entry_id(get_foldername(target)), source_id)
  900. // Flush caches
  901. dict_add(caches["models"], target, caches["models"][source])
  902. dict_delete(caches["models"], source)
  903. // Done
  904. return "Success"!
  905. else:
  906. return "Write permission denied to: " + target!
  907. else:
  908. return "Write permission denied to: " + target!
  909. else:
  910. return "Write permission denied to: " + source!
  911. else:
  912. return "Model not found: " + source!
  913. String function cmd_model_add(type : String, name : String, code : String):
  914. // Model addition operation, which uses model upload commands of the compiler
  915. String location
  916. String type_id
  917. Element new_model
  918. String new_model_id
  919. Element mm
  920. type_id = get_entry_id(type)
  921. if (type_id != ""):
  922. // Type exists
  923. if (allow_read(current_user_id, type_id)):
  924. if (read_type(core, type_id) != "Folder"):
  925. // And is readable
  926. mm = get_full_model(type_id, get_entry_id("formalisms/SimpleClassDiagrams"))
  927. if (element_neq(mm, read_root())):
  928. if (element_neq(create_folders(current_user_id, get_foldername(name), False), read_root())):
  929. if (allow_write(current_user_id, get_entry_id(get_foldername(name)))):
  930. new_model = compile_model(code, mm)
  931. if (is_physical_string(new_model)):
  932. return "Compilation error: " + cast_string(new_model)!
  933. model_create(new_model, name, type_id, "Model")
  934. return "Success"!
  935. else:
  936. return "Write permission denied to: " + name!
  937. else:
  938. return "Write permission denied to: " + name!
  939. else:
  940. return "Type cannot be typed as formalisms/SimpleClassDiagrams: " + type!
  941. else:
  942. return "Not a model: " + type!
  943. else:
  944. return "Read permission denied to: " + type!
  945. else:
  946. return "Model not found: " + type!
  947. String function cmd_process_execute(process : String, mapping : Element):
  948. // Execute a process model until it reaches termination
  949. String process_id
  950. String name
  951. process_id = get_entry_id(process)
  952. if (process_id != ""):
  953. if (read_type(core, process) != "Folder"):
  954. if (allow_execute(current_user_id, process_id)):
  955. Element pm
  956. pm = get_full_model(process_id, get_entry_id("formalisms/ProcessModel"))
  957. if (element_eq(pm, read_root())):
  958. return "Not a ProcessModel: " + process!
  959. String ver
  960. ver = conformance_scd(pm)
  961. if (ver == "OK"):
  962. Element all_data
  963. String data
  964. Element names
  965. all_data = allInstances(pm, "Data")
  966. names = set_create()
  967. while (set_len(all_data) > 0):
  968. data = set_pop(all_data)
  969. set_add(names, read_attribute(pm, data, "name"))
  970. if (set_len(allIncomingAssociationInstances(pm, data, "Consumes")) > 0):
  971. name = read_attribute(pm, data, "name")
  972. // It is used somewhere for consumption: require read permissions
  973. // First check if this is a defined name
  974. if (dict_in(mapping, name)):
  975. // Yes, it is defined seperately, so we have to check
  976. if (get_entry_id(mapping[name]) != ""):
  977. // Existing model, so have to check
  978. if (bool_not(allow_read(current_user_id, get_entry_id(mapping[name])))):
  979. // Not readable model, while it has to be read: error
  980. return "Read permission denied to: " + cast_string(mapping[name])!
  981. if (read_type(core, mapping[name]) == "Folder"):
  982. return "Not a model: " + cast_string(mapping[name])!
  983. if (set_len(allIncomingAssociationInstances(pm, data, "Produces")) > 0):
  984. name = read_attribute(pm, data, "name")
  985. // It is used somewhere for consumption: require read permissions
  986. // First check if this is a defined name
  987. if (dict_in(mapping, name)):
  988. // Yes, it is defined seperately, so we have to check
  989. if (get_entry_id(mapping[name]) != ""):
  990. // Existing model, so have to check
  991. if (bool_not(allow_write(current_user_id, get_entry_id(mapping[name])))):
  992. // Not readable model, while it has to be read: error
  993. return "Write permission denied to: " + cast_string(mapping[name])!
  994. if (read_type(core, mapping[name]) == "Folder"):
  995. return "Not a model: " + cast_string(mapping[name])!
  996. // Now check all keys in the mapping
  997. Element map_keys
  998. map_keys = dict_keys(mapping)
  999. if (set_len(set_difference(map_keys, names)) > 0):
  1000. return "Signature mismatch: " + cast_string(set_pop(set_difference(map_keys, names)))!
  1001. // Now execute!
  1002. enact_PM(pm, mapping)
  1003. return "Success"!
  1004. else:
  1005. return "Not a valid ProcessModel: " + ver!
  1006. else:
  1007. return "Execute permission denied to: " + process!
  1008. else:
  1009. return "Not a model: " + process!
  1010. else:
  1011. return "Model not found: " + process!
  1012. String function cmd_process_signature(process : String):
  1013. // Execute a process model until it reaches termination
  1014. String process_id
  1015. String result
  1016. Element signature
  1017. Element keys
  1018. String key
  1019. process_id = get_entry_id(process)
  1020. if (process_id != ""):
  1021. if (allow_execute(current_user_id, process_id)):
  1022. if (read_type(core, process_id) == "Model"):
  1023. Element pm
  1024. pm = get_full_model(process_id, get_entry_id("formalisms/ProcessModel"))
  1025. if (element_eq(pm, read_root())):
  1026. return "Not a ProcessModel: " + process!
  1027. result = "Success: "
  1028. signature = PM_signature(pm)
  1029. keys = dict_keys(signature)
  1030. Element found_keys
  1031. found_keys = set_create()
  1032. while (set_len(keys) > 0):
  1033. key = set_pop(keys)
  1034. result = result + key + " : " + cast_string(signature[key]) + "\n"
  1035. if set_in(found_keys, key):
  1036. return "Not a valid ProcessModel: " + process!
  1037. else:
  1038. set_add(found_keys, key)
  1039. return result!
  1040. else:
  1041. return "Not a model: " + process!
  1042. else:
  1043. return "Execute permission denied to: " + process!
  1044. else:
  1045. return "Model not found: " + process!
  1046. String function cmd_transformation_between(source_dict : String, target_dict : String):
  1047. Element result
  1048. Element subresult
  1049. String tag
  1050. String mm
  1051. String mm_id
  1052. Element links
  1053. String link
  1054. Element keys
  1055. if (dict_len(source_dict) + dict_len(target_dict) == 0):
  1056. return "Incorrect format: cannot resolve empty input and output list as this would return all transformations."!
  1057. result = allInstances(core, "Transformation")
  1058. // Iterate over all inputs
  1059. keys = dict_keys(source_dict)
  1060. while (set_len(keys) > 0):
  1061. subresult = set_create()
  1062. tag = set_pop(keys)
  1063. mm = source_dict[tag]
  1064. mm_id = get_entry_id(mm)
  1065. if (mm_id != ""):
  1066. if (read_type(core, mm_id) != "Folder"):
  1067. links = allIncomingAssociationInstances(core, mm_id, "transformInput")
  1068. while (set_len(links) > 0):
  1069. link = set_pop(links)
  1070. if (value_eq(read_attribute(core, link, "name"), tag)):
  1071. // Correct tag, so make transformation a possibility
  1072. set_add(subresult, readAssociationSource(core, link))
  1073. else:
  1074. return "Not a model: " + mm!
  1075. else:
  1076. return "Model not found: " + mm!
  1077. // Got a set of subresults now, which we use to find the overlap
  1078. result = set_overlap(result, subresult)
  1079. keys = dict_keys(target_dict)
  1080. while (set_len(keys) > 0):
  1081. subresult = set_create()
  1082. tag = set_pop(keys)
  1083. mm = target_dict[tag]
  1084. mm_id = get_entry_id(mm)
  1085. if (mm_id != ""):
  1086. if (read_type(core, mm_id) != "Folder"):
  1087. links = allIncomingAssociationInstances(core, mm_id, "transformOutput")
  1088. while (set_len(links) > 0):
  1089. link = set_pop(links)
  1090. if (value_eq(read_attribute(core, link, "name"), tag)):
  1091. // Correct tag, so make transformation a possibility
  1092. set_add(subresult, readAssociationSource(core, link))
  1093. else:
  1094. return "Not a model: " + mm!
  1095. else:
  1096. return "Model not found: " + mm!
  1097. // Got a set of subresults now, which we use to find the overlap
  1098. result = set_overlap(result, subresult)
  1099. String r
  1100. String transformation
  1101. r = "Success: "
  1102. while (set_len(result) > 0):
  1103. transformation = set_pop(result)
  1104. if (allow_read(current_user_id, transformation)):
  1105. r = r + string_join(full_name(transformation), "\n")
  1106. return r!
  1107. String function cmd_model_rendered(model_name : String, mapper_name : String):
  1108. Element trace_links
  1109. String rendered
  1110. String trace_link_id
  1111. String allowed_rendered
  1112. Element trace_links_2
  1113. String trace_link_2_id
  1114. allowed_rendered = "Success: "
  1115. trace_links = allOutgoingAssociationInstances(core, get_entry_id(model_name), "tracability")
  1116. while (set_len(trace_links) > 0):
  1117. trace_link_id = set_pop(trace_links)
  1118. if (value_eq(read_attribute(core, trace_link_id, "type"), "CS_perceptualized")):
  1119. rendered = readAssociationDestination(core, trace_link_id)
  1120. trace_links_2 = allOutgoingAssociationInstances(core, rendered, "tracability")
  1121. while (set_len(trace_links_2) > 0):
  1122. trace_link_2_id = set_pop(trace_links_2)
  1123. if (value_eq(read_attribute(core, readAssociationDestination(core, trace_link_2_id), "name"), mapper_name)):
  1124. if (value_eq(read_attribute(core, trace_link_2_id, "type"), "CS_mapper")):
  1125. allowed_rendered = string_join(allowed_rendered, read_attribute(core, rendered, "name")) + "\n"
  1126. return allowed_rendered!
  1127. String function cmd_model_render(model_name : String, mapper_name : String, rendered_name : String):
  1128. String model_ID
  1129. String mapper_ID
  1130. String tracability_name
  1131. Element inputs
  1132. Element output_map
  1133. Element rendered_model
  1134. Element tracability_model
  1135. Element result
  1136. Element out_links
  1137. String link
  1138. String ID_rendered_M
  1139. String ID_rendered_MM
  1140. String ID_SCD
  1141. String ID_tracability_MM
  1142. String ID_tracability_M
  1143. model_ID = get_entry_id(model_name)
  1144. if (model_ID != ""):
  1145. mapper_ID = get_entry_id(mapper_name)
  1146. if (allow_read(current_user_id, model_ID)):
  1147. if (mapper_ID != ""):
  1148. if (allow_execute(current_user_id, mapper_ID)):
  1149. // Everything is fine; start the actual operation
  1150. // Find metamodel to render to
  1151. tracability_name = "tmp/" + rendered_name
  1152. // Take the abstract syntax model and the previously rendered model
  1153. inputs = dict_create()
  1154. dict_add(inputs, "abstract", model_name)
  1155. dict_add(inputs, "rendered", rendered_name)
  1156. // Fetch the output types
  1157. output_map = dict_create()
  1158. out_links = allOutgoingAssociationInstances(core, mapper_ID, "transformOutput")
  1159. while(set_len(out_links) > 0):
  1160. link = set_pop(out_links)
  1161. dict_add(output_map, read_attribute(core, link, "name"), full_name(readAssociationDestination(core, link)))
  1162. ID_rendered_MM = get_entry_id(output_map["rendered"])
  1163. ID_SCD = get_entry_id("formalisms/SimpleClassDiagrams")
  1164. ID_tracability_MM = get_entry_id("formalisms/Tracability")
  1165. ID_tracability_M = get_entry_id(tracability_name)
  1166. ID_rendered_M = get_entry_id(rendered_name)
  1167. if (ID_rendered_M == ""):
  1168. // Instantiate
  1169. Element rendered
  1170. rendered = get_full_model(ID_rendered_MM, ID_SCD)
  1171. if (element_eq(rendered, read_root())):
  1172. return "Type cannot be typed as formalisms/SimpleClassDiagrams: 'rendered'"!
  1173. rendered_model = instantiate_model(rendered)
  1174. model_create(rendered_model, rendered_name, ID_rendered_MM, "Model")
  1175. // Tracability model won't exist either
  1176. tracability_model = instantiate_model(get_full_model(ID_tracability_MM, ID_SCD))
  1177. model_create(tracability_model, tracability_name, ID_tracability_MM, "Model")
  1178. ID_rendered_M = get_entry_id(rendered_name)
  1179. ID_tracability_M = get_entry_id(tracability_name)
  1180. else:
  1181. // Read out tracability model
  1182. tracability_model = get_full_model(ID_tracability_M, ID_tracability_MM)
  1183. if (element_eq(tracability_model, read_root())):
  1184. return "Tracability model not typed by Tracability metamodel: " + tracability_name!
  1185. // Do the operation itself!
  1186. result = execute_operation(mapper_ID, inputs, tracability_model)
  1187. // Overwrite the previous rendered model
  1188. model_overwrite(result["rendered"], ID_rendered_M, ID_rendered_MM)
  1189. // Tracability updated in-place
  1190. model_overwrite(tracability_model, ID_tracability_M, ID_tracability_MM)
  1191. if (element_eq(tracability_model, read_root())):
  1192. return "Tracability model not typed by Tracability metamodel: " + tracability_name!
  1193. // Link all information in the megamodel
  1194. String tr_link
  1195. tr_link = instantiate_link(core, "tracability", "", get_entry_id(model_name), ID_rendered_M)
  1196. instantiate_attribute(core, tr_link, "type", "CS_perceptualized")
  1197. tr_link = instantiate_link(core, "tracability", "", ID_rendered_M, get_entry_id(mapper_name))
  1198. instantiate_attribute(core, tr_link, "type", "CS_mapper")
  1199. // Output the resulting model
  1200. String value
  1201. value = JSON_print(result["rendered"])
  1202. return ("Success: " + value) !
  1203. else:
  1204. return "Execute permission denied to: " + mapper_name!
  1205. else:
  1206. return "Model not found: " + mapper_name!
  1207. else:
  1208. return "Read permission denied to: " + model_name!
  1209. else:
  1210. return "Model not found: " + model_name!
  1211. Boolean function do_spawn_modify(model_name : String, write : Boolean):
  1212. output("Please edit this model before sending next input: " + model_name)
  1213. input()
  1214. return False!
  1215. String function do_spawn_activity(transformation_id : String, tracability_name : String, inputs : Element, outputs : Element, output_map : Element):
  1216. Element lst
  1217. Element returnvalue
  1218. String taskname
  1219. lst = list_create()
  1220. returnvalue = set_create()
  1221. list_append(lst, returnvalue)
  1222. list_append(lst, transformation_id)
  1223. list_append(lst, tracability_name)
  1224. list_append(lst, inputs)
  1225. list_append(lst, outputs)
  1226. list_append(lst, output_map)
  1227. taskname = spawn(spawn_activity, lst)
  1228. output("Spawned activity on task: " + taskname)
  1229. while (set_len(returnvalue) == 0):
  1230. sleep(0.1)
  1231. output("Finished task: " + taskname)
  1232. return set_pop(returnvalue)!
  1233. Void function spawn_activity(returnvalue : Element, transformation_id : String, tracability_name : String, inputs : Element, outputs : Element, output_map : Element):
  1234. Element result
  1235. Element keys
  1236. String key
  1237. if (read_type(core, transformation_id) == "ActionLanguage"):
  1238. output("Success: ready for AL execution")
  1239. elif (read_type(core, transformation_id) == "ManualOperation"):
  1240. output("Success: ready for MANUAL execution")
  1241. else:
  1242. output("Success: ready for MT execution")
  1243. // Do tracability
  1244. Element tracability_model
  1245. if (tracability_name != ""):
  1246. // Check if exists
  1247. if (get_entry_id(tracability_name) == ""):
  1248. // No, so create
  1249. tracability_model = instantiate_model(get_full_model(get_entry_id("formalisms/Tracability"), get_entry_id("formalisms/SimpleClassDiagrams")))
  1250. model_create(tracability_model, tracability_name, get_entry_id("formalisms/Tracability"), "Model")
  1251. else:
  1252. // Yes, so read out
  1253. tracability_model = get_full_model(get_entry_id(tracability_name), get_entry_id("formalisms/Tracability"))
  1254. else:
  1255. tracability_model = read_root()
  1256. result = execute_operation(transformation_id, inputs, tracability_model)
  1257. // Flush tracability again, just to be sure
  1258. if (tracability_name != ""):
  1259. model_overwrite(tracability_model, get_entry_id(tracability_name), get_entry_id("formalisms/Tracability"))
  1260. // Now write out the models again
  1261. if (is_physical_string(result)):
  1262. // Something went wrong!
  1263. set_add(returnvalue, result)
  1264. output("Nothing to modify")
  1265. else:
  1266. keys = dict_keys(outputs)
  1267. while (set_len(keys) > 0):
  1268. key = set_pop(keys)
  1269. if (get_entry_id(outputs[key]) == ""):
  1270. // New model
  1271. model_create(result[key], outputs[key], get_entry_id(output_map[key]), "Model")
  1272. else:
  1273. model_overwrite(result[key], get_entry_id(outputs[key]), get_entry_id(output_map[key]))
  1274. set_add(returnvalue, "Success")
  1275. output("Success")
  1276. while (other_has_output(get_taskname())):
  1277. sleep(1)
  1278. return!
  1279. String function cmd_transformation_execute(transformation_name : String, source_models : Element, target_models : Element, tracability_name : String):
  1280. // Execute a transformation, whatever type it is
  1281. // First we detect the type, so we know how to prepare for invocation
  1282. String transformation_id
  1283. String exact_type
  1284. Element sources
  1285. Element targets
  1286. String source
  1287. String target
  1288. Element inputs
  1289. Element outputs
  1290. Element output_map
  1291. Element trace_links
  1292. String target_model_name
  1293. String source_model_name
  1294. String source_model_ID
  1295. String assoc_name
  1296. transformation_id = get_entry_id(transformation_name)
  1297. if (transformation_id != ""):
  1298. if (allow_execute(current_user_id, transformation_id)):
  1299. if (is_nominal_instance(core, transformation_id, "Transformation")):
  1300. // Read out source and target links
  1301. inputs = dict_create()
  1302. sources = allOutgoingAssociationInstances(core, transformation_id, "transformInput")
  1303. while (set_len(sources) > 0):
  1304. source = set_pop(sources)
  1305. assoc_name = read_attribute(core, source, "name")
  1306. if (dict_in(source_models, assoc_name)):
  1307. source_model_name = source_models[assoc_name]
  1308. else:
  1309. return "Source model not bound: " + assoc_name!
  1310. source_model_ID = get_entry_id(source_model_name)
  1311. if (source_model_ID != ""):
  1312. if (allow_read(current_user_id, source_model_ID)):
  1313. if (read_type(core, source_model_ID) != "Folder"):
  1314. // Check for conformance to the requested metamodel
  1315. dict_add(inputs, assoc_name, source_model_name)
  1316. continue!
  1317. else:
  1318. return "Not a model: " + source_model_name!
  1319. else:
  1320. return "Read permission denied to: " + source_model_name!
  1321. else:
  1322. return "Model not found: " + source_model_name!
  1323. Element diff
  1324. diff = set_difference(dict_keys(source_models), dict_keys(inputs))
  1325. if (set_len(diff) > 0):
  1326. return "Source model not bound: " + cast_string(set_pop(diff))!
  1327. targets = allOutgoingAssociationInstances(core, transformation_id, "transformOutput")
  1328. outputs = dict_create()
  1329. output_map = dict_create()
  1330. while (set_len(targets) > 0):
  1331. target = set_pop(targets)
  1332. assoc_name = read_attribute(core, target, "name")
  1333. if (dict_in(target_models, assoc_name)):
  1334. target_model_name = target_models[assoc_name]
  1335. if (get_entry_id(target_model_name) == ""):
  1336. // Doesn't exist yet, so we can easily create
  1337. if (get_entry_id(get_foldername(target_model_name)) == ""):
  1338. return "Model not found: " + get_foldername(target_model_name)!
  1339. else:
  1340. if (allow_write(current_user_id, get_entry_id(get_foldername(target_model_name)))):
  1341. dict_add(output_map, assoc_name, full_name(readAssociationDestination(core, target)))
  1342. dict_add(outputs, assoc_name, target_model_name)
  1343. else:
  1344. return "Write permission denied to: " + get_foldername(target_model_name)!
  1345. else:
  1346. // Already exists, so we need to check for write access
  1347. if (read_type(core, get_entry_id(target_model_name)) == "Folder"):
  1348. // Is a folder, so reject
  1349. return "Not a model: " + target_model_name!
  1350. else:
  1351. if (allow_write(current_user_id, get_entry_id(target_model_name))):
  1352. dict_add(output_map, assoc_name, read_attribute(core, readAssociationDestination(core, target), "name"))
  1353. dict_add(outputs, assoc_name, target_model_name)
  1354. else:
  1355. return "Write permission denied to: " + target_model_name!
  1356. diff = set_difference(dict_keys(target_models), dict_keys(outputs))
  1357. if (set_len(diff) > 0):
  1358. return "Target model not bound: " + cast_string(set_pop(diff))!
  1359. return do_spawn_activity(transformation_id, tracability_name, inputs, outputs, output_map)!
  1360. else:
  1361. return "Execute permission denied to: " + transformation_name!
  1362. else:
  1363. return "Execute permission denied to: " + transformation_name!
  1364. else:
  1365. return "Model not found: " + transformation_name!
  1366. String function cmd_verify(model_name : String, metamodel_name : String):
  1367. // Check whether a model conforms to its specification (with the selected type mapping)
  1368. String model_id
  1369. String result
  1370. Element inputs
  1371. model_id = get_entry_id(model_name)
  1372. if (model_id != ""):
  1373. if (allow_read(current_user_id, model_id)):
  1374. if (read_type(core, model_id) != "Folder"):
  1375. if (get_entry_id(metamodel_name) != ""):
  1376. if (read_type(core, get_entry_id(metamodel_name)) != "Folder"):
  1377. if (allow_read(current_user_id, get_entry_id(metamodel_name))):
  1378. Element m
  1379. m = get_full_model(get_entry_id(model_name), get_entry_id(metamodel_name))
  1380. if (element_eq(m, read_root())):
  1381. return "Conformance hierarchy unknown for: " + model_name!
  1382. return string_join("Success: ", conformance_scd(m))!
  1383. else:
  1384. return "Read permission denied to: " + metamodel_name!
  1385. else:
  1386. return "Not a model: " + metamodel_name!
  1387. else:
  1388. return "Model not found: " + metamodel_name!
  1389. else:
  1390. return "Not a model: " + model_name!
  1391. else:
  1392. return "Read permission denied to: " + model_name!
  1393. else:
  1394. return "Model not found: " + model_name!
  1395. String function cmd_model_overwrite(model_name : String, metamodel_name : String):
  1396. // Overwrites an existing model without changing any metadata
  1397. String model_id
  1398. String type_id
  1399. Element new_model
  1400. model_id = get_entry_id(model_name)
  1401. if (model_id != ""):
  1402. if (allow_write(current_user_id, model_id)):
  1403. type_id = anAssociationDestination(core, model_id, "instanceOf")
  1404. if (allow_read(current_user_id, type_id)):
  1405. if (read_type(core, type_id) != "Folder"):
  1406. Element mm
  1407. mm = get_full_model(get_entry_id(metamodel_name), get_entry_id("formalisms/SimpleClassDiagrams"))
  1408. if (element_eq(mm, read_root())):
  1409. return "Type cannot be typed as formalisms/SimpleClassDiagrams: " + metamodel_name!
  1410. output("Waiting for model constructors...")
  1411. new_model = compile_model(input(), mm)
  1412. if (is_physical_string(new_model)):
  1413. return "Compilation error: " + cast_string(new_model)!
  1414. model_overwrite(new_model, model_id, get_entry_id(metamodel_name))
  1415. return "Success"!
  1416. else:
  1417. return "Not a model: " + full_name(type_id)!
  1418. else:
  1419. return "Read permission denied to: " + full_name(type_id)!
  1420. else:
  1421. return "Write permission denied to: " + model_name!
  1422. else:
  1423. return "Model not found: " + model_name!
  1424. String function cmd_model_modify(model_name : String, metamodel_name : String):
  1425. // Model modify operation, which uses the mini_modify.alc operations, though with extensions for access control
  1426. String model_id
  1427. String type_id
  1428. model_id = get_entry_id(model_name)
  1429. if (model_id != ""):
  1430. if (allow_read(current_user_id, model_id)):
  1431. type_id = get_entry_id(metamodel_name)
  1432. if (type_id != ""):
  1433. if (allow_read(current_user_id, type_id)):
  1434. if (read_type(core, model_id) != "Folder"):
  1435. if (read_type(core, type_id) != "Folder"):
  1436. Element new_model
  1437. new_model = get_full_model(model_id, type_id)
  1438. if (element_eq(new_model, read_root())):
  1439. return "Conformance hierarchy unknown for: " + model_name!
  1440. modify(new_model, allow_write(current_user_id, model_id))
  1441. if (allow_write(current_user_id, model_id)):
  1442. // Overwrite the modified model
  1443. model_overwrite(new_model, model_id, type_id)
  1444. return "Success"!
  1445. else:
  1446. return "Not a model: " + metamodel_name!
  1447. else:
  1448. return "Not a model: " + model_name!
  1449. else:
  1450. return "Read permission denied to: " + metamodel_name!
  1451. else:
  1452. return "Model not found: " + metamodel_name!
  1453. else:
  1454. return "Read permission denied to: " + model_name!
  1455. else:
  1456. return "Model not found: " + model_name!
  1457. Void function model_delete(model_id : String):
  1458. if (read_type(core, model_id) == "Folder"):
  1459. // Folder, so delete all containing elements as well!
  1460. Element elems
  1461. elems = allAssociationDestinations(core, model_id, "contains")
  1462. while (set_len(elems) > 0):
  1463. model_delete(set_pop(elems))
  1464. // Delete self
  1465. model_delete_element(core, model_id)
  1466. else:
  1467. // Only delete this element
  1468. model_delete_element(core, model_id)
  1469. return!
  1470. String function cmd_model_delete(model_name : String):
  1471. String model_id
  1472. model_id = get_entry_id(model_name)
  1473. if (model_id != ""):
  1474. if (allow_write(current_user_id, model_id)):
  1475. model_delete(model_id)
  1476. return "Success"!
  1477. else:
  1478. return "Write permission denied to: " + model_name!
  1479. else:
  1480. return "Model not found: " + model_name!
  1481. String function cmd_read_permissions(location : String):
  1482. if (get_entry_id(location) != ""):
  1483. String result
  1484. result = "Success: "
  1485. if (allow_read(current_user_id, get_entry_id(location))):
  1486. result = result + "R"
  1487. if (allow_write(current_user_id, get_entry_id(location))):
  1488. result = result + "W"
  1489. return result!
  1490. else:
  1491. return "Unknown location: " + location!
  1492. String function cmd_model_list(location : String):
  1493. // List all models
  1494. if (get_entry_id(location) != ""):
  1495. if (allow_read(current_user_id, get_entry_id(location))):
  1496. Element models
  1497. String result
  1498. String m
  1499. result = "Success: "
  1500. models = allAssociationDestinations(core, get_entry_id(location), "contains")
  1501. while (set_len(models) > 0):
  1502. m = set_pop(models)
  1503. if (is_nominal_instance(core, m, "Folder")):
  1504. result = result + (get_filename(read_attribute(core, m, "name")) + "/\n")
  1505. else:
  1506. result = result + (get_filename(read_attribute(core, m, "name")) + "\n")
  1507. return result!
  1508. else:
  1509. return "Read permission denied to: " + location!
  1510. else:
  1511. return "Unknown location: " + location!
  1512. String function cmd_model_list_full(location : String):
  1513. // List all models with full info
  1514. if (get_entry_id(location) != ""):
  1515. if (allow_read(current_user_id, get_entry_id(location))):
  1516. Element models
  1517. String m
  1518. String permissions
  1519. String owner
  1520. String group
  1521. String name
  1522. String type
  1523. String result
  1524. result = "Success: "
  1525. models = allAssociationDestinations(core, get_entry_id(location), "contains")
  1526. while (set_len(models) > 0):
  1527. m = set_pop(models)
  1528. permissions = read_attribute(core, m, "permissions")
  1529. owner = read_attribute(core, anAssociationDestination(core, m, "owner"), "name")
  1530. group = read_attribute(core, anAssociationDestination(core, m, "group"), "name")
  1531. if (is_nominal_instance(core, m, "Folder")):
  1532. name = get_filename(read_attribute(core, m, "name")) + "/"
  1533. else:
  1534. name = get_filename(read_attribute(core, m, "name"))
  1535. result = result + permissions + " " + owner + " " + group + " " + name + "\n"
  1536. return result!
  1537. else:
  1538. return "Read permission denied to: " + location!
  1539. else:
  1540. return "Unknown location: " + location!
  1541. String function cmd_transformation_add_MANUAL(source_models : Element, target_models : Element, operation_name : String):
  1542. return transformation_add(source_models, target_models, operation_name, "manual")!
  1543. String function cmd_transformation_add_AL(source_models : Element, target_models : Element, operation_name : String):
  1544. return transformation_add(source_models, target_models, operation_name, "actionlanguage")!
  1545. String function transformation_add(source_models : Element, target_models : Element, operation_name : String, operation_type : String):
  1546. // Add a manual transformation model
  1547. String model_id
  1548. Element models
  1549. Element source
  1550. Element target
  1551. String name
  1552. Element all_formalisms
  1553. Element formalism_map
  1554. Element merged_formalism
  1555. String merged_formalism_id
  1556. String source_formalism_id
  1557. String tracability_link
  1558. Element fused
  1559. Element keys
  1560. String key
  1561. Element mm
  1562. source = dict_create()
  1563. target = dict_create()
  1564. all_formalisms = set_create()
  1565. formalism_map = set_create()
  1566. fused = set_create()
  1567. keys = dict_keys(source_models)
  1568. while (set_len(keys) > 0):
  1569. key = set_pop(keys)
  1570. name = source_models[key]
  1571. model_id = get_entry_id(name)
  1572. if (model_id != ""):
  1573. if (allow_read(current_user_id, model_id)):
  1574. if (bool_not(dict_in(source, key))):
  1575. dict_add(source, key, model_id)
  1576. if (read_type(core, model_id) == "Folder"):
  1577. return "Not a model: " + name!
  1578. mm = get_full_model(model_id, get_entry_id("formalisms/SimpleClassDiagrams"))
  1579. if (element_eq(mm, read_root())):
  1580. return "Type cannot be typed as formalisms/SimpleClassDiagrams: " + key!
  1581. if (bool_not(set_in(fused, key))):
  1582. set_add_node(formalism_map, create_tuple(key, mm))
  1583. set_add(fused, key)
  1584. if (bool_not(set_in(all_formalisms, model_id))):
  1585. set_add(all_formalisms, model_id)
  1586. else:
  1587. return "Name already selected for source: " + key!
  1588. else:
  1589. return "Read permission denied to: " + name!
  1590. else:
  1591. return "Model not found: " + name!
  1592. keys = dict_keys(target_models)
  1593. while (set_len(keys) > 0):
  1594. key = set_pop(keys)
  1595. name = target_models[key]
  1596. model_id = get_entry_id(name)
  1597. if (model_id != ""):
  1598. if (allow_read(current_user_id, model_id)):
  1599. if (bool_not(dict_in(target, key))):
  1600. if (read_type(core, model_id) == "Folder"):
  1601. return "Not a model: " + name!
  1602. if (dict_in(source, key)):
  1603. if (value_neq(source[key], model_id)):
  1604. return "Input and output signature differ for same key: " + key!
  1605. dict_add(target, key, model_id)
  1606. mm = get_full_model(model_id, get_entry_id("formalisms/SimpleClassDiagrams"))
  1607. if (element_eq(mm, read_root())):
  1608. return "Type cannot be typed as formalisms/SimpleClassDiagrams: " + key!
  1609. if (bool_not(set_in(fused, key))):
  1610. set_add_node(formalism_map, create_tuple(key, mm))
  1611. set_add(fused, key)
  1612. if (bool_not(set_in(all_formalisms, model_id))):
  1613. set_add(all_formalisms, model_id)
  1614. else:
  1615. return "Name already selected for target: " + key!
  1616. else:
  1617. return "Read permission denied to: " + name!
  1618. else:
  1619. return "Model not found: " + name!
  1620. if (get_entry_id(operation_name) != ""):
  1621. model_delete(get_entry_id(operation_name))
  1622. // Write out a merged metamodel containing all these models: this is the MM for the manual operation
  1623. // New location is available, so write
  1624. if (dict_len(source_models) + dict_len(target_models) > 0):
  1625. merged_formalism = model_fuse(formalism_map)
  1626. model_create(merged_formalism, "merged/" + operation_name, get_entry_id("formalisms/SimpleClassDiagrams"), "Model")
  1627. do_spawn_modify("merged/" + operation_name, True)
  1628. merged_formalism = get_full_model(get_entry_id("merged/" + operation_name), get_entry_id("formalisms/SimpleClassDiagrams"))
  1629. if (operation_type == "manual"):
  1630. // Finished with all information, now create the model itself!
  1631. Element m
  1632. m = get_full_model(get_entry_id("formalisms/ManualOperation"), get_entry_id("formalisms/SimpleClassDiagrams"))
  1633. model_create(instantiate_model(m), operation_name, get_entry_id("formalisms/ManualOperation"), "ManualOperation")
  1634. model_id = get_entry_id(operation_name)
  1635. elif (operation_type == "actionlanguage"):
  1636. // Finished with all information, now create the model itself!
  1637. output("Waiting for code constructors...")
  1638. Element compiled
  1639. compiled = compile_code(input())
  1640. if (is_physical_string(compiled)):
  1641. return "Compilation error: " + cast_string(compiled)!
  1642. model_create(compiled, operation_name, get_entry_id("formalisms/ActionLanguage"), "ActionLanguage")
  1643. model_id = get_entry_id(operation_name)
  1644. if (dict_len(source_models) + dict_len(target_models) > 0):
  1645. merged_formalism_id = get_entry_id("merged/" + operation_name)
  1646. // Add tracability links at this level
  1647. while (set_len(all_formalisms) > 0):
  1648. source_formalism_id = set_pop(all_formalisms)
  1649. tracability_link = instantiate_link(core, "tracability", "", merged_formalism_id, source_formalism_id)
  1650. instantiate_attribute(core, tracability_link, "type", "merged")
  1651. tracability_link = instantiate_link(core, "tracability", "", model_id, merged_formalism_id)
  1652. instantiate_attribute(core, tracability_link, "type", "operatesOn")
  1653. // Extend metadata with info on source and target
  1654. String link
  1655. String dst
  1656. keys = dict_keys(source)
  1657. while (set_len(keys) > 0):
  1658. key = set_pop(keys)
  1659. link = instantiate_link(core, "transformInput", "", model_id, source[key])
  1660. instantiate_attribute(core, link, "name", key)
  1661. keys = dict_keys(target)
  1662. while (set_len(keys) > 0):
  1663. key = set_pop(keys)
  1664. link = instantiate_link(core, "transformOutput", "", model_id, target[key])
  1665. instantiate_attribute(core, link, "name", key)
  1666. return "Success"!
  1667. String function cmd_transformation_add_MT(source_models : Element, target_models : Element, operation_name : String):
  1668. // Add a model transformation model
  1669. // Just a usual model instantiation, but need to add the source and target links based on user info
  1670. String ramified_metamodel_id
  1671. Element ramified_metamodel
  1672. String model_id
  1673. Element models
  1674. Element links
  1675. String link_id
  1676. Element supported
  1677. Element source
  1678. Element target
  1679. String name
  1680. String new_model_id
  1681. String merged_link_id
  1682. Element links_merged
  1683. String merged_formalism
  1684. Element keys
  1685. String key
  1686. Element mm
  1687. Element to_ramify
  1688. String source_formalism_id
  1689. String merged_formalism_id
  1690. String tracability_link
  1691. source = dict_create()
  1692. target = dict_create()
  1693. to_ramify = set_create()
  1694. if (dict_len(source_models) + dict_len(target_models) == 0):
  1695. return "Model transformation needs at least one formalism in its input or output signature!"!
  1696. keys = dict_keys(source_models)
  1697. while (set_len(keys) > 0):
  1698. key = set_pop(keys)
  1699. name = source_models[key]
  1700. model_id = get_entry_id(name)
  1701. if (model_id != ""):
  1702. if (allow_read(current_user_id, model_id)):
  1703. // Check whether or not it is formalisms/SimpleClassDiagrams
  1704. if (read_type(core, model_id) == "Folder"):
  1705. return "Not a model: " + name!
  1706. mm = get_full_model(model_id, get_entry_id("formalisms/SimpleClassDiagrams"))
  1707. if (element_neq(mm, read_root())):
  1708. if (bool_not(dict_in(source, key))):
  1709. dict_add(source, key, model_id)
  1710. set_add_node(to_ramify, create_tuple(key, mm))
  1711. else:
  1712. return "Name was already assigned a metamodel: " + key!
  1713. else:
  1714. return "Type cannot be typed as formalisms/SimpleClassDiagrams: " + name!
  1715. else:
  1716. return "Read permission denied to: " + name!
  1717. else:
  1718. return "Model not found: " + name!
  1719. keys = dict_keys(target_models)
  1720. while (set_len(keys) > 0):
  1721. key = set_pop(keys)
  1722. name = target_models[key]
  1723. model_id = get_entry_id(name)
  1724. if (model_id != ""):
  1725. if (allow_read(current_user_id, model_id)):
  1726. if (read_type(core, model_id) == "Folder"):
  1727. return "Not a model: " + name!
  1728. mm = get_full_model(model_id, get_entry_id("formalisms/SimpleClassDiagrams"))
  1729. if (element_neq(mm, read_root())):
  1730. if (bool_not(dict_in(target, key))):
  1731. if (dict_in(source, key)):
  1732. if (value_eq(model_id, source[key])):
  1733. dict_add(target, key, model_id)
  1734. // Don't add to to_ramify, as it is already in there!
  1735. else:
  1736. return "Input and output signature differ for same key: " + key!
  1737. else:
  1738. dict_add(target, key, model_id)
  1739. set_add_node(to_ramify, create_tuple(key, mm))
  1740. else:
  1741. return "Name was already assigned a metamodel: " + key!
  1742. else:
  1743. return "Type cannot be typed as formalisms/SimpleClassDiagrams: " + name!
  1744. else:
  1745. return "Read permission denied to: " + name!
  1746. else:
  1747. return "Model not found: " + name!
  1748. merged_formalism = model_fuse(to_ramify)
  1749. model_create(merged_formalism, "merged/" + operation_name, get_entry_id("formalisms/SimpleClassDiagrams"), "Model")
  1750. do_spawn_modify("merged/" + operation_name, True)
  1751. merged_formalism = get_full_model(get_entry_id("merged/" + operation_name), get_entry_id("formalisms/SimpleClassDiagrams"))
  1752. model_overwrite(merged_formalism, get_entry_id("merged/" + operation_name), get_entry_id("formalisms/SimpleClassDiagrams"))
  1753. ramified_metamodel = ramify(merged_formalism)
  1754. model_create(ramified_metamodel, "RAMified/" + operation_name, get_entry_id("formalisms/SimpleClassDiagrams"), "Model")
  1755. ramified_metamodel_id = get_entry_id("RAMified/" + operation_name)
  1756. // Now use the RAMified model to create the instance
  1757. if (get_entry_id(operation_name) != ""):
  1758. model_delete(get_entry_id(operation_name))
  1759. String new_model
  1760. // Finished with all information, now create the model itself!
  1761. output("Waiting for model constructors...")
  1762. new_model = compile_model(input(), get_full_model(ramified_metamodel_id, get_entry_id("formalisms/SimpleClassDiagrams")))
  1763. if (is_physical_string(new_model)):
  1764. return "Compilation error: " + cast_string(new_model)!
  1765. model_create(new_model, operation_name, ramified_metamodel_id, "ModelTransformation")
  1766. model_id = get_entry_id(operation_name)
  1767. // Write out a merged metamodel containing all these models: this is the MM for the manual operation
  1768. // New location is available, so write
  1769. model_create(merged_formalism, "merged/" + operation_name, get_entry_id("formalisms/SimpleClassDiagrams"), "Model")
  1770. merged_formalism_id = get_entry_id("merged/" + operation_name)
  1771. // Add tracability links at this level
  1772. tracability_link = instantiate_link(core, "tracability", "", model_id, merged_formalism_id)
  1773. instantiate_attribute(core, tracability_link, "type", "operatesOn")
  1774. // Add tracability links at this level
  1775. tracability_link = instantiate_link(core, "tracability", "", merged_formalism_id, ramified_metamodel_id)
  1776. instantiate_attribute(core, tracability_link, "type", "RAMified")
  1777. // Extend metadata with info on source and target
  1778. String link
  1779. String dst
  1780. keys = dict_keys(source)
  1781. while (set_len(keys) > 0):
  1782. key = set_pop(keys)
  1783. dst = source[key]
  1784. link = instantiate_link(core, "transformInput", "", model_id, dst)
  1785. instantiate_attribute(core, link, "name", key)
  1786. keys = dict_keys(target)
  1787. while (set_len(keys) > 0):
  1788. key = set_pop(keys)
  1789. dst = target[key]
  1790. link = instantiate_link(core, "transformOutput", "", model_id, dst)
  1791. instantiate_attribute(core, link, "name", key)
  1792. return "Success"!
  1793. String function cmd_permission_modify(model_name : String, permissions : String):
  1794. Integer permission
  1795. String model_id
  1796. model_id = get_entry_id(model_name)
  1797. if (model_id != ""):
  1798. if (get_relation_to_model(current_user_id, model_id) == 0):
  1799. Boolean fail
  1800. Integer i
  1801. i = 0
  1802. fail = False
  1803. if (string_len(permissions) != 3):
  1804. fail = True
  1805. while (i < 3):
  1806. permission = cast_integer(string_get(permissions, i))
  1807. if (bool_or(permission < 0, permission > 2)):
  1808. fail = True
  1809. break!
  1810. i = i + 1
  1811. if (bool_not(fail)):
  1812. unset_attribute(core, model_id, "permissions")
  1813. instantiate_attribute(core, model_id, "permissions", permissions)
  1814. return "Success"!
  1815. else:
  1816. return "Incorrect format: must be a string of three characters, with each character being a digit between 0 and 2"!
  1817. else:
  1818. return "User permission denied to: " + model_name!
  1819. else:
  1820. return "Model not found: " + model_name!
  1821. String function cmd_permission_owner(model_name : String, new_user_name : String):
  1822. String model_id
  1823. String new_user_id
  1824. model_id = get_entry_id(model_name)
  1825. if (model_id != ""):
  1826. if (bool_or(get_relation_to_model(current_user_id, model_id) == 0, is_admin(current_user_id))):
  1827. new_user_id = get_user_id(new_user_name)
  1828. if (new_user_id != ""):
  1829. model_delete_element(core, set_pop(allOutgoingAssociationInstances(core, model_id, "owner")))
  1830. instantiate_link(core, "owner", "", model_id, new_user_id)
  1831. return "Success"!
  1832. else:
  1833. return "Unknown user: " + new_user_name!
  1834. else:
  1835. return "User permission denied to: " + model_name!
  1836. else:
  1837. return "Model not found: " + model_name!
  1838. String function cmd_permission_group(model_name : String, new_group_name : String):
  1839. String model_id
  1840. String group_id
  1841. model_id = get_entry_id(model_name)
  1842. if (model_id != ""):
  1843. if (bool_or(get_relation_to_model(current_user_id, model_id) == 0, is_admin(current_user_id))):
  1844. group_id = get_group_id(new_group_name)
  1845. if (group_id != ""):
  1846. model_delete_element(core, set_pop(allOutgoingAssociationInstances(core, model_id, "group")))
  1847. instantiate_link(core, "group", "", model_id, group_id)
  1848. return "Success"!
  1849. else:
  1850. return "Unknown group: " + new_group_name!
  1851. else:
  1852. return "User permission denied to: " + model_name!
  1853. else:
  1854. return "Model not found: " + model_name!
  1855. String function cmd_group_create(group_name : String):
  1856. // Create a new group and become its owner
  1857. String group_id
  1858. String name
  1859. group_id = get_group_id(group_name)
  1860. if (group_id == ""):
  1861. group_id = instantiate_node(core, "Group", "")
  1862. instantiate_attribute(core, group_id, "name", group_name)
  1863. instantiate_link(core, "belongsTo", "", current_user_id, group_id)
  1864. instantiate_link(core, "owner", "", group_id, current_user_id)
  1865. return "Success"!
  1866. else:
  1867. return "Group exists: " + group_name!
  1868. String function cmd_group_delete(group_name : String):
  1869. // Delete an existing group
  1870. String group_id
  1871. group_id = get_group_id(group_name)
  1872. if (group_id != ""):
  1873. if (allow_group_modify(current_user_id, group_id)):
  1874. model_delete_element(core, group_id)
  1875. return "Success"!
  1876. else:
  1877. return "Group permission denied to: " + group_name!
  1878. else:
  1879. return "Unknown group: " + group_name!
  1880. String function cmd_group_owner_add(group_name : String, other_user_name : String):
  1881. // Add an owner to your group
  1882. String group_id
  1883. String other_user_id
  1884. group_id = get_group_id(group_name)
  1885. if (group_id != ""):
  1886. if (allow_group_modify(current_user_id, group_id)):
  1887. other_user_id = get_user_id(other_user_name)
  1888. if (other_user_id != ""):
  1889. if (bool_not(set_in(allAssociationDestinations(core, group_id, "owner"), other_user_id))):
  1890. instantiate_link(core, "owner", "", group_id, other_user_id)
  1891. if (bool_not(set_in(allAssociationDestinations(core, other_user_id, "belongsTo"), group_id))):
  1892. instantiate_link(core, "belongsTo", "", other_user_id, group_id)
  1893. return "Success"!
  1894. else:
  1895. return "Success"!
  1896. else:
  1897. return "Unknown user: " + other_user_name!
  1898. else:
  1899. return "Group permission denied to: " + group_name!
  1900. else:
  1901. return "Unknown group: " + group_name!
  1902. String function cmd_group_owner_delete(group_name : String, other_user_name : String):
  1903. // Remove an owner from your group
  1904. String group_id
  1905. String other_user_id
  1906. group_id = get_group_id(group_name)
  1907. if (group_id != ""):
  1908. if (allow_group_modify(current_user_id, group_id)):
  1909. other_user_id = get_user_id(other_user_name)
  1910. if (other_user_id != ""):
  1911. if (set_in(allAssociationDestinations(core, other_user_id, "belongsTo"), group_id)):
  1912. if (set_in(allAssociationDestinations(core, group_id, "owner"), other_user_id)):
  1913. Element overlap
  1914. overlap = set_overlap(allIncomingAssociationInstances(core, other_user_id, "owner"), allOutgoingAssociationInstances(core, group_id, "owner"))
  1915. model_delete_element(core, set_pop(overlap))
  1916. return "Success"!
  1917. else:
  1918. return "Success"!
  1919. else:
  1920. return "User is not a member of the group!"!
  1921. else:
  1922. return "Unknown user: " + other_user_name!
  1923. else:
  1924. return "Group permission denied to: " + group_name!
  1925. else:
  1926. return "Unknown group: " + group_name!
  1927. String function cmd_group_join(group_name : String, other_user_name : String):
  1928. // Add someone to your group
  1929. String group_id
  1930. String other_user_id
  1931. group_id = get_group_id(group_name)
  1932. if (group_id != ""):
  1933. if (allow_group_modify(current_user_id, group_id)):
  1934. other_user_id = get_user_id(other_user_name)
  1935. if (other_user_id != ""):
  1936. if (bool_not(set_in(allAssociationDestinations(core, other_user_id, "belongsTo"), group_id))):
  1937. instantiate_link(core, "belongsTo", "", other_user_id, group_id)
  1938. return "Success"!
  1939. else:
  1940. return "User exists: " + other_user_name!
  1941. else:
  1942. return "Unknown user: " + other_user_name!
  1943. else:
  1944. return "Group permission denied to: " + group_name!
  1945. else:
  1946. return "Unknown group: " + group_name!
  1947. String function cmd_group_kick(group_name : String, other_user_name : String):
  1948. // Remove someone from your group
  1949. String group_id
  1950. String other_user_id
  1951. group_id = get_group_id(group_name)
  1952. if (group_id != ""):
  1953. if (allow_group_modify(current_user_id, group_id)):
  1954. other_user_id = get_user_id(other_user_name)
  1955. if (other_user_id != ""):
  1956. Element overlap
  1957. if (set_in(allAssociationDestinations(core, other_user_id, "belongsTo"), group_id)):
  1958. overlap = set_overlap(allIncomingAssociationInstances(core, group_id, "belongsTo"), allOutgoingAssociationInstances(core, other_user_id, "belongsTo"))
  1959. model_delete_element(core,set_pop(overlap))
  1960. // Check if user was an owner as well
  1961. if (set_in(allAssociationDestinations(core, group_id, "owner"), other_user_id)):
  1962. overlap = set_overlap(allIncomingAssociationInstances(core, other_user_id, "owner"), allOutgoingAssociationInstances(core, group_id, "owner"))
  1963. model_delete_element(core, set_pop(overlap))
  1964. return "Success"!
  1965. else:
  1966. return "User is not a member of the group!"!
  1967. else:
  1968. return "Unknown user: " + other_user_name!
  1969. else:
  1970. return "Group permission denied to: " + group_name!
  1971. else:
  1972. return "Unknown group: " + group_name!
  1973. String function cmd_group_list():
  1974. // List all groups you are a member of (and whether you are admin or not!)
  1975. Element groups
  1976. String group_id
  1977. String admin
  1978. String result
  1979. result = "Success: "
  1980. groups = allAssociationDestinations(core, current_user_id, "belongsTo")
  1981. while (set_len(groups) > 0):
  1982. group_id = set_pop(groups)
  1983. if (set_in(allAssociationDestinations(core, group_id, "owner"), current_user_id)):
  1984. admin = " A "
  1985. else:
  1986. admin = " "
  1987. result = result + (string_join(admin, read_attribute(core, group_id, "name")) + "\n")
  1988. return result!
  1989. String function cmd_admin_promote(other_user_name : String):
  1990. // Promote a user to admin status
  1991. if (is_admin(current_user_id)):
  1992. String other_user_id
  1993. other_user_id = get_user_id(other_user_name)
  1994. if (other_user_id != ""):
  1995. unset_attribute(core, other_user_id, "admin")
  1996. instantiate_attribute(core, other_user_id, "admin", True)
  1997. return "Success"!
  1998. else:
  1999. return "Unknown user: " + other_user_name!
  2000. else:
  2001. return "Admin permission denied"!
  2002. String function cmd_admin_demote(other_user_name : String):
  2003. // Demote a user to normal status
  2004. if (is_admin(current_user_id)):
  2005. String other_user_id
  2006. other_user_id = get_user_id(other_user_name)
  2007. if (other_user_id != ""):
  2008. unset_attribute(core, other_user_id, "admin")
  2009. instantiate_attribute(core, other_user_id, "admin", False)
  2010. return "Success"!
  2011. else:
  2012. return "Unknown user: " + other_user_name!
  2013. else:
  2014. return "Admin permission denied"!
  2015. String function cmd_service_register(service_name : String):
  2016. // Active a service for this specific user
  2017. if (get_service_id(service_name) == ""):
  2018. String service
  2019. service = instantiate_node(core, "Service", "")
  2020. instantiate_attribute(core, service, "name", service_name)
  2021. instantiate_attribute(core, service, "port", get_taskname())
  2022. output("Success: " + get_taskname())
  2023. // Wait until we can stop the service
  2024. while (cast_value(input()) != "\"service_stop\""):
  2025. output("Service is running on this task: stop it by sending 'service_stop'")
  2026. model_delete_element(core, service)
  2027. // Service terminated
  2028. return "Success"!
  2029. else:
  2030. return "Service exists: " + service_name!
  2031. String function cmd_user_password(user_name : String, new_password : String):
  2032. if (bool_or(current_user_id == get_user_id(user_name), is_admin(current_user_id))):
  2033. if (get_user_id(user_name) != ""):
  2034. instantiate_attribute(core, get_user_id(user_name), "password", hash(new_password))
  2035. return "Success"!
  2036. else:
  2037. return "Unknown user: " + user_name!
  2038. else:
  2039. return "User permission denied to: " + user_name!
  2040. String function cmd_transformation_signature(transformation_name : String):
  2041. String model_id
  2042. model_id = get_entry_id(transformation_name)
  2043. if (model_id != ""):
  2044. if (allow_execute(current_user_id, model_id)):
  2045. if (is_nominal_instance(core, model_id, "Transformation")):
  2046. String result
  2047. String elem
  2048. Element inputs
  2049. Element outputs
  2050. result = "Success: "
  2051. inputs = allOutgoingAssociationInstances(core, model_id, "transformInput")
  2052. while (set_len(inputs) > 0):
  2053. elem = set_pop(inputs)
  2054. result = string_join(string_join(string_join(string_join(result + "I ", read_attribute(core, elem, "name")), " "), full_name(readAssociationDestination(core, elem))), "\n")
  2055. outputs = allOutgoingAssociationInstances(core, model_id, "transformOutput")
  2056. while (set_len(outputs) > 0):
  2057. elem = set_pop(outputs)
  2058. result = string_join(string_join(string_join(string_join(result + "O ", read_attribute(core, elem, "name")), " "), full_name(readAssociationDestination(core, elem))), "\n")
  2059. return result!
  2060. else:
  2061. return "Model is not an operation: " + transformation_name!
  2062. else:
  2063. return "Execute permission denied to: " + transformation_name!
  2064. else:
  2065. return "Model not found: " + transformation_name!
  2066. String function cmd_model_types(model_name : String):
  2067. if (get_entry_id(model_name) != ""):
  2068. String model_id
  2069. model_id = get_entry_id(model_name)
  2070. Element types
  2071. String type_link
  2072. String result
  2073. result = "Success: "
  2074. if (read_type(core, model_id) != "Folder"):
  2075. types = allOutgoingAssociationInstances(core, model_id, "instanceOf")
  2076. while (set_len(types) > 0):
  2077. type_link = set_pop(types)
  2078. result = result + cast_string(full_name(readAssociationDestination(core, type_link))) + ", " + cast_string(full_name(anAssociationDestination(core, type_link, "typing"))) + ", None\n"
  2079. return result!
  2080. else:
  2081. return "Not a model: " + model_name!
  2082. else:
  2083. return "Model not found: " + model_name!
  2084. String function cmd_folder_create(folder_name : String):
  2085. if (get_entry_id(folder_name) == ""):
  2086. if (element_neq(create_folders(current_user_id, folder_name, False), read_root())):
  2087. return "Success"!
  2088. else:
  2089. return "Write permission denied to: " + folder_name!
  2090. else:
  2091. return "Folder exists: " + folder_name!
  2092. String function cmd_conformance_delete(model_name : String, metamodel_name : String, typemapping_name : String):
  2093. // Remove an existing instanceOf relation
  2094. if (get_entry_id(model_name) != ""):
  2095. if (get_entry_id(metamodel_name) != ""):
  2096. if (bool_not(read_type(core, get_entry_id(model_name)) == "Folder")):
  2097. if (bool_not(read_type(core, get_entry_id(metamodel_name)) == "Folder")):
  2098. Element links
  2099. String link
  2100. String found_typing_name
  2101. Element typings
  2102. links = get_instanceOf_links(get_entry_id(model_name), get_entry_id(metamodel_name))
  2103. while (set_len(links) > 0):
  2104. // Iterate over each link and remove if type mapping matches
  2105. link = set_pop(links)
  2106. typings = allAssociationDestinations(core, link, "typing")
  2107. if (set_len(typings) > 0):
  2108. found_typing_name = set_pop(typings)
  2109. else:
  2110. found_typing_name = ""
  2111. if (bool_and(typemapping_name == "", found_typing_name == "")):
  2112. // Remove only the link
  2113. model_delete_element(core, link)
  2114. elif found_typing_name == "":
  2115. continue!
  2116. else:
  2117. found_typing_name = read_attribute(core, found_typing_name, "name")
  2118. if (found_typing_name == typemapping_name):
  2119. // Type mappings match, so remove link
  2120. // Optional: type mapping model can also be removed, but is potentially in use as it is itself a model!
  2121. model_delete_element(core, link)
  2122. return "Success"!
  2123. else:
  2124. return "Not a model: " + metamodel_name!
  2125. else:
  2126. return "Not a model: " + model_name!
  2127. else:
  2128. return "Model not found: " + metamodel_name!
  2129. else:
  2130. return "Model not found: " + model_name!
  2131. String function cmd_conformance_add(model_name : String, metamodel_name : String):
  2132. // Create a new instanceOf relation now
  2133. if (get_entry_id(model_name) != ""):
  2134. if (get_entry_id(metamodel_name) != ""):
  2135. if (bool_not(read_type(core, get_entry_id(model_name)) == "Folder")):
  2136. if (bool_not(read_type(core, get_entry_id(metamodel_name)) == "Folder")):
  2137. instantiate_link(core, "instanceOf", "", get_entry_id(model_name), get_entry_id(metamodel_name))
  2138. else:
  2139. return "Not a model: " + metamodel_name!
  2140. else:
  2141. return "Not a model: " + model_name!
  2142. else:
  2143. return "Model not found: " + metamodel_name!
  2144. else:
  2145. return "Model not found: " + model_name!
  2146. return "Success"!
  2147. Void function new_task():
  2148. String username
  2149. String user_id
  2150. String password
  2151. // Load in a hard-reference to the previously created model
  2152. core = import_node(core_model_location)
  2153. caches = dict_create()
  2154. dict_add_fast(caches, "models", dict_create())
  2155. dict_add_fast(caches, "users", dict_create())
  2156. dict_add_fast(caches, "full_name", dict_create())
  2157. // Find root element
  2158. Element elems
  2159. String elem
  2160. elems = allInstances(core, "Folder")
  2161. while (set_len(elems) > 0):
  2162. elem = set_pop(elems)
  2163. if (value_eq(read_attribute(core, elem, "name"), "")):
  2164. if (set_len(allIncomingAssociationInstances(core, elem, "contains")) == 0):
  2165. dict_add_fast(caches, "root", elem)
  2166. break!
  2167. while (True):
  2168. output("Log on as which user?")
  2169. username = input()
  2170. user_id = get_user_id(username)
  2171. if (user_id == ""):
  2172. // New user
  2173. // Add user to Core Formalism
  2174. user_id = instantiate_node(core, "User", "")
  2175. instantiate_attribute(core, user_id, "name", username)
  2176. instantiate_attribute(core, user_id, "admin", False)
  2177. output("This is a new user: please give password!")
  2178. password = hash(input())
  2179. output("Please repeat the password")
  2180. if (password == hash(input())):
  2181. output("Passwords match!")
  2182. instantiate_attribute(core, user_id, "password", password)
  2183. // Now create a folder for this user's models!
  2184. current_user_id = get_user_id("admin")
  2185. create_folders(user_id, "users/" + username, False)
  2186. current_user_id = user_id
  2187. // Can continue as we are logged on now!
  2188. else:
  2189. output("Not the same password!")
  2190. continue!
  2191. elif (bool_not(check_login(user_id))):
  2192. output("Wrong password!")
  2193. continue!
  2194. String cmd
  2195. String result
  2196. current_user_id = user_id
  2197. output("Welcome to the Model Management Interface v2.0!")
  2198. output("Use the 'help' command for a list of possible commands")
  2199. while (True):
  2200. cmd = input()
  2201. if (cmd == "help"):
  2202. output(cmd_help())
  2203. elif (cmd == "model_add"):
  2204. output(cmd_model_add(single_input("Model type?"), single_input("Model name?"), single_input("Model textual representation?")))
  2205. elif (cmd == "model_move"):
  2206. output(cmd_model_move(single_input("Model name?"), single_input("New location?")))
  2207. elif (cmd == "process_execute"):
  2208. output(cmd_process_execute(single_input("Process to execute?"), dict_input("Model bindings to use?")))
  2209. elif (cmd == "process_signature"):
  2210. output(cmd_process_signature(single_input("Process to query?")))
  2211. elif (cmd == "transformation_between"):
  2212. output(cmd_transformation_between(dict_input("Source signature?"), dict_input("Target signature?")))
  2213. elif (cmd == "model_render"):
  2214. output(cmd_model_render(single_input("Model name?"), single_input("Mapper name?"), single_input("Rendered name?")))
  2215. elif (cmd == "model_rendered"):
  2216. output(cmd_model_rendered(single_input("Model name?"), single_input("Mapper name?")))
  2217. elif (cmd == "transformation_execute"):
  2218. output(cmd_transformation_execute(single_input("Transformation name?"), dict_input("Source models?"), dict_input("Target models?"), single_input("Tracability model?")))
  2219. elif (cmd == "verify"):
  2220. result = cmd_verify(single_input("Model name?"), single_input("Metamodel name?"))
  2221. if (result != ""):
  2222. output(result)
  2223. elif (cmd == "model_overwrite"):
  2224. output(cmd_model_overwrite(single_input("Model name?"), single_input("Metamodel name?")))
  2225. elif (cmd == "model_modify"):
  2226. output(cmd_model_modify(single_input("Model name?"), single_input("Metamodel name?")))
  2227. elif (cmd == "model_delete"):
  2228. output(cmd_model_delete(single_input("Model name?")))
  2229. elif (cmd == "model_list"):
  2230. output(cmd_model_list(single_input("Location?")))
  2231. elif (cmd == "model_list_full"):
  2232. output(cmd_model_list_full(single_input("Location?")))
  2233. elif (cmd == "read_permissions"):
  2234. output(cmd_read_permissions(single_input("Location?")))
  2235. elif (cmd == "transformation_add_MANUAL"):
  2236. output(cmd_transformation_add_MANUAL(dict_input("Source models?"), dict_input("Target models?"), single_input("Operation name?")))
  2237. elif (cmd == "transformation_add_AL"):
  2238. output(cmd_transformation_add_AL(dict_input("Source models?"), dict_input("Target models?"), single_input("Operation name?")))
  2239. elif (cmd == "transformation_add_MT"):
  2240. output(cmd_transformation_add_MT(dict_input("Source models?"), dict_input("Target models?"), single_input("Operation name?")))
  2241. elif (cmd == "permission_modify"):
  2242. output(cmd_permission_modify(single_input("Model name?"), single_input("Permissions?")))
  2243. elif (cmd == "permission_owner"):
  2244. output(cmd_permission_owner(single_input("Model name?"), single_input("New owning user?")))
  2245. elif (cmd == "permission_group"):
  2246. output(cmd_permission_group(single_input("Model name?"), single_input("New owning group?")))
  2247. elif (cmd == "group_create"):
  2248. output(cmd_group_create(single_input("New group name?")))
  2249. elif (cmd == "group_delete"):
  2250. output(cmd_group_delete(single_input("Group name?")))
  2251. elif (cmd == "group_owner_add"):
  2252. output(cmd_group_owner_add(single_input("Group name?"), single_input("User name?")))
  2253. elif (cmd == "group_owner_delete"):
  2254. output(cmd_group_owner_delete(single_input("Group name?"), single_input("User name?")))
  2255. elif (cmd == "group_join"):
  2256. output(cmd_group_join(single_input("Group name?"), single_input("User name?")))
  2257. elif (cmd == "group_kick"):
  2258. output(cmd_group_kick(single_input("Group name?"), single_input("User name?")))
  2259. elif (cmd == "group_list"):
  2260. output(cmd_group_list())
  2261. elif (cmd == "admin_promote"):
  2262. output(cmd_admin_promote(single_input("User name?")))
  2263. elif (cmd == "admin_demote"):
  2264. output(cmd_admin_demote(single_input("User name?")))
  2265. elif (cmd == "service_register"):
  2266. output(cmd_service_register(single_input("Service name?")))
  2267. elif (cmd == "user_password"):
  2268. output(cmd_user_password(single_input("User name?"), single_input("New password?")))
  2269. elif (cmd == "transformation_read_signature"):
  2270. output(cmd_transformation_signature(single_input("Transformation name?")))
  2271. elif (cmd == "verbose"):
  2272. set_verbose(True)
  2273. elif (cmd == "quiet"):
  2274. set_verbose(False)
  2275. elif (cmd == "user_logout"):
  2276. output("Success")
  2277. break!
  2278. elif (cmd == "exit"):
  2279. output("Success")
  2280. sleep(5)
  2281. return !
  2282. elif (cmd == "folder_create"):
  2283. output(cmd_folder_create(single_input("Folder name?")))
  2284. elif (cmd == "add_conformance"):
  2285. output(cmd_conformance_add(single_input("Model name?"), single_input("Metamodel name?")))
  2286. elif (cmd == "remove_conformance"):
  2287. output(cmd_conformance_delete(single_input("Model name?"), single_input("Metamodel name?"), single_input("Type mapping name?")))
  2288. elif (cmd == "service_poll"):
  2289. // TODO
  2290. cmd = "FAIL"
  2291. elif (cmd == "model_types"):
  2292. output(cmd_model_types(single_input("Model name?")))
  2293. elif (cmd == "AL_text"):
  2294. output(cmd_AL_text(single_input("Code location?")))
  2295. else:
  2296. output("Unknown command: " + cmd)
  2297. // We never get here!
  2298. return !