trace_facts.bend

bendvy-ecs-core@0.1.0.3 · 111 lines · 22 definitions

1import Base
2# Detached operation data. Affine ECS owners never enter this journal.
3type Category is Data:
4  ComponentValue{}
5  ResourceValue{}
6  EventValue{}
7  NextStateValue{}
8  DeferredCommand{}
9  RelationOperation{}
10  TransitionHandler{}
11  MissedValue{}
12type Operation is Data:
13  ReadOperation{}
14  WriteOperation{}
15  RemoveOperation{}
16  SendOperation{}
17  StageOperation{}
18  ApplyOperation{}
19  HandlerOperation{}
20type FactResult is Data:
21  Attempted{}
22  Applied{}
23  Refused{reason:String}
24  RolledBack{}
25type Value is Data:
26  OpaqueValue{}
27  UnavailableValue{}
28  DisplayedValue{text:String}
29type HandleValue is Data:
30  HandleValue{namespace:U32,index:U32,generation:U32}
31type RelationKind is Data:
32  OrdinaryRelation{}
33  HierarchyRelation{}
34type RelationIdentity is Data:
35  RelationIdentity{id:U32,name:String,inverseName:String,kind:RelationKind}
36type RelationAction is Data:
37  RelateAction{target:HandleValue}
38  UnrelateAction{}
39  ReorderAction{children:List<&2,HandleValue>}
40type HandlerPhase is Data:
41  ExitPhase{}
42  TransitionPhase{}
43  EnterPhase{}
44  InitialEnterPhase{}
45type Boundary is Data:
46  PhaseBoundary{name:String}
47  BarrierEntered{}
48  BarrierFinished{}
49type Fact is Data:
50  ReaderFact{namespace:U32,id:U32,name:String,readerId:U32,domain:String,lagged:Bool}
51  BoundaryFact{boundary:Boundary}
52  Fact{category:Category,key:String,operation:Operation,subject:Maybe<&2,String>,before:Value,after:Value,result:FactResult}
53  RelationFact{identity:RelationIdentity,operation:Operation,source:HandleValue,action:RelationAction,result:FactResult}
54  HandlerFact{namespace:U32,id:U32,name:String,ordinal:U32,phase:HandlerPhase,from:Value,to:Value,result:FactResult}
55  TransitionFact{key:String,from:Value,to:Value,result:FactResult}
56type State is Data:
57  Off{}
58  On{reverseFacts:List<&2,Fact>}
59def enabled(state:State) -> Bool:
60  match state:
61    case Off{}:False{}
62    case On{_}:True{}
63def append(state:State,fact:Fact) -> State:
64  match state:
65    case Off{}:Off{}
66    case On{facts}:On{fact <> facts}
67def facts(state:State) -> List<&2,Fact>:
68  match state:
69    case Off{}:[]
70    case On{facts}:List.reverse(&2,Fact,facts)
71def annotate_newest(state:State,before:Value,after:Value) -> State:
72  match state:
73    case Off{}:Off{}
74    case On{[]}:On{[]}
75    case On{Fact{category,key,operation,subject,_,_,result} <> rest}:On{Fact{category,key,operation,subject,before,after,result} <> rest}
76    case On{RelationFact{identity,operation,source,action,result} <> rest}:On{RelationFact{identity,operation,source,action,result} <> rest}
77    case On{HandlerFact{namespace,id,name,ordinal,phase,_,_,result} <> rest}:On{HandlerFact{namespace,id,name,ordinal,phase,before,after,result} <> rest}
78    case On{TransitionFact{key,_,_,result} <> rest}:On{TransitionFact{key,before,after,result} <> rest}
79    case On{ReaderFact{namespace,id,name,readerId,domain,lagged} <> rest}:On{ReaderFact{namespace,id,name,readerId,domain,lagged} <> rest}
80    case On{BoundaryFact{boundary} <> rest}:On{BoundaryFact{boundary} <> rest}
81def settled_result(result:FactResult,committed:Bool) -> FactResult:
82  match result committed:
83    case Attempted{} True{}:Applied{}
84    case Attempted{} False{}:RolledBack{}
85    case result _:result
86def settle_facts(facts:List<&2,Fact>,+committed:Bool) -> List<&2,Fact>:
87  match facts:
88    case []:[]
89    case ReaderFact{namespace,id,name,readerId,domain,lagged} <> rest:ReaderFact{namespace,id,name,readerId,domain,lagged} <> settle_facts(rest,committed)
90    case BoundaryFact{boundary} <> rest:BoundaryFact{boundary} <> settle_facts(rest,committed)
91    case Fact{category,key,operation,subject,before,after,result} <> rest:Fact{category,key,operation,subject,before,after,settled_result(result,committed)} <> settle_facts(rest,committed)
92    case RelationFact{identity,operation,source,action,result} <> rest:RelationFact{identity,operation,source,action,settled_result(result,committed)} <> settle_facts(rest,committed)
93    case HandlerFact{namespace,id,name,ordinal,phase,from,to,result} <> rest:HandlerFact{namespace,id,name,ordinal,phase,from,to,settled_result(result,committed)} <> settle_facts(rest,committed)
94    case TransitionFact{key,from,to,result} <> rest:TransitionFact{key,from,to,settled_result(result,committed)} <> settle_facts(rest,committed)
95def settle(state:State,committed:Bool) -> State:
96  match state:
97    case Off{}:Off{}
98    case On{facts}:On{settle_facts(facts,committed)}
99
100def phase_boundary(state:State,name:String) -> State:
101  match state:
102    case Off{}:Off{}
103    case On{facts}:On{BoundaryFact{PhaseBoundary{name}} <> facts}
104def barrier_entered(state:State) -> State:
105  match state:
106    case Off{}:Off{}
107    case On{facts}:On{BoundaryFact{BarrierEntered{}} <> facts}
108def barrier_finished(state:State) -> State:
109  match state:
110    case Off{}:Off{}
111    case On{facts}:On{BoundaryFact{BarrierFinished{}} <> facts}