core_algorithm.alc 94 KB

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