Skip to content

Commit

Permalink
Move id out of specific model types and into the general wrapper st…
Browse files Browse the repository at this point in the history
…ruct
  • Loading branch information
uint committed Mar 14, 2021
1 parent 5d277a3 commit 9df24cf
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 55 deletions.
5 changes: 0 additions & 5 deletions src/models/exclusive_gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use crate::utils::error::SimulationError;
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ExclusiveGateway {
id: String,
ports_in: PortsIn,
ports_out: PortsOut,
port_weights: IndexRandomVariable,
Expand Down Expand Up @@ -101,10 +100,6 @@ impl ExclusiveGateway {
}

impl AsModel for ExclusiveGateway {
fn id(&self) -> String {
self.id.clone()
}

fn status(&self) -> String {
String::from("Active")
}
Expand Down
5 changes: 0 additions & 5 deletions src/models/gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use crate::utils::error::SimulationError;
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Gate {
id: String,
ports_in: PortsIn,
ports_out: PortsOut,
#[serde(default)]
Expand Down Expand Up @@ -124,10 +123,6 @@ impl Gate {
}

impl AsModel for Gate {
fn id(&self) -> String {
self.id.clone()
}

fn status(&self) -> String {
match self.state.phase {
Phase::Open => String::from("Listening"),
Expand Down
5 changes: 0 additions & 5 deletions src/models/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use crate::utils::error::SimulationError;
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Generator {
id: String,
// Time between job generations
message_interdeparture_time: ContinuousRandomVariable,
// Thinning for non-stationarity
Expand Down Expand Up @@ -114,10 +113,6 @@ impl Generator {
}

impl AsModel for Generator {
fn id(&self) -> String {
self.id.clone()
}

fn status(&self) -> String {
format!["Generating {}s", self.ports_out.job]
}
Expand Down
5 changes: 0 additions & 5 deletions src/models/load_balancer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use crate::utils::error::SimulationError;
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct LoadBalancer {
id: String,
ports_in: PortsIn,
ports_out: PortsOut,
#[serde(default)]
Expand Down Expand Up @@ -100,10 +99,6 @@ impl LoadBalancer {
}

impl AsModel for LoadBalancer {
fn id(&self) -> String {
self.id.clone()
}

fn status(&self) -> String {
format!["Listening for {}s", self.ports_in.job]
}
Expand Down
49 changes: 46 additions & 3 deletions src/models/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,56 @@ use super::ModelMessage;
use crate::input_modeling::uniform_rng::UniformRNG;
use crate::utils::error::SimulationError;

/// The overall "wrapper" around a model, complete with the model's ID.
/// This is what you probably want to use.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Model {
id: String,
#[serde(flatten)]
inner: ModelType,
}

impl Model {
pub fn id(&self) -> &str {
self.id.as_str()
}
}

impl AsModel for Model {
fn status(&self) -> String {
self.inner.status()
}

fn events_ext(
&mut self,
uniform_rng: &mut UniformRNG,
incoming_message: ModelMessage,
) -> Result<Vec<ModelMessage>, SimulationError> {
self.inner.events_ext(uniform_rng, incoming_message)
}

fn events_int(
&mut self,
uniform_rng: &mut UniformRNG,
) -> Result<Vec<ModelMessage>, SimulationError> {
self.inner.events_int(uniform_rng)
}

fn time_advance(&mut self, time_delta: f64) {
self.inner.time_advance(time_delta)
}

fn until_next_event(&self) -> f64 {
self.inner.until_next_event()
}
}

/// An enum encompassing all the available types of models. Each variant
/// holds a concrete type that implements AsModel.
#[enum_dispatch(AsModel)]
#[derive(Serialize, Deserialize, Clone)]
#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(tag = "type")]
pub enum Model {
pub enum ModelType {
ExclusiveGateway,
Gate,
Generator,
Expand All @@ -36,7 +80,6 @@ pub enum Model {
/// reporting method `status`.
#[enum_dispatch]
pub trait AsModel {
fn id(&self) -> String;
fn status(&self) -> String;
fn events_ext(
&mut self,
Expand Down
5 changes: 0 additions & 5 deletions src/models/parallel_gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use crate::utils::error::SimulationError;
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ParallelGateway {
id: String,
ports_in: PortsIn,
ports_out: PortsOut,
#[serde(default)]
Expand Down Expand Up @@ -106,10 +105,6 @@ impl ParallelGateway {
}

impl AsModel for ParallelGateway {
fn id(&self) -> String {
self.id.clone()
}

fn status(&self) -> String {
String::from("Active")
}
Expand Down
5 changes: 0 additions & 5 deletions src/models/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use crate::utils::error::SimulationError;
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Processor {
id: String,
service_time: ContinuousRandomVariable,
#[serde(default = "max_usize")]
queue_capacity: usize,
Expand Down Expand Up @@ -138,10 +137,6 @@ impl Processor {
}

impl AsModel for Processor {
fn id(&self) -> String {
self.id.clone()
}

fn status(&self) -> String {
match self.state.phase {
Phase::Active => String::from("Processing"),
Expand Down
5 changes: 0 additions & 5 deletions src/models/stochastic_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use crate::utils::error::SimulationError;
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct StochasticGate {
id: String,
pass_distribution: BooleanRandomVariable,
ports_in: PortsIn,
ports_out: PortsOut,
Expand Down Expand Up @@ -114,10 +113,6 @@ impl StochasticGate {
}

impl AsModel for StochasticGate {
fn id(&self) -> String {
self.id.clone()
}

fn status(&self) -> String {
match self.state.phase {
Phase::Open => String::from("Pass"),
Expand Down
5 changes: 0 additions & 5 deletions src/models/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use crate::utils::error::SimulationError;
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Storage {
id: String,
ports_in: PortsIn,
ports_out: PortsOut,
#[serde(default)]
Expand Down Expand Up @@ -101,10 +100,6 @@ impl Storage {
}

impl AsModel for Storage {
fn id(&self) -> String {
self.id.clone()
}

fn status(&self) -> String {
match &self.state.job {
Some(stored) => format!["Storing {}", stored],
Expand Down
6 changes: 4 additions & 2 deletions src/simulator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,9 @@ impl Simulation {
target_ids.iter().zip(target_ports.iter()).for_each(
|(target_id, target_port)| {
next_messages.push(Message {
source_id: self.models[model_index].id(),
source_id: self.models[model_index]
.id()
.to_string(),
source_port: outgoing_message.port_name.clone(),
target_id: target_id.clone(),
target_port: target_port.clone(),
Expand Down Expand Up @@ -276,7 +278,7 @@ impl Simulation {
target_ids.iter().zip(target_ports.iter()).for_each(
|(target_id, target_port)| {
next_messages.push(Message {
source_id: self.models[model_index].id(),
source_id: self.models[model_index].id().to_string(),
source_port: outgoing_message.port_name.clone(),
target_id: target_id.clone(),
target_port: target_port.clone(),
Expand Down
20 changes: 10 additions & 10 deletions src/simulator/test_simulations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -665,16 +665,16 @@ mod tests {
#[test]
fn exclusive_gateway_proportions_chi_square() {
let models = r#"
- type: "Generator"
id: "generator-01"
- id: "generator-01"
type: "Generator"
portsIn: {}
portsOut:
job: "job"
messageInterdepartureTime:
exp:
lambda: 5.0
- type: "ExclusiveGateway"
id: "exclusive-01"
- id: "exclusive-01"
type: "ExclusiveGateway"
portsIn:
flowPaths:
- "in"
Expand All @@ -686,22 +686,22 @@ mod tests {
portWeights:
weightedIndex:
weights: [6, 3, 1]
- type: "Storage"
id: "storage-01"
- id: "storage-01"
type: "Storage"
portsIn:
store: "store"
read: "read"
portsOut:
stored: "stored"
- type: "Storage"
id: "storage-02"
- id: "storage-02"
type: "Storage"
portsIn:
store: "store"
read: "read"
portsOut:
stored: "stored"
- type: "Storage"
id: "storage-03"
- id: "storage-03"
type: "Storage"
portsIn:
store: "store"
read: "read"
Expand Down

0 comments on commit 9df24cf

Please sign in to comment.