use serde::{Deserialize, Serialize};
use crate::ast::{ScalarField, VectorField};
use crate::coupling::{Coupling, EdgePathRule};
use crate::registry::{FieldId, FieldRegistry};
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct FieldProjection {
pub registry: FieldRegistry,
pub couplings: Vec<Coupling>,
pub edge_path_rules: Vec<EdgePathRule>,
pub z_field: Option<FieldId>,
}
impl FieldProjection {
pub fn new() -> Self {
Self::default()
}
pub fn add_scalar(&mut self, name: impl Into<String>, field: ScalarField) -> FieldId {
self.registry.insert_scalar(name, field)
}
pub fn add_vector(&mut self, name: impl Into<String>, field: VectorField) -> FieldId {
self.registry.insert_vector(name, field)
}
pub fn add_coupling(&mut self, coupling: Coupling) {
self.couplings.push(coupling);
}
pub fn add_edge_path_rule(&mut self, rule: EdgePathRule) {
self.edge_path_rules.push(rule);
}
}
pub struct FieldProjectionBuilder;
#[cfg(test)]
mod tests {
use super::*;
use crate::coupling::{CouplingResponse, EdgePath, NodeSelector};
#[test]
fn new_projection_is_empty() {
let p = FieldProjection::new();
assert!(p.registry.is_empty());
assert!(p.couplings.is_empty());
assert!(p.edge_path_rules.is_empty());
assert!(p.z_field.is_none());
}
#[test]
fn add_scalar_returns_lookup_id() {
let mut p = FieldProjection::new();
let id = p.add_scalar("focus", ScalarField::gaussian_at(0.0, 0.0, 10.0));
assert_eq!(p.registry.lookup("focus"), Some(id));
}
#[test]
fn add_coupling_appends() {
let mut p = FieldProjection::new();
let id = p.add_scalar("f", ScalarField::Const(1.0));
p.add_coupling(Coupling::new(
numen::CouplingId::from_uuid(uuid::Uuid::from_u128(0)),
id,
NodeSelector::All,
CouplingResponse::AttractToMin,
1.0,
));
assert_eq!(p.couplings.len(), 1);
}
#[test]
fn add_edge_path_rule_appends() {
let mut p = FieldProjection::new();
p.add_edge_path_rule(EdgePathRule {
edge_kind: "cites".into(),
path: EdgePath::Straight,
});
assert_eq!(p.edge_path_rules.len(), 1);
}
#[test]
fn serde_roundtrip_preserves_projection() {
let mut p = FieldProjection::new();
let id = p.add_scalar("focus", ScalarField::gaussian_at(0.0, 0.0, 10.0));
p.z_field = Some(id);
p.add_coupling(Coupling::new(
numen::CouplingId::from_uuid(uuid::Uuid::from_u128(1)),
id,
NodeSelector::Kind("paper".into()),
CouplingResponse::AttractToMin,
0.8,
));
p.add_edge_path_rule(EdgePathRule {
edge_kind: "cites".into(),
path: EdgePath::FieldLine {
field: id,
max_steps: 32,
step_size: 4.0,
},
});
let s = serde_json::to_string(&p).unwrap();
let back: FieldProjection = serde_json::from_str(&s).unwrap();
assert_eq!(p, back);
}
}