core_algorithm.alc 72 KB

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