Skip to main content

Context

Struct Context 

Source
pub struct Context { /* private fields */ }
Expand description

The shared context between emitter and receiver

Implementations§

Source§

impl Context

Source

pub fn new() -> Self

Create a new empty context

Source

pub fn with_config(config: ContextConfig) -> Self

Create context with custom configuration

Source

pub fn with_evolution(evolution_config: EvolutionConfig) -> Self

Create context with evolution configuration

Source

pub fn observation_count(&self) -> u64

Get observation count

Source

pub fn version(&self) -> u32

Get current version

Source

pub fn scale_factor(&self) -> u32

Get scale factor

Source

pub fn hash(&self) -> u64

Calculate hash of the entire context for sync verification

Source

pub fn pattern_count(&self) -> usize

Get number of patterns in dictionary

Source

pub fn source_count(&self) -> usize

Get number of tracked sources

Source

pub fn memory_usage(&self) -> usize

Estimate memory usage in bytes

Source

pub fn estimated_memory(&self) -> usize

Alias for memory_usage (for metrics compatibility)

Source

pub fn evolve(&mut self)

Run context evolution (pruning + reordering)

Source

pub fn observe(&mut self, data: &RawData)

Observe a new data point (update statistics and trigger evolution)

Source

pub fn predict(&self, source_id: u32) -> Option<Prediction>

Get prediction for a source

Source

pub fn last_value(&self, source_id: u32) -> Option<f64>

Get last observed value for a source

Source

pub fn moving_average(&self, source_id: u32, window: usize) -> Option<f64>

Get moving average for a source

Source

pub fn register_pattern(&mut self, pattern: Pattern) -> Result<u32>

Register a new pattern in the dictionary

Source

pub fn get_pattern(&self, code: u32) -> Option<&Pattern>

Get pattern by code

Source

pub fn find_pattern(&self, data: &[u8]) -> Option<u32>

Find pattern code by data

Source

pub fn remove_pattern(&mut self, id: u32)

Remove a pattern by ID

Source

pub fn set_pattern(&mut self, id: u32, pattern: Pattern)

Set a pattern at a specific ID (for sync)

Source

pub fn has_pattern(&self, id: u32) -> bool

Check if a pattern exists by ID

Source

pub fn patterns_iter(&self) -> impl Iterator<Item = (&u32, &Pattern)>

Iterate over patterns (id, pattern)

Source

pub fn pattern_ids(&self) -> impl Iterator<Item = u32> + '_

Get all pattern IDs

Source

pub fn set_version(&mut self, version: u32)

Set version directly (for sync)

Source

pub fn pattern_hashes(&self) -> impl Iterator<Item = u64> + '_

Get iterator over pattern hashes (for fleet sync)

Source

pub fn export_full(&self) -> Vec<u8>

Export full context for synchronization

Source

pub fn export_diff(&self, _from_version: u32) -> Vec<u8>

Export diff since a given version

Source

pub fn import_full(&mut self, data: &[u8]) -> Result<()>

Import full context

Source

pub fn reset(&mut self)

Reset context to initial state

Source

pub fn verify(&self, expected_hash: u64) -> bool

Verify hash matches

Source

pub fn model_type(&self) -> PredictionModel

Get the type of prediction model being used

Source

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 file
  • sensor_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();
Source

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);
Source

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.

Source

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 Clone for Context

Source§

fn clone(&self) -> Context

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Context

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Context

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl HealthCheckable for Context

Available on crate feature std only.
Source§

fn health_check(&self) -> HealthCheck

Perform health check

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.