core_algorithm.alc 64 KB

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