core_algorithm.alc 61 KB

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