core_algorithm.alc 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160
  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. model_create(new_model, name, type_id, "Model")
  840. return "Success"!
  841. else:
  842. return "Model exists: " + name!
  843. else:
  844. return "Permission denied to folder: " + get_foldername(name)!
  845. else:
  846. return "No such folder: " + get_foldername(name)!
  847. else:
  848. return "Permission denied to model: " + type!
  849. else:
  850. return "Model not found: " + type!
  851. String function cmd_process_execute(process : String, prefix : String):
  852. // Execute a process model until it reaches termination
  853. String process_id
  854. process_id = get_entry_id(process)
  855. if (process_id != ""):
  856. if (allow_read(current_user_id, process_id)):
  857. Element pm
  858. pm = get_full_model(process_id, get_entry_id("formalisms/ProcessModel"))
  859. if (element_eq(pm, read_root())):
  860. return "Specified model cannot be interpreted as a ProcessModel: " + process!
  861. enact_PM(pm, prefix)
  862. return "Success"!
  863. else:
  864. return "Permission denied to model: " + process!
  865. else:
  866. return "Model not found: " + process!
  867. String function cmd_transformation_between(source_name : String, target_name : String):
  868. String source_id
  869. String target_id
  870. Element onSource
  871. Element onTarget
  872. Element result
  873. String transformation
  874. source_id = get_entry_id(source_name)
  875. if (source_id != ""):
  876. target_id = get_entry_id(target_name)
  877. if (target_id != ""):
  878. onSource = allAssociationOrigins(core, source_id, "transformInput")
  879. onTarget = allAssociationOrigins(core, target_id, "transformOutput")
  880. result = set_overlap(onSource, onTarget)
  881. String r
  882. r = "Success: "
  883. while (set_len(result) > 0):
  884. transformation = set_pop(result)
  885. if (allow_read(current_user_id, transformation)):
  886. r = r + string_join(full_name(transformation), "\n")
  887. return r!
  888. else:
  889. return "Model not found: " + target_name!
  890. else:
  891. return "Model not found: " + source_name!
  892. String function cmd_model_render(model_name : String, mapper_name : String):
  893. String model_ID
  894. String mapper_ID
  895. String rendered_name
  896. String tracability_name
  897. Element inputs
  898. Element output_map
  899. Element rendered_model
  900. Element tracability_model
  901. Element result
  902. Element out_links
  903. String link
  904. model_ID = get_entry_id(model_name)
  905. if (model_ID != ""):
  906. mapper_ID = get_entry_id(mapper_name)
  907. if (allow_read(current_user_id, model_ID)):
  908. if (mapper_ID != ""):
  909. if (allow_read(current_user_id, mapper_ID)):
  910. // Everything is fine; start the actual operation
  911. // Find metamodel to render to
  912. rendered_name = ("rendered/" + model_name) + mapper_name
  913. tracability_name = ("tracability/" + model_name) + mapper_name
  914. // Take the abstract syntax model and the previously rendered model
  915. inputs = dict_create()
  916. dict_add(inputs, "abstract", model_name)
  917. dict_add(inputs, "rendered", rendered_name)
  918. // Fetch the output types
  919. output_map = dict_create()
  920. out_links = allOutgoingAssociationInstances(core, mapper_ID, "transformOutput")
  921. while(set_len(out_links) > 0):
  922. link = set_pop(out_links)
  923. dict_add(output_map, read_attribute(core, link, "name"), full_name(readAssociationDestination(core, link)))
  924. if (get_entry_id(rendered_name) == ""):
  925. // Instantiate
  926. Element rendered
  927. rendered = get_full_model(get_entry_id(output_map["rendered"]), get_entry_id("formalisms/SimpleClassDiagrams"))
  928. if (element_eq(rendered, read_root())):
  929. return "Rendered metamodel cannot conform to formalisms/SimpleClassDiagrams"!
  930. rendered_model = instantiate_model(rendered)
  931. model_create(rendered_model, rendered_name, get_entry_id(output_map["rendered"]), "Model")
  932. // Tracability model won't exist either
  933. tracability_model = instantiate_model(get_full_model(get_entry_id("formalisms/Tracability"), get_entry_id("formalisms/SimpleClassDiagrams")))
  934. model_create(tracability_model, tracability_name, get_entry_id("formalisms/Tracability"), "Model")
  935. else:
  936. // Read out tracability model
  937. tracability_model = get_full_model(get_entry_id(tracability_name), get_entry_id("formalisms/Tracability"))
  938. if (element_eq(tracability_model, read_root())):
  939. return "Tracability model not typed by Tracability metamodel: " + tracability_name!
  940. // Do the operation itself!
  941. result = execute_operation(mapper_ID, inputs, tracability_model)
  942. // Overwrite the previous rendered model
  943. model_overwrite(result["rendered"], get_entry_id(rendered_name), get_entry_id(output_map["rendered"]))
  944. model_overwrite(result["abstract"], get_entry_id(model_name), get_entry_id(output_map["abstract"]))
  945. // Tracability updated in-place
  946. model_overwrite(tracability_model, get_entry_id(tracability_name), get_entry_id("formalisms/Tracability"))
  947. tracability_model = get_full_model(get_entry_id(tracability_name), get_entry_id("formalisms/Tracability"))
  948. if (element_eq(tracability_model, read_root())):
  949. return "Tracability model not typed by Tracability metamodel: " + tracability_name!
  950. // Also output the resulting model
  951. return "Success: " + JSON_print(get_full_model(get_entry_id(rendered_name), get_entry_id(output_map["rendered"])))!
  952. else:
  953. return "Permission denied to model: " + mapper_name!
  954. else:
  955. return "Model not found: " + mapper_name!
  956. else:
  957. return "Permission denied to model: " + model_name!
  958. else:
  959. return "Model not found: " + model_name!
  960. String function cmd_transformation_execute(transformation_name : String, source_models : Element, target_models : Element):
  961. // Execute a transformation, whatever type it is
  962. // First we detect the type, so we know how to prepare for invocation
  963. String transformation_id
  964. String exact_type
  965. Element sources
  966. Element targets
  967. String source
  968. String target
  969. Element inputs
  970. Element outputs
  971. Element output_map
  972. Element trace_links
  973. String target_model_name
  974. String source_model_name
  975. String source_model_ID
  976. Element result
  977. Element keys
  978. String key
  979. String assoc_name
  980. transformation_id = get_entry_id(transformation_name)
  981. if (transformation_id != ""):
  982. if (allow_read(current_user_id, transformation_id)):
  983. if (is_nominal_instance(core, transformation_id, "Transformation")):
  984. // Read out source and target links
  985. inputs = dict_create()
  986. sources = allOutgoingAssociationInstances(core, transformation_id, "transformInput")
  987. while (set_len(sources) > 0):
  988. source = set_pop(sources)
  989. assoc_name = read_attribute(core, source, "name")
  990. if (dict_in(source_models, assoc_name)):
  991. source_model_name = source_models[assoc_name]
  992. else:
  993. return "Source model not bound: " + assoc_name!
  994. source_model_ID = get_entry_id(source_model_name)
  995. if (source_model_ID != ""):
  996. if (allow_read(current_user_id, source_model_ID)):
  997. // Check for conformance to the requested metamodel
  998. dict_add(inputs, assoc_name, source_model_name)
  999. continue!
  1000. else:
  1001. return "Permission denied to model: " + source_model_name!
  1002. else:
  1003. return "Model not found: " + source_model_name!
  1004. targets = allOutgoingAssociationInstances(core, transformation_id, "transformOutput")
  1005. outputs = dict_create()
  1006. output_map = dict_create()
  1007. while (set_len(targets) > 0):
  1008. target = set_pop(targets)
  1009. assoc_name = read_attribute(core, target, "name")
  1010. if (dict_in(target_models, assoc_name)):
  1011. target_model_name = target_models[assoc_name]
  1012. else:
  1013. return "Target model not bound: " + assoc_name!
  1014. if (get_entry_id(target_model_name) == ""):
  1015. // Doesn't exist yet, so we can easily create
  1016. if (get_entry_id(get_foldername(target_model_name)) == ""):
  1017. return "Folder not found: " + get_foldername(target_model_name)!
  1018. else:
  1019. if (allow_write(current_user_id, get_entry_id(get_foldername(target_model_name)))):
  1020. dict_add(output_map, assoc_name, full_name(readAssociationDestination(core, target)))
  1021. dict_add(outputs, assoc_name, target_model_name)
  1022. else:
  1023. return "Permission denied to folder: " + get_foldername(target_model_name)!
  1024. else:
  1025. // Already exists, so we need to check for write access
  1026. if (allow_write(current_user_id, get_entry_id(target_model_name))):
  1027. dict_add(output_map, assoc_name, read_attribute(core, readAssociationDestination(core, target), "name"))
  1028. dict_add(outputs, assoc_name, target_model_name)
  1029. else:
  1030. return "Permission denied to model: " + target_model_name!
  1031. if (read_type(core, transformation_id) == "ActionLanguage"):
  1032. output("Success: ready for AL execution")
  1033. elif (read_type(core, transformation_id) == "ManualOperation"):
  1034. output("Success: ready for MANUAL execution")
  1035. else:
  1036. output("Success: ready for MT execution")
  1037. result = execute_operation(transformation_id, inputs, read_root())
  1038. // Now write out the models again
  1039. if (element_eq(result, read_root())):
  1040. // Something went wrong!
  1041. return "Failure"!
  1042. else:
  1043. keys = dict_keys(result)
  1044. while (set_len(keys) > 0):
  1045. key = set_pop(keys)
  1046. if (get_entry_id(outputs[key]) == ""):
  1047. // New model
  1048. model_create(result[key], outputs[key], get_entry_id(output_map[key]), "Model")
  1049. else:
  1050. model_overwrite(result[key], get_entry_id(outputs[key]), get_entry_id(output_map[key]))
  1051. return "Success"!
  1052. else:
  1053. return "Model is not executable: " + transformation_name!
  1054. else:
  1055. return "Permission denied to model: " + transformation_name!
  1056. else:
  1057. return "Model not found: " + transformation_name!
  1058. String function cmd_verify(model_name : String, metamodel_name : String):
  1059. // Check whether a model conforms to its specification (with the selected type mapping)
  1060. String model_id
  1061. String result
  1062. Element inputs
  1063. model_id = get_entry_id(model_name)
  1064. if (model_id != ""):
  1065. if (allow_read(current_user_id, model_id)):
  1066. if (get_entry_id(metamodel_name) != ""):
  1067. if (allow_read(current_user_id, get_entry_id(metamodel_name))):
  1068. Element m
  1069. m = get_full_model(get_entry_id(model_name), get_entry_id(metamodel_name))
  1070. if (element_eq(m, read_root())):
  1071. return "No conformance relation can be found between these models"!
  1072. return string_join("Success: ", conformance_scd(m))!
  1073. else:
  1074. return "Permission denied to model: " + metamodel_name!
  1075. else:
  1076. return "Model not found: " + metamodel_name!
  1077. else:
  1078. return "Permission denied to model: " + model_name!
  1079. else:
  1080. return "Model not found: " + model_name!
  1081. String function cmd_model_overwrite(model_name : String, metamodel_name : String):
  1082. // Overwrites an existing model without changing any metadata
  1083. String model_id
  1084. String type_id
  1085. Element new_model
  1086. model_id = get_entry_id(model_name)
  1087. if (model_id != ""):
  1088. if (allow_write(current_user_id, model_id)):
  1089. type_id = set_pop(allAssociationDestinations(core, model_id, "instanceOf"))
  1090. if (allow_read(current_user_id, type_id)):
  1091. Element mm
  1092. mm = get_full_model(get_entry_id(metamodel_name), get_entry_id("formalisms/SimpleClassDiagrams"))
  1093. if (element_eq(mm, read_root())):
  1094. return "Metamodel does not conform to formalisms/SimpleClassDiagrams: " + metamodel_name!
  1095. output("Waiting for model constructors...")
  1096. new_model = compile_model(input(), mm)
  1097. model_overwrite(new_model, model_id, get_entry_id(metamodel_name))
  1098. return "Success"!
  1099. else:
  1100. return string_join("Permission denied to model: ", full_name(type_id))!
  1101. else:
  1102. return "Permission denied to model: " + model_name!
  1103. else:
  1104. return "Model not found: " + model_name!
  1105. String function cmd_model_modify(model_name : String, metamodel_name : String):
  1106. // Model modify operation, which uses the mini_modify.alc operations, though with extensions for access control
  1107. String model_id
  1108. String type_id
  1109. model_id = get_entry_id(model_name)
  1110. if (model_id != ""):
  1111. if (allow_read(current_user_id, model_id)):
  1112. type_id = set_pop(allAssociationDestinations(core, model_id, "instanceOf"))
  1113. if (allow_read(current_user_id, type_id)):
  1114. Element new_model
  1115. new_model = get_full_model(model_id, get_entry_id(metamodel_name))
  1116. if (element_eq(new_model, read_root())):
  1117. return "No conformance relation can be found between these models"!
  1118. output("Success")
  1119. modify(new_model, allow_write(current_user_id, model_id))
  1120. if (allow_write(current_user_id, model_id)):
  1121. // Overwrite the modified model
  1122. model_overwrite(new_model, model_id, get_entry_id(metamodel_name))
  1123. return "Success"!
  1124. else:
  1125. return string_join("Permission denied to model: ", full_name(type_id))!
  1126. else:
  1127. return "Permission denied to model: " + model_name!
  1128. else:
  1129. return "Model not found: " + model_name!
  1130. Void function model_delete(model_id : String):
  1131. if (read_type(core, model_id) == "Folder"):
  1132. // Folder, so delete all containing elements as well!
  1133. Element elems
  1134. elems = allAssociationDestinations(core, model_id, "contains")
  1135. while (set_len(elems) > 0):
  1136. model_delete(set_pop(elems))
  1137. // Delete self
  1138. model_delete_element(core, model_id)
  1139. else:
  1140. // Only delete this element
  1141. model_delete_element(core, model_id)
  1142. return!
  1143. String function cmd_model_delete(model_name : String):
  1144. String model_id
  1145. model_id = get_entry_id(model_name)
  1146. if (model_id != ""):
  1147. if (allow_write(current_user_id, model_id)):
  1148. model_delete(model_id)
  1149. return "Success"!
  1150. else:
  1151. return "Permission denied to model: " + model_name!
  1152. else:
  1153. return "Model not found: " + model_name!
  1154. String function cmd_model_list(location : String):
  1155. // List all models
  1156. if (get_entry_id(location) != ""):
  1157. if (allow_read(current_user_id, get_entry_id(location))):
  1158. Element models
  1159. String result
  1160. String m
  1161. result = "Success: "
  1162. models = allAssociationDestinations(core, get_entry_id(location), "contains")
  1163. while (set_len(models) > 0):
  1164. m = set_pop(models)
  1165. if (is_nominal_instance(core, m, "Folder")):
  1166. result = result + (get_filename(read_attribute(core, m, "name")) + "/\n")
  1167. else:
  1168. result = result + (get_filename(read_attribute(core, m, "name")) + "\n")
  1169. return result!
  1170. else:
  1171. return "Permission denied to folder: " + location!
  1172. else:
  1173. return "Location not found: " + location!
  1174. String function cmd_model_list_full(location : String):
  1175. // List all models with full info
  1176. if (get_entry_id(location) != ""):
  1177. if (allow_read(current_user_id, get_entry_id(location))):
  1178. Element models
  1179. String m
  1180. String permissions
  1181. String owner
  1182. String group
  1183. String name
  1184. String type
  1185. String result
  1186. result = "Success: "
  1187. models = allAssociationDestinations(core, get_entry_id(location), "contains")
  1188. while (set_len(models) > 0):
  1189. m = set_pop(models)
  1190. permissions = read_attribute(core, m, "permissions")
  1191. owner = read_attribute(core, set_pop(allAssociationDestinations(core, m, "owner")), "name")
  1192. group = read_attribute(core, set_pop(allAssociationDestinations(core, m, "group")), "name")
  1193. if (is_nominal_instance(core, m, "Folder")):
  1194. name = get_filename(read_attribute(core, m, "name")) + "/"
  1195. else:
  1196. name = get_filename(read_attribute(core, m, "name"))
  1197. result = result + (((((((permissions + " ") + owner) + " ") + group) + " ") + name) + "\n")
  1198. return result!
  1199. else:
  1200. return "Permission denied to folder: " + location!
  1201. else:
  1202. return "Location not found: " + location!
  1203. String function cmd_transformation_add_MANUAL(source_models : Element, target_models : Element, operation_name : String):
  1204. return transformation_add(source_models, target_models, operation_name, "manual")!
  1205. String function cmd_transformation_add_AL(source_models : Element, target_models : Element, operation_name : String):
  1206. return transformation_add(source_models, target_models, operation_name, "actionlanguage")!
  1207. String function transformation_add(source_models : Element, target_models : Element, operation_name : String, operation_type : String):
  1208. // Add a manual transformation model
  1209. String model_id
  1210. Element models
  1211. Element source
  1212. Element target
  1213. String name
  1214. Element all_formalisms
  1215. Element formalism_map
  1216. Element merged_formalism
  1217. String merged_formalism_id
  1218. String source_formalism_id
  1219. String tracability_link
  1220. Element fused
  1221. Element keys
  1222. String key
  1223. Element mm
  1224. source = dict_create()
  1225. target = dict_create()
  1226. all_formalisms = set_create()
  1227. formalism_map = set_create()
  1228. fused = set_create()
  1229. keys = dict_keys(source_models)
  1230. while (set_len(keys) > 0):
  1231. key = set_pop(keys)
  1232. name = source_models[key]
  1233. model_id = get_entry_id(name)
  1234. if (model_id != ""):
  1235. if (allow_read(current_user_id, model_id)):
  1236. if (bool_not(dict_in(source, key))):
  1237. dict_add(source, key, model_id)
  1238. mm = get_full_model(model_id, get_entry_id("formalisms/SimpleClassDiagrams"))
  1239. if (element_eq(mm, read_root())):
  1240. return "Transformation source type not in formalisms/SimpleClassDiagrams hierarchy: " + key!
  1241. if (bool_not(set_in(fused, key))):
  1242. set_add_node(formalism_map, create_tuple(key, mm))
  1243. set_add(fused, key)
  1244. if (bool_not(set_in(all_formalisms, model_id))):
  1245. set_add(all_formalisms, model_id)
  1246. else:
  1247. return "Name already selected for source: " + key!
  1248. else:
  1249. return "Permission denied to model: " + name!
  1250. else:
  1251. return "Model not found: " + name!
  1252. keys = dict_keys(target_models)
  1253. while (set_len(keys) > 0):
  1254. key = set_pop(keys)
  1255. name = target_models[key]
  1256. model_id = get_entry_id(name)
  1257. if (model_id != ""):
  1258. if (allow_read(current_user_id, model_id)):
  1259. if (bool_not(dict_in(target, key))):
  1260. dict_add(target, key, model_id)
  1261. mm = get_full_model(model_id, get_entry_id("formalisms/SimpleClassDiagrams"))
  1262. if (element_eq(mm, read_root())):
  1263. return "Transformation target type not in formalisms/SimpleClassDiagrams hierarchy: " + key!
  1264. if (bool_not(set_in(fused, key))):
  1265. set_add_node(formalism_map, create_tuple(key, mm))
  1266. set_add(fused, key)
  1267. if (bool_not(set_in(all_formalisms, model_id))):
  1268. set_add(all_formalisms, model_id)
  1269. else:
  1270. return "Name already selected for target: " + key!
  1271. else:
  1272. return "Permission denied to model: " + name!
  1273. else:
  1274. return "Model not found: " + name!
  1275. if (get_entry_id(operation_name) == ""):
  1276. // Write out a merged metamodel containing all these models: this is the MM for the manual operation
  1277. // New location is available, so write
  1278. if (bool_not(bool_and(dict_len(source_models) == 0, dict_len(target_models) == 0))):
  1279. merged_formalism = model_fuse(formalism_map)
  1280. modify(merged_formalism, True)
  1281. if (operation_type == "manual"):
  1282. // Finished with all information, now create the model itself!
  1283. Element m
  1284. m = get_full_model(get_entry_id("formalisms/ManualOperation"), get_entry_id("formalisms/SimpleClassDiagrams"))
  1285. if (element_eq(m, read_root())):
  1286. log("Error when opening ManualOperation: no conformance relation found")
  1287. return "Error when opening ManualOperation: no conformance relation found"!
  1288. model_create(instantiate_model(m), operation_name, get_entry_id("formalisms/ManualOperation"), "ManualOperation")
  1289. model_id = get_entry_id(operation_name)
  1290. elif (operation_type == "actionlanguage"):
  1291. // Finished with all information, now create the model itself!
  1292. output("Waiting for code constructors...")
  1293. add_code_model(get_full_model(get_entry_id("formalisms/ActionLanguage"), get_entry_id("formalisms/SimpleClassDiagrams")), "AL/" + operation_name, compile_code(input()))
  1294. model_create(import_node("AL/" + operation_name), operation_name, get_entry_id("formalisms/ActionLanguage"), "ActionLanguage")
  1295. model_id = get_entry_id(operation_name)
  1296. if (bool_not(bool_and(dict_len(source_models) == 0, dict_len(target_models) == 0))):
  1297. model_create(merged_formalism, "merged/" + operation_name, get_entry_id("formalisms/SimpleClassDiagrams"), "Model")
  1298. merged_formalism_id = get_entry_id("merged/" + operation_name)
  1299. // Add tracability links at this level
  1300. while (set_len(all_formalisms) > 0):
  1301. source_formalism_id = set_pop(all_formalisms)
  1302. tracability_link = instantiate_link(core, "tracability", "", merged_formalism_id, source_formalism_id)
  1303. instantiate_attribute(core, tracability_link, "type", "merged")
  1304. tracability_link = instantiate_link(core, "tracability", "", model_id, merged_formalism_id)
  1305. instantiate_attribute(core, tracability_link, "type", "operatesOn")
  1306. // Extend metadata with info on source and target
  1307. String link
  1308. String dst
  1309. keys = dict_keys(source)
  1310. while (set_len(keys) > 0):
  1311. key = set_pop(keys)
  1312. link = instantiate_link(core, "transformInput", "", model_id, source[key])
  1313. instantiate_attribute(core, link, "name", key)
  1314. keys = dict_keys(target)
  1315. while (set_len(keys) > 0):
  1316. key = set_pop(keys)
  1317. link = instantiate_link(core, "transformOutput", "", model_id, target[key])
  1318. instantiate_attribute(core, link, "name", key)
  1319. return "Success"!
  1320. else:
  1321. return "Model exists: " + operation_name!
  1322. String function cmd_transformation_add_MT(source_models : Element, target_models : Element, operation_name : String):
  1323. // Add a model transformation model
  1324. // Just a usual model instantiation, but need to add the source and target links based on user info
  1325. String ramified_metamodel_id
  1326. Element ramified_metamodel
  1327. String model_id
  1328. Element models
  1329. Element links
  1330. String link_id
  1331. Element supported
  1332. Element source
  1333. Element target
  1334. String name
  1335. String new_model_id
  1336. String merged_link_id
  1337. Element links_merged
  1338. String merged_formalism
  1339. Element keys
  1340. String key
  1341. Element mm
  1342. Element to_ramify
  1343. String source_formalism_id
  1344. String merged_formalism_id
  1345. String tracability_link
  1346. source = dict_create()
  1347. target = dict_create()
  1348. to_ramify = set_create()
  1349. keys = dict_keys(source_models)
  1350. while (set_len(keys) > 0):
  1351. key = set_pop(keys)
  1352. name = source_models[key]
  1353. model_id = get_entry_id(name)
  1354. if (model_id != ""):
  1355. if (allow_read(current_user_id, name)):
  1356. // Check whether or not it is formalisms/SimpleClassDiagrams
  1357. if (is_typed_by(model_id, get_entry_id("formalisms/SimpleClassDiagrams"))):
  1358. if (bool_not(dict_in(source, key))):
  1359. dict_add(source, key, model_id)
  1360. mm = get_full_model(model_id, get_entry_id("formalisms/SimpleClassDiagrams"))
  1361. if (element_eq(mm, read_root())):
  1362. return "ModelTransformation source type not in formalisms/SimpleClassDiagrams hierarchy: " + name!
  1363. set_add_node(to_ramify, create_tuple(key, mm))
  1364. else:
  1365. return "Name was already assigned a metamodel: " + key!
  1366. else:
  1367. return "Model not supported for RAMification: " + name!
  1368. else:
  1369. return "Permission denied: " + name!
  1370. else:
  1371. return "Model not found: " + name!
  1372. keys = dict_keys(target_models)
  1373. while (set_len(keys) > 0):
  1374. key = set_pop(keys)
  1375. name = target_models[key]
  1376. model_id = get_entry_id(name)
  1377. if (model_id != ""):
  1378. if (allow_read(current_user_id, name)):
  1379. if (is_typed_by(model_id, get_entry_id("formalisms/SimpleClassDiagrams"))):
  1380. if (bool_not(dict_in(target, key))):
  1381. if (dict_in(source, key)):
  1382. if (value_eq(model_id, source[key])):
  1383. dict_add(target, key, model_id)
  1384. // Don't add to to_ramify, as it is already in there!
  1385. else:
  1386. return "Name in output cannot have different type than input: " + key!
  1387. else:
  1388. dict_add(target, key, model_id)
  1389. mm = get_full_model(model_id, get_entry_id("formalisms/SimpleClassDiagrams"))
  1390. if (element_eq(mm, read_root())):
  1391. return "ModelTransformation target type not in formalisms/SimpleClassDiagrams hierarchy: " + name!
  1392. set_add_node(to_ramify, create_tuple(key, mm))
  1393. else:
  1394. return "Name was already assigned a metamodel: " + key!
  1395. else:
  1396. return "Model not supported for RAMification: " + name!
  1397. else:
  1398. return "Permission denied: " + name!
  1399. else:
  1400. return "Model not found: " + name!
  1401. merged_formalism = model_fuse(to_ramify)
  1402. modify(merged_formalism, True)
  1403. ramified_metamodel = ramify(merged_formalism)
  1404. model_create(ramified_metamodel, "RAMified/" + operation_name, get_entry_id("formalisms/SimpleClassDiagrams"), "Model")
  1405. ramified_metamodel_id = get_entry_id("RAMified/" + operation_name)
  1406. // Now use the RAMified model to create the instance
  1407. if (get_entry_id(operation_name) == ""):
  1408. String new_model
  1409. // Finished with all information, now create the model itself!
  1410. output("Waiting for model constructors...")
  1411. new_model = compile_model(input(), get_full_model(ramified_metamodel_id, get_entry_id("formalisms/SimpleClassDiagrams")))
  1412. model_create(new_model, operation_name, ramified_metamodel_id, "ModelTransformation")
  1413. model_id = get_entry_id(operation_name)
  1414. // Write out a merged metamodel containing all these models: this is the MM for the manual operation
  1415. // New location is available, so write
  1416. model_create(merged_formalism, "merged/" + operation_name, get_entry_id("formalisms/SimpleClassDiagrams"), "Model")
  1417. merged_formalism_id = get_entry_id("merged/" + operation_name)
  1418. // Add tracability links at this level
  1419. tracability_link = instantiate_link(core, "tracability", "", model_id, merged_formalism_id)
  1420. instantiate_attribute(core, tracability_link, "type", "operatesOn")
  1421. // Add tracability links at this level
  1422. tracability_link = instantiate_link(core, "tracability", "", merged_formalism_id, ramified_metamodel_id)
  1423. instantiate_attribute(core, tracability_link, "type", "RAMified")
  1424. // Extend metadata with info on source and target
  1425. String link
  1426. String dst
  1427. keys = dict_keys(source)
  1428. while (set_len(keys) > 0):
  1429. key = set_pop(keys)
  1430. dst = source[key]
  1431. link = instantiate_link(core, "transformInput", "", model_id, dst)
  1432. instantiate_attribute(core, link, "name", key)
  1433. keys = dict_keys(target)
  1434. while (set_len(keys) > 0):
  1435. key = set_pop(keys)
  1436. dst = target[key]
  1437. link = instantiate_link(core, "transformOutput", "", model_id, dst)
  1438. instantiate_attribute(core, link, "name", key)
  1439. return "Success"!
  1440. else:
  1441. return "Model exists: " + operation_name!
  1442. String function cmd_permission_modify(model_name : String, permissions : String):
  1443. Integer permission
  1444. String model_id
  1445. model_id = get_entry_id(model_name)
  1446. if (model_id != ""):
  1447. if (get_relation_to_model(current_user_id, model_id) == 0):
  1448. Boolean fail
  1449. Integer i
  1450. i = 0
  1451. if (string_len(permissions) != 3):
  1452. fail = True
  1453. while (i < 3):
  1454. permission = cast_s2i(string_get(permissions, i))
  1455. if (bool_or(permission < 0, permission > 2)):
  1456. fail = True
  1457. break!
  1458. i = i + 1
  1459. if (bool_not(fail)):
  1460. unset_attribute(core, model_id, "permissions")
  1461. instantiate_attribute(core, model_id, "permissions", permissions)
  1462. return "Success"!
  1463. else:
  1464. return "Permission has incorrect format: must be a string of three characters, with each character being a digit between 0 and 2"!
  1465. else:
  1466. return "Permission denied to model: " + model_name!
  1467. else:
  1468. return "Model not found: " + model_name!
  1469. String function cmd_permission_owner(model_name : String, new_user_name : String):
  1470. String model_id
  1471. String new_user_id
  1472. model_id = get_entry_id(model_name)
  1473. if (model_id != ""):
  1474. if (bool_or(get_relation_to_model(current_user_id, model_id) == 0, is_admin(current_user_id))):
  1475. new_user_id = get_user_id(new_user_name)
  1476. if (new_user_id != ""):
  1477. model_delete_element(core, set_pop(allOutgoingAssociationInstances(core, model_id, "owner")))
  1478. instantiate_link(core, "owner", "", model_id, new_user_id)
  1479. return "Success"!
  1480. else:
  1481. return "No such user: " + new_user_name!
  1482. else:
  1483. return "Permission denied to model: " + model_name!
  1484. else:
  1485. return "Model not found: " + model_name!
  1486. String function cmd_permission_group(model_name : String, new_group_name : String):
  1487. String model_id
  1488. String group_id
  1489. model_id = get_entry_id(model_name)
  1490. if (model_id != ""):
  1491. if (bool_or(get_relation_to_model(current_user_id, model_id) == 0, is_admin(current_user_id))):
  1492. group_id = get_group_id(new_group_name)
  1493. if (group_id != ""):
  1494. model_delete_element(core, set_pop(allOutgoingAssociationInstances(core, model_id, "group")))
  1495. instantiate_link(core, "group", "", model_id, group_id)
  1496. return "Success"!
  1497. else:
  1498. return "No such group: " + new_group_name!
  1499. else:
  1500. return "Permission denied to model: " + model_name!
  1501. else:
  1502. return "Model not found: " + model_name!
  1503. String function cmd_group_create(group_name : String):
  1504. // Create a new group and become its owner
  1505. String group_id
  1506. String name
  1507. group_id = get_group_id(group_name)
  1508. if (group_id == ""):
  1509. group_id = instantiate_node(core, "Group", "")
  1510. instantiate_attribute(core, group_id, "name", group_name)
  1511. instantiate_link(core, "belongsTo", "", current_user_id, group_id)
  1512. instantiate_link(core, "owner", "", group_id, current_user_id)
  1513. return "Success"!
  1514. else:
  1515. return "Group exists: " + group_name!
  1516. String function cmd_group_delete(group_name : String):
  1517. // Delete an existing group
  1518. String group_id
  1519. group_id = get_group_id(group_name)
  1520. if (group_id != ""):
  1521. if (allow_group_modify(current_user_id, group_id)):
  1522. model_delete_element(core, group_id)
  1523. return "Success"!
  1524. else:
  1525. return "Permission denied to group: " + group_name!
  1526. else:
  1527. return "Group not found: " + group_name!
  1528. String function cmd_group_owner_add(group_name : String, other_user_name : String):
  1529. // Add an owner to your group
  1530. String group_id
  1531. String other_user_id
  1532. group_id = get_group_id(group_name)
  1533. if (group_id != ""):
  1534. if (allow_group_modify(current_user_id, group_id)):
  1535. other_user_id = get_user_id(other_user_name)
  1536. if (other_user_id != ""):
  1537. if (set_in(allAssociationDestinations(core, group_id, "owner"), other_user_id)):
  1538. instantiate_link(core, "owner", "", group_id, other_user_id)
  1539. if (set_in(allAssociationDestinations(core, other_user_id, "belongsTo"), group_id)):
  1540. instantiate_link(core, "belongsTo", "", other_user_id, group_id)
  1541. return "Success"!
  1542. else:
  1543. return "User is already the owner!"!
  1544. else:
  1545. return "User not found: " + other_user_name!
  1546. else:
  1547. return "Permission denied to group: " + group_name!
  1548. else:
  1549. return "Group not found: " + group_name!
  1550. String function cmd_group_owner_delete(group_name : String, other_user_name : String):
  1551. // Remove an owner from your group
  1552. String group_id
  1553. String other_user_id
  1554. group_id = get_group_id(group_name)
  1555. if (group_id != ""):
  1556. if (allow_group_modify(current_user_id, group_id)):
  1557. other_user_id = get_user_id(other_user_name)
  1558. if (other_user_id != ""):
  1559. if (set_in(allAssociationDestinations(core, other_user_id, "belongsTo"), group_id)):
  1560. if (set_in(allAssociationDestinations(core, group_id, "owner"), other_user_id)):
  1561. Element overlap
  1562. overlap = set_overlap(allIncomingAssociationInstances(core, group_id, "owner"), allOutgoingAssociationInstances(core, other_user_id, "owner"))
  1563. model_delete_element(core, set_pop(overlap))
  1564. return "Success"!
  1565. else:
  1566. return "User is not an owner of the group!"!
  1567. else:
  1568. return "User is not a member of the group!"!
  1569. else:
  1570. return "User not found: " + other_user_name!
  1571. else:
  1572. return "Permission denied to group: " + group_name!
  1573. else:
  1574. return "Group not found: " + group_name!
  1575. String function cmd_group_join(group_name : String, other_user_name : String):
  1576. // Add someone to your group
  1577. String group_id
  1578. String other_user_id
  1579. group_id = get_group_id(group_name)
  1580. if (group_id != ""):
  1581. if (allow_group_modify(current_user_id, group_id)):
  1582. other_user_id = get_user_id(other_user_name)
  1583. if (other_user_id != ""):
  1584. if (set_in(allOutgoingAssociationInstances(core, other_user_id, "belongsTo"), group_id)):
  1585. instantiate_link(core, "belongsTo", "", other_user_id, group_id)
  1586. return "Success"!
  1587. else:
  1588. return "User is already a member of the group!"!
  1589. else:
  1590. return "User not found: " + other_user_name!
  1591. else:
  1592. return "Permission denied to group: " + group_name!
  1593. else:
  1594. return "Group not found: " + group_name!
  1595. String function cmd_group_kick(group_name : String, other_user_name : String):
  1596. // Remove someone from your group
  1597. String group_id
  1598. String other_user_id
  1599. group_id = get_group_id(group_name)
  1600. if (group_id != ""):
  1601. if (allow_group_modify(current_user_id, group_id)):
  1602. other_user_id = get_user_id(other_user_name)
  1603. if (other_user_id != ""):
  1604. Element overlap
  1605. if (set_in(allAssociationDestinations(core, other_user_id, "belongsTo"), group_id)):
  1606. overlap = set_overlap(allIncomingAssociationInstances(core, group_id, "belongsTo"), allOutgoingAssociationInstances(core, other_user_id, "belongsTo"))
  1607. model_delete_element(core,set_pop(overlap))
  1608. // Check if user was an owner as well
  1609. if (set_in(allAssociationDestinations(core, group_id, "owner"), other_user_id)):
  1610. overlap = set_overlap(allIncomingAssociationInstances(core, other_user_id, "owner"), allOutgoingAssociationInstances(core, group_id, "owner"))
  1611. model_delete_element(core, set_pop(overlap))
  1612. return "Success"!
  1613. else:
  1614. return "User is not 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_list():
  1622. // List all groups you are a member of (and whether you are admin or not!)
  1623. Element groups
  1624. String group_id
  1625. String admin
  1626. String result
  1627. result = ""
  1628. groups = allAssociationDestinations(core, current_user_id, "belongsTo")
  1629. while (set_len(groups) > 0):
  1630. group_id = set_pop(groups)
  1631. if (set_in(allAssociationDestinations(core, group_id, "owner"), current_user_id)):
  1632. admin = " A "
  1633. else:
  1634. admin = " "
  1635. result = result + (string_join(admin, read_attribute(core, group_id, "name")) + "\n")
  1636. return result!
  1637. String function cmd_admin_promote(other_user_name : String):
  1638. // Promote a user to admin status
  1639. if (is_admin(current_user_id)):
  1640. String other_user_id
  1641. other_user_id = get_user_id(other_user_name)
  1642. if (other_user_id != ""):
  1643. unset_attribute(core, other_user_id, "admin")
  1644. instantiate_attribute(core, other_user_id, "admin", True)
  1645. return "Success"!
  1646. else:
  1647. return "User not found: " + other_user_name!
  1648. else:
  1649. return "Permission denied to administrate: " + other_user_name!
  1650. String function cmd_admin_demote(other_user_name : String):
  1651. // Demote a user to normal status
  1652. if (is_admin(current_user_id)):
  1653. String other_user_id
  1654. other_user_id = get_user_id(other_user_name)
  1655. if (other_user_id != ""):
  1656. unset_attribute(core, other_user_id, "admin")
  1657. instantiate_attribute(core, other_user_id, "admin", False)
  1658. return "Success"!
  1659. else:
  1660. return "User not found: " + other_user_name!
  1661. else:
  1662. return "Permission denied to administrate: " + other_user_name!
  1663. String function cmd_service_register(service_name : String):
  1664. // Active a service for this specific user
  1665. if (get_service_id(service_name) == ""):
  1666. String service
  1667. service = instantiate_node(core, "Service", "")
  1668. instantiate_attribute(core, service, "name", service_name)
  1669. instantiate_attribute(core, service, "port", get_taskname())
  1670. output("Success: " + get_taskname())
  1671. // Wait until we can stop the service
  1672. log("Waiting for service_stop")
  1673. while (cast_v2s(input()) != "\"service_stop\""):
  1674. output("Service is running on this task: stop it by sending 'service_stop'")
  1675. log("Finished")
  1676. model_delete_element(core, service)
  1677. // Service terminated
  1678. return "Success"!
  1679. else:
  1680. return "Service already exists: " + service_name!
  1681. String function cmd_user_password(user_name : String, new_password : String):
  1682. if (bool_or(current_user_id == get_user_id(user_name), is_admin(current_user_id))):
  1683. if (get_user_id(user_name) != ""):
  1684. instantiate_attribute(core, get_user_id(user_name), "password", hash(new_password))
  1685. return "Success"!
  1686. else:
  1687. return "No such user: " + user_name!
  1688. else:
  1689. return "Permission denied to user: " + user_name!
  1690. String function cmd_transformation_signature(transformation_name : String):
  1691. String model_id
  1692. model_id = get_entry_id(transformation_name)
  1693. if (model_id != ""):
  1694. if (is_nominal_instance(core, model_id, "Transformation")):
  1695. if (allow_read(current_user_id, transformation_name)):
  1696. String result
  1697. String elem
  1698. Element inputs
  1699. Element outputs
  1700. result = "Success: "
  1701. inputs = allOutgoingAssociationInstances(core, model_id, "transformInput")
  1702. while (set_len(inputs) > 0):
  1703. elem = set_pop(inputs)
  1704. result = string_join(string_join(string_join(string_join("I ", read_attribute(core, elem, "name")), " "), full_name(readAssociationDestination(core, elem))), "\n")
  1705. outputs = allOutgoingAssociationInstances(core, model_id, "transformOutput")
  1706. while (set_len(outputs) > 0):
  1707. elem = set_pop(outputs)
  1708. result = string_join(string_join(string_join(string_join("O ", read_attribute(core, elem, "name")), " "), full_name(readAssociationDestination(core, elem))), "\n")
  1709. return result!
  1710. else:
  1711. return "Permission denied to operation: " + transformation_name!
  1712. else:
  1713. return "Model is not an operation: " + transformation_name!
  1714. else:
  1715. return "No such operation: " + transformation_name!
  1716. String function cmd_element_list_nice(model_name : String, metamodel_name : String):
  1717. if (get_entry_id(model_name) != ""):
  1718. if (allow_read(current_user_id, get_entry_id(model_name))):
  1719. if (get_entry_id(metamodel_name) != ""):
  1720. if (allow_read(current_user_id, get_entry_id(metamodel_name))):
  1721. Element mm
  1722. mm = get_full_model(get_entry_id(model_name), get_entry_id(metamodel_name))
  1723. if (element_eq(mm, read_root())):
  1724. return "No conformance relation between these models"!
  1725. return "Success: " + JSON_print(mm)!
  1726. else:
  1727. return "Permission denied to model: " + metamodel_name!
  1728. else:
  1729. return "No such metamodel: " + metamodel_name!
  1730. else:
  1731. return "Permission denied to model: " + model_name!
  1732. else:
  1733. return "No such model: " + model_name!
  1734. String function cmd_folder_create(folder_name : String):
  1735. if (get_entry_id(folder_name) == ""):
  1736. if (element_neq(create_folders(current_user_id, folder_name), read_root())):
  1737. return "Success"!
  1738. else:
  1739. return "Permission denied to folder: " + folder_name!
  1740. else:
  1741. return "Folder alreay exists: " + folder_name!
  1742. Void function user_function_skip_init(user_id : String):
  1743. String cmd
  1744. String result
  1745. current_user_id = user_id
  1746. caches = dict_create()
  1747. dict_add_fast(caches, "models", dict_create())
  1748. dict_add_fast(caches, "users", dict_create())
  1749. dict_add_fast(caches, "full_name", dict_create())
  1750. // Find root element
  1751. Element elems
  1752. String elem
  1753. elems = allInstances(core, "Folder")
  1754. while (set_len(elems) > 0):
  1755. elem = set_pop(elems)
  1756. if (value_eq(read_attribute(core, elem, "name"), "")):
  1757. if (set_len(allIncomingAssociationInstances(core, elem, "contains")) == 0):
  1758. dict_add_fast(caches, "root", elem)
  1759. break!
  1760. output("Welcome to the Model Management Interface v2.0!")
  1761. output("Use the 'help' command for a list of possible commands")
  1762. while (True):
  1763. cmd = input()
  1764. if (cmd == "help"):
  1765. output(cmd_help())
  1766. elif (cmd == "model_add"):
  1767. output(cmd_model_add(single_input("Model type?"), single_input("Model name?")))
  1768. elif (cmd == "process_execute"):
  1769. output(cmd_process_execute(single_input("Process to execute?"), single_input("Model prefix to use?")))
  1770. elif (cmd == "transformation_between"):
  1771. output(cmd_transformation_between(single_input("Source type?"), single_input("Target type?")))
  1772. elif (cmd == "model_render"):
  1773. output(cmd_model_render(single_input("Model name?"), single_input("Mapper name?")))
  1774. elif (cmd == "transformation_execute"):
  1775. output(cmd_transformation_execute(single_input("Transformation name?"), dict_input("Source models?"), dict_input("Target models?")))
  1776. elif (cmd == "verify"):
  1777. result = cmd_verify(single_input("Model name?"), single_input("Metamodel name?"))
  1778. if (result != ""):
  1779. output(result)
  1780. elif (cmd == "model_overwrite"):
  1781. output(cmd_model_overwrite(single_input("Model name?"), single_input("Metamodel name?")))
  1782. elif (cmd == "model_modify"):
  1783. output(cmd_model_modify(single_input("Model name?"), single_input("Metamodel name?")))
  1784. elif (cmd == "model_delete"):
  1785. output(cmd_model_delete(single_input("Model name?")))
  1786. elif (cmd == "model_list"):
  1787. output(cmd_model_list(single_input("Location?")))
  1788. elif (cmd == "model_list_full"):
  1789. output(cmd_model_list_full(single_input("Location?")))
  1790. elif (cmd == "transformation_add_MANUAL"):
  1791. output(cmd_transformation_add_MANUAL(dict_input("Source model names?"), dict_input("Target model names?"), single_input("Operation name?")))
  1792. elif (cmd == "transformation_add_AL"):
  1793. output(cmd_transformation_add_AL(dict_input("Source model names?"), dict_input("Target model names?"), single_input("Operation name?")))
  1794. elif (cmd == "transformation_add_MT"):
  1795. output(cmd_transformation_add_MT(dict_input("Source model names?"), dict_input("Target models?"), single_input("Operation name?")))
  1796. elif (cmd == "permission_modify"):
  1797. output(cmd_permission_modify(single_input("Model name?"), single_input("Permissions?")))
  1798. elif (cmd == "permission_owner"):
  1799. output(cmd_permission_owner(single_input("Model name?"), single_input("New owning user?")))
  1800. elif (cmd == "permission_group"):
  1801. output(cmd_permission_group(single_input("Model name?"), single_input("New owning group?")))
  1802. elif (cmd == "group_create"):
  1803. output(cmd_group_create(single_input("New group name?")))
  1804. elif (cmd == "group_delete"):
  1805. output(cmd_group_delete(single_input("Group name?")))
  1806. elif (cmd == "group_owner_add"):
  1807. output(cmd_group_owner_add(single_input("Group name?"), single_input("User name?")))
  1808. elif (cmd == "group_owner_delete"):
  1809. output(cmd_group_owner_delete(single_input("Group name?"), single_input("User name?")))
  1810. elif (cmd == "group_join"):
  1811. output(cmd_group_join(single_input("Group name?"), single_input("User name?")))
  1812. elif (cmd == "group_kick"):
  1813. output(cmd_group_kick(single_input("Group name?"), single_input("User name?")))
  1814. elif (cmd == "group_list"):
  1815. output(cmd_group_list())
  1816. elif (cmd == "admin_promote"):
  1817. output(cmd_admin_promote(single_input("User name?")))
  1818. elif (cmd == "admin_demote"):
  1819. output(cmd_admin_demote(single_input("User name?")))
  1820. elif (cmd == "service_register"):
  1821. output(cmd_service_register(single_input("Service name?")))
  1822. elif (cmd == "user_password"):
  1823. output(cmd_user_password(single_input("User name?"), single_input("New password?")))
  1824. elif (cmd == "transformation_read_signature"):
  1825. output(cmd_transformation_signature(single_input("Transformation name?")))
  1826. elif (cmd == "element_list_nice"):
  1827. output(cmd_element_list_nice(single_input("Model name?"), single_input("Metamodel name?")))
  1828. elif (cmd == "verbose"):
  1829. set_verbose(True)
  1830. elif (cmd == "quiet"):
  1831. set_verbose(False)
  1832. elif (cmd == "self-destruct"):
  1833. // Delete user from Core Formalism
  1834. model_delete_element(core, user_id)
  1835. return !
  1836. elif (cmd == "exit"):
  1837. // Exit by actually removing the user and decoupling it from all of its models
  1838. // Restarting with the same user name will NOT grant you access to anything of the previous user with that same name
  1839. // as the current user will have been deleted
  1840. return !
  1841. elif (cmd == "folder_create"):
  1842. output(cmd_folder_create(single_input("Folder name?")))
  1843. elif (cmd == "add_conformance"):
  1844. // TODO
  1845. cmd = "FAIL"
  1846. elif (cmd == "remove_conformance"):
  1847. // TODO
  1848. cmd = "FAIL"
  1849. elif (cmd == "user_name"):
  1850. // TODO
  1851. cmd = "FAIL"
  1852. elif (cmd == "service_poll"):
  1853. // TODO
  1854. cmd = "FAIL"
  1855. else:
  1856. output("Unknown command: " + cmd)
  1857. // We never get here!
  1858. return !