Skip to main content

FileObserver

Struct FileObserver 

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

Reads a single JSON (or plain-text) file on each observe call.

Implementations§

Source§

impl FileObserver

Source

pub fn new(path: impl AsRef<Path>) -> Self

Examples found in repository?
examples/custom_adapters.rs (line 27)
12fn main() -> Result<()> {
13    // Prepare a tiny status file the FileObserver will read.
14    let status_path = PathBuf::from("/tmp/omega-status-example.json");
15    {
16        let mut f = std::fs::File::create(&status_path).expect("create status file");
17        writeln!(
18            f,
19            r#"{{"signal": "external", "load": 0.42, "note": "hello from file"}}"#
20        )
21        .expect("write status");
22    }
23
24    let log_path = PathBuf::from("/tmp/omega-actions.log");
25    let _ = std::fs::remove_file(&log_path);
26
27    let observer = FileObserver::new(&status_path);
28    let executor = LoggingExecutor::new([
29        "noop".into(),
30        "observe".into(),
31        "explore".into(),
32        "exploit".into(),
33        "stabilize".into(),
34    ])
35    .with_log_file(&log_path)
36    .echo_stdout(true);
37
38    let mut kernel = Kernel::with_adapters(observer, executor);
39    kernel.autosave = false;
40    kernel.auto_checkpoint_on_patch = false;
41
42    let report = kernel.run("adapt to external file signal", 5)?;
43    println!(
44        "\nReport: steps={} err {:.3} → {:.3} decreased={} actions={:?}",
45        report.steps,
46        report.early_avg_err,
47        report.final_avg_err,
48        report.error_decreased,
49        report.last_actions
50    );
51
52    println!("\nAction log written to {}", log_path.display());
53    if let Ok(log) = std::fs::read_to_string(&log_path) {
54        println!("{log}");
55    }
56
57    Ok(())
58}

Trait Implementations§

Source§

impl Observer for FileObserver

Available on crate feature std only.

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.