core_algorithm.alc 61 KB

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