execution_composer_schema.bend
bendvy-ecs-core@0.1.0.3 · 67 lines · 20 definitions
1import Base
2import bendvy-ecs-core@0.1.0.3/execution_resource_policy.bend as Policy
3import bendvy-ecs-core@0.1.0.3/clock_state.bend as Clock
4import bendvy-ecs-core@0.1.0.3/ordinary_declaration.bend as Identity
5import bendvy-ecs-core@0.1.0.3/ordinary_schema.bend as Resource
6import bendvy-ecs-core@0.1.0.3/schema_fragments.bend as Fragment
7import bendvy-ecs-core@0.1.0.3/execution_composer_layout.bend as Layout
8import bendvy-ecs-core@0.1.0.3/persistence_declaration.bend as Persist
9import bendvy-ecs-core@0.1.0.3/decode_data.bend as Decode
10import bendvy-ecs-core@0.1.0.3/world.bend as W
11
12type FragmentSchema<-S:Data,-C:Type,-R:Type> is Data:
13 FragmentSchema{entries:List<&2,Fragment.Entry<S>>}
14def component(~S:Data,~P:Type,~V:Data,~refresh:P -> P & V,identity:Identity.Declaration<S,P>) -> Layout.Layout:
15 Layout.LayoutLeaf{P,V,refresh,Identity.key(~S,~P,identity),Policy.Ordinary{}}
16def resource(~S:Data,~P:Type,~V:Data,~refresh:P -> P & V,identity:Resource.Resource<S,P>) -> Layout.Layout:
17 Layout.LayoutLeaf{P,V,refresh,Resource.resource_key(~S,~P,identity),Policy.Ordinary{}}
18def plain(~S:Data,~P:Type,~identity:Identity.Declaration<S,P>,~V:Data,~refresh:P -> P & V,extension:Persist.Plain<S,P,identity>) -> Layout.Layout:
19 Layout.ClassifiedLayoutLeaf{P,V,refresh,Identity.key(~S,~P,identity),Fragment.Plain{},Policy.Ordinary{}}
20def transient(~S:Data,~P:Type,~identity:Identity.Declaration<S,P>,~V:Data,~refresh:P -> P & V,extension:Persist.Transient<S,P,identity>) -> Layout.Layout:
21 Layout.ClassifiedLayoutLeaf{P,V,refresh,Identity.key(~S,~P,identity),Fragment.Transient{},Policy.Ordinary{}}
22def constructed(~S:Data,~P:Type,~identity:Identity.Declaration<S,P>,~project:P -> P & Decode.Raw,~V:Data,~refresh:P -> P & V,extension:Persist.Constructed<S,P,identity,project>) -> Layout.Layout:
23 Layout.ClassifiedLayoutLeaf{P,V,refresh,Identity.key(~S,~P,identity),Fragment.Constructed{},Policy.Ordinary{}}
24def layout_entries(~S:Data,+kind:Fragment.RegistryKind,layout:Layout.Layout) -> List<&2,Fragment.Entry<S>>:
25 match layout:
26 case Layout.EmptyLayout{}:[]
27 case Layout.LayoutLeaf{_,_,_,+key,_}:[Fragment.Entry{kind,key,key}]
28 case Layout.ClassifiedLayoutLeaf{_,_,_,+key,persistence,_}:[Fragment.Classified{kind,key,key,persistence}]
29 case Layout.LayoutPair{left,right}:List.append(&2,Fragment.Entry<S>,layout_entries(~S,kind,left),layout_entries(~S,kind,right))
30def inventory(~S:Data,~components:Layout.Layout,~resources:Layout.Layout) -> List<&2,Fragment.Entry<S>>:
31 List.append(&2,Fragment.Entry<S>,layout_entries(~S,Fragment.Component{},components),layout_entries(~S,Fragment.Resource{},resources))
32def declared(~S:Data,~components:Layout.Layout,~resources:Layout.Layout) -> FragmentSchema<S,Layout.columns(~S,components),Layout.resources(~S,resources)>:
33 FragmentSchema{inventory(~S,~components,~resources)}
34def entries(~S:Data,~C:Type,~R:Type,value:FragmentSchema<S,C,R>) -> List<&2,Fragment.Entry<S>>:
35 match value:
36 case FragmentSchema{entries}:entries
37def fragment(~S:Data,~components:Layout.Layout,~resources:Layout.Layout,~Provisions:Type,provisions:Provisions) -> Fragment.Fragment<S,Provisions>:
38 Fragment.Fragment{inventory(~S,~components,~resources),provisions}
39def validate(~S:Data,~components:Layout.Layout,~resources:Layout.Layout) -> Fragment.Validation:
40 Fragment.validate(~S,inventory(~S,~components,~resources))
41def empty_columns(~S:Data,~components:Layout.Layout,_:Unit) -> Layout.columns(~S,components):
42 Layout.empty_columns(~S,components)
43type Initialized<-S:Data,-Components:Layout.Layout,-Resources:Layout.Layout,-E:Data> is Type:
44 SchemaCreated{factory:W.Factory,world:W.World<S,Layout.columns(~S,Components),Layout.resources(~S,Resources),E>}
45 SchemaRefused{factory:W.Factory,resources:Layout.resources(~S,Resources),error:Fragment.Error}
46 NamespaceRefused{factory:W.Factory,resources:Layout.resources(~S,Resources)}
47def initialized_world(~S:Data,~components:Layout.Layout,~resources:Layout.Layout,~E:Data,world:W.World<S,Layout.columns(~S,components),Layout.resources(~S,resources),E>) -> W.World<S,Layout.columns(~S,components),Layout.resources(~S,resources),E>:
48 match world:
49 case W.World{namespace,allocator,store,owners,events,pending,registrations,next,+clock,lifecycle}:W.World{namespace,allocator,store,Layout.initialize_resources(~S,resources,Clock.current(clock),owners),events,pending,registrations,next,clock,lifecycle}
50def created(~S:Data,~components:Layout.Layout,~resources:Layout.Layout,~E:Data,result:W.Creation<S,Layout.columns(~S,components),Layout.resources(~S,resources),E>) -> Initialized<S,components,resources,E>:
51 match result:
52 case W.Created{factory,world}:SchemaCreated{factory,initialized_world(~S,~components,~resources,~E,world)}
53 case W.CreateRejected{factory,resource}:NamespaceRefused{factory,resource}
54def create_checked(~S:Data,~components:Layout.Layout,~resources:Layout.Layout,~E:Data,result:Fragment.Validation,factory:W.Factory,owners:Layout.resources(~S,resources)) -> Initialized<S,components,resources,E>:
55 match result:
56 case Fragment.Invalid{error}:SchemaRefused{factory,owners,error}
57 case Fragment.Valid{}:created(~S,~components,~resources,~E,W.create(~S,~Layout.columns(~S,components),~Layout.resources(~S,resources),~E,~empty_columns(~S,~components),factory,owners))
58def create(~S:Data,~components:Layout.Layout,~resources:Layout.Layout,~E:Data,factory:W.Factory,owners:Layout.resources(~S,resources)) -> Initialized<S,components,resources,E>:
59 create_checked(~S,~components,~resources,~E,validate(~S,~components,~resources),factory,owners)
60
61
62def trusted_create_seeded_checked(~S:Data,~components:Layout.Layout,~resources:Layout.Layout,~E:Data,~seed:Unit -> Layout.columns(~S,components),result:Fragment.Validation,factory:W.Factory,owners:Layout.resources(~S,resources)) -> Initialized<S,components,resources,E>:
63 match result:
64 case Fragment.Invalid{error}:SchemaRefused{factory,owners,error}
65 case Fragment.Valid{}:created(~S,~components,~resources,~E,W.create(~S,~Layout.columns(~S,components),~Layout.resources(~S,resources),~E,~seed,factory,owners))
66def trusted_create_seeded(~S:Data,~components:Layout.Layout,~resources:Layout.Layout,~E:Data,~seed:Unit -> Layout.columns(~S,components),factory:W.Factory,owners:Layout.resources(~S,resources)) -> Initialized<S,components,resources,E>:
67 trusted_create_seeded_checked(~S,~components,~resources,~E,~seed,validate(~S,~components,~resources),factory,owners)