core_algorithm.alc 44 KB

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