From bc7752376cc4fa7a4d22bfa13bf996fc313552ec Mon Sep 17 00:00:00 2001 From: Hauke Strasdat Date: Tue, 3 Oct 2023 12:26:07 -0700 Subject: [PATCH] FromPropState --- Cargo.toml | 2 +- examples/moving_average.rs | 15 ++++++---- examples/one_dim_robot.rs | 15 ++++++---- examples/print_ticks.rs | 5 ++-- hollywood_macros/Cargo.toml | 2 +- hollywood_macros/src/lib.rs | 2 +- src/actors/periodic.rs | 42 ++++++++++------------------ src/actors/printer.rs | 4 +-- src/core/actor.rs | 16 ++--------- src/core/mod.rs | 2 +- src/core/outbound.rs | 6 +++- src/core/value.rs | 2 +- src/examples/moving_average/mod.rs | 2 +- src/examples/one_dim_robot/filter.rs | 2 +- src/examples/one_dim_robot/sim.rs | 2 +- src/lib.rs | 4 +-- 16 files changed, 57 insertions(+), 66 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index bb6dca7..bda625c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,7 +11,7 @@ license = "Apache-2.0" keywords = ["actor", "compute", "graph", "pipeline"] readme = "README.md" repository = "https://github.com/farm-ng/hollywood/" -version = "0.2.1" +version = "0.2.2" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/examples/moving_average.rs b/examples/moving_average.rs index c730a88..903e54f 100644 --- a/examples/moving_average.rs +++ b/examples/moving_average.rs @@ -1,32 +1,37 @@ use hollywood::actors::printer::PrinterProp; use hollywood::actors::{Periodic, Printer}; use hollywood::compute::Context; -use hollywood::core::ActorFacade; +use hollywood::core::{FromPropState, NullState}; -use hollywood::examples::moving_average::{MovingAverage, MovingAverageProp}; +use hollywood::examples::moving_average::{MovingAverage, MovingAverageProp, MovingAverageState}; /// pub async fn run_moving_average_example() { let pipeline = Context::configure(&mut |context| { let mut timer = Periodic::new_with_period(context, 1.0); - let mut moving_average = MovingAverage::new_default_init_state( + let mut moving_average = MovingAverage::from_prop_and_state( context, MovingAverageProp { alpha: 0.3, ..Default::default() }, + MovingAverageState { + moving_average: 0.0, + }, ); - let mut time_printer = Printer::::new_default_init_state( + let mut time_printer = Printer::::from_prop_and_state( context, PrinterProp { topic: "time".to_string(), }, + NullState {}, ); - let mut average_printer = Printer::::new_default_init_state( + let mut average_printer = Printer::::from_prop_and_state( context, PrinterProp { topic: "average".to_string(), }, + NullState {}, ); timer .outbound diff --git a/examples/one_dim_robot.rs b/examples/one_dim_robot.rs index 6e899d6..cc86f77 100644 --- a/examples/one_dim_robot.rs +++ b/examples/one_dim_robot.rs @@ -3,6 +3,8 @@ use hollywood::actors::Periodic; use hollywood::actors::Printer; use hollywood::compute::Context; use hollywood::core::*; +use hollywood::examples::one_dim_robot::draw::DrawState; +use hollywood::examples::one_dim_robot::filter::FilterState; use hollywood::examples::one_dim_robot::{ DrawActor, Filter, NamedFilterState, Robot, Sim, SimState, Stamped, }; @@ -10,7 +12,7 @@ use hollywood::examples::one_dim_robot::{ async fn run_robot_example() { let pipeline = Context::configure(&mut |context| { let mut timer = Periodic::new_with_period(context, 0.25); - let mut sim = Sim::new_with_state( + let mut sim = Sim::from_prop_and_state( context, NullProp {}, SimState { @@ -22,21 +24,24 @@ async fn run_robot_example() { }, }, ); - let mut filter = Filter::new_default_init_state(context, NullProp {}); - let mut filter_state_printer = Printer::::new_default_init_state( + let mut filter = Filter::from_prop_and_state(context, NullProp {}, FilterState::default()); + let mut filter_state_printer = Printer::::from_prop_and_state( context, PrinterProp { topic: "filter state".to_owned(), }, + NullState::default(), ); - let mut truth_printer = Printer::>::new_default_init_state( + let mut truth_printer = Printer::>::from_prop_and_state( context, PrinterProp { topic: "truth".to_owned(), }, + NullState::default(), ); - let mut draw_actor = DrawActor::new_default_init_state(context, NullProp {}); + let mut draw_actor = + DrawActor::from_prop_and_state(context, NullProp {}, DrawState::default()); timer .outbound diff --git a/examples/print_ticks.rs b/examples/print_ticks.rs index b4d39ce..02f2164 100644 --- a/examples/print_ticks.rs +++ b/examples/print_ticks.rs @@ -1,17 +1,18 @@ use hollywood::actors::printer::PrinterProp; use hollywood::actors::{Periodic, Printer}; use hollywood::compute::Context; -use hollywood::core::ActorFacade; +use hollywood::core::*; /// pub async fn run_tick_print_example() { let pipeline = Context::configure(&mut |context| { let mut timer = Periodic::new_with_period(context, 1.0); - let mut time_printer = Printer::::new_default_init_state( + let mut time_printer = Printer::::from_prop_and_state( context, PrinterProp { topic: "time".to_string(), }, + NullState::default(), ); timer .outbound diff --git a/hollywood_macros/Cargo.toml b/hollywood_macros/Cargo.toml index 430a76b..2ad8b2c 100644 --- a/hollywood_macros/Cargo.toml +++ b/hollywood_macros/Cargo.toml @@ -11,7 +11,7 @@ license = "Apache-2.0" keywords = ["actor", "compute", "graph", "pipeline"] readme = "../README.md" repository = "https://github.com/farm-ng/hollywood/tree/main/hollywood_macros" -version = "0.2.1" +version = "0.2.2" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [lib] diff --git a/hollywood_macros/src/lib.rs b/hollywood_macros/src/lib.rs index 7a4e1cb..059f091 100644 --- a/hollywood_macros/src/lib.rs +++ b/hollywood_macros/src/lib.rs @@ -303,7 +303,7 @@ pub fn actor(attr: TokenStream, item: TokenStream) -> TokenStream { #( #attrs )* pub type #actor_name = Actor<#prop, #inbound, #state_type, #out>; - impl ActorFacade<#prop, #inbound, #state_type, #out, #message_type, #runner_type> + impl FromPropState<#prop, #inbound, #state_type, #out, #message_type, #runner_type> for #actor_name { fn name_hint(prop: &#prop) -> String { diff --git a/src/actors/periodic.rs b/src/actors/periodic.rs index 8a19d03..aa85cdd 100644 --- a/src/actors/periodic.rs +++ b/src/actors/periodic.rs @@ -4,12 +4,23 @@ use async_trait::async_trait; use crate::compute::context::Context; use crate::core::{ - actor::{ActorFacade, ActorNode, DormantActorNode, GenericActor}, + actor::{FromPropState, ActorNode, DormantActorNode, GenericActor}, inbound::{ForwardMessage, NullInbound, NullMessage}, outbound::{ConnectionEnum, Morph, OutboundChannel, OutboundHub}, runner::Runner, value::Value, }; +use crate::macros::*; + + +/// Outbound hub of periodic actor, which consists of a single outbound channel. +#[actor_outputs] +pub struct PeriodicOutbound { + /// Time stamp outbound channel, which sends a messages every `period` + /// seconds with the current time stamp. + pub time_stamp: OutboundChannel, +} + /// A periodic actor. /// @@ -20,7 +31,7 @@ pub type Periodic = impl Periodic { /// Create a new periodic actor, with a period of `period` seconds. pub fn new_with_period(context: &mut Context, period: f64) -> Periodic { - Periodic::new_with_state( + Periodic::from_prop_and_state( context, PeriodicProp { period, @@ -35,7 +46,7 @@ impl Periodic { } impl - ActorFacade< + FromPropState< PeriodicProp, NullInbound, PeriodicState, @@ -85,32 +96,7 @@ impl Default for PeriodicState { impl Value for PeriodicState {} -/// Outbound hub of periodic actor, which consists of a single outbound channel. -pub struct PeriodicOutbound { - /// Time stamp outbound channel, which sends a messages every `period` - /// seconds with the current time stamp. - pub time_stamp: OutboundChannel, -} - -impl Morph for PeriodicOutbound { - fn extract(&mut self) -> Self { - Self { - time_stamp: self.time_stamp.extract(), - } - } - - fn activate(&mut self) { - self.time_stamp.activate(); - } -} -impl OutboundHub for PeriodicOutbound { - fn from_context_and_parent(context: &mut Context, actor_name: &str) -> Self { - Self { - time_stamp: OutboundChannel::::new(context, "time_stamp".to_owned(), actor_name), - } - } -} /// The custom runner for the periodic actor. pub struct PeriodicRunner {} diff --git a/src/actors/printer.rs b/src/actors/printer.rs index 392fe79..c52459f 100644 --- a/src/actors/printer.rs +++ b/src/actors/printer.rs @@ -1,7 +1,7 @@ use std::fmt::{Debug, Display}; use crate::core::{ - Actor, ActorBuilder, DefaultRunner, ActorFacade, InboundChannel, InboundHub, InboundMessage, + Actor, ActorBuilder, DefaultRunner, FromPropState, InboundChannel, InboundHub, InboundMessage, InboundMessageNew, NullOutbound, NullState, OnMessage, Value, }; @@ -51,7 +51,7 @@ impl InboundMessageNew pub type Printer = Actor, NullState, NullOutbound>; impl - ActorFacade< + FromPropState< PrinterProp, PrinterInbound, NullState, diff --git a/src/core/actor.rs b/src/core/actor.rs index 957260c..98660e6 100644 --- a/src/core/actor.rs +++ b/src/core/actor.rs @@ -39,8 +39,8 @@ pub type Actor = GenericActor< DefaultRunner, >; -/// The actor facade trait is used to configure the actor's channel connections. -pub trait ActorFacade< +/// New actor from properties and state. +pub trait FromPropState< Prop, Inbound: InboundHub, State: Value, @@ -53,20 +53,10 @@ pub trait ActorFacade< /// generate a unique name. fn name_hint(prop: &Prop) -> String; - /// Produces a new actor with default state. - /// - /// Also, a dormant actor node is created added to the context. - fn new_default_init_state( - context: &mut Context, - prop: Prop, - ) -> GenericActor { - Self::new_with_state(context, prop, State::default()) - } - /// Produces a new actor with the given state. /// /// Also, a dormant actor node is created added to the context. - fn new_with_state( + fn from_prop_and_state( context: &mut Context, prop: Prop, initial_state: State, diff --git a/src/core/mod.rs b/src/core/mod.rs index a6c13ff..dd4c299 100644 --- a/src/core/mod.rs +++ b/src/core/mod.rs @@ -3,7 +3,7 @@ /// Actor pub mod actor; -pub use actor::{Actor, ActorFacade}; +pub use actor::{Actor, FromPropState}; pub(crate) use actor::{ActorNode, DormantActorNode}; /// Actor builder diff --git a/src/core/outbound.rs b/src/core/outbound.rs index 4d71b7b..237da39 100644 --- a/src/core/outbound.rs +++ b/src/core/outbound.rs @@ -12,7 +12,8 @@ pub trait OutboundHub: Send + Sync + 'static + Morph { /// An empty outbound hub - used for actors that do not have any outbound channels. #[derive(Debug, Clone)] -pub struct NullOutbound {} +pub struct NullOutbound { +} impl Morph for NullOutbound { fn extract(&mut self) -> Self { @@ -191,6 +192,8 @@ impl ConnectionEnu impl Morph for ConnectionEnum { fn extract(&mut self) -> Self { + + println!("ConnectionEnum::extract"); match self { Self::Config(config) => Self::Active(ActiveConnection { maybe_registers: None, @@ -203,6 +206,7 @@ impl Morph for ConnectionEnum { } fn activate(&mut self) { + println!("ConnectionEnum::activate"); match self { Self::Config(_) => { panic!("Cannot activate config connection"); diff --git a/src/core/value.rs b/src/core/value.rs index 5963a11..b31988c 100644 --- a/src/core/value.rs +++ b/src/core/value.rs @@ -1,5 +1,5 @@ /// Trait for actor state and props. -pub trait Value: Default + std::fmt::Debug + Send + Sync + Clone + 'static {} +pub trait Value: std::fmt::Debug + Send + Sync + Clone + 'static {} /// Empty state - for stateless actors. diff --git a/src/examples/moving_average/mod.rs b/src/examples/moving_average/mod.rs index 2c4de43..a2ee7b8 100644 --- a/src/examples/moving_average/mod.rs +++ b/src/examples/moving_average/mod.rs @@ -11,7 +11,7 @@ pub use crate::core::{ }; // needed for actor macro -pub use crate::core::{Actor, DefaultRunner, ActorFacade}; +pub use crate::core::{Actor, DefaultRunner, FromPropState}; /// Outbound hub for the MovingAverage. #[actor_outputs] diff --git a/src/examples/one_dim_robot/filter.rs b/src/examples/one_dim_robot/filter.rs index 6daef7d..f92f148 100644 --- a/src/examples/one_dim_robot/filter.rs +++ b/src/examples/one_dim_robot/filter.rs @@ -2,7 +2,7 @@ use std::fmt::{Debug, Display}; use crate::compute::Context; use crate::core::{ - Actor, ActorBuilder, DefaultRunner, ActorFacade, InboundChannel, InboundHub, InboundMessage, + Actor, ActorBuilder, DefaultRunner, FromPropState, InboundChannel, InboundHub, InboundMessage, InboundMessageNew, Morph, NullProp, OnMessage, OutboundChannel, OutboundHub, Value, }; use crate::examples::one_dim_robot::{RangeMeasurementModel, Stamped}; diff --git a/src/examples/one_dim_robot/sim.rs b/src/examples/one_dim_robot/sim.rs index 5963155..9c35586 100644 --- a/src/examples/one_dim_robot/sim.rs +++ b/src/examples/one_dim_robot/sim.rs @@ -4,7 +4,7 @@ use rand_distr::{Distribution, Normal}; use crate::compute::Context; use crate::core::{ - Actor, ActorBuilder, DefaultRunner, ActorFacade, InboundChannel, InboundHub, InboundMessage, + Actor, ActorBuilder, DefaultRunner, FromPropState, InboundChannel, InboundHub, InboundMessage, InboundMessageNew, Morph, NullProp, OnMessage, OutboundChannel, OutboundHub, Value, }; use crate::examples::one_dim_robot::{RangeMeasurementModel, Robot, Stamped}; diff --git a/src/lib.rs b/src/lib.rs index af435c8..bcef9b2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -169,7 +169,7 @@ //! //! ```rust //! # use hollywood::actors::{Periodic, Printer, PrinterProp}; -//! # use hollywood::core::ActorFacade; +//! # use hollywood::core::FromPropState; //! # use hollywood::compute::Context; //! # use hollywood::examples::moving_average::{MovingAverage, MovingAverageProp}; //! let pipeline = Context::configure(&mut |context| { @@ -377,7 +377,7 @@ pub mod macros { /// trait. /// /// Effect: - /// - This macro implements the [ActorFacade](crate::core::ActorFacade) trait for the ACTOR + /// - This macro implements the [FromPropState](crate::core::FromPropState) trait for the ACTOR /// type. /// /// This is the last of the three macros that need to be used to define a new actor type. The