core_algorithm.alc 57 KB

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