core_algorithm.alc 62 KB

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