core_algorithm.alc 64 KB

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