Skip to content

Commit

Permalink
FromPropState
Browse files Browse the repository at this point in the history
  • Loading branch information
strasdat committed Oct 5, 2023
1 parent 9ba3868 commit bc77523
Show file tree
Hide file tree
Showing 16 changed files with 57 additions and 66 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
15 changes: 10 additions & 5 deletions examples/moving_average.rs
Original file line number Diff line number Diff line change
@@ -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::<f64>::new_default_init_state(
let mut time_printer = Printer::<f64>::from_prop_and_state(
context,
PrinterProp {
topic: "time".to_string(),
},
NullState {},
);
let mut average_printer = Printer::<f64>::new_default_init_state(
let mut average_printer = Printer::<f64>::from_prop_and_state(
context,
PrinterProp {
topic: "average".to_string(),
},
NullState {},
);
timer
.outbound
Expand Down
15 changes: 10 additions & 5 deletions examples/one_dim_robot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ 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,
};

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 {
Expand All @@ -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::<NamedFilterState>::new_default_init_state(
let mut filter = Filter::from_prop_and_state(context, NullProp {}, FilterState::default());
let mut filter_state_printer = Printer::<NamedFilterState>::from_prop_and_state(
context,
PrinterProp {
topic: "filter state".to_owned(),
},
NullState::default(),
);
let mut truth_printer = Printer::<Stamped<Robot>>::new_default_init_state(
let mut truth_printer = Printer::<Stamped<Robot>>::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
Expand Down
5 changes: 3 additions & 2 deletions examples/print_ticks.rs
Original file line number Diff line number Diff line change
@@ -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::<f64>::new_default_init_state(
let mut time_printer = Printer::<f64>::from_prop_and_state(
context,
PrinterProp {
topic: "time".to_string(),
},
NullState::default(),
);
timer
.outbound
Expand Down
2 changes: 1 addition & 1 deletion hollywood_macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion hollywood_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
42 changes: 14 additions & 28 deletions src/actors/periodic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<f64>,
}


/// A periodic actor.
///
Expand All @@ -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,
Expand All @@ -35,7 +46,7 @@ impl Periodic {
}

impl
ActorFacade<
FromPropState<
PeriodicProp,
NullInbound,
PeriodicState,
Expand Down Expand Up @@ -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<f64>,
}

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::<f64>::new(context, "time_stamp".to_owned(), actor_name),
}
}
}

/// The custom runner for the periodic actor.
pub struct PeriodicRunner {}
Expand Down
4 changes: 2 additions & 2 deletions src/actors/printer.rs
Original file line number Diff line number Diff line change
@@ -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,
};

Expand Down Expand Up @@ -51,7 +51,7 @@ impl<T: Debug + Display + Clone + Sync + Send + 'static> InboundMessageNew<T>
pub type Printer<T> = Actor<PrinterProp, PrinterInbound<T>, NullState, NullOutbound>;

impl<T: Clone + Sync + Send + 'static + Debug + Display + Default>
ActorFacade<
FromPropState<
PrinterProp,
PrinterInbound<T>,
NullState,
Expand Down
16 changes: 3 additions & 13 deletions src/core/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ pub type Actor<Prop, Inbound, State, OutboundHub> = GenericActor<
DefaultRunner<Prop, Inbound, State, OutboundHub>,
>;

/// 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<Prop, State, Outbound, M>,
State: Value,
Expand All @@ -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<Prop, Inbound, State, Outbound, Run> {
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,
Expand Down
2 changes: 1 addition & 1 deletion src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion src/core/outbound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -191,6 +192,8 @@ impl<T: Default + Clone + Send + Sync + std::fmt::Debug + 'static> ConnectionEnu

impl<T> Morph for ConnectionEnum<T> {
fn extract(&mut self) -> Self {

println!("ConnectionEnum::extract");
match self {
Self::Config(config) => Self::Active(ActiveConnection {
maybe_registers: None,
Expand All @@ -203,6 +206,7 @@ impl<T> Morph for ConnectionEnum<T> {
}

fn activate(&mut self) {
println!("ConnectionEnum::activate");
match self {
Self::Config(_) => {
panic!("Cannot activate config connection");
Expand Down
2 changes: 1 addition & 1 deletion src/core/value.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/examples/moving_average/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion src/examples/one_dim_robot/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
2 changes: 1 addition & 1 deletion src/examples/one_dim_robot/sim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit bc77523

Please sign in to comment.