core_algorithm.alc 72 KB

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