Skip to main content

rufield_core/
inference.rs

1//! Inference query / result / embedding types (ADR-260 §16 / §24).
2
3use crate::privacy::PrivacyClass;
4use serde::{Deserialize, Serialize};
5
6/// A query against the fusion engine (ADR-260 §16 `FusionEngine::infer`).
7#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
8pub struct InferenceQuery {
9    /// Inference labels of interest (empty = all available).
10    pub labels: Vec<String>,
11    /// Optional zone scope.
12    pub zone_id: Option<String>,
13    /// "As of" time, nanoseconds since Unix epoch; `None` = latest.
14    pub as_of_ns: Option<u64>,
15}
16
17impl InferenceQuery {
18    /// Query for all available inferences at the latest time.
19    #[must_use]
20    pub fn all() -> Self {
21        InferenceQuery {
22            labels: Vec::new(),
23            zone_id: None,
24            as_of_ns: None,
25        }
26    }
27}
28
29/// A single fused inference (ADR-260 §24 — every inference must carry these
30/// fields).
31#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
32pub struct FieldInference {
33    /// Inference label (e.g. `person_present`, `bed_exit`).
34    pub label: String,
35    /// Confidence `0.0..=1.0`.
36    pub confidence: f32,
37    /// Event ids supporting this inference.
38    pub supporting_events: Vec<String>,
39    /// Event ids contradicting this inference.
40    pub contradicting_events: Vec<String>,
41    /// Privacy class of the inference itself.
42    pub privacy_class: PrivacyClass,
43    /// Calibration id, if applicable.
44    pub calibration_id: Option<String>,
45    /// Model / rule id that produced the inference.
46    pub model_id: String,
47    /// Time the inference was produced, ns since epoch.
48    pub produced_ns: u64,
49    /// Time the inference expires, ns since epoch.
50    pub expires_ns: u64,
51}
52
53/// A field embedding produced by a [`crate::traits::FieldEncoder`]
54/// (ADR-260 §16). v0.1 carries a plain feature vector.
55#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
56pub struct FieldEmbedding {
57    /// Modality string code of the source.
58    pub modality: String,
59    /// Embedding vector.
60    pub vector: Vec<f32>,
61    /// Privacy class of the embedding.
62    pub privacy_class: PrivacyClass,
63    /// Source event id.
64    pub source_event_id: String,
65}
66
67#[cfg(test)]
68mod tests {
69    use super::*;
70
71    #[test]
72    fn inference_round_trips() {
73        let inf = FieldInference {
74            label: "person_present".into(),
75            confidence: 0.91,
76            supporting_events: vec!["e1".into(), "e2".into()],
77            contradicting_events: vec![],
78            privacy_class: PrivacyClass::P2,
79            calibration_id: Some("cal".into()),
80            model_id: "rule.person_present".into(),
81            produced_ns: 100,
82            expires_ns: 200,
83        };
84        let j = serde_json::to_string(&inf).unwrap();
85        let back: FieldInference = serde_json::from_str(&j).unwrap();
86        assert_eq!(inf, back);
87    }
88}