core_algorithm.alc 63 KB

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