core_algorithm.alc 76 KB

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