core_algorithm.alc 71 KB

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