pub struct Context { /* private fields */ }Expand description
The shared context between emitter and receiver
Implementations§
Source§impl Context
impl Context
Sourcepub fn with_config(config: ContextConfig) -> Self
pub fn with_config(config: ContextConfig) -> Self
Create context with custom configuration
Sourcepub fn with_evolution(evolution_config: EvolutionConfig) -> Self
pub fn with_evolution(evolution_config: EvolutionConfig) -> Self
Create context with evolution configuration
Sourcepub fn observation_count(&self) -> u64
pub fn observation_count(&self) -> u64
Get observation count
Sourcepub fn scale_factor(&self) -> u32
pub fn scale_factor(&self) -> u32
Get scale factor
Sourcepub fn pattern_count(&self) -> usize
pub fn pattern_count(&self) -> usize
Get number of patterns in dictionary
Sourcepub fn source_count(&self) -> usize
pub fn source_count(&self) -> usize
Get number of tracked sources
Sourcepub fn memory_usage(&self) -> usize
pub fn memory_usage(&self) -> usize
Estimate memory usage in bytes
Sourcepub fn estimated_memory(&self) -> usize
pub fn estimated_memory(&self) -> usize
Alias for memory_usage (for metrics compatibility)
Sourcepub fn observe(&mut self, data: &RawData)
pub fn observe(&mut self, data: &RawData)
Observe a new data point (update statistics and trigger evolution)
Sourcepub fn predict(&self, source_id: u32) -> Option<Prediction>
pub fn predict(&self, source_id: u32) -> Option<Prediction>
Get prediction for a source
Sourcepub fn last_value(&self, source_id: u32) -> Option<f64>
pub fn last_value(&self, source_id: u32) -> Option<f64>
Get last observed value for a source
Sourcepub fn moving_average(&self, source_id: u32, window: usize) -> Option<f64>
pub fn moving_average(&self, source_id: u32, window: usize) -> Option<f64>
Get moving average for a source
Sourcepub fn register_pattern(&mut self, pattern: Pattern) -> Result<u32>
pub fn register_pattern(&mut self, pattern: Pattern) -> Result<u32>
Register a new pattern in the dictionary
Sourcepub fn get_pattern(&self, code: u32) -> Option<&Pattern>
pub fn get_pattern(&self, code: u32) -> Option<&Pattern>
Get pattern by code
Sourcepub fn find_pattern(&self, data: &[u8]) -> Option<u32>
pub fn find_pattern(&self, data: &[u8]) -> Option<u32>
Find pattern code by data
Sourcepub fn remove_pattern(&mut self, id: u32)
pub fn remove_pattern(&mut self, id: u32)
Remove a pattern by ID
Sourcepub fn set_pattern(&mut self, id: u32, pattern: Pattern)
pub fn set_pattern(&mut self, id: u32, pattern: Pattern)
Set a pattern at a specific ID (for sync)
Sourcepub fn has_pattern(&self, id: u32) -> bool
pub fn has_pattern(&self, id: u32) -> bool
Check if a pattern exists by ID
Sourcepub fn patterns_iter(&self) -> impl Iterator<Item = (&u32, &Pattern)>
pub fn patterns_iter(&self) -> impl Iterator<Item = (&u32, &Pattern)>
Iterate over patterns (id, pattern)
Sourcepub fn pattern_ids(&self) -> impl Iterator<Item = u32> + '_
pub fn pattern_ids(&self) -> impl Iterator<Item = u32> + '_
Get all pattern IDs
Sourcepub fn set_version(&mut self, version: u32)
pub fn set_version(&mut self, version: u32)
Set version directly (for sync)
Sourcepub fn pattern_hashes(&self) -> impl Iterator<Item = u64> + '_
pub fn pattern_hashes(&self) -> impl Iterator<Item = u64> + '_
Get iterator over pattern hashes (for fleet sync)
Sourcepub fn export_full(&self) -> Vec<u8> ⓘ
pub fn export_full(&self) -> Vec<u8> ⓘ
Export full context for synchronization
Sourcepub fn import_full(&mut self, data: &[u8]) -> Result<()>
pub fn import_full(&mut self, data: &[u8]) -> Result<()>
Import full context
Sourcepub fn model_type(&self) -> PredictionModel
pub fn model_type(&self) -> PredictionModel
Get the type of prediction model being used
Sourcepub fn save_to_file(&self, path: &Path, sensor_type: &str) -> Result<()>
pub fn save_to_file(&self, path: &Path, sensor_type: &str) -> Result<()>
Save current context state to a preload file
This allows the context to be loaded later for instant optimal compression.
§Arguments
path- Path to save the preload filesensor_type- Identifier for the sensor type (e.g., “temperature”, “humidity”)
§Example
use alec::context::Context;
use std::path::Path;
let mut ctx = Context::new();
// ... train the context with data ...
ctx.save_to_file(Path::new("temperature.alec-context"), "temperature").unwrap();Sourcepub fn load_from_file(path: &Path) -> Result<Self>
pub fn load_from_file(path: &Path) -> Result<Self>
Load a preload file and initialize context
This allows achieving optimal compression from the first byte by loading a pre-trained context.
§Arguments
path- Path to the preload file
§Returns
A new Context initialized with the preload data
§Example
use alec::context::Context;
use std::path::Path;
let ctx = Context::load_from_file(Path::new("temperature.alec-context")).unwrap();
assert!(ctx.pattern_count() > 0);Sourcepub fn context_version(&self) -> u32
pub fn context_version(&self) -> u32
Get context version for sync checking
This version should be included in message headers to allow the decoder to verify it has the correct context.
Sourcepub fn check_version(&self, message_version: u32) -> VersionCheckResult
pub fn check_version(&self, message_version: u32) -> VersionCheckResult
Check if context version matches expected version
Returns a VersionCheckResult indicating whether versions match
or providing details about the mismatch.
Trait Implementations§
Source§impl HealthCheckable for Context
Available on crate feature std only.
impl HealthCheckable for Context
std only.