core_algorithm.alc 81 KB

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