core_algorithm.alc 72 KB

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