Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,9 @@ members = [
"sim",
"sim_derive",
"simx",
]
]


[profile.release]
# Tell `rustc` to optimize for small code size.
opt-level = "s"
4 changes: 0 additions & 4 deletions sim/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,3 @@ wee_alloc = { version = "0.4", optional = true }

[dev-dependencies]
wasm-bindgen-test = "0.3"

[profile.release]
# Tell `rustc` to optimize for small code size.
opt-level = "s"
38 changes: 20 additions & 18 deletions sim/src/models/batcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use sim_derive::SerializableModel;

#[cfg(feature = "simx")]
use simx::event_rules;
use crate::simulator::time::{SDuration, STime};

/// The batching process begins when the batcher receives a job. It will
/// then accept additional jobs, adding them to a batch with the first job,
Expand All @@ -24,7 +25,7 @@ use simx::event_rules;
pub struct Batcher {
ports_in: PortsIn,
ports_out: PortsOut,
max_batch_time: f64,
max_batch_time: SDuration,
max_batch_size: usize,
#[serde(default)]
store_records: bool,
Expand All @@ -48,7 +49,7 @@ struct PortsOut {
#[serde(rename_all = "camelCase")]
struct State {
phase: Phase,
until_next_event: f64,
until_next_event: SDuration,
jobs: Vec<String>,
records: Vec<ModelRecord>,
}
Expand All @@ -57,7 +58,7 @@ impl Default for State {
fn default() -> Self {
State {
phase: Phase::Passive,
until_next_event: f64::INFINITY,
until_next_event: SDuration::INFINITY,
jobs: Vec::new(),
records: Vec::new(),
}
Expand All @@ -76,7 +77,7 @@ impl Batcher {
pub fn new(
job_in_port: String,
job_out_port: String,
max_batch_time: f64,
max_batch_time: SDuration,
max_batch_size: usize,
store_records: bool,
) -> Self {
Expand All @@ -90,7 +91,7 @@ impl Batcher {
}
}

fn add_to_batch(&mut self, incoming_message: &ModelMessage, services: &mut Services) {
fn add_to_batch(&mut self, incoming_message: &ModelMessage, services: &Services) {
self.state.phase = Phase::Batching;
self.state.jobs.push(incoming_message.content.clone());
self.record(
Expand All @@ -100,7 +101,7 @@ impl Batcher {
);
}

fn start_batch(&mut self, incoming_message: &ModelMessage, services: &mut Services) {
fn start_batch(&mut self, incoming_message: &ModelMessage, services: & Services) {
self.state.phase = Phase::Batching;
self.state.until_next_event = self.max_batch_time;
self.state.jobs.push(incoming_message.content.clone());
Expand All @@ -111,9 +112,9 @@ impl Batcher {
);
}

fn fill_batch(&mut self, incoming_message: &ModelMessage, services: &mut Services) {
fn fill_batch(&mut self, incoming_message: &ModelMessage, services: & Services) {
self.state.phase = Phase::Release;
self.state.until_next_event = 0.0;
self.state.until_next_event = SDuration::NOW;
self.state.jobs.push(incoming_message.content.clone());
self.record(
services.global_time(),
Expand All @@ -122,9 +123,9 @@ impl Batcher {
);
}

fn release_full_queue(&mut self, services: &mut Services) -> Vec<ModelMessage> {
fn release_full_queue(&mut self, services: &Services) -> Vec<ModelMessage> {
self.state.phase = Phase::Passive;
self.state.until_next_event = f64::INFINITY;
self.state.until_next_event = SDuration::INFINITY;
(0..self.state.jobs.len())
.map(|_| {
self.record(
Expand All @@ -140,7 +141,7 @@ impl Batcher {
.collect()
}

fn release_partial_queue(&mut self, services: &mut Services) -> Vec<ModelMessage> {
fn release_partial_queue(&mut self, services: &Services) -> Vec<ModelMessage> {
self.state.phase = Phase::Batching;
self.state.until_next_event = self.max_batch_time;
(0..self.max_batch_size)
Expand All @@ -158,9 +159,9 @@ impl Batcher {
.collect()
}

fn release_multiple(&mut self, services: &mut Services) -> Vec<ModelMessage> {
fn release_multiple(&mut self, services: &Services) -> Vec<ModelMessage> {
self.state.phase = Phase::Release;
self.state.until_next_event = 0.0;
self.state.until_next_event = SDuration::NOW;
(0..self.max_batch_size)
.map(|_| {
self.record(
Expand All @@ -176,7 +177,7 @@ impl Batcher {
.collect()
}

fn record(&mut self, time: f64, action: String, subject: String) {
fn record(&mut self, time: STime, action: String, subject: String) {
if self.store_records {
self.state.records.push(ModelRecord {
time,
Expand All @@ -187,12 +188,13 @@ impl Batcher {
}
}


#[cfg_attr(feature = "simx", event_rules)]
impl DevsModel for Batcher {
fn events_ext(
&mut self,
incoming_message: &ModelMessage,
services: &mut Services,
services: &Services,
) -> Result<(), SimulationError> {
match (
&self.state.phase,
Expand All @@ -207,7 +209,7 @@ impl DevsModel for Batcher {

fn events_int(
&mut self,
services: &mut Services,
services: &Services,
) -> Result<Vec<ModelMessage>, SimulationError> {
match (
self.state.jobs.len() <= self.max_batch_size,
Expand All @@ -220,11 +222,11 @@ impl DevsModel for Batcher {
}
}

fn time_advance(&mut self, time_delta: f64) {
fn time_advance(&mut self, time_delta: SDuration) {
self.state.until_next_event -= time_delta;
}

fn until_next_event(&self) -> f64 {
fn until_next_event(&self) -> SDuration {
self.state.until_next_event
}
}
Expand Down
20 changes: 11 additions & 9 deletions sim/src/models/coupled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ use super::model_trait::{DevsModel, Reportable, ReportableModel, SerializableMod
use super::{Model, ModelMessage, ModelRecord};

use crate::simulator::Services;

use crate::utils::errors::SimulationError;

use sim_derive::SerializableModel;

#[cfg(feature = "simx")]
use simx::event_rules;
use crate::simulator::time::SDuration;

#[derive(Clone, Deserialize, Serialize, SerializableModel)]
#[serde(rename_all = "camelCase")]
Expand Down Expand Up @@ -168,7 +170,7 @@ impl Coupled {
fn distribute_events_ext(
&mut self,
parked_messages: &[ParkedMessage],
services: &mut Services,
services: &Services,
) -> Result<(), SimulationError> {
parked_messages.iter().try_for_each(|parked_message| {
self.components
Expand All @@ -187,7 +189,7 @@ impl Coupled {

fn distribute_events_int(
&mut self,
services: &mut Services,
services: &Services,
) -> Result<Vec<ModelMessage>, SimulationError> {
// Find the (internal message) events_ext relevant models (parked message id == component id)
let ext_transitioning_component_triggers: Vec<(usize, String, String)> = (0..self
Expand Down Expand Up @@ -230,7 +232,7 @@ impl Coupled {
// Run events_int for each model, and compile the internal and external messages
// Store the internal messages in the Coupled model struct, and output the external messages
let int_transitioning_component_indexes: Vec<usize> = (0..self.components.len())
.filter(|component_index| self.components[*component_index].until_next_event() == 0.0)
.filter(|component_index| self.components[*component_index].until_next_event() == SDuration::NOW)
.collect();
Ok(int_transitioning_component_indexes
.iter()
Expand Down Expand Up @@ -280,7 +282,7 @@ impl DevsModel for Coupled {
fn events_ext(
&mut self,
incoming_message: &ModelMessage,
services: &mut Services,
services: &Services,
) -> Result<(), SimulationError> {
match self.park_incoming_messages(incoming_message) {
None => Ok(()),
Expand All @@ -290,22 +292,22 @@ impl DevsModel for Coupled {

fn events_int(
&mut self,
services: &mut Services,
services: &Services,
) -> Result<Vec<ModelMessage>, SimulationError> {
self.distribute_events_int(services)
}

fn time_advance(&mut self, time_delta: f64) {
fn time_advance(&mut self, time_delta: SDuration) {
self.components.iter_mut().for_each(|component| {
component.time_advance(time_delta);
});
}

fn until_next_event(&self) -> f64 {
fn until_next_event(&self) -> SDuration {
self.components
.iter()
.fold(f64::INFINITY, |min, component| {
f64::min(min, component.until_next_event())
.fold(SDuration::INFINITY, |min, component| {
SDuration::min(min, component.until_next_event())
})
}
}
Expand Down
25 changes: 13 additions & 12 deletions sim/src/models/exclusive_gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use sim_derive::SerializableModel;

#[cfg(feature = "simx")]
use simx::event_rules;
use crate::simulator::time::{SDuration, STime};

/// The exclusive gateway splits a process flow into a set of possible paths.
/// The process will only follow one of the possible paths. Path selection is
Expand Down Expand Up @@ -47,7 +48,7 @@ struct PortsOut {
#[serde(rename_all = "camelCase")]
struct State {
phase: Phase,
until_next_event: f64,
until_next_event: SDuration,
jobs: Vec<String>, // port, message, time
records: Vec<ModelRecord>, // port, message, time
}
Expand All @@ -56,7 +57,7 @@ impl Default for State {
fn default() -> Self {
State {
phase: Phase::Passive,
until_next_event: f64::INFINITY,
until_next_event: SDuration::INFINITY,
jobs: Vec::new(),
records: Vec::new(),
}
Expand Down Expand Up @@ -92,9 +93,9 @@ impl ExclusiveGateway {
}
}

fn pass_job(&mut self, incoming_message: &ModelMessage, services: &mut Services) {
fn pass_job(&mut self, incoming_message: &ModelMessage, services: &Services) {
self.state.phase = Phase::Pass;
self.state.until_next_event = 0.0;
self.state.until_next_event = SDuration::NOW;
self.state.jobs.push(incoming_message.content.clone());
self.record(
services.global_time(),
Expand All @@ -107,9 +108,9 @@ impl ExclusiveGateway {
);
}

fn send_jobs(&mut self, services: &mut Services) -> Result<Vec<ModelMessage>, SimulationError> {
fn send_jobs(&mut self, services: &Services) -> Result<Vec<ModelMessage>, SimulationError> {
self.state.phase = Phase::Passive;
self.state.until_next_event = f64::INFINITY;
self.state.until_next_event = SDuration::INFINITY;
let departure_port_index = match &self.rng {
Some(rng) => self.port_weights.random_variate(rng.clone())?,
None => self.port_weights.random_variate(services.global_rng())?,
Expand All @@ -135,11 +136,11 @@ impl ExclusiveGateway {

fn passivate(&mut self) -> Vec<ModelMessage> {
self.state.phase = Phase::Passive;
self.state.until_next_event = f64::INFINITY;
self.state.until_next_event = SDuration::INFINITY;
Vec::new()
}

fn record(&mut self, time: f64, action: String, subject: String) {
fn record(&mut self, time: STime, action: String, subject: String) {
if self.store_records {
self.state.records.push(ModelRecord {
time,
Expand All @@ -155,26 +156,26 @@ impl DevsModel for ExclusiveGateway {
fn events_ext(
&mut self,
incoming_message: &ModelMessage,
services: &mut Services,
services: &Services,
) -> Result<(), SimulationError> {
Ok(self.pass_job(incoming_message, services))
}

fn events_int(
&mut self,
services: &mut Services,
services: &Services,
) -> Result<Vec<ModelMessage>, SimulationError> {
match &self.state.phase {
Phase::Passive => Ok(self.passivate()),
Phase::Pass => self.send_jobs(services),
}
}

fn time_advance(&mut self, time_delta: f64) {
fn time_advance(&mut self, time_delta: SDuration) {
self.state.until_next_event -= time_delta;
}

fn until_next_event(&self) -> f64 {
fn until_next_event(&self) -> SDuration {
self.state.until_next_event
}
}
Expand Down
Loading