execution_query_matches.bend
bendvy-ecs-core@0.1.0.3 · 67 lines · 14 definitions
1import Base
2import bendvy-ecs-core@0.1.0.3/lifecycle.bend as Life
3import bendvy-ecs-core@0.1.0.3/execution_query_domain.bend as D
4
5type Recipe is Data:
6 Read{domain:D.Domain}
7 Write{domain:D.Domain}
8 Both{left:Recipe,right:Recipe}
9type Presence is Data:
10 Optional{}
11 Required{}
12 With{}
13 Without{}
14type Row<-V:Data> is Data:
15 Row{offset:U32,view:Maybe<&2,V>}
16 StampedRow{offset:U32,view:Maybe<&2,V>,stamp:Life.Stamp}
17
18def row_offset(-V:Data,row:Row<V>) -> U32:
19 match row:
20 case Row{offset,_}:offset
21 case StampedRow{offset,_,_}:offset
22def row_view(-V:Data,row:Row<V>) -> Maybe<&2,V>:
23 match row:
24 case Row{_,view}:view
25 case StampedRow{_,view,_}:view
26def row_stamp(-V:Data,row:Row<V>) -> Life.Stamp:
27 match row:
28 case Row{_,_}:Life.Stamp{None{},None{}}
29 case StampedRow{_,None{},_}:Life.Stamp{None{},None{}}
30 case StampedRow{_,Some{_},stamp}:stamp
31type Single<-V:Data> is Data:
32 NoMatch{}
33 One{row:Row<V>}
34 Multiple{}
35def present(-V:Data,value:Maybe<&2,V>) -> Bool:
36 match value:
37 case None{}:False{}
38 case Some{_}:True{}
39def membership(-V:Data,presence:Presence,value:Maybe<&2,V>) -> Bool:
40 match presence:
41 case Optional{}:True{}
42 case Required{}:present(V,value)
43 case With{}:present(V,value)
44 case Without{}:Bool.not(present(V,value))
45def selected_row(-V:Data,offset:U32,view:Maybe<&2,V>,rows:List<&2,Row<V>>,matched:Bool) -> List<&2,Row<V>>:
46 match matched:
47 case False{}:rows
48 case True{}:Row{offset,view} <> rows
49def single(-V:Data,rows:List<&2,Row<V>>) -> Single<V>:
50 match rows:
51 case []:NoMatch{}
52 case row <> []:One{row}
53 case _ <> _ <> _:Multiple{}
54def count(-V:Data,rows:List<&2,Row<V>>) -> U32:
55 match rows:
56 case []:0
57 case _ <> rest:U32.add(1,count(V,rest))
58
59def inventory_valid(result:D.Validation) -> Bool:
60 match result:
61 case D.Valid{}:True{}
62 case _:False{}
63def validate_recipe(+capacity:U32,recipe:Recipe) -> Bool:
64 match recipe:
65 case Read{domain}:inventory_valid(D.validate(capacity,domain))
66 case Write{domain}:inventory_valid(D.validate(capacity,domain))
67 case Both{left,right}:Bool.and(validate_recipe(capacity,left),validate_recipe(capacity,right))