use crate::episodic::episode::Episode;
use crate::semantic::fact::Fact;
pub trait ConsolidationPolicy: Send + Sync + 'static {
fn should_consolidate(&self, episode: &Episode) -> bool;
fn extract_facts(&self, episode: &Episode) -> Vec<Fact>;
}
#[derive(Debug, Clone)]
pub struct FrequencyPolicy {
pub threshold: f32,
}
impl FrequencyPolicy {
pub fn new(threshold: f32) -> Self {
Self { threshold }
}
}
impl ConsolidationPolicy for FrequencyPolicy {
fn should_consolidate(&self, episode: &Episode) -> bool {
episode.strength > self.threshold
}
fn extract_facts(&self, episode: &Episode) -> Vec<Fact> {
use crate::id::EntityId;
use crate::semantic::fact::FactValue;
let subject = EntityId::new(format!("agent:{}", episode.agent_id));
let predicate = format!("{:?}", episode.kind).to_lowercase();
let fact = Fact::new(
subject,
predicate,
FactValue::Text(episode.content.clone()),
episode.strength,
);
match fact {
Ok(f) => vec![f],
Err(_) => vec![], }
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::episodic::episode::{Episode, EpisodeKind};
use crate::id::{AgentId, SessionId};
fn make_episode(strength: f32) -> Episode {
Episode::new(
AgentId::new(),
SessionId::new(),
EpisodeKind::Observation,
"test content",
strength,
vec![],
)
.unwrap()
}
#[test]
fn test_frequency_policy_consolidates_above_threshold() {
let policy = FrequencyPolicy::new(0.7);
let ep = make_episode(0.9);
assert!(policy.should_consolidate(&ep));
}
#[test]
fn test_frequency_policy_does_not_consolidate_at_threshold() {
let policy = FrequencyPolicy::new(0.7);
let ep = make_episode(0.7);
assert!(!policy.should_consolidate(&ep)); }
#[test]
fn test_frequency_policy_does_not_consolidate_below_threshold() {
let policy = FrequencyPolicy::new(0.7);
let ep = make_episode(0.5);
assert!(!policy.should_consolidate(&ep));
}
#[test]
fn test_frequency_policy_extract_facts_returns_one_fact() {
let policy = FrequencyPolicy::new(0.7);
let ep = make_episode(0.9);
let facts = policy.extract_facts(&ep);
assert_eq!(facts.len(), 1);
}
#[test]
fn test_frequency_policy_extracted_fact_content_matches_episode() {
let policy = FrequencyPolicy::new(0.7);
let ep = make_episode(0.9);
let facts = policy.extract_facts(&ep);
use crate::semantic::fact::FactValue;
assert_eq!(facts[0].object, FactValue::Text("test content".into()));
}
}