core_algorithm.alc 78 KB

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