use std::collections::HashMap;
use serde_json::Value;
use crate::raster::deck::Agent;
use crate::Hypergraph;
use super::{AttrContract, IngestError, TensionSource, DEFAULT_BLOC};
pub(crate) fn index_of(ids: &[String]) -> HashMap<&str, usize> {
let mut out = HashMap::with_capacity(ids.len());
for (i, id) in ids.iter().enumerate() {
out.insert(id.as_str(), i);
}
out
}
fn read_required_f64(attrs: &Value, key: &str, owner: &str) -> Result<f64, IngestError> {
let v = attrs.get(key).ok_or_else(|| IngestError::MissingNodeAttr {
node: owner.to_string(),
key: key.to_string(),
})?;
let f = v.as_f64().ok_or_else(|| IngestError::BadAttrType {
owner: owner.to_string(),
key: key.to_string(),
})?;
if !f.is_finite() {
return Err(IngestError::BadAttrType {
owner: owner.to_string(),
key: key.to_string(),
});
}
Ok(f)
}
fn read_bloc(
attrs: &Value,
key: &str,
owner: &str,
defaulted: &mut usize,
) -> Result<u8, IngestError> {
match attrs.get(key) {
None => {
*defaulted += 1;
Ok(DEFAULT_BLOC)
}
Some(v) => {
let n = v.as_u64().ok_or_else(|| IngestError::BadAttrType {
owner: owner.to_string(),
key: key.to_string(),
})?;
if n > 2 {
return Err(IngestError::BadAttrType {
owner: owner.to_string(),
key: key.to_string(),
});
}
Ok(n as u8)
}
}
}
fn read_h3_cell(attrs: &Value, key: &str, owner: &str) -> Result<Option<u64>, IngestError> {
match attrs.get(key) {
None => Ok(None),
Some(Value::Number(n)) => n
.as_u64()
.map(Some)
.ok_or_else(|| IngestError::BadAttrType {
owner: owner.to_string(),
key: key.to_string(),
}),
Some(Value::String(s)) => {
s.parse::<u64>()
.map(Some)
.map_err(|_| IngestError::BadAttrType {
owner: owner.to_string(),
key: key.to_string(),
})
}
Some(_) => Err(IngestError::BadAttrType {
owner: owner.to_string(),
key: key.to_string(),
}),
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct IngestedNodes {
pub agents: Vec<Agent>,
pub node_ids: Vec<String>,
pub h3_cells: Vec<Option<u64>>,
pub bloc_defaulted: usize,
pub tension_source: TensionSource,
}
pub fn ingest_nodes(
h: &Hypergraph<Value, Value, Value>,
c: &AttrContract,
repression: f64,
) -> Result<IngestedNodes, IngestError> {
let ids = h.node_ids();
static EMPTY: Value = Value::Null;
let mut any_tension = false;
for id in &ids {
let attrs = h.node_attrs(id).unwrap_or(&EMPTY);
if attrs.get(&c.tension).is_some() {
any_tension = true;
break;
}
}
let tension_source = if any_tension {
TensionSource::Attr
} else {
TensionSource::Derived
};
let mut agents = Vec::with_capacity(ids.len());
let mut h3_cells = Vec::with_capacity(ids.len());
let mut bloc_defaulted = 0usize;
for (idx, id) in ids.iter().enumerate() {
let attrs = h.node_attrs(id).unwrap_or(&EMPTY);
let wealth = read_required_f64(attrs, &c.wealth, id)?;
let subsistence = if any_tension {
let t = read_required_f64(attrs, &c.tension, id)?;
if t < 0.0 {
return Err(IngestError::BadAttrType {
owner: id.clone(),
key: c.tension.clone(),
});
}
if repression.is_finite() && repression > 0.0 {
wealth + t / repression
} else {
wealth
}
} else {
read_required_f64(attrs, &c.subsistence, id)?
};
let bloc = read_bloc(attrs, &c.bloc, id, &mut bloc_defaulted)?;
let h3 = read_h3_cell(attrs, &c.h3_cell, id)?;
agents.push(Agent {
id: idx,
bloc,
wealth,
subsistence,
});
h3_cells.push(h3);
}
Ok(IngestedNodes {
agents,
node_ids: ids,
h3_cells,
bloc_defaulted,
tension_source,
})
}