core_algorithm.alc 72 KB

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