import Base import ./deferred_command.bend as Deferred import ./trace_facts.bend as TraceFacts 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 world.bend as W import transaction.bend as X import query.bend as Q import component.bend as Cmp import capabilities.bend as Cap # Relation requirements have arbitrary declaration length. The application C # remains Type; only immutable relation handles/snapshots are Data. type End is Data: Outgoing{} Incoming{} type Requirement is Data: Requirement{key:String,descriptor:G.Descriptor,end:End,mode:Q.Selection,project:Bool} type Spec is Data: Spec{requirements:List<&2,Requirement>} type Value is Data: Missing{} Target{handle:W.Handle} Sources{handles:List<&2,W.Handle>} type Cell is Data: Cell{key:String,end:End,value:Value} type Row is Data: Row{entity:W.Handle,cells:List<&2,Cell>,component:V} def read_relation(~S:Data,key:String,descriptor:G.Descriptor) -> Requirement: Requirement{key,descriptor,Outgoing{},Q.Required{},True{}} def optional_relation(~S:Data,key:String,descriptor:G.Descriptor) -> Requirement: Requirement{key,descriptor,Outgoing{},Q.Optional{},True{}} def read_related(~S:Data,key:String,descriptor:G.Descriptor) -> Requirement: Requirement{key,descriptor,Incoming{},Q.Required{},True{}} def optional_related(~S:Data,key:String,descriptor:G.Descriptor) -> Requirement: Requirement{key,descriptor,Incoming{},Q.Optional{},True{}} def with_relation(~S:Data,descriptor:G.Descriptor) -> Requirement: Requirement{"",descriptor,Outgoing{},Q.Present{},False{}} def without_relation(~S:Data,descriptor:G.Descriptor) -> Requirement: Requirement{"",descriptor,Outgoing{},Q.Absent{},False{}} def with_related(~S:Data,descriptor:G.Descriptor) -> Requirement: Requirement{"",descriptor,Incoming{},Q.Present{},False{}} def without_related(~S:Data,descriptor:G.Descriptor) -> Requirement: Requirement{"",descriptor,Incoming{},Q.Absent{},False{}} def handles_acc(~S:Data,ids:List<&2,Key.Key>,+namespace:U32,acc:List<&2,W.Handle>) -> List<&2,W.Handle>: match ids: case []: List.reverse(&2,W.Handle,acc) case id <> rest: handles_acc(~S,rest,namespace,W.Handle{namespace,id} <> acc) def outgoing_value(~S:Data,value:Maybe<&2,Key.Key>,namespace:U32) -> Value: match value: case None{}: Missing{} case Some{id}: Target{W.Handle{namespace,id}} def incoming_value(~S:Data,ids:List<&2,Key.Key>,namespace:U32) -> Value: match ids: case []: Missing{} case first <> rest: Sources{handles_acc(~S,first <> rest,namespace,[])} def value(~S:Data,graph:G.Graph,descriptor:G.Descriptor,end:End,+namespace:U32,id:Key.Key) -> Value: match end: case Outgoing{}: outgoing_value(~S,Graph.target(~S,graph,descriptor,id),namespace) case Incoming{}: incoming_value(~S,Graph.inverse(~S,graph,descriptor,id),namespace) def presence(~S:Data,value:Value) -> Cmp.Access: match value: case Missing{}: Cmp.ComponentAbsent{} case _: Cmp.Found{True{}} def add_cell(~S:Data,project:Bool,key:String,end:End,value:Value,cells:List<&2,Cell>) -> List<&2,Cell>: match project: case False{}: cells case True{}: Cell{key,end,value} <> cells def requirements(~S:Data,remaining:List<&2,Requirement>,+graph:G.Graph,+namespace:U32,+id:Key.Key,selected:Bool,acc:List<&2,Cell>) -> Bool & List<&2,Cell>: match remaining: case []: (selected,List.reverse(&2,Cell,acc)) case Requirement{key,descriptor,+end,mode,project} <> rest: +observed = value(~S,graph,descriptor,end,namespace,id) requirements(~S,rest,graph,namespace,id,selected && Q.selection_aux(~Bool,mode,presence(~S,observed)),add_cell(~S,project,key,end,observed,acc)) def spec_rows_result(~S:Data,~V:Data,observed:Bool & List<&2,Cell>,handle:W.Handle,component:V) -> Bool & Row: (selected,cells) = observed (selected,Row{handle,cells,component}) def spec_rows(~S:Data,~V:Data,spec:Spec,graph:G.Graph,handle:W.Handle,component:V) -> Bool & Row: match spec handle: case Spec{items} W.Handle{+namespace,+id}: spec_rows_result(~S,~V,requirements(~S,items,graph,namespace,id,True{},[]),W.Handle{namespace,id},component) def graph_read(~S:Data,~C:Type,store:A.Store) -> A.Store & G.Graph: match store: case A.Store{components,+graph}: (A.Store{components,graph},graph) def inspect_done(~S:Data,~V:Data,~C:Type,~R:Type,~E:Data,~spec:Spec,observed:W.World,R,A.Notice> & G.Graph,handle:W.Handle,component:V,undo:List<(W.World,R,A.Notice> -> W.World,R,A.Notice>)>,commands:List,R,A.Notice>>>,events:List<&2,A.Notice>,trace:TraceFacts.State) -> X.Tx,R,A.Notice>,A.Notice> & (Bool & Row): (world,graph) = observed (X.Tx{world,undo,commands,events,trace},spec_rows(~S,~V,spec,graph,handle,component)) def inspect(~S:Data,~V:Data,~C:Type,~R:Type,~E:Data,~spec:Spec,tx:X.Tx,R,A.Notice>,A.Notice>,handle:W.Handle,component:V) -> X.Tx,R,A.Notice>,A.Notice> & (Bool & Row): match tx: case X.Tx{world,undo,commands,events,trace}: inspect_done(~S,~V,~C,~R,~E,~spec,W.with_store(~S,~A.Store,~R,~A.Notice,~G.Graph,graph_read(~S,~C),world),handle,component,undo,commands,events,trace) def row_get(~S:Data,~V:Data,~H:Type,+row:Row,owner:H) -> H & Row: (owner,row) def invoke(~S:Data,~V:Data,~C:Type,~R:Type,~E:Data,~Out:Data,~body:@-H:Type -> Cap.ValueRead> -> H -> H & Out,row:Row,tx:X.Tx,R,A.Notice>,A.Notice>) -> X.Tx,R,A.Notice>,A.Notice> & Out: body(X.Tx,R,A.Notice>,A.Notice>,Cap.ValueRead{row_get(~S,~V,~X.Tx,R,A.Notice>,A.Notice>,row)},tx) def output_prepend(~S:Data,~C:Type,~R:Type,~E:Data,~Out:Data,value:Out,observed:X.Tx,R,A.Notice>,A.Notice> & List<&2,Out>) -> X.Tx,R,A.Notice>,A.Notice> & List<&2,Out>: (tx,rest) = observed (tx,value <> rest) def output_join(~S:Data,~V:Data,~C:Type,~R:Type,~E:Data,~Out:Data,observed:X.Tx,R,A.Notice>,A.Notice> & Out,next:X.Tx,R,A.Notice>,A.Notice> -> X.Tx,R,A.Notice>,A.Notice> & List<&2,Out>) -> X.Tx,R,A.Notice>,A.Notice> & List<&2,Out>: (tx,value) = observed output_prepend(~S,~C,~R,~E,~Out,value,next(tx)) def selected_row(~S:Data,~V:Data,~C:Type,~R:Type,~E:Data,~Out:Data,~body:@-H:Type -> Cap.ValueRead> -> H -> H & Out,observed:X.Tx,R,A.Notice>,A.Notice> & (Bool & Row),next:X.Tx,R,A.Notice>,A.Notice> -> X.Tx,R,A.Notice>,A.Notice> & List<&2,Out>) -> X.Tx,R,A.Notice>,A.Notice> & List<&2,Out>: match observed: case (tx,(False{},_)): next(tx) case (tx,(True{},row)): output_join(~S,~V,~C,~R,~E,~Out,invoke(~S,~V,~C,~R,~E,~Out,~body,row,tx),next) def component_join(~S:Data,~V:Data,~C:Type,~R:Type,~E:Data,~Out:Data,~spec:Spec,~body:@-H:Type -> Cap.ValueRead> -> H -> H & Out,handle:W.Handle,observed:X.Tx,R,A.Notice>,A.Notice> & (Bool & V),next:X.Tx,R,A.Notice>,A.Notice> -> X.Tx,R,A.Notice>,A.Notice> & List<&2,Out>) -> X.Tx,R,A.Notice>,A.Notice> & List<&2,Out>: match observed: case (tx,(False{},_)): next(tx) case (tx,(True{},component)): selected_row(~S,~V,~C,~R,~E,~Out,~body,inspect(~S,~V,~C,~R,~E,~spec,tx,handle,component),next) def scan(~S:Data,~V:Data,~C:Type,~R:Type,~E:Data,~Out:Data,~spec:Spec,~component_selected:X.Tx,R,A.Notice>,A.Notice> -> W.Handle -> X.Tx,R,A.Notice>,A.Notice> & (Bool & V),~body:@-H:Type -> Cap.ValueRead> -> H -> H & Out,handles:List<&2,W.Handle>,tx:X.Tx,R,A.Notice>,A.Notice>) -> X.Tx,R,A.Notice>,A.Notice> & List<&2,Out>: match handles: case []: (tx,[]) case +handle <> rest: component_join(~S,~V,~C,~R,~E,~Out,~spec,~body,handle,component_selected(tx,handle),tx => scan(~S,~V,~C,~R,~E,~Out,~spec,~component_selected,~body,rest,tx)) def finish(~S:Data,~C:Type,~R:Type,~E:Data,~Out:Data,observed:X.Tx,R,A.Notice>,A.Notice> & List<&2,Out>) -> W.World,R,A.Notice> & List<&2,Out>: match observed: case (X.Tx{world,_,_,_,_},values): (world,values) def reserved(~S:Data,~V:Data,~C:Type,~R:Type,~E:Data,~Out:Data,~spec:Spec,~component_selected:X.Tx,R,A.Notice>,A.Notice> -> W.Handle -> X.Tx,R,A.Notice>,A.Notice> & (Bool & V),~body:@-H:Type -> Cap.ValueRead> -> H -> H & Out,observed:W.World,R,A.Notice> & List<&2,W.Handle>) -> W.World,R,A.Notice> & List<&2,Out>: (world,handles) = observed finish(~S,~C,~R,~E,~Out,scan(~S,~V,~C,~R,~E,~Out,~spec,~component_selected,~body,handles,X.begin(~W.World,R,A.Notice>,~A.Notice,world))) def each(~S:Data,~V:Data,~C:Type,~R:Type,~E:Data,~Out:Data,~spec:Spec,~component_selected:X.Tx,R,A.Notice>,A.Notice> -> W.Handle -> X.Tx,R,A.Notice>,A.Notice> & (Bool & V),~body:@-H:Type -> Cap.ValueRead> -> H -> H & Out,world:W.World,R,A.Notice>) -> W.World,R,A.Notice> & List<&2,Out>: reserved(~S,~V,~C,~R,~E,~Out,~spec,~component_selected,~body,W.handles(~S,~A.Store,~R,~A.Notice,world))