ordinary_app_hierarchy.bend
bendvy-ecs-core@0.1.0.1 · 73 lines · 0 definitions
1import Base
2import bendvy-ecs-core@0.1.0.1/ordinary_app_relation.bend as Relation
3import bendvy-ecs-core@0.1.0.1/ordinary_app_composer.bend as Build
4import bendvy-ecs-core@0.1.0.1/ordinary_app_observation_data.bend as DTO
5import bendvy-ecs-core@0.1.0.1/ordinary_app_registration_fields.bend as Fields
6import bendvy-ecs-core@0.1.0.1/relation_failure_reader.bend as Ord
7import bendvy-ecs-core@0.1.0.1/relation_types.bend as G
8import bendvy-ecs-core@0.1.0.1/relation_reorder.bend as Reorder
9import bendvy-ecs-core@0.1.0.1/hierarchy_traversal_provider.bend as H
10import bendvy-ecs-core@0.1.0.1/hierarchy_provider.bend as Path
11import bendvy-ecs-core@0.1.0.1/hierarchy_traversal.bend as Traversal
12import bendvy-ecs-core@0.1.0.1/relation_providers.bend as P
13import bendvy-ecs-core@0.1.0.1/relation_commands.bend as A
14import bendvy-ecs-core@0.1.0.1/transaction.bend as Tx
15import bendvy-ecs-core@0.1.0.1/world.bend as W
16
17# One hierarchy declaration supplies the installed relation and traversal authority.
18# Trusted setup projection of a hierarchy descriptor into its relation declaration.
19# Hierarchy authority retains the descriptor, rather than relying on its display name.
20def declaration(~S:Data,hierarchy:G.HierarchyDescriptor<S>) -> Ord.Declaration<S>:
21 Ord.relation(~S,Reorder.descriptor(~S,hierarchy))
22# Hierarchy App with the relation storage/notice wrappers and installed observer.
23# Its World and registration owners are affine and returned by the operations below.
24def Application(~S:Data,~C:Type,~R:Type,~E:Data,~hierarchy:G.HierarchyDescriptor<S>,~Owners:Type,~Rows:Data,~prior:Relation.World(~S,~C,~R,~E) -> Relation.World(~S,~C,~R,~E) & DTO.Observation<Rows>,~fold:Owners -> Owners & List<&2,Fields.Fields<S>>) -> Type:
25 Relation.Application(~S,~C,~R,~E,~declaration(~S,hierarchy),~Owners,~Rows,~prior,~fold)
26# Install hierarchy observation on the existing App, preserving World and owners.
27# Does not create parent edges. See docs/parity/hierarchy.md for accepted App and
28# traversal scenarios; relate_app followed by barrier_app establishes visible edges.
29def install(~S:Data,~C:Type,~R:Type,~E:Data,~hierarchy:G.HierarchyDescriptor<S>,~Owners:Type,~Rows:Data,~prior:Relation.World(~S,~C,~R,~E) -> Relation.World(~S,~C,~R,~E) & DTO.Observation<Rows>,~fold:Owners -> Owners & List<&2,Fields.Fields<S>>,app:Build.Registered<S,Relation.World(~S,~C,~R,~E),Owners,Rows,prior,fold>) -> Application(~S,~C,~R,~E,~hierarchy,~Owners,~Rows,~prior,~fold):
30 Relation.install(~S,~C,~R,~E,~declaration(~S,hierarchy),~Owners,~Rows,~prior,~fold,app)
31def returned(~S:Data,~C:Type,~R:Type,~E:Data,~hierarchy:G.HierarchyDescriptor<S>,~Owners:Type,~Rows:Data,~prior:Relation.World(~S,~C,~R,~E) -> Relation.World(~S,~C,~R,~E) & DTO.Observation<Rows>,~fold:Owners -> Owners & List<&2,Fields.Fields<S>>,~Value:Data,owners:Owners,recipe:Relation.Recipe(~S,~C,~R,~E,~declaration(~S,hierarchy),~Owners,~Rows,~prior,~fold),debug:Bool,result:Relation.World(~S,~C,~R,~E) & Value) -> Application(~S,~C,~R,~E,~hierarchy,~Owners,~Rows,~prior,~fold) & Value:
32 match result:
33 case (world,value):(Build.Registered{world,owners,recipe,debug},value)
34# Return the App and root of a valid target, including the target itself when it
35# has no parent. Invalid targets or incomplete ancestor traversal return None.
36# Reads current edges without applying queued changes.
37def root_app(~S:Data,~C:Type,~R:Type,~E:Data,~hierarchy:G.HierarchyDescriptor<S>,~Owners:Type,~Rows:Data,~prior:Relation.World(~S,~C,~R,~E) -> Relation.World(~S,~C,~R,~E) & DTO.Observation<Rows>,~fold:Owners -> Owners & List<&2,Fields.Fields<S>>,app:Application(~S,~C,~R,~E,~hierarchy,~Owners,~Rows,~prior,~fold),target:W.Handle<S>) -> Application(~S,~C,~R,~E,~hierarchy,~Owners,~Rows,~prior,~fold) & Maybe<&2,W.Handle<S>>:
38 match app:
39 case Build.Registered{world,owners,recipe,debug}:returned(~S,~C,~R,~E,~hierarchy,~Owners,~Rows,~prior,~fold,~Maybe<&2,W.Handle<S>>,owners,recipe,debug,H.lookup_root(~S,~C,~R,~E,~hierarchy,world,target))
40# Return the App and raw ancestor path, nearest parent first, excluding target.
41# None means invalid target; Some may be Complete or an Incomplete diagnostic.
42# The seed is validated; this raw path is not a query that revalidates every member.
43def ancestors_app(~S:Data,~C:Type,~R:Type,~E:Data,~hierarchy:G.HierarchyDescriptor<S>,~Owners:Type,~Rows:Data,~prior:Relation.World(~S,~C,~R,~E) -> Relation.World(~S,~C,~R,~E) & DTO.Observation<Rows>,~fold:Owners -> Owners & List<&2,Fields.Fields<S>>,app:Application(~S,~C,~R,~E,~hierarchy,~Owners,~Rows,~prior,~fold),target:W.Handle<S>) -> Application(~S,~C,~R,~E,~hierarchy,~Owners,~Rows,~prior,~fold) & Maybe<&2,Path.Path<S>>:
44 match app:
45 case Build.Registered{world,owners,recipe,debug}:returned(~S,~C,~R,~E,~hierarchy,~Owners,~Rows,~prior,~fold,~Maybe<&2,Path.Path<S>>,owners,recipe,debug,H.lookup_ancestors(~S,~C,~R,~E,~hierarchy,world,target))
46# Return the App and raw descendants, excluding target, in requested depth-first
47# or breadth-first order using stored child order. None means invalid target.
48# Some Incomplete preserves visited/pending diagnostics; forged cyclic graphs are
49# not promised complete traversal. Queued reparenting needs a barrier first.
50def descendants_app(~S:Data,~C:Type,~R:Type,~E:Data,~hierarchy:G.HierarchyDescriptor<S>,~Owners:Type,~Rows:Data,~prior:Relation.World(~S,~C,~R,~E) -> Relation.World(~S,~C,~R,~E) & DTO.Observation<Rows>,~fold:Owners -> Owners & List<&2,Fields.Fields<S>>,app:Application(~S,~C,~R,~E,~hierarchy,~Owners,~Rows,~prior,~fold),target:W.Handle<S>,order:Traversal.Order) -> Application(~S,~C,~R,~E,~hierarchy,~Owners,~Rows,~prior,~fold) & Maybe<&2,Path.Path<S>>:
51 match app:
52 case Build.Registered{world,owners,recipe,debug}:returned(~S,~C,~R,~E,~hierarchy,~Owners,~Rows,~prior,~fold,~Maybe<&2,Path.Path<S>>,owners,recipe,debug,H.lookup_descendants(~S,~C,~R,~E,~hierarchy,world,H.Descendants{target,order}))
53# Stage a child (source) to parent (target) edge, returning App, queue status and
54# transaction outcome separately. Applied hierarchy edges enforce one parent and
55# reject self/cycle violations; barrier-time rejection appears as relation notices.
56def relate_app(~S:Data,~C:Type,~R:Type,~E:Data,~hierarchy:G.HierarchyDescriptor<S>,~Owners:Type,~Rows:Data,~prior:Relation.World(~S,~C,~R,~E) -> Relation.World(~S,~C,~R,~E) & DTO.Observation<Rows>,~fold:Owners -> Owners & List<&2,Fields.Fields<S>>,~Error:Data,app:Application(~S,~C,~R,~E,~hierarchy,~Owners,~Rows,~prior,~fold),targets:P.Targets<S>,outcome:Tx.Outcome<Error>) -> Application(~S,~C,~R,~E,~hierarchy,~Owners,~Rows,~prior,~fold) & DTO.Product<A.QueueStatus,Tx.Outcome<Error>>:
57 Relation.relate_app(~S,~C,~R,~E,~declaration(~S,hierarchy),~Owners,~Rows,~prior,~fold,~Error,app,targets,outcome)
58# Apply all pending World structural commands and return the hierarchy App.
59# Use before traversal or observation when queued graph changes must be visible.
60def barrier_app(~S:Data,~C:Type,~R:Type,~E:Data,~hierarchy:G.HierarchyDescriptor<S>,~Owners:Type,~Rows:Data,~prior:Relation.World(~S,~C,~R,~E) -> Relation.World(~S,~C,~R,~E) & DTO.Observation<Rows>,~fold:Owners -> Owners & List<&2,Fields.Fields<S>>,app:Application(~S,~C,~R,~E,~hierarchy,~Owners,~Rows,~prior,~fold)) -> Application(~S,~C,~R,~E,~hierarchy,~Owners,~Rows,~prior,~fold):
61 Relation.barrier_app(~S,~C,~R,~E,~declaration(~S,hierarchy),~Owners,~Rows,~prior,~fold,app)
62# Return the hierarchy App and optional combined snapshot under flag enablement.
63# Observation does not itself apply pending graph commands.
64def snapshot_app(~S:Data,~C:Type,~R:Type,~E:Data,~hierarchy:G.HierarchyDescriptor<S>,~Owners:Type,~Rows:Data,~prior:Relation.World(~S,~C,~R,~E) -> Relation.World(~S,~C,~R,~E) & DTO.Observation<Rows>,~fold:Owners -> Owners & List<&2,Fields.Fields<S>>,app:Application(~S,~C,~R,~E,~hierarchy,~Owners,~Rows,~prior,~fold),flag:Bool) -> Application(~S,~C,~R,~E,~hierarchy,~Owners,~Rows,~prior,~fold) & Maybe<&2,DTO.Product<List<&2,DTO.Binding<S>>,DTO.Observation<Relation.ObservedRows(~S,~Rows)>>>:
65 Relation.snapshot_app(~S,~C,~R,~E,~declaration(~S,hierarchy),~Owners,~Rows,~prior,~fold,app,flag)
66
67# Stage an exact permutation of a parent's complete current child set, then finish
68# with the supplied transaction outcome. Queue admission is not application success.
69# Missing, duplicate, unrelated or mismatched child sets are rejected at application
70# without changing edges, with a failure notice; apply through barrier_app.
71def reorder_app(~S:Data,~C:Type,~R:Type,~E:Data,~hierarchy:G.HierarchyDescriptor<S>,~Owners:Type,~Rows:Data,~prior:Relation.World(~S,~C,~R,~E) -> Relation.World(~S,~C,~R,~E) & DTO.Observation<Rows>,~fold:Owners -> Owners & List<&2,Fields.Fields<S>>,~Error:Data,app:Application(~S,~C,~R,~E,~hierarchy,~Owners,~Rows,~prior,~fold),args:Reorder.Args<S>,outcome:Tx.Outcome<Error>) -> Application(~S,~C,~R,~E,~hierarchy,~Owners,~Rows,~prior,~fold) & DTO.Product<A.QueueStatus,Tx.Outcome<Error>>:
72 match app:
73 case Build.Registered{world,owners,recipe,debug}:Relation.queued(~S,~C,~R,~E,~declaration(~S,hierarchy),~Relation.builtin_row(~S),~Owners,~Rows,~prior,~fold,~Error,owners,recipe,debug,outcome,Reorder.invoke(~S,~C,~R,~E,~hierarchy,~A.QueueStatus,~Reorder.body(~S),args,Tx.begin(~Relation.World(~S,~C,~R,~E),~A.Notice<S,E>,world)))