use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use crate::error::MemoryError;
use crate::id::{EntityId, MemoryId};
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum FactValue {
Text(String),
Number(f64),
Bool(bool),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Fact {
pub id: MemoryId,
pub subject: EntityId,
pub predicate: String,
pub object: FactValue,
pub confidence: f32,
pub asserted_at: DateTime<Utc>,
}
impl Fact {
pub fn new(
subject: EntityId,
predicate: impl Into<String>,
object: FactValue,
confidence: f32,
) -> Result<Self, MemoryError> {
if !(0.0..=1.0).contains(&confidence) {
return Err(MemoryError::InvalidConfidence { value: confidence });
}
Ok(Self {
id: MemoryId::new(),
subject,
predicate: predicate.into(),
object,
confidence,
asserted_at: Utc::now(),
})
}
}
impl Fact {
pub fn with_id(
id: MemoryId,
subject: EntityId,
predicate: impl Into<String>,
object: FactValue,
confidence: f32,
) -> Result<Self, MemoryError> {
if !(0.0..=1.0).contains(&confidence) {
return Err(MemoryError::InvalidConfidence { value: confidence });
}
Ok(Self {
id,
subject,
predicate: predicate.into(),
object,
confidence,
asserted_at: chrono::Utc::now(),
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_fact_new_valid_returns_ok() {
let e = EntityId::new("user:1");
let f = Fact::new(e, "name", FactValue::Text("Alice".into()), 0.9);
assert!(f.is_ok());
}
#[test]
fn test_fact_new_invalid_confidence_above_one_returns_error() {
let e = EntityId::new("user:1");
let err = Fact::new(e, "age", FactValue::Number(30.0), 1.5).unwrap_err();
assert!(matches!(err, MemoryError::InvalidConfidence { value } if (value - 1.5).abs() < 1e-5));
}
#[test]
fn test_fact_new_invalid_confidence_negative_returns_error() {
let e = EntityId::new("user:1");
let err = Fact::new(e, "age", FactValue::Number(30.0), -0.1).unwrap_err();
assert!(matches!(err, MemoryError::InvalidConfidence { .. }));
}
#[test]
fn test_fact_value_text_stores_correctly() {
let e = EntityId::new("e");
let f = Fact::new(e, "p", FactValue::Text("hello".into()), 1.0).unwrap();
assert_eq!(f.object, FactValue::Text("hello".into()));
}
#[test]
fn test_fact_value_number_stores_correctly() {
let e = EntityId::new("e");
let f = Fact::new(e, "p", FactValue::Number(42.0), 1.0).unwrap();
assert_eq!(f.object, FactValue::Number(42.0));
}
#[test]
fn test_fact_value_bool_stores_correctly() {
let e = EntityId::new("e");
let f = Fact::new(e, "p", FactValue::Bool(true), 1.0).unwrap();
assert_eq!(f.object, FactValue::Bool(true));
}
#[test]
fn test_fact_predicate_stored_correctly() {
let e = EntityId::new("e");
let f = Fact::new(e, "my-predicate", FactValue::Bool(false), 0.5).unwrap();
assert_eq!(f.predicate, "my-predicate");
}
}