core_algorithm.alc 64 KB

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