core_algorithm.alc 62 KB

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