import Base import ./world.bend as W import ./entity_allocator.bend as Allocator import ./entity_key.bend as Key import ./lifetime_scope.bend as Scope import ./relation_types.bend as T import ./relation_cleanup_engine.bend as Engine import ./execution_composer_layout.bend as Layout import ./execution_command_buffer.bend as Buffer import ./execution_command_permission.bend as Permission import ./execution_relation_context.bend as Context import ./execution_relation_resource_snapshot.bend as Snapshot import ./execution_lifetime_scope_command.bend as Commands import ./execution_lifetime_scope_store.bend as Store import ./execution_lifetime_scope_resource_dispatch.bend as Dispatch import ./execution_lifetime_scope_world_access.bend as Access import ./execution_lifetime_scope_coverage.bend as Coverage import ./execution_lifetime_scope_cleanup.bend as Cleanup import ./change_tick.bend as Tick def denied(-S:Data,-C:Type,-R:Type,-E:Data,-P:Type,-D:Type,route:Layout.Route,operation:Commands.Operation,reason:Buffer.ScopeDenial,world:W.World) -> W.World & Buffer.Receipt: (world,Buffer.ScopePublished{route,operation,Buffer.ScopeDenied{reason},[],[]}) def completed(-S:Data,-C:Type,-R:Type,-E:Data,-P:Type,-D:Type,route:Layout.Route,operation:Commands.Operation,result:Engine.Progress>) -> W.World & Buffer.Receipt: match result: case Engine.Complete{Cleanup.State{world,receipts,_,_,_}}:(world,Buffer.ScopePublished{route,operation,Buffer.ScopeComplete{},List.reverse(&1,Buffer.Receipt,receipts),[]}) case Engine.Incomplete{Cleanup.State{world,receipts,_,_,_},frames}:(world,Buffer.ScopePublished{route,operation,Buffer.ScopePaused{},List.reverse(&1,Buffer.Receipt,receipts),frames}) def start(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~D:Type,~resources:Layout.Layout,~same:{R == Layout.resources(~S,resources) : Type},~selection:Permission.Selection,~dispose:Cleanup.Dispose(~S,~C,~R,~E,~P,~D),+route:Layout.Route,scope:Scope.Scope,members:List<&2,Key.Key>,+views:List<&2,Snapshot.GraphView>,tick:Tick.Tick,receipts:List>,world:W.World) -> W.World & Buffer.Receipt: match world: case W.World{+namespace,Allocator.Allocator{next,+high,slots,capacity,depth,free,order},columns,resource,events,pending,registrations,nextSystem,clock,lifecycle}:completed(S,C,R,E,P,D,route,Commands.Cleanup{scope},Cleanup.members_step(~S,~C,~R,~E,~P,~D,~resources,~same,~selection,~dispose,U32.to_nat(high),Cleanup.edges(S,views),namespace,Cleanup.descriptors(S,views),members,Cleanup.State{W.World{namespace,Allocator.Allocator{next,high,slots,capacity,depth,free,order},columns,resource,events,pending,registrations,nextSystem,clock,lifecycle},receipts,Cleanup.ForgetMembership{[route]},tick,False{}})) def selected(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~D:Type,~resources:Layout.Layout,~same:{R == Layout.resources(~S,resources) : Type},~selection:Permission.Selection,~dispose:Cleanup.Dispose(~S,~C,~R,~E,~P,~D),+route:Layout.Route,+scope:Scope.Scope,views:List<&2,Snapshot.GraphView>,tick:Tick.Tick,result:W.World & Dispatch.Outcome) -> W.World & Buffer.Receipt: match result: case (world,Dispatch.Completed{Store.Selection{Store.Selected{selectedScope,+members}}}):start(~S,~C,~R,~E,~P,~D,~resources,~same,~selection,~dispose,route,scope,members,views,tick,[Buffer.ScopeStoreApplied{route,Store.Select{scope},Dispatch.Completed{Store.Selection{Store.Selected{selectedScope,members}}}}],world) case (world,result):(world,Buffer.ScopePublished{route,Commands.Cleanup{scope},Buffer.ScopeDenied{Buffer.ScopeStoreRefused{}},[Buffer.ScopeStoreApplied{route,Store.Select{scope},result}],[]}) def validated(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~D:Type,~resources:Layout.Layout,~same:{R == Layout.resources(~S,resources) : Type},~selection:Permission.Selection,~dispose:Cleanup.Dispose(~S,~C,~R,~E,~P,~D),sameNamespace:Bool,context:Context.Context,+route:Layout.Route,+scope:Scope.Scope,views:List<&2,Snapshot.GraphView>,+tick:Tick.Tick,world:W.World) -> W.World & Buffer.Receipt: match sameNamespace: case False{}:denied(S,C,R,E,P,D,route,Commands.Cleanup{scope},Buffer.ForeignNamespace{},world) case True{}:selected(~S,~C,~R,~E,~P,~D,~resources,~same,~selection,~dispose,route,scope,views,tick,Access.apply(~S,~C,~R,~E,~resources,~same,~selection,context,route,Store.Select{scope},world,tick)) def graphs_observed(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~D:Type,~resources:Layout.Layout,~same:{R == Layout.resources(~S,resources) : Type},~selection:Permission.Selection,~dispose:Cleanup.Dispose(~S,~C,~R,~E,~P,~D),context:Context.Context,route:Layout.Route,scope:Scope.Scope,tick:Tick.Tick,result:W.World & List<&2,Snapshot.GraphView>) -> W.World & Buffer.Receipt: match context result: case Context.Context{+namespace}, (world,+views):validated(~S,~C,~R,~E,~P,~D,~resources,~same,~selection,~dispose,Coverage.namespaces(~S,namespace,views),Context.Context{namespace},route,scope,views,tick,world) def cleanup_allowed(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~D:Type,~resources:Layout.Layout,~same:{R == Layout.resources(~S,resources) : Type},~selection:Permission.Selection,~dispose:Cleanup.Dispose(~S,~C,~R,~E,~P,~D),allowed:Bool,context:Context.Context,route:Layout.Route,scope:Scope.Scope,world:W.World,tick:Tick.Tick) -> W.World & Buffer.Receipt: match allowed: case False{}:denied(S,C,R,E,P,D,route,Commands.Cleanup{scope},Buffer.MissingAuthority{},world) case True{}:graphs_observed(~S,~C,~R,~E,~P,~D,~resources,~same,~selection,~dispose,context,route,scope,tick,Access.graphs(~S,~C,~R,~E,~resources,~same,~selection,world)) def assigned(-S:Data,-C:Type,-R:Type,-E:Data,-P:Type,-D:Type,+route:Layout.Route,+scope:Scope.Scope,+handle:W.Handle,result:W.World & Dispatch.Outcome) -> W.World & Buffer.Receipt: match result: case (world,Dispatch.Completed{Store.Assignment{Store.Assigned{actualScope,actualHandle}}}):(world,Buffer.ScopePublished{route,Commands.Assign{scope,handle},Buffer.ScopeComplete{},[Buffer.ScopeStoreApplied{route,Store.Assign{scope,handle},Dispatch.Completed{Store.Assignment{Store.Assigned{actualScope,actualHandle}}}}],[]}) case (world,result):(world,Buffer.ScopePublished{route,Commands.Assign{scope,handle},Buffer.ScopeDenied{Buffer.ScopeStoreRefused{}},[Buffer.ScopeStoreApplied{route,Store.Assign{scope,handle},result}],[]}) # Unprovisioned callers retain the original graph-catalogue requirement. # Provisioned cleanup interprets the schema catalogue without gameplay grants. def cleanup_coverage(~S:Data,~resources:Layout.Layout,relations:Permission.Relations,routes:Maybe<&2,Permission.CleanupRoutes>) -> Bool: match routes: case None{}:Coverage.layout(~S,~resources,resources,Layout.AtLeaf{},relations) case Some{routes}:Coverage.cleanup_complete(~S,~resources,relations,Some{routes}) def apply_checked(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~D:Type,~resources:Layout.Layout,~same:{R == Layout.resources(~S,resources) : Type},~selection:Permission.Selection,~relations:Permission.Relations,~dispose:Cleanup.Dispose(~S,~C,~R,~E,~P,~D),+context:Context.Context,+route:Layout.Route,operation:Commands.Operation,tick:Tick.Tick,result:W.World & Bool) -> W.World & Buffer.Receipt: match operation result: case operation, (world,False{}):denied(S,C,R,E,P,D,route,operation,Buffer.ForeignNamespace{},world) case Commands.Assign{+scope,+handle}, (world,True{}):assigned(S,C,R,E,P,D,route,scope,handle,Access.apply(~S,~C,~R,~E,~resources,~same,~selection,context,route,Store.Assign{scope,handle},world,tick)) case Commands.Cleanup{scope}, (world,True{}):cleanup_allowed(~S,~C,~R,~E,~P,~D,~resources,~same,~Permission.cleanup_selection(S,resources,selection),~dispose,cleanup_coverage(~S,~resources,relations,Permission.cleanup_routes(S,resources,selection)),context,route,scope,world,tick) def apply(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~D:Type,~resources:Layout.Layout,~same:{R == Layout.resources(~S,resources) : Type},~selection:Permission.Selection,~relations:Permission.Relations,~dispose:Cleanup.Dispose(~S,~C,~R,~E,~P,~D),+context:Context.Context,route:Layout.Route,operation:Commands.Operation,world:W.World,tick:Tick.Tick) -> W.World & Buffer.Receipt: apply_checked(~S,~C,~R,~E,~P,~D,~resources,~same,~selection,~relations,~dispose,context,route,operation,tick,Context.matches(~S,~C,~R,~E,~resources,~same,context,world)) def Interpreter(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~D:Type,~resources:Layout.Layout,~same:{R == Layout.resources(~S,resources) : Type}) -> Type: @+context:Context.Context -> Layout.Route -> Commands.Operation -> W.World -> Tick.Tick -> W.World & Buffer.Receipt def unavailable(-S:Data,-C:Type,-R:Type,-E:Data,-P:Type,-D:Type,+route:Layout.Route,+scope:Scope.Scope,result:W.World & Dispatch.Outcome) -> W.World & Buffer.Receipt: match result: case (world,result):(world,Buffer.ScopePublished{route,Commands.Cleanup{scope},Buffer.ScopeDenied{Buffer.ScopeStoreRefused{}},[Buffer.ScopeStoreApplied{route,Store.Select{scope},result}],[]}) def unprotected_checked(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~D:Type,~resources:Layout.Layout,~same:{R == Layout.resources(~S,resources) : Type},~selection:Permission.Selection,+context:Context.Context,+route:Layout.Route,operation:Commands.Operation,tick:Tick.Tick,result:W.World & Bool) -> W.World & Buffer.Receipt: match operation result: case operation, (world,False{}):denied(S,C,R,E,P,D,route,operation,Buffer.ForeignNamespace{},world) case Commands.Assign{+scope,+handle}, (world,True{}):assigned(S,C,R,E,P,D,route,scope,handle,Access.apply(~S,~C,~R,~E,~resources,~same,~selection,context,route,Store.Assign{scope,handle},world,tick)) case Commands.Cleanup{+scope}, (world,True{}):unavailable(S,C,R,E,P,D,route,scope,Access.apply(~S,~C,~R,~E,~resources,~same,~selection,context,route,Store.Select{scope},world,tick)) def unprotected(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~D:Type,~resources:Layout.Layout,~same:{R == Layout.resources(~S,resources) : Type},~selection:Permission.Selection,+context:Context.Context,route:Layout.Route,operation:Commands.Operation,world:W.World,tick:Tick.Tick) -> W.World & Buffer.Receipt: unprotected_checked(~S,~C,~R,~E,~P,~D,~resources,~same,~selection,context,route,operation,tick,Context.matches(~S,~C,~R,~E,~resources,~same,context,world)) # Select fixed code while building the declaration, before the enclosing # application specializes a cleanup engine for an ordinary resource schema. def interpreter(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~D:Type,~resources:Layout.Layout,~same:{R == Layout.resources(~S,resources) : Type},~selection:Permission.Selection,~relations:Permission.Relations,~dispose:Cleanup.Dispose(~S,~C,~R,~E,~P,~D),~protected:Bool) -> Interpreter(~S,~C,~R,~E,~P,~D,~resources,~same): Bool.pick(Interpreter(~S,~C,~R,~E,~P,~D,~resources,~same),protected,apply(~S,~C,~R,~E,~P,~D,~resources,~same,~selection,~relations,~dispose),unprotected(~S,~C,~R,~E,~P,~D,~resources,~same,~selection))