rustsim-core 0.0.1

Core ABM engine: agents, models, stores, schedulers, stepping, data collection
Documentation
#![allow(clippy::type_complexity)]

use rand::rngs::StdRng;
use rand::SeedableRng;
use rustsim_core::prelude::*;
mod support;
use support::NothingSpace;

#[derive(Debug, Clone)]
struct Counter {
    id: AgentId,
    count: u64,
}

impl Agent for Counter {
    fn id(&self) -> AgentId {
        self.id
    }
}

type CounterModel =
    StandardModel<NothingSpace, Counter, VecStore<Counter>, Vec<&'static str>, StdRng, ById>;

fn agent_step(
    agent: &mut Counter,
    ctx: &mut StepContext<'_, NothingSpace, Counter, Vec<&'static str>, StdRng, ById>,
) {
    agent.count += 1;
    ctx.properties_mut().push("agent");
}

fn model_step(model: &mut CounterModel) {
    model.properties_mut().push("model");
}

#[test]
fn standard_model_builder_style_setup_works() {
    let mut store = VecStore::new();
    store.insert(Counter { id: 1, count: 0 });

    let mut model = CounterModel::new_base(
        store,
        NothingSpace,
        ById::new(),
        Vec::new(),
        StdRng::seed_from_u64(1),
    )
    .with_agent_step_ctx(agent_step)
    .with_model_step(model_step)
    .with_agents_first(false);

    model.step();

    assert_eq!(model.properties().as_slice(), &["model", "agent"]);
    assert_eq!(model.agent(1).unwrap().count, 1);
}

#[test]
fn standard_model_convenience_constructors_work() {
    let mut store = VecStore::new();
    store.insert(Counter { id: 1, count: 0 });

    let mut agent_model = CounterModel::new_with_agent_step(
        store,
        NothingSpace,
        ById::new(),
        Vec::new(),
        StdRng::seed_from_u64(1),
        agent_step,
        true,
    );
    agent_model.step();
    assert_eq!(agent_model.agent(1).unwrap().count, 1);

    let mut store2 = VecStore::new();
    store2.insert(Counter { id: 1, count: 0 });
    let mut model_only = CounterModel::new_with_model_step(
        store2,
        NothingSpace,
        ById::new(),
        Vec::new(),
        StdRng::seed_from_u64(1),
        model_step,
        true,
    );
    model_only.step();
    assert_eq!(model_only.properties().as_slice(), &["model"]);
    assert_eq!(model_only.agent(1).unwrap().count, 0);
}