core_algorithm.alc 64 KB

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