core_algorithm.alc 62 KB

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