core_algorithm.alc 94 KB

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