core_algorithm.alc 59 KB

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