ixa/people/
mod.rs

1//! A generic mechanism for representing people and associated data.
2//!
3//! We have a set of people indexed by [`PersonId`] and then each person
4//! can have an arbitrary number of person properties
5//! [`PersonProperty`], which are values keyed by a type. Person
6//! properties are defined with a macro ([`define_person_property!()`]
7//! or [`define_person_property_with_default!()`])
8//!
9//! # Initializing Person Properties
10//!
11//! Person properties can have their initial values set in several ways:
12//!
13//! * An initial value can be provided at person creation time in
14//!   [`Context::add_person()`].
15//! * The property can have a default value (provided when the
16//!   property is defined.)
17//! * The property can have an initializer function (provided when
18//!   the property is defined) that is called lazily when the
19//!   property is first accessed.
20//!
21//! If neither a default or an initializer is provided, then you
22//! must provide an initial value for each person on person
23//! creation. Failure to do so will generally cause failure of
24//! [`Context::add_person()`].
25//!
26//! # Setting Person Properties
27//!
28//! Properties can also have their values changed with [`Context::set_person_property()`].
29//! If the property is not initialized yet, this will implicitly call the
30//! initializer (or set the default) and then reset the value.
31//!
32//! # Derived Properties
33//!
34//! It is also possible to have a "derived property" whose value is
35//! computed based on a set of other properties. When a derived
36//! property is defined, you must supply a function that takes the
37//! values for those dependencies and computes the current value
38//! of the property. Note that this function cannot access the context
39//! directly and therefore cannot read any other properties. It also
40//! should have a consistent result for any set of inputs, because
41//! it may be called multiple times with those inputs, depending
42//! on the program structure.
43//!
44//! # Change Events
45//!
46//! Whenever a person property `E` has potentially changed, either
47//! because it was set directly or because it is a derived property
48//! and one of its dependencies changed, a
49//! [`PersonPropertyChangeEvent<E>`] will be emitted. Note that Ixa does
50//! not currently check that the new value is actually different from the old value,
51//! so calling [`Context::set_person_property()`] will always emit an event.
52//! Initialization is not considered a change, but [`Context::set_person_property()`]
53//! on a lazily initialized event will emit an event for the change from
54//! the initialized value to the new value.
55//!
56//! # Querying
57//!
58//! Person properties provides an interface to query for people matching
59//! a given set of properties. The basic syntax is to supply a set of
60//! (property, value) pairs, like so `query_people(((Age, 30), (Gender, Female)))`.
61//! Note that these need to be wrapped in an extra set of parentheses
62//! to make them a single tuple to pass to [`Context::query_people()`]. Queries implement
63//! strict equality, so if you want a fancier predicate you need to implement
64//! a derived property that computes it and then query over the derived property.
65//!
66//! The internals of query are deliberately opaque in that Ixa may or
67//! may not ordinarily choose to create caches or indexes for
68//! queries. However, you force an index to be created for a single
69//! property by using [`Context::index_property()`].
70
71mod context_extension;
72mod data;
73mod event;
74pub(crate) mod external_api;
75mod index;
76pub(crate) mod methods;
77mod property;
78mod query;
79pub use query::{Query, QueryAnd};
80
81use crate::{context::Context, define_data_plugin};
82pub use context_extension::ContextPeopleExt;
83use data::PeopleData;
84pub use data::PersonPropertyHolder;
85pub use event::{PersonCreatedEvent, PersonPropertyChangeEvent};
86pub use property::{
87    define_derived_property, define_person_property, define_person_property_with_default,
88    PersonProperty,
89};
90
91use crate::{HashMap, HashMapExt, HashSet, HashSetExt};
92use seq_macro::seq;
93use serde::{Deserialize, Serialize};
94use std::cell::RefCell;
95use std::fmt::{Debug, Display, Formatter};
96use std::{any::TypeId, hash::Hash};
97
98define_data_plugin!(
99    PeoplePlugin,
100    PeopleData,
101    PeopleData {
102        is_initializing: false,
103        current_population: 0,
104        methods: RefCell::new(HashMap::new()),
105        properties_map: RefCell::new(HashMap::new()),
106        registered_properties: RefCell::new(HashSet::new()),
107        dependency_map: RefCell::new(HashMap::new()),
108        property_indexes: RefCell::new(HashMap::new()),
109        people_types: RefCell::new(HashMap::new()),
110    }
111);
112
113/// Represents a unique person.
114//  the id refers to that person's index in the range 0 to population
115// - 1 in the PeopleData container.
116#[derive(Clone, Copy, Hash, PartialEq, Eq, Serialize, Deserialize)]
117pub struct PersonId(pub(crate) usize);
118
119impl Display for PersonId {
120    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
121        write!(f, "{}", self.0)
122    }
123}
124
125impl Debug for PersonId {
126    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
127        write!(f, "Person {}", self.0)
128    }
129}
130
131/// A trait that contains the initialization values for a
132/// new person. Do not use this directly, but instead use
133/// the tuple syntax.
134pub trait InitializationList {
135    fn has_property(&self, t: TypeId) -> bool;
136    fn set_properties(&self, context: &mut Context, person_id: PersonId);
137}
138
139// Implement the query version with 0 and 1 parameters
140impl InitializationList for () {
141    fn has_property(&self, _: TypeId) -> bool {
142        false
143    }
144    fn set_properties(&self, _context: &mut Context, _person_id: PersonId) {}
145}
146
147impl<T1: PersonProperty> InitializationList for (T1, T1::Value) {
148    fn has_property(&self, t: TypeId) -> bool {
149        t == TypeId::of::<T1>()
150    }
151
152    fn set_properties(&self, context: &mut Context, person_id: PersonId) {
153        context.set_person_property(person_id, T1::get_instance(), self.1);
154    }
155}
156
157// Implement the versions with 1..20 parameters.
158macro_rules! impl_initialization_list {
159    ($ct:expr) => {
160        seq!(N in 0..$ct {
161            impl<
162                #(
163                    T~N : PersonProperty,
164                )*
165            > InitializationList for (
166                #(
167                    (T~N, T~N::Value),
168                )*
169            )
170            {
171                fn has_property(&self, t: TypeId) -> bool {
172                    #(
173                        if t == TypeId::of::<T~N>() { return true; }
174                    )*
175                    return false
176                }
177
178                fn set_properties(&self, context: &mut Context, person_id: PersonId)  {
179                    #(
180                       context.set_person_property(person_id, T~N::get_instance(), self.N.1 );
181                    )*
182                }
183            }
184        });
185    }
186}
187
188seq!(Z in 1..20 {
189    impl_initialization_list!(Z);
190});