core_algorithm.alc 56 KB

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