import Base import ./restore_graph_data.bend as R import ./entity_key.bend as Key import ./snapshot_data.bend as Basic import ./relation_types.bend as G import ./relation_graph.bend as Graph import ./relation_failure_reader.bend as Ordinary def contains(+key:Key.Key,entities:List<&2,Basic.Entity>) -> Bool: match entities: case []:False{} case Basic.Entity{other,_} <> rest:Bool.or(Key.equal(key,other),contains(key,rest)) def descriptor_found(~S:Data,found:Bool,descriptor:G.Descriptor,next:Unit -> Maybe<&2,G.Descriptor>) -> Maybe<&2,G.Descriptor>: match found: case True{}:Some{descriptor} case False{}:next(Unit{}) def descriptor(~S:Data,+name:String,declarations:List<&2,Ordinary.Declaration>) -> Maybe<&2,G.Descriptor>: match declarations: case []:None{} case Ordinary.Declaration{+value} <> rest:descriptor_found(~S,String.eq(name,Graph.name(~S,value)),value,_ => descriptor(~S,name,rest)) def absent(~S:Data,value:Maybe<&2,Key.Key>) -> Bool: match value: case None{}:True{} case Some{_}:False{} def added(~S:Data,graph:G.Graph,descriptor:G.Descriptor,source:Key.Key,target:Key.Key) -> G.Graph: match graph: case G.Graph{edges,inverses}:Graph.updated(~S,False{},edges,inverses,descriptor,source,target) def source_checked(~S:Data,valid:Bool,graph:G.Graph,descriptor:G.Descriptor,source:Key.Key,target:Key.Key,next:G.Graph -> R.GraphResult) -> R.GraphResult: match valid: case False{}:R.Rejected{R.InvalidRelation{Graph.name(~S,descriptor),source,target}} case True{}:next(added(~S,graph,descriptor,source,target)) def sources(~S:Data,values:List<&2,Key.Key>,+target:Key.Key,+descriptor:G.Descriptor,+entities:List<&2,Basic.Entity>,+graph:G.Graph,next:G.Graph -> R.GraphResult) -> R.GraphResult: match values: case []:next(graph) case +source <> rest:source_checked(~S,Bool.and(contains(source,entities),Bool.and(contains(target,entities),Bool.and(Bool.not(Key.equal(source,target)),absent(~S,Graph.target(~S,graph,descriptor,source))))),graph,descriptor,source,target,graph => sources(~S,rest,target,descriptor,entities,graph,next)) def groups(~S:Data,values:List<&2,R.Group>,+descriptor:G.Descriptor,+entities:List<&2,Basic.Entity>,graph:G.Graph,next:G.Graph -> R.GraphResult) -> R.GraphResult: match values: case []:next(graph) case R.Group{target,members} <> rest:sources(~S,members,target,descriptor,entities,graph,graph => groups(~S,rest,descriptor,entities,graph,next)) def cycle_checked(~S:Data,cyclic:Bool,descriptor:G.Descriptor,source:Key.Key,target:Key.Key,next:Unit -> R.GraphResult) -> R.GraphResult: match cyclic: case True{}:R.Rejected{R.InvalidRelation{Graph.name(~S,descriptor),source,target}} case False{}:next(Unit{}) def cycles(~S:Data,edges:List<&2,G.Edge>,+graph:G.Graph,next:Unit -> R.GraphResult) -> R.GraphResult: match edges: case []:next(Unit{}) case G.Edge{+descriptor,+source,+target} <> rest:cycle_checked(~S,Graph.cycle(~S,graph,descriptor,source,target),descriptor,source,target,_ => cycles(~S,rest,graph,next)) def acyclic(~S:Data,+graph:G.Graph,next:G.Graph -> R.GraphResult) -> R.GraphResult: match graph: case G.Graph{+edges,+inverses}:cycles(~S,edges,G.Graph{edges,inverses},_ => next(G.Graph{edges,inverses})) def declared(~S:Data,name:String,values:List<&2,R.Group>,entities:List<&2,Basic.Entity>,graph:G.Graph,next:G.Graph -> R.GraphResult,found:Maybe<&2,G.Descriptor>) -> R.GraphResult: match found: case None{}:R.Rejected{R.UnknownRelation{name}} case Some{descriptor}:groups(~S,values,descriptor,entities,graph,graph => acyclic(~S,graph,next)) def relations(~S:Data,values:List<&2,R.Relation>,+entities:List<&2,Basic.Entity>,+declarations:List<&2,Ordinary.Declaration>,graph:G.Graph) -> R.GraphResult: match values: case []:R.Accepted{graph} case R.Relation{+name,groups} <> rest:declared(~S,name,groups,entities,graph,graph => relations(~S,rest,entities,declarations,graph),descriptor(~S,name,declarations)) def validate(~S:Data,values:List<&2,R.Relation>,entities:List<&2,Basic.Entity>,declarations:List<&2,Ordinary.Declaration>) -> R.GraphResult: relations(~S,values,entities,declarations,G.Graph{[],[]})