core_algorithm.alc 72 KB

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