1
0
forked from ROMEO/nexosim

Add instance name to model contexts

This commit is contained in:
Jaŭhien Piatlicki
2024-05-10 10:54:45 +02:00
parent 4b5195f981
commit 195bcdceba
13 changed files with 131 additions and 44 deletions

View File

@ -231,10 +231,10 @@
//! // Pick an arbitrary simulation start time and build the simulation.
//! let t0 = MonotonicTime::EPOCH;
//! let mut simu = SimInit::new()
//! .add_model(multiplier1, multiplier1_mbox)
//! .add_model(multiplier2, multiplier2_mbox)
//! .add_model(delay1, delay1_mbox)
//! .add_model(delay2, delay2_mbox)
//! .add_model(multiplier1, multiplier1_mbox, "multiplier1")
//! .add_model(multiplier2, multiplier2_mbox, "multiplier2")
//! .add_model(delay1, delay1_mbox, "delay1")
//! .add_model(delay2, delay2_mbox, "delay2")
//! .init(t0);
//! ```
//!
@ -319,10 +319,10 @@
//! # let input_address = multiplier1_mbox.address();
//! # let t0 = MonotonicTime::EPOCH;
//! # let mut simu = SimInit::new()
//! # .add_model(multiplier1, multiplier1_mbox)
//! # .add_model(multiplier2, multiplier2_mbox)
//! # .add_model(delay1, delay1_mbox)
//! # .add_model(delay2, delay2_mbox)
//! # .add_model(multiplier1, multiplier1_mbox, "multiplier1")
//! # .add_model(multiplier2, multiplier2_mbox, "multiplier2")
//! # .add_model(delay1, delay1_mbox, "delay1")
//! # .add_model(delay2, delay2_mbox, "delay2")
//! # .init(t0);
//! // Send a value to the first multiplier.
//! simu.process_event(Multiplier::input, 21.0, &input_address);

View File

@ -81,6 +81,7 @@ use super::Model;
// The self-scheduling caveat seems related to this issue:
// https://github.com/rust-lang/rust/issues/78649
pub struct Context<M: Model> {
name: String,
sender: Sender<M>,
scheduler_queue: Arc<Mutex<SchedulerQueue>>,
time: SyncCellReader<TearableAtomicTime>,
@ -89,17 +90,24 @@ pub struct Context<M: Model> {
impl<M: Model> Context<M> {
/// Creates a new local context.
pub(crate) fn new(
name: String,
sender: Sender<M>,
scheduler_queue: Arc<Mutex<SchedulerQueue>>,
time: SyncCellReader<TearableAtomicTime>,
) -> Self {
Self {
name,
sender,
scheduler_queue,
time,
}
}
/// Returns the model instance name.
pub fn name(&self) -> &str {
&self.name
}
/// Returns the current simulation time.
///
/// # Examples
@ -440,11 +448,13 @@ impl<M: Model> fmt::Debug for Context<M> {
/// let b = SubmodelB::default();
/// let a_mbox = Mailbox::new();
/// let b_mbox = Mailbox::new();
/// let a_name = setup_context.name().to_string() + "::a";
/// let b_name = setup_context.name().to_string() + "::b";
///
/// a.out.connect(SubmodelB::input, &b_mbox);
///
/// setup_context.add_model(a, a_mbox);
/// setup_context.add_model(b, b_mbox);
/// setup_context.add_model(a, a_mbox, a_name);
/// setup_context.add_model(b, b_mbox, b_name);
/// }
/// }
///
@ -472,11 +482,25 @@ impl<'a, M: Model> SetupContext<'a, M> {
}
}
/// Returns the model instance name.
pub fn name(&self) -> &str {
&self.context.name
}
/// Adds a new model and its mailbox to the simulation bench.
pub fn add_model<N: Model>(&self, model: N, mailbox: Mailbox<N>) {
///
/// The `name` argument needs not be unique (it can be an empty string) and
/// is used for convenience for model instance identification (e.g. for
/// logging purposes).
pub fn add_model<N: Model>(&self, model: N, mailbox: Mailbox<N>, name: impl Into<String>) {
let mut submodel_name = name.into();
if !self.context.name().is_empty() && !submodel_name.is_empty() {
submodel_name = self.context.name().to_string() + "." + &submodel_name;
}
simulation::add_model(
model,
mailbox,
submodel_name,
self.context.scheduler_queue.clone(),
self.context.time.clone(),
self.executor,

View File

@ -65,7 +65,8 @@
//! let mut child = ChildModel::new();
//! let child_mbox = Mailbox::new();
//! child.output = self.output.clone();
//! setup_context.add_model(child, child_mbox);
//! let child_name = setup_context.name().to_string() + "::child";
//! setup_context.add_model(child, child_mbox, child_name);
//! }
//! }
//! ```

View File

@ -614,8 +614,12 @@ mod tests {
let dummy_priority_queue = Arc::new(Mutex::new(PriorityQueue::new()));
let dummy_time =
SyncCell::new(TearableAtomicTime::new(MonotonicTime::EPOCH)).reader();
let dummy_context =
Context::new(dummy_address, dummy_priority_queue, dummy_time);
let dummy_context = Context::new(
String::new(),
dummy_address,
dummy_priority_queue,
dummy_time,
);
block_on(mailbox.recv(&mut counter, &dummy_context)).unwrap();
}
})
@ -665,8 +669,12 @@ mod tests {
let dummy_priority_queue = Arc::new(Mutex::new(PriorityQueue::new()));
let dummy_time =
SyncCell::new(TearableAtomicTime::new(MonotonicTime::EPOCH)).reader();
let dummy_context =
Context::new(dummy_address, dummy_priority_queue, dummy_time);
let dummy_context = Context::new(
String::new(),
dummy_address,
dummy_priority_queue,
dummy_time,
);
block_on(mailbox.recv(&mut counter, &dummy_context)).unwrap();
thread::sleep(std::time::Duration::from_millis(100));
}

View File

@ -497,8 +497,12 @@ mod tests {
let dummy_priority_queue = Arc::new(Mutex::new(PriorityQueue::new()));
let dummy_time =
SyncCell::new(TearableAtomicTime::new(MonotonicTime::EPOCH)).reader();
let dummy_context =
Context::new(dummy_address, dummy_priority_queue, dummy_time);
let dummy_context = Context::new(
String::new(),
dummy_address,
dummy_priority_queue,
dummy_time,
);
block_on(mailbox.recv(&mut counter, &dummy_context)).unwrap();
}
})
@ -548,8 +552,12 @@ mod tests {
let dummy_priority_queue = Arc::new(Mutex::new(PriorityQueue::new()));
let dummy_time =
SyncCell::new(TearableAtomicTime::new(MonotonicTime::EPOCH)).reader();
let dummy_context =
Context::new(dummy_address, dummy_priority_queue, dummy_time);
let dummy_context = Context::new(
String::new(),
dummy_address,
dummy_priority_queue,
dummy_time,
);
block_on(mailbox.recv(&mut counter, &dummy_context)).unwrap();
thread::sleep(std::time::Duration::from_millis(100));
}

View File

@ -666,13 +666,14 @@ impl Error for QueryError {}
pub(crate) fn add_model<M: Model>(
mut model: M,
mailbox: Mailbox<M>,
name: String,
scheduler_queue: Arc<Mutex<SchedulerQueue>>,
time: SyncCellReader<TearableAtomicTime>,
executor: &Executor,
) {
let sender = mailbox.0.sender();
let context = Context::new(sender, scheduler_queue, time);
let context = Context::new(name, sender, scheduler_queue, time);
let setup_context = SetupContext::new(&mailbox, &context, executor);
model.setup(&setup_context);

View File

@ -41,11 +41,27 @@ impl SimInit {
}
/// Adds a model and its mailbox to the simulation bench.
pub fn add_model<M: Model>(self, model: M, mailbox: Mailbox<M>) -> Self {
///
/// The `name` argument needs not be unique (it can be the empty string) and
/// is used for convenience for the model instance identification (e.g. for
/// logging purposes).
pub fn add_model<M: Model>(
self,
model: M,
mailbox: Mailbox<M>,
name: impl Into<String>,
) -> Self {
let scheduler_queue = self.scheduler_queue.clone();
let time = self.time.reader();
add_model(model, mailbox, scheduler_queue, time, &self.executor);
add_model(
model,
mailbox,
name.into(),
scheduler_queue,
time,
&self.executor,
);
self
}