core_algorithm.alc 62 KB

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