core_algorithm.alc 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737
  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. log("FUSE")
  1037. merged_formalism = model_fuse(to_ramify)
  1038. log("Fuse OK")
  1039. modify(merged_formalism, True)
  1040. log("Modify OK")
  1041. ramified_metamodel = ramify(merged_formalism)
  1042. log("RAMify OK")
  1043. model_create(ramified_metamodel, "__RAM_" + operation_name, user_id, get_model_id("SimpleClassDiagrams"), "Model")
  1044. ramified_metamodel_id = get_model_id("__RAM_" + operation_name)
  1045. // Now use the RAMified model to create the instance
  1046. if (get_model_id(operation_name) == ""):
  1047. String new_model
  1048. // Finished with all information, now create the model itself!
  1049. output("Waiting for model constructors...")
  1050. new_model = construct_model_raw(get_full_model(ramified_metamodel_id))
  1051. model_create(new_model, operation_name, user_id, ramified_metamodel_id, "ModelTransformation")
  1052. model_id = get_model_id(operation_name)
  1053. // Write out a merged metamodel containing all these models: this is the MM for the manual operation
  1054. // New location is available, so write
  1055. model_create(merged_formalism, "__merged_" + operation_name, user_id, get_model_id("SimpleClassDiagrams"), "Model")
  1056. merged_formalism_id = get_model_id("__merged_" + operation_name)
  1057. // Add tracability links at this level
  1058. tracability_link = instantiate_link(core, "tracability", "", model_id, merged_formalism_id)
  1059. instantiate_attribute(core, tracability_link, "type", "operatesOn")
  1060. // Extend metadata with info on source and target
  1061. String link
  1062. String dst
  1063. keys = dict_keys(source)
  1064. while (read_nr_out(keys) > 0):
  1065. key = set_pop(keys)
  1066. dst = source[key]
  1067. link = instantiate_link(core, "transformInput", "", model_id, dst)
  1068. instantiate_attribute(core, link, "name", key)
  1069. keys = dict_keys(target)
  1070. while (read_nr_out(keys) > 0):
  1071. key = set_pop(keys)
  1072. dst = target[key]
  1073. link = instantiate_link(core, "transformOutput", "", model_id, dst)
  1074. instantiate_attribute(core, link, "name", key)
  1075. return "Success"!
  1076. else:
  1077. return "Model exists: " + operation_name!
  1078. String function cmd_transformation_list():
  1079. // List all models
  1080. Element models
  1081. String m
  1082. String type
  1083. String result
  1084. result = "Success: "
  1085. models = allInstances(core, "Transformation")
  1086. while (read_nr_out(models) > 0):
  1087. m = set_pop(models)
  1088. 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")
  1089. return result!
  1090. String function cmd_transformation_list_full():
  1091. // List all models with full info
  1092. Element models
  1093. String m
  1094. String permissions
  1095. String owner
  1096. String group
  1097. String name
  1098. String type
  1099. String result
  1100. result = ""
  1101. models = allInstances(core, "Transformation")
  1102. while (read_nr_out(models) > 0):
  1103. m = set_pop(models)
  1104. permissions = read_attribute(core, m, "permissions")
  1105. owner = read_attribute(core, set_pop(allAssociationDestinations(core, m, "owner")), "name")
  1106. group = read_attribute(core, set_pop(allAssociationDestinations(core, m, "group")), "name")
  1107. name = read_attribute(core, m, "name")
  1108. type = read_attribute(core, set_pop(allAssociationDestinations(core, m, "instanceOf")), "name")
  1109. result = result + ((((((((((" " + permissions) + " ") + owner) + " ") + group) + " ") + ((("[" + read_type(core, m)) + "] ") + name)) + " : ") + type) + "\n")
  1110. return result!
  1111. String function cmd_permission_modify(user_id : String, model_name : String, permissions : String):
  1112. Integer permission
  1113. String model_id
  1114. model_id = get_model_id(model_name)
  1115. if (model_id != ""):
  1116. if (get_relation_to_model(user_id, model_id) == 0):
  1117. Boolean fail
  1118. Integer i
  1119. i = 0
  1120. if (string_len(permissions) != 3):
  1121. fail = True
  1122. while (i < 3):
  1123. permission = cast_s2i(string_get(permissions, i))
  1124. if (bool_or(permission < 0, permission > 2)):
  1125. fail = True
  1126. break!
  1127. i = i + 1
  1128. if (bool_not(fail)):
  1129. unset_attribute(core, model_id, "permissions")
  1130. instantiate_attribute(core, model_id, "permissions", permissions)
  1131. return "Success"!
  1132. else:
  1133. return "Permission has incorrect format: must be a string of three characters, with each character being a digit between 0 and 2"!
  1134. else:
  1135. return "Permission denied to model: " + model_name!
  1136. else:
  1137. return "Model not found: " + model_name!
  1138. String function cmd_permission_owner(user_id : String, model_name : String, new_user_name : String):
  1139. String model_id
  1140. String new_user_id
  1141. model_id = get_model_id(model_name)
  1142. if (model_id != ""):
  1143. if (bool_or(get_relation_to_model(user_id, model_id) == 0, is_admin(user_id))):
  1144. new_user_id = get_user_id(new_user_name)
  1145. if (new_user_id != ""):
  1146. model_delete_element(core, set_pop(allOutgoingAssociationInstances(core, model_id, "owner")))
  1147. instantiate_link(core, "owner", "", model_id, new_user_id)
  1148. return "Success"!
  1149. else:
  1150. return "No such user: " + new_user_name!
  1151. else:
  1152. return "Permission denied to model: " + model_name!
  1153. else:
  1154. return "Model not found: " + model_name!
  1155. String function cmd_permission_group(user_id : String, model_name : String, new_group_name : String):
  1156. String model_id
  1157. String group_id
  1158. model_id = get_model_id(model_name)
  1159. if (model_id != ""):
  1160. if (bool_or(get_relation_to_model(user_id, model_id) == 0, is_admin(user_id))):
  1161. group_id = get_group_id(new_group_name)
  1162. if (group_id != ""):
  1163. model_delete_element(core, set_pop(allOutgoingAssociationInstances(core, model_id, "group")))
  1164. instantiate_link(core, "group", "", model_id, group_id)
  1165. return "Success"!
  1166. else:
  1167. return "No such group: " + new_group_name!
  1168. else:
  1169. return "Permission denied to model: " + model_name!
  1170. else:
  1171. return "Model not found: " + model_name!
  1172. String function cmd_group_create(user_id : String, group_name : String):
  1173. // Create a new group and become its owner
  1174. String group_id
  1175. String name
  1176. group_id = get_group_id(group_name)
  1177. if (group_id == ""):
  1178. group_id = instantiate_node(core, "Group", "")
  1179. instantiate_attribute(core, group_id, "name", group_name)
  1180. instantiate_link(core, "belongsTo", "", user_id, group_id)
  1181. instantiate_link(core, "owner", "", group_id, user_id)
  1182. return "Success"!
  1183. else:
  1184. return "Group exists: " + group_name!
  1185. String function cmd_group_delete(user_id : String, group_name : String):
  1186. // Delete an existing group
  1187. String group_id
  1188. group_id = get_group_id(group_name)
  1189. if (group_id != ""):
  1190. if (allow_group_modify(user_id, group_id)):
  1191. model_delete_element(core, group_id)
  1192. return "Success"!
  1193. else:
  1194. return "Permission denied to group: " + group_name!
  1195. else:
  1196. return "Group not found: " + group_name!
  1197. String function cmd_group_owner_add(user_id : String, group_name : String, other_user_name : String):
  1198. // Add an owner to your group
  1199. String group_id
  1200. String other_user_id
  1201. group_id = get_group_id(group_name)
  1202. if (group_id != ""):
  1203. if (allow_group_modify(user_id, group_id)):
  1204. other_user_id = get_user_id(other_user_name)
  1205. if (other_user_id != ""):
  1206. Element overlap
  1207. overlap = set_overlap(allIncomingAssociationInstances(core, other_user_id, "owner"), allOutgoingAssociationInstances(core, group_id, "owner"))
  1208. if (read_nr_out(overlap) == 0):
  1209. instantiate_link(core, "owner", "", group_id, other_user_id)
  1210. overlap = set_overlap(allOutgoingAssociationInstances(core, other_user_id, "belongsTo"), allIncomingAssociationInstances(core, group_id, "belongsTo"))
  1211. if (read_nr_out(overlap) == 0):
  1212. instantiate_link(core, "belongsTo", "", other_user_id, group_id)
  1213. return "Success"!
  1214. else:
  1215. return "User is already the owner!"!
  1216. else:
  1217. return "User not found: " + other_user_name!
  1218. else:
  1219. return "Permission denied to group: " + group_name!
  1220. else:
  1221. return "Group not found: " + group_name!
  1222. String function cmd_group_owner_delete(user_id : String, group_name : String, other_user_name : String):
  1223. // Remove an owner from your group
  1224. String group_id
  1225. String other_user_id
  1226. group_id = get_group_id(group_name)
  1227. if (group_id != ""):
  1228. if (allow_group_modify(user_id, group_id)):
  1229. other_user_id = get_user_id(other_user_name)
  1230. if (other_user_id != ""):
  1231. Element overlap
  1232. overlap = set_overlap(allOutgoingAssociationInstances(core, other_user_id, "belongsTo"), allIncomingAssociationInstances(core, group_id, "belongsTo"))
  1233. if (read_nr_out(overlap) > 0):
  1234. overlap = set_overlap(allIncomingAssociationInstances(core, other_user_id, "owner"), allOutgoingAssociationInstances(core, group_id, "owner"))
  1235. if (read_nr_out(overlap) > 0):
  1236. model_delete_element(core, set_pop(overlap))
  1237. return "Success"!
  1238. else:
  1239. return "User is not an owner of the group!"!
  1240. else:
  1241. return "User is not a member of the group!"!
  1242. else:
  1243. return "User not found: " + other_user_name!
  1244. else:
  1245. return "Permission denied to group: " + group_name!
  1246. else:
  1247. return "Group not found: " + group_name!
  1248. String function cmd_group_join(user_id : String, group_name : String, other_user_name : String):
  1249. // Add someone to your group
  1250. String group_id
  1251. String other_user_id
  1252. group_id = get_group_id(group_name)
  1253. if (group_id != ""):
  1254. if (allow_group_modify(user_id, group_id)):
  1255. other_user_id = get_user_id(other_user_name)
  1256. if (other_user_id != ""):
  1257. Element overlap
  1258. overlap = set_overlap(allOutgoingAssociationInstances(core, other_user_id, "belongsTo"), allIncomingAssociationInstances(core, group_id, "belongsTo"))
  1259. if (read_nr_out(overlap) == 0):
  1260. instantiate_link(core, "belongsTo", "", other_user_id, group_id)
  1261. return "Success"!
  1262. else:
  1263. return "User is already a member of the group!"!
  1264. else:
  1265. return "User not found: " + other_user_name!
  1266. else:
  1267. return "Permission denied to group: " + group_name!
  1268. else:
  1269. return "Group not found: " + group_name!
  1270. String function cmd_group_kick(user_id : String, group_name : String, other_user_name : String):
  1271. // Remove someone from your group
  1272. String group_id
  1273. String other_user_id
  1274. group_id = get_group_id(group_name)
  1275. if (group_id != ""):
  1276. if (allow_group_modify(user_id, group_id)):
  1277. other_user_id = get_user_id(other_user_name)
  1278. if (other_user_id != ""):
  1279. Element overlap
  1280. overlap = set_overlap(allOutgoingAssociationInstances(core, other_user_id, "belongsTo"), allIncomingAssociationInstances(core, group_id, "belongsTo"))
  1281. if (read_nr_out(overlap) > 0):
  1282. model_delete_element(core, set_pop(overlap))
  1283. // Check if user was an owner as well
  1284. overlap = set_overlap(allIncomingAssociationInstances(core, other_user_id, "owner"), allOutgoingAssociationInstances(core, group_id, "owner"))
  1285. if (read_nr_out(overlap) > 0):
  1286. model_delete_element(core, set_pop(overlap))
  1287. return "Success"!
  1288. else:
  1289. return "User is not a member of the group!"!
  1290. else:
  1291. return "User not found: " + other_user_name!
  1292. else:
  1293. return "Permission denied to group: " + group_name!
  1294. else:
  1295. return "Group not found: " + group_name!
  1296. String function cmd_group_list(user_id : String):
  1297. // List all groups you are a member of (and whether you are admin or not!)
  1298. Element groups
  1299. String group_id
  1300. String admin
  1301. String result
  1302. result = ""
  1303. groups = allAssociationDestinations(core, user_id, "belongsTo")
  1304. while (read_nr_out(groups) > 0):
  1305. group_id = set_pop(groups)
  1306. if (set_in(allOutgoingAssociationInstances(core, group_id, "owner"), user_id)):
  1307. admin = " A "
  1308. else:
  1309. admin = " "
  1310. result = result + (string_join(admin, read_attribute(core, group_id, "name")) + "\n")
  1311. return result!
  1312. String function cmd_admin_promote(user_id : String, other_user_name : String):
  1313. // Promote a user to admin status
  1314. if (is_admin(user_id)):
  1315. String other_user_id
  1316. other_user_id = get_user_id(other_user_name)
  1317. if (other_user_id != ""):
  1318. unset_attribute(core, other_user_id, "admin")
  1319. instantiate_attribute(core, other_user_id, "admin", True)
  1320. return "Success"!
  1321. else:
  1322. return "User not found: " + other_user_name!
  1323. else:
  1324. return "Permission denied to administrate: " + other_user_name!
  1325. String function cmd_admin_demote(user_id : String, other_user_name : String):
  1326. // Demote a user to normal status
  1327. if (is_admin(user_id)):
  1328. String other_user_id
  1329. other_user_id = get_user_id(other_user_name)
  1330. if (other_user_id != ""):
  1331. unset_attribute(core, other_user_id, "admin")
  1332. instantiate_attribute(core, other_user_id, "admin", False)
  1333. return "Success"!
  1334. else:
  1335. return "User not found: " + other_user_name!
  1336. else:
  1337. return "Permission denied to administrate: " + other_user_name!
  1338. String function cmd_service_register(user_id : String, service_name : String):
  1339. // Active a service for this specific user
  1340. if (get_service_id(service_name) == ""):
  1341. String service
  1342. service = instantiate_node(core, "Service", "")
  1343. instantiate_attribute(core, service, "name", service_name)
  1344. instantiate_attribute(core, service, "port", get_taskname())
  1345. output("Success: " + get_taskname())
  1346. // Wait until we can stop the service
  1347. log("Waiting for service_stop")
  1348. while (cast_v2s(input()) != "\"service_stop\""):
  1349. output("Service is running on this task: stop it by sending 'service_stop'")
  1350. log("Finished")
  1351. model_delete_element(core, service)
  1352. // Service terminated
  1353. return "Success"!
  1354. else:
  1355. return "Service already exists: " + service_name!
  1356. String function cmd_user_password(user_id : String, user_name : String, new_password : String):
  1357. if (bool_or(user_id == get_user_id(user_name), is_admin(user_id))):
  1358. if (get_user_id(user_name) != ""):
  1359. instantiate_attribute(core, get_user_id(user_name), "password", hash(new_password))
  1360. return "Success"!
  1361. else:
  1362. return "No such user: " + user_name!
  1363. else:
  1364. return "Permission denied to user: " + user_name!
  1365. String function cmd_transformation_signature(user_id : String, transformation_name : String):
  1366. String model_id
  1367. model_id = get_model_id(transformation_name)
  1368. if (model_id != ""):
  1369. if (is_nominal_instance(core, model_id, "Transformation")):
  1370. if (allow_read(user_id, transformation_name)):
  1371. String result
  1372. String elem
  1373. Element inputs
  1374. Element outputs
  1375. result = "Success: "
  1376. inputs = allOutgoingAssociationInstances(core, model_id, "transformInput")
  1377. while (read_nr_out(inputs) > 0):
  1378. elem = set_pop(inputs)
  1379. result = string_join(string_join(string_join(string_join("I ", read_attribute(core, elem, "name")), " "), read_attribute(core, readAssociationDestination(core, elem), "name")), "\n")
  1380. outputs = allOutgoingAssociationInstances(core, model_id, "transformOutput")
  1381. while (read_nr_out(outputs) > 0):
  1382. elem = set_pop(outputs)
  1383. result = string_join(string_join(string_join(string_join("O ", read_attribute(core, elem, "name")), " "), read_attribute(core, readAssociationDestination(core, elem), "name")), "\n")
  1384. return result!
  1385. else:
  1386. return "Permission denied to transformation: " + transformation_name!
  1387. else:
  1388. return "Model is not a transformation: " + transformation_name!
  1389. else:
  1390. return "No such transformation: " + transformation_name!
  1391. String function cmd_element_list_nice(user_id : String, model_name : String):
  1392. if (get_model_id(model_name) != ""):
  1393. if (allow_read(user_id, get_model_id(model_name))):
  1394. return "Success: " + JSON_print(get_full_model(get_model_id(model_name)))!
  1395. else:
  1396. return "Permission denied to model: " + model_name!
  1397. else:
  1398. return "No such model: " + model_name!
  1399. Void function user_function_skip_init(user_id : String):
  1400. String cmd
  1401. String result
  1402. output("Welcome to the Model Management Interface v2.0!")
  1403. output("Use the 'help' command for a list of possible commands")
  1404. while (True):
  1405. cmd = input()
  1406. if (cmd == "help"):
  1407. output(cmd_help(user_id))
  1408. elif (cmd == "model_add"):
  1409. output(cmd_model_add(user_id, single_input("Model type?"), single_input("Model name?")))
  1410. elif (cmd == "process_execute"):
  1411. output(cmd_process_execute(user_id, single_input("Process to execute?"), single_input("Model prefix to use?")))
  1412. elif (cmd == "transformation_between"):
  1413. output(cmd_transformation_between(user_id, single_input("Source type?"), single_input("Target type?")))
  1414. elif (cmd == "model_render"):
  1415. output(cmd_model_render(user_id, single_input("Model name?"), single_input("Mapper name?")))
  1416. elif (cmd == "transformation_execute"):
  1417. output(cmd_transformation_execute(user_id, single_input("Transformation name?"), dict_input("Source models?"), dict_input("Target models?")))
  1418. elif (cmd == "verify"):
  1419. result = cmd_verify(user_id, single_input("Model name?"))
  1420. if (result != ""):
  1421. output(result)
  1422. elif (cmd == "model_overwrite"):
  1423. output(cmd_model_overwrite(user_id, single_input("Model name?")))
  1424. elif (cmd == "model_modify"):
  1425. output(cmd_model_modify(user_id, single_input("Model name?")))
  1426. elif (cmd == "model_delete"):
  1427. output(cmd_model_delete(user_id, single_input("Model name?")))
  1428. elif (cmd == "model_list"):
  1429. output(cmd_model_list())
  1430. elif (cmd == "model_list_full"):
  1431. output(cmd_model_list_full())
  1432. elif (cmd == "transformation_add_MANUAL"):
  1433. output(cmd_transformation_add_MANUAL(user_id, dict_input("Source model names?"), dict_input("Target model names?"), single_input("Operation name?")))
  1434. elif (cmd == "transformation_add_AL"):
  1435. output(cmd_transformation_add_AL(user_id, dict_input("Source model names?"), dict_input("Target model names?"), single_input("Operation name?")))
  1436. elif (cmd == "transformation_add_MT"):
  1437. output(cmd_transformation_add_MT(user_id, dict_input("Source model names?"), dict_input("Target models?"), single_input("Operation name?")))
  1438. elif (cmd == "transformation_list"):
  1439. output(cmd_transformation_list())
  1440. elif (cmd == "transformation_list_full"):
  1441. output(cmd_transformation_list_full())
  1442. elif (cmd == "permission_modify"):
  1443. output(cmd_permission_modify(user_id, single_input("Model name?"), single_input("Permissions?")))
  1444. elif (cmd == "permission_owner"):
  1445. output(cmd_permission_owner(user_id, single_input("Model name?"), single_input("New owning user?")))
  1446. elif (cmd == "permission_group"):
  1447. output(cmd_permission_group(user_id, single_input("Model name?"), single_input("New owning group?")))
  1448. elif (cmd == "group_create"):
  1449. output(cmd_group_create(user_id, single_input("New group name?")))
  1450. elif (cmd == "group_delete"):
  1451. output(cmd_group_delete(user_id, single_input("Group name?")))
  1452. elif (cmd == "group_owner_add"):
  1453. output(cmd_group_owner_add(user_id, single_input("Group name?"), single_input("User name?")))
  1454. elif (cmd == "group_owner_delete"):
  1455. output(cmd_group_owner_delete(user_id, single_input("Group name?"), single_input("User name?")))
  1456. elif (cmd == "group_join"):
  1457. output(cmd_group_join(user_id, single_input("Group name?"), single_input("User name?")))
  1458. elif (cmd == "group_kick"):
  1459. output(cmd_group_kick(user_id, single_input("Group name?"), single_input("User name?")))
  1460. elif (cmd == "group_list"):
  1461. output(cmd_group_list(user_id))
  1462. elif (cmd == "admin_promote"):
  1463. output(cmd_admin_promote(user_id, single_input("User name?")))
  1464. elif (cmd == "admin_demote"):
  1465. output(cmd_admin_demote(user_id, single_input("User name?")))
  1466. elif (cmd == "service_register"):
  1467. output(cmd_service_register(user_id, single_input("Service name?")))
  1468. elif (cmd == "user_password"):
  1469. output(cmd_user_password(user_id, single_input("User name?"), single_input("New password?")))
  1470. elif (cmd == "transformation_read_signature"):
  1471. output(cmd_transformation_signature(user_id, single_input("Transformation name?")))
  1472. elif (cmd == "element_list_nice"):
  1473. output(cmd_element_list_nice(user_id, single_input("Model name?")))
  1474. elif (cmd == "verbose"):
  1475. set_verbose(True)
  1476. elif (cmd == "quiet"):
  1477. set_verbose(False)
  1478. elif (cmd == "self-destruct"):
  1479. // Delete user from Core Formalism
  1480. model_delete_element(core, user_id)
  1481. return !
  1482. elif (cmd == "exit"):
  1483. // Exit by actually removing the user and decoupling it from all of its models
  1484. // Restarting with the same user name will NOT grant you access to anything of the previous user with that same name
  1485. // as the current user will have been deleted
  1486. return !
  1487. else:
  1488. output("Unknown command: " + cmd)
  1489. // We never get here!
  1490. return !