core_algorithm.alc 72 KB

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