import Base import entity_key.bend as Key import ./relation_types.bend as G import ./relation_graph.bend as Graph import ./relation_commands.bend as A import ./relation_reorder.bend as Reorder import ./relation_providers.bend as Relations import ./world.bend as W import ./capabilities.bend as Cap import ./hierarchy_traversal.bend as T type Path is Data: Complete{handles:List<&2,W.Handle>} Incomplete{handles:List<&2,W.Handle>,pending:List<&2,W.Handle>} type Frame is Data: Frame{entity:W.Handle,parent:Maybe<&2,W.Handle>,children:List<&2,W.Handle>,ancestors:Path,root:Maybe<&2,W.Handle>,depth:Path,breadth:Path} type Read is Type: Read{lookup:Cap.Request,Maybe<&2,Frame>>} def path(~S:Data,walk:T.Walk,+namespace:U32) -> Path: match walk: case T.Complete{ids}: Complete{Relations.handle_ids(~S,ids,namespace)} case T.Incomplete{ids,pending}: Incomplete{Relations.handle_ids(~S,ids,namespace),Relations.handle_ids(~S,pending,namespace)} def handle(~S:Data,id:Maybe<&2,Key.Key>,namespace:U32) -> Maybe<&2,W.Handle>: match id: case None{}: None{} case Some{id}: Some{W.Handle{namespace,id}} def snapshot(~S:Data,+graph:G.Graph,+descriptor:G.Descriptor,target:W.Handle) -> Frame: match target: case W.Handle{+namespace,+id}: Frame{W.Handle{namespace,id},handle(~S,Graph.target(~S,graph,descriptor,id),namespace),Relations.handle_ids(~S,Graph.inverse(~S,graph,descriptor,id),namespace),path(~S,T.ancestors(~S,graph,descriptor,id),namespace),handle(~S,T.root(T.ancestors(~S,graph,descriptor,id),id),namespace),path(~S,T.descendants(~S,graph,descriptor,id,T.Depth{}),namespace),path(~S,T.descendants(~S,graph,descriptor,id,T.Breadth{}),namespace)} def store_snapshot(~S:Data,~C:Type,store:A.Store,descriptor:G.Descriptor,target:W.Handle) -> A.Store & Frame: match store: case A.Store{components,+graph}: (A.Store{components,graph},snapshot(~S,graph,descriptor,target)) def present(~S:Data,~C:Type,~R:Type,~E:Data,result:W.World,R,A.Notice> & Frame) -> W.World,R,A.Notice> & Maybe<&2,Frame>: (world,frame) = result (world,Some{frame}) def checked(~S:Data,~C:Type,~R:Type,~E:Data,~descriptor:G.HierarchyDescriptor,observed:W.World,R,A.Notice> & Bool,target:W.Handle) -> W.World,R,A.Notice> & Maybe<&2,Frame>: match observed: case (world,False{}): (world,None{}) case (world,True{}): present(~S,~C,~R,~E,W.with_store(~S,~A.Store,~R,~A.Notice,~Frame,store => store_snapshot(~S,~C,store,Reorder.descriptor(~S,descriptor),target),world)) def lookup(~S:Data,~C:Type,~R:Type,~E:Data,~descriptor:G.HierarchyDescriptor,world:W.World,R,A.Notice>,target:W.Handle) -> W.World,R,A.Notice> & Maybe<&2,Frame>: match target: case W.Handle{+ns,+id}: checked(~S,~C,~R,~E,~descriptor,W.valid(~S,~A.Store,~R,~A.Notice,world,W.Handle{ns,id}),W.Handle{ns,id}) def read(-H:Type,-S:Data,caps:Read,owner:H,target:W.Handle) -> H & Maybe<&2,Frame>: match caps: case Read{Cap.Request{get}}: get(owner,target) def grant(~S:Data,~C:Type,~R:Type,~E:Data,~descriptor:G.HierarchyDescriptor,~Out:Type,~body:@-H:Type -> Read -> W.Handle -> H -> H & Out,target:W.Handle,world:W.World,R,A.Notice>) -> W.World,R,A.Notice> & Out: body(W.World,R,A.Notice>,Read{Cap.Request{lookup(~S,~C,~R,~E,~descriptor)}},target,world)