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 index::{add_multi_property_index, Index, IndexValue};
87pub use property::{
88    define_derived_property, define_person_property, define_person_property_with_default,
89    PersonProperty,
90};
91
92use crate::{HashMap, HashMapExt, HashSet, HashSetExt};
93use seq_macro::seq;
94use serde::{Deserialize, Serialize};
95use std::cell::RefCell;
96use std::fmt::{Debug, Display, Formatter};
97use std::{any::TypeId, hash::Hash};
98
99define_data_plugin!(
100    PeoplePlugin,
101    PeopleData,
102    PeopleData {
103        is_initializing: false,
104        current_population: 0,
105        methods: RefCell::new(HashMap::new()),
106        properties_map: RefCell::new(HashMap::new()),
107        registered_properties: RefCell::new(HashSet::new()),
108        dependency_map: RefCell::new(HashMap::new()),
109        property_indexes: RefCell::new(HashMap::new()),
110        people_types: RefCell::new(HashMap::new()),
111    }
112);
113
114/// Represents a unique person.
115//  the id refers to that person's index in the range 0 to population
116// - 1 in the PeopleData container.
117#[derive(Clone, Copy, Hash, PartialEq, Eq, Serialize, Deserialize)]
118pub struct PersonId(pub(crate) usize);
119
120impl Display for PersonId {
121    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
122        write!(f, "{}", self.0)
123    }
124}
125
126impl Debug for PersonId {
127    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
128        write!(f, "Person {}", self.0)
129    }
130}
131
132/// A trait that contains the initialization values for a
133/// new person. Do not use this directly, but instead use
134/// the tuple syntax.
135pub trait InitializationList {
136    fn has_property(&self, t: TypeId) -> bool;
137    fn set_properties(&self, context: &mut Context, person_id: PersonId);
138}
139
140// Implement the query version with 0 and 1 parameters
141impl InitializationList for () {
142    fn has_property(&self, _: TypeId) -> bool {
143        false
144    }
145    fn set_properties(&self, _context: &mut Context, _person_id: PersonId) {}
146}
147
148impl<T1: PersonProperty> InitializationList for (T1, T1::Value) {
149    fn has_property(&self, t: TypeId) -> bool {
150        t == TypeId::of::<T1>()
151    }
152
153    fn set_properties(&self, context: &mut Context, person_id: PersonId) {
154        context.set_person_property(person_id, T1::get_instance(), self.1);
155    }
156}
157
158// Implement the versions with 1..20 parameters.
159macro_rules! impl_initialization_list {
160    ($ct:expr) => {
161        seq!(N in 0..$ct {
162            impl<
163                #(
164                    T~N : PersonProperty,
165                )*
166            > InitializationList for (
167                #(
168                    (T~N, T~N::Value),
169                )*
170            )
171            {
172                fn has_property(&self, t: TypeId) -> bool {
173                    #(
174                        if t == TypeId::of::<T~N>() { return true; }
175                    )*
176                    return false
177                }
178
179                fn set_properties(&self, context: &mut Context, person_id: PersonId)  {
180                    #(
181                       context.set_person_property(person_id, T~N::get_instance(), self.N.1 );
182                    )*
183                }
184            }
185        });
186    }
187}
188
189seq!(Z in 1..20 {
190    impl_initialization_list!(Z);
191});