import Base import ./persistence_declaration.bend as Persistence import ./restore_data.bend as R import ./decode_data.bend as D import ./decode_construction.bend as Constructor import ./bundle_construction.bend as Build import ./ordinary_declaration.bend as Identity type Record is Type: Record{position:R.Position,owner:P,undo:P -> I} type Applied is Type: Applied{position:R.Position,undo:P -> I} type State is Type: State{context:Context,records:List>,returned:List,removed:List

,applied:List>} type FamilyKind is Data:Component{} Resource{} def same_kind(kind:FamilyKind,position:R.Position) -> Bool: match kind position: case Component{} R.Component{_}:True{} case Resource{} R.Resource{}:True{} case _ _:False{} def constructed(~Context:Type,~I:Type,~P:Type,~Custom:Data,position:R.Position,records:List>,returned:List,removed:List

,applied:List>,context:Context,result:Build.Construction>) -> State & R.Dispatch: match result: case Build.Invalid{owner,error}:(State{context,records,List.append(&1,I,returned,[owner]),removed,applied},R.Rejected{error}) case Build.Constructed{owner,undo}:(State{context,Record{position,owner,undo} <> records,returned,removed,applied},R.Staged{}) def stored(~I:Type,~P:Type,~Custom:Data,undo:P -> I,receipt:D.Receipt

) -> Build.Construction>: match receipt: case D.Receipt{owner,D.Accepted{_}}:Build.Constructed{owner,undo} case D.Receipt{owner,D.Rejected{error}}:Build.Invalid{undo(owner),Constructor.Validation{error}} def admitted(~S:Data,~I:Type,~P:Type,~Custom:Data,~identity:Identity.Declaration,~project:P -> P & D.Raw,declaration:Persistence.Constructed,result:Build.Construction>) -> Build.Construction>: match result: case Build.Invalid{owner,error}:Build.Invalid{owner,error} case Build.Constructed{owner,undo}:stored(~I,~P,~Custom,undo,Persistence.validate(~S,~P,~identity,~project,declaration,owner)) def declaration(~S:Data,~I:Type,~P:Type,~Custom:Data,~identity:Identity.Declaration,~project:P -> P & D.Raw,~incoming:I -> I & D.Raw,~build:I -> D.Raw -> Build.Construction,factory:Constructor.FallibleFactory) -> Persistence.Constructed: match factory: case Constructor.FallibleFactory{declaration,_}:declaration def supplied(~S:Data,~Context:Type,~I:Type,~P:Type,~Custom:Data,~identity:Identity.Declaration,~project:P -> P & D.Raw,~incoming:I -> I & D.Raw,~build:I -> D.Raw -> Build.Construction,~factory:Constructor.FallibleFactory,position:R.Position,records:List>,returned:List,removed:List

,applied:List>,observed:Context & I) -> State & R.Dispatch: match observed: case (context,input):constructed(~Context,~I,~P,~Custom,position,records,returned,removed,applied,context,admitted(~S,~I,~P,~Custom,~identity,~project,declaration(~S,~I,~P,~Custom,~identity,~project,~incoming,~build,factory),Constructor.fallible_construct(~S,~I,~P,~Custom,~identity,~project,~incoming,~build,factory,input))) def selected(~S:Data,~Context:Type,~I:Type,~P:Type,~Custom:Data,~identity:Identity.Declaration,~project:P -> P & D.Raw,~incoming:I -> I & D.Raw,~build:I -> D.Raw -> Build.Construction,~factory:Constructor.FallibleFactory,~supply:Context -> D.Raw -> Context & I,matches:Bool,state:State,position:R.Position,field:D.Field) -> State & R.Dispatch: match matches state field: case False{} state _:(state,R.Unhandled{}) case True{} State{context,records,returned,removed,applied} D.Field{_,raw}:supplied(~S,~Context,~I,~P,~Custom,~identity,~project,~incoming,~build,~factory,position,records,returned,removed,applied,supply(context,raw)) def stage(~S:Data,~Context:Type,~I:Type,~P:Type,~Custom:Data,~identity:Identity.Declaration,~project:P -> P & D.Raw,~incoming:I -> I & D.Raw,~build:I -> D.Raw -> Build.Construction,~factory:Constructor.FallibleFactory,~supply:Context -> D.Raw -> Context & I,~kind:FamilyKind,state:State,+position:R.Position,+field:D.Field) -> State & R.Dispatch: match field: case D.Field{+name,+raw}:selected(~S,~Context,~I,~P,~Custom,~identity,~project,~incoming,~build,~factory,~supply,Bool.and(same_kind(kind,position),String.eq(name,Identity.key(~S,~P,identity))),state,position,D.Field{name,raw}) def released(~I:Type,~P:Type,head:I,rest:List) -> List: List.append(&1,I,rest,[head]) def release_records(~I:Type,~P:Type,records:List>,returned:List) -> List: match records: case []:returned case Record{_,owner,undo} <> rest:release_records(~I,~P,rest,undo(owner) <> returned) def release(~Context:Type,~I:Type,~P:Type,state:State) -> State: match state: case State{context,records,returned,removed,applied}:State{context,[],release_records(~I,~P,records,returned),removed,applied} def empty(~Context:Type,~I:Type,~P:Type,context:Context) -> State: State{context,[],[],[],[]} def commit(~Context:Type,~I:Type,~P:Type,state:State) -> State: match state: case State{context,_,_,_,_}:State{context,[],[],[],[]} def take_recovery(~Context:Type,~I:Type,~P:Type,state:State) -> State & List: match state: case State{context,records,returned,removed,applied}:(State{context,records,[],removed,applied},returned)