1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
/*!
# Actors model
The module implements the high-level integrated model interface.
The model is build from a collection of [actor]s.
The model has 4 states:
1. [Unknown]: model state at its creation
2. [Ready]: model state after succesfully performing runtime checks on inputs and outputs on all the actors, the model can move to the [Ready] state only from the [Unknown] state
3. [Running]: model state while all the actors are performing their respective tasks, the model can move to the [Running] state only from the [Ready] state
4. [Completed]: model state after the succesful completion of the tasks of all the actors, the model can move to the [Completed] state only from the [Running] state
# Example
A 3 actors model with [Signals], [Sampler] and [Logging] clients is build with:
```
use gmt_dos_actors::prelude::*;
use gmt_dos_clients::signals::Signals;
use gmt_dos_clients::sampler::Sampler;
use gmt_dos_clients::logging::Logging;
use interface::UID;
let mut source: Initiator<_> = Signals::new(1, 100).into();
#[derive(UID)]
enum Source {};
let mut sampler: Actor<_, 1, 10> = Sampler::<Vec<f64>, Source>::default().into();
let logging = Logging::<f64>::default().into_arcx();
let mut sink = Terminator::<_, 10>::new(logging);
```
`sampler` decimates `source` with a 1:10 ratio.
The `source` connects to the `sampler` using the empty enum type `Source` as the data identifier.
The source data is then logged into the client of the `sink` actor.
```
# use gmt_dos_actors::prelude::*;
# use gmt_dos_clients::signals::Signals;
# use gmt_dos_clients::sampler::Sampler;
# use gmt_dos_clients::logging::Logging;
# use interface::UID;
# let mut source: Initiator<_> = Signals::new(1, 100).into();
# #[derive(UID)]
# enum Source {};
# let mut sampler: Actor<_> = Sampler::<Vec<f64>, Source>::default().into();
# let logging = Logging::<f64>::default().into_arcx();
# let mut sink = Terminator::<_>::new(logging);
source.add_output().build::<Source>().into_input(&mut sampler);
sampler.add_output().build::<Source>().into_input(&mut sink);
```
A [model](mod@crate::model) is build from the set of actors:
```
# use gmt_dos_actors::prelude::*;
# use gmt_dos_clients::signals::Signals;
# use gmt_dos_clients::sampler::Sampler;
# use gmt_dos_clients::logging::Logging;
# use interface::UID;
# let mut source: Initiator<_> = Signals::new(1, 100).into();
# #[derive(UID)]
# enum Source {};
# let mut sampler: Actor<_> = Sampler::<Vec<f64>, Source>::default().into();
# let logging = Logging::<f64>::default().into_arcx();
# let mut sink = Terminator::<_>::new(logging.clone());
# source.add_output().build::<Source>().into_input(&mut sampler);
# sampler.add_output().build::<Source>().into_input(&mut sink);
Model::new(vec![Box::new(source), Box::new(sampler), Box::new(sink)]);
```
Actors are checked for inputs/outputs consistencies:
```
# use gmt_dos_actors::prelude::*;
# use gmt_dos_clients::signals::Signals;
# use gmt_dos_clients::sampler::Sampler;
# use gmt_dos_clients::logging::Logging;
# use interface::UID;
# let mut source: Initiator<_> = Signals::new(1, 100).into();
# #[derive(UID)]
# enum Source {};
# let mut sampler: Actor<_> = Sampler::<Vec<f64>, Source>::default().into();
# let logging = Logging::<f64>::default().into_arcx();
# let mut sink = Terminator::<_>::new(logging.clone());
# source.add_output().build::<Source>().into_input(&mut sampler);
# sampler.add_output().build::<Source>().into_input(&mut sink);
Model::new(vec![Box::new(source), Box::new(sampler), Box::new(sink)])
.check()?;
# Ok::<(), gmt_dos_actors::model::ModelError>(())
```
The model run the actor tasks:
```
# tokio_test::block_on(async {
# use gmt_dos_actors::prelude::*;
# use gmt_dos_clients::signals::Signals;
# use gmt_dos_clients::sampler::Sampler;
# use gmt_dos_clients::logging::Logging;
# use interface::UID;
# let mut source: Initiator<_> = Signals::new(1, 100).into();
# #[derive(UID)]
# enum Source {};
# let mut sampler: Actor<_> = Sampler::<Vec<f64>, Source>::default().into();
# let logging = Logging::<f64>::default().into_arcx();
# let mut sink = Terminator::<_>::new(logging.clone());
# source.add_output().build::<Source>().into_input(&mut sampler);
# sampler.add_output().build::<Source>().into_input(&mut sink);
Model::new(vec![Box::new(source), Box::new(sampler), Box::new(sink)])
.check()?
.run();
# Ok::<(), gmt_dos_actors::model::ModelError>(())
# });
```
and wait for the tasks to finish:
```
# tokio_test::block_on(async {
# use gmt_dos_actors::prelude::*;
# use gmt_dos_clients::signals::Signals;
# use gmt_dos_clients::sampler::Sampler;
# use gmt_dos_clients::logging::Logging;
# use interface::UID;
# let mut source: Initiator<_> = Signals::new(1, 100).into();
# #[derive(UID)]
# enum Source {};
# let mut sampler: Actor<_> = Sampler::<Vec<f64>, Source>::default().into();
# let logging = Logging::<f64>::default().into_arcx();
# let mut sink = Terminator::<_>::new(logging.clone());
# source.add_output().build::<Source>().into_input(&mut sampler);
# sampler.add_output().build::<Source>().into_input(&mut sink);
Model::new(vec![Box::new(source), Box::new(sampler), Box::new(sink)])
.check()?
.run()
.wait()
.await?;
# Ok::<(), gmt_dos_actors::model::ModelError>(())
# });
```
Once the model run to completion, the data from `logging` is read with:
```
# tokio_test::block_on(async {
# use gmt_dos_actors::prelude::*;
# use gmt_dos_clients::signals::Signals;
# use gmt_dos_clients::sampler::Sampler;
# use gmt_dos_clients::logging::Logging;
# use interface::UID;
# let mut source: Initiator<_> = Signals::new(1, 100).into();
# #[derive(UID)]
# enum Source {};
# let mut sampler: Actor<_> = Sampler::<Vec<f64>, Source>::default().into();
# let logging = Logging::<f64>::default().into_arcx();
# let mut sink = Terminator::<_>::new(logging.clone());
# source.add_output().build::<Source>().into_input(&mut sampler);
# sampler.add_output().build::<Source>().into_input(&mut sink);
# Model::new(vec![Box::new(source), Box::new(sampler), Box::new(sink)])
# .check()?
# .run()
# .wait()
# .await?;
let data: &[f64] = &logging.lock().await;
# Ok::<(), gmt_dos_actors::model::ModelError>(())
# });
```
[Actor]: crate::actor::Actor
[Write]: interface::Write
[Read]: interface::Read
[Update]: interface::Update
[Model]: crate::model::Model
[Mutex]: tokio::sync::Mutex
[Arc]: std::sync::Arc
[Arcmutex]: crate::ArcMutex
[into_arcx]: crate::ArcMutex::into_arcx
[Signals]: https://docs.rs/gmt_dos-clients/latest/gmt_dos_clients/logging/struct.Signals.html
[Sampler]: https://docs.rs/gmt_dos-clients/latest/gmt_dos_clients/logging/struct.Sampler.html
[Logging]: https://docs.rs/gmt_dos-clients/latest/gmt_dos_clients/logging/struct.Logging.html
*/
use crate::framework::model::{CheckError, Task, TaskError};
use std::{fmt::Display, marker::PhantomData, time::Instant};
mod flowchart;
use tokio::task::JoinHandle;
#[derive(thiserror::Error, Debug)]
pub enum ModelError {
#[error("no actors found in the model")]
NoActors,
#[error("failed to join the task")]
TaskError(#[from] tokio::task::JoinError),
#[error("Actor IO inconsistency")]
ActorIO(#[from] crate::ActorError),
#[error("error in Task implementation")]
Task(#[from] Box<TaskError>),
#[error("error in Check implementation")]
Check(#[from] Box<CheckError>),
}
type Result<T> = std::result::Result<T, ModelError>;
/// [Model] initial state
pub enum Unknown {}
/// Valid [Model] state
pub enum Ready {}
/// [Model]ing in-progress state
pub enum Running {}
/// [Model] final state
pub enum Completed {}
type Actors = Vec<Box<dyn Task>>;
/// Actor model
pub struct Model<State> {
pub(crate) name: Option<String>,
pub(crate) actors: Option<Actors>,
pub(crate) task_handles: Option<Vec<JoinHandle<std::result::Result<(), TaskError>>>>,
pub(crate) state: PhantomData<State>,
pub(crate) start: Instant,
pub(crate) verbose: bool,
pub(crate) elapsed_time: f64,
}
impl<S> Display for Model<S> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(
f,
"{} [{},{:?}]",
self.name.as_ref().unwrap_or(&"ACTOR MODEL".to_string()),
self.n_actors(),
self.n_io()
)?;
if let Some(actors) = &self.actors {
for actor in actors {
write!(f, " {}", actor)?;
}
}
Ok(())
}
}
impl<S> Model<S> {
/// Prints some informations about the model and the actors within
pub fn inspect(self) -> Self {
println!("{self}");
self
}
/// Returns the total number of inputs and the total number of outputs
///
/// Both numbers should be the same
pub fn n_io(&self) -> (usize, usize) {
if let Some(ref actors) = self.actors {
actors
.iter()
.fold((0usize, 0usize), |(mut i, mut o), actor| {
i += actor.n_inputs();
o += actor.n_outputs();
(i, o)
})
} else {
(0, 0)
}
}
/// Returns the number of actors
pub fn n_actors(&self) -> usize {
self.actors.as_ref().map_or(0, |actors| actors.len())
}
pub fn get_name(&self) -> String {
self.name.clone().unwrap_or("model".to_string())
}
pub fn elapsed_time(&self) -> std::time::Duration {
std::time::Duration::from_secs_f64(self.elapsed_time)
}
}
#[doc(hidden)]
pub trait UnknownOrReady {}
impl UnknownOrReady for Unknown {}
impl UnknownOrReady for Ready {}
mod plain;
pub mod ready;
pub mod running;
pub mod unknown;
pub use plain::PlainModel;
// mod task;