trace_relation.bend
bendvy-ecs-core@0.1.0.1 · 55 lines · 0 definitions
1import Base
2import bendvy-ecs-core@0.1.0.1/trace_facts.bend as Facts
3import bendvy-ecs-core@0.1.0.1/relation_types.bend as G
4import bendvy-ecs-core@0.1.0.1/world.bend as W
5import bendvy-ecs-core@0.1.0.1/entity_key.bend as Key
6import bendvy-ecs-core@0.1.0.1/entity_key_render.bend as Render
7
8def kind(value:G.RelationKind) -> Facts.RelationKind:
9 match value:
10 case G.Ordinary{}:Facts.OrdinaryRelation{}
11 case G.Hierarchy{}:Facts.HierarchyRelation{}
12def identity(~S:Data,value:G.Descriptor<S>) -> Facts.RelationIdentity:
13 match value:
14 case G.Descriptor{id,name,inverseName,value}:Facts.RelationIdentity{id,name,inverseName,kind(value)}
15def handle(~S:Data,target:W.Handle<S>) -> Facts.HandleValue:
16 match target:
17 case W.Handle{namespace,+key}:Facts.HandleValue{namespace,Key.index(key),Key.generation(key)}
18def error(value:G.Error) -> String:
19 match value:
20 case G.MissingEntity{source}:"MissingEntity:" ++ Render.show(source)
21 case G.MissingTargetEntity{source,target,name}:"MissingTargetEntity:" ++ Render.show(source) ++ ":" ++ Render.show(target) ++ ":" ++ name
22 case G.SelfRelationNotAllowed{source,name}:"SelfRelationNotAllowed:" ++ Render.show(source) ++ ":" ++ name
23 case G.HierarchyCycle{source,target,name}:"HierarchyCycle:" ++ Render.show(source) ++ ":" ++ Render.show(target) ++ ":" ++ name
24 case G.MissingChildEntity{source,child,name}:"MissingChildEntity:" ++ Render.show(source) ++ ":" ++ Render.show(child) ++ ":" ++ name
25 case G.DuplicateChild{source,child,name}:"DuplicateChild:" ++ Render.show(source) ++ ":" ++ Render.show(child) ++ ":" ++ name
26 case G.ChildNotRelatedToParent{source,child,name}:"ChildNotRelatedToParent:" ++ Render.show(source) ++ ":" ++ Render.show(child) ++ ":" ++ name
27 case G.ChildSetMismatch{source,name}:"ChildSetMismatch:" ++ Render.show(source) ++ ":" ++ name
28def outcome(value:Maybe<&2,G.Error>) -> Facts.FactResult:
29 match value:
30 case None{}:Facts.Applied{}
31 case Some{why}:Facts.Refused{error(why)}
32def relate(~S:Data,state:Facts.State,descriptor:G.Descriptor<S>,operation:Facts.Operation,source:W.Handle<S>,target:W.Handle<S>,result:Facts.FactResult) -> Facts.State:
33 match state:
34 case Facts.Off{}:Facts.Off{}
35 case Facts.On{facts}:Facts.On{Facts.RelationFact{identity(~S,descriptor),operation,handle(~S,source),Facts.RelateAction{handle(~S,target)},result} <> facts}
36def relate_applied(~S:Data,state:Facts.State,descriptor:G.Descriptor<S>,source:W.Handle<S>,target:W.Handle<S>,error:Maybe<&2,G.Error>) -> Facts.State:
37 match state:
38 case Facts.Off{}:Facts.Off{}
39 case state:relate(~S,state,descriptor,Facts.ApplyOperation{},source,target,outcome(error))
40def unrelate(~S:Data,state:Facts.State,descriptor:G.Descriptor<S>,operation:Facts.Operation,source:W.Handle<S>,result:Facts.FactResult) -> Facts.State:
41 match state:
42 case Facts.Off{}:Facts.Off{}
43 case Facts.On{facts}:Facts.On{Facts.RelationFact{identity(~S,descriptor),operation,handle(~S,source),Facts.UnrelateAction{},result} <> facts}
44def handles(~S:Data,targets:List<&2,W.Handle<S>>) -> List<&2,Facts.HandleValue>:
45 match targets:
46 case []:[]
47 case target <> rest:handle(~S,target) <> handles(~S,rest)
48def reorder(~S:Data,state:Facts.State,descriptor:G.Descriptor<S>,operation:Facts.Operation,parent:W.Handle<S>,children:List<&2,W.Handle<S>>,result:Facts.FactResult) -> Facts.State:
49 match state:
50 case Facts.Off{}:Facts.Off{}
51 case Facts.On{facts}:Facts.On{Facts.RelationFact{identity(~S,descriptor),operation,handle(~S,parent),Facts.ReorderAction{handles(~S,children)},result} <> facts}
52def reorder_applied(~S:Data,state:Facts.State,descriptor:G.Descriptor<S>,parent:W.Handle<S>,children:List<&2,W.Handle<S>>,error:Maybe<&2,G.Error>) -> Facts.State:
53 match state:
54 case Facts.Off{}:Facts.Off{}
55 case state:reorder(~S,state,descriptor,Facts.ApplyOperation{},parent,children,outcome(error))