diff --git a/src/examples.rs b/src/examples.rs index 4ffaec9..b083e8b 100644 --- a/src/examples.rs +++ b/src/examples.rs @@ -1,7 +1,8 @@ #![allow(dead_code)] use crate::rsprocess::structure::RSsystem; -use crate::rsprocess::translator::{Translator, WithTranslator}; +use crate::rsprocess::translator; +use crate::rsprocess::translator::Translator; use crate::rsprocess::{frequency, perpetual, statistics, transitions}; use std::env; @@ -69,7 +70,7 @@ pub fn target() -> std::io::Result<()> { println!( "After {} steps we arrive at state:\n{}", res.0, - WithTranslator::from_RSset(&translator, &res.1) + translator::RSsetDisplay::from(&translator, &res.1) ); Ok(()) @@ -93,10 +94,10 @@ pub fn run() -> std::io::Result<()> { } }; - println!("The trace is composed of the entities:"); + println!("The trace is composed by the set of entities:"); for (e, _c, _t) in res { - println!("{}", WithTranslator::from_RSset(&translator, &e)); + println!("{}", translator::RSsetDisplay::from(&translator, &e)); } Ok(()) @@ -123,7 +124,7 @@ pub fn hoop() -> std::io::Result<()> { println!("The loop is composed by the sets:"); for e in res { - println!("{}", WithTranslator::from_RSset(&translator, &e)); + println!("{}", translator::RSsetDisplay::from(&translator, &e)); } Ok(()) @@ -147,7 +148,7 @@ pub fn freq() -> std::io::Result<()> { println!( "Frequency of encountered symbols:\n{}", - WithTranslator::from_Frequency(&translator, &res) + translator::FrequencyDisplay::from(&translator, &res) ); Ok(()) diff --git a/src/rsprocess/statistics.rs b/src/rsprocess/statistics.rs index 79e2f11..26877cc 100644 --- a/src/rsprocess/statistics.rs +++ b/src/rsprocess/statistics.rs @@ -2,7 +2,8 @@ use super::structure::RSset; use super::structure::RSsystem; -use super::translator::{Translator, WithTranslator}; +use super::translator; +use super::translator::Translator; #[allow(non_snake_case)] pub fn of_RSsystem<'a>(translator: &'a Translator, system: &'a RSsystem) -> String { @@ -16,7 +17,7 @@ pub fn of_RSsystem<'a>(translator: &'a Translator, system: &'a RSsystem) -> Stri )); result.push_str(&format!( "{}\n", - WithTranslator::from_RSset(translator, system.get_available_entities()) + translator::RSsetDisplay::from(translator, system.get_available_entities()) )); let reactants = system @@ -26,7 +27,7 @@ pub fn of_RSsystem<'a>(translator: &'a Translator, system: &'a RSsystem) -> Stri result.push_str(&format!( "The reactants are {}:\n{}\n", reactants.len(), - WithTranslator::from_RSset(translator, &reactants) + translator::RSsetDisplay::from(translator, &reactants) )); let inhibitors = system @@ -36,7 +37,7 @@ pub fn of_RSsystem<'a>(translator: &'a Translator, system: &'a RSsystem) -> Stri result.push_str(&format!( "The inhibitors are {}:\n{}\n", inhibitors.len(), - WithTranslator::from_RSset(translator, &inhibitors) + translator::RSsetDisplay::from(translator, &inhibitors) )); let products = system @@ -46,28 +47,28 @@ pub fn of_RSsystem<'a>(translator: &'a Translator, system: &'a RSsystem) -> Stri result.push_str(&format!( "The products are {}:\n{}\n", products.len(), - WithTranslator::from_RSset(translator, &products) + translator::RSsetDisplay::from(translator, &products) )); let total = reactants.union(&inhibitors.union(&products)); result.push_str(&format!( "The reactions involve {} entities:\n{}\n", total.len(), - WithTranslator::from_RSset(translator, &total) + translator::RSsetDisplay::from(translator, &total) )); let entities_env = system.get_delta().all_elements(); result.push_str(&format!( "The environment involves {} entities:\n{}\n", entities_env.len(), - WithTranslator::from_RSset(translator, &entities_env) + translator::RSsetDisplay::from(translator, &entities_env) )); let entities_context = system.get_context_process().all_elements(); result.push_str(&format!( "The context involves {} entities:\n{}\n", entities_context.len(), - WithTranslator::from_RSset(translator, &entities_context) + translator::RSsetDisplay::from(translator, &entities_context) )); let entities_all = total @@ -78,7 +79,7 @@ pub fn of_RSsystem<'a>(translator: &'a Translator, system: &'a RSsystem) -> Stri result.push_str(&format!( "The whole RS involves {} entities:\n{}\n", entities_all.len(), - WithTranslator::from_RSset(translator, &entities_all) + translator::RSsetDisplay::from(translator, &entities_all) )); let possible_e = products @@ -88,14 +89,14 @@ pub fn of_RSsystem<'a>(translator: &'a Translator, system: &'a RSsystem) -> Stri result.push_str(&format!( "There are {} reactants that will never be available:\n{}\n", missing_e.len(), - WithTranslator::from_RSset(translator, &missing_e) + translator::RSsetDisplay::from(translator, &missing_e) )); let entities_not_needed = entities_context.subtraction(&total); result.push_str(&format!( "The context can provide {} entities that will never be used:\n{}\n", entities_not_needed.len(), - WithTranslator::from_RSset(translator, &entities_not_needed) + translator::RSsetDisplay::from(translator, &entities_not_needed) )); result.push_str(&format!( diff --git a/src/rsprocess/structure.rs b/src/rsprocess/structure.rs index f041812..dd97d35 100644 --- a/src/rsprocess/structure.rs +++ b/src/rsprocess/structure.rs @@ -186,7 +186,7 @@ impl Default for RSreaction { pub enum RSprocess { Nill, RecursiveIdentifier { - identifier: IdType, + identifier: IdType, }, EntitySet { entities: RSset, @@ -482,7 +482,7 @@ impl Default for RSsystem { } // ----------------------------------------------------------------------------- -// RSsystem +// RSlabel // ----------------------------------------------------------------------------- #[derive(Clone, Debug)] pub struct RSlabel { diff --git a/src/rsprocess/transitions.rs b/src/rsprocess/transitions.rs index 93fc192..6bc7378 100644 --- a/src/rsprocess/transitions.rs +++ b/src/rsprocess/transitions.rs @@ -147,7 +147,6 @@ pub fn run_separated( let current = current.unwrap(); res.push(current.0.get_context()); let mut current = current.1; - while let Some((label, next)) = one_transition(¤t)? { current = next; res.push(label.get_context()); diff --git a/src/rsprocess/translator.rs b/src/rsprocess/translator.rs index 5b0f82f..7faabfd 100644 --- a/src/rsprocess/translator.rs +++ b/src/rsprocess/translator.rs @@ -44,6 +44,8 @@ impl Translator { } } +// ----------------------------------------------------------------------------- +// print structures // ----------------------------------------------------------------------------- use super::{ frequency::Frequency, @@ -54,101 +56,33 @@ use super::{ }; use std::fmt; -#[allow(clippy::large_enum_variant)] -#[allow(clippy::upper_case_acronyms)] -#[derive(Clone, Debug)] -pub enum WithTranslator<'a> { - RSset { - translator: &'a Translator, - set: &'a RSset, - }, - RSreaction { - translator: &'a Translator, - reaction: &'a RSreaction, - }, - RSprocess { - translator: &'a Translator, - process: &'a RSprocess, - }, - RSchoices { - translator: &'a Translator, - choices: &'a RSchoices, - }, - RSenvironment { - translator: &'a Translator, - environment: &'a RSenvironment, - }, - RSsystem { - translator: &'a Translator, - system: &'a RSsystem, - }, - RSlabel { - translator: &'a Translator, - label: &'a RSlabel, - }, - RSassertOp { - translator: &'a Translator, - assert_op: &'a RSassertOp, - }, - RSassert { - translator: &'a Translator, - assert: &'a RSassert, - }, - RSBHML { - translator: &'a Translator, - bhml: &'a RSBHML, - }, - Frequency { - translator: &'a Translator, - frequency: &'a Frequency, - }, -} - -macro_rules! from_RS { - ($name:ident, $type:ty, $dataname:ident, $type2: ident) => { - pub fn $name(translator: &'a Translator, $dataname: &'a $type) -> Self { - WithTranslator::$type2 { - translator, - $dataname, - } +macro_rules! translator_structure { + ($name:ident, $type:ty, $dataname:ident, $print_func:ident) => { + #[derive(Clone, Debug)] + #[allow(dead_code)] + pub struct $name<'a> { + translator: &'a Translator, + $dataname: &'a $type, } + + #[allow(dead_code)] + impl <'a>$name<'a> { + pub fn from(translator: &'a Translator, $dataname: &'a $type) -> Self { + $name { translator, $dataname } + } + } + + impl<'a> fmt::Display for $name<'a> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + $print_func(f, self.translator, self.$dataname) + } + } }; } -#[allow(non_snake_case)] -#[allow(dead_code)] -impl<'a> WithTranslator<'a> { - from_RS!(from_RSset, RSset, set, RSset); - from_RS!(from_RSreaction, RSreaction, reaction, RSreaction); - from_RS!(from_RSprocess, RSprocess, process, RSprocess); - - from_RS!(from_RSchoices, RSchoices, choices, RSchoices); - - from_RS!( - from_RSenvironment, - RSenvironment, - environment, - RSenvironment - ); - - from_RS!(from_RSsystem, RSsystem, system, RSsystem); - - from_RS!(from_RSlabel, RSlabel, label, RSlabel); - - from_RS!(from_RSassertOp, RSassertOp, assert_op, RSassertOp); - - from_RS!(from_RSassert, RSassert, assert, RSassert); - - from_RS!(from_RSBHML, RSBHML, bhml, RSBHML); - - from_RS!(from_Frequency, Frequency, frequency, Frequency); -} - -// ----------------------------------------------------------------------------- -// Printing functions -// ----------------------------------------------------------------------------- +// RSset fn print_set( f: &mut fmt::Formatter, @@ -171,6 +105,12 @@ fn print_set( write!(f, "}}") } +translator_structure!(RSsetDisplay, RSset, set, print_set); + + + +// RSreaction + fn print_reaction( f: &mut fmt::Formatter, translator: &Translator, @@ -179,12 +119,18 @@ fn print_reaction( write!( f, "(r: {}, i: {}, p: {})", - WithTranslator::from_RSset(translator, reaction.reactants()), - WithTranslator::from_RSset(translator, reaction.inihibitors()), - WithTranslator::from_RSset(translator, reaction.products()) + RSsetDisplay::from(translator, reaction.reactants()), + RSsetDisplay::from(translator, reaction.inihibitors()), + RSsetDisplay::from(translator, reaction.products()) ) } +translator_structure!(RSreactionDisplay, RSreaction, reaction, print_reaction); + + + +// RSprocess + fn print_process( f: &mut fmt::Formatter, translator: &Translator, @@ -207,8 +153,8 @@ fn print_process( write!( f, "[entities: {}, next_process: {}]", - WithTranslator::from_RSset(translator, entities), - WithTranslator::from_RSprocess(translator, next_process) + RSsetDisplay::from(translator, entities), + RSprocessDisplay::from(translator, next_process) ) } WaitEntity { @@ -219,8 +165,8 @@ fn print_process( write!( f, "[repeat: {repeat}, repeated_process: {}, next_process: {}]", - WithTranslator::from_RSprocess(translator, repeated_process), - WithTranslator::from_RSprocess(translator, next_process) + RSprocessDisplay::from(translator, repeated_process), + RSprocessDisplay::from(translator, next_process) ) } Summation { children } => { @@ -231,13 +177,13 @@ fn print_process( write!( f, "{}", - WithTranslator::from_RSprocess(translator, child) + RSprocessDisplay::from(translator, child) )?; } else { write!( f, "{} + ", - WithTranslator::from_RSprocess(translator, child) + RSprocessDisplay::from(translator, child) )?; } } @@ -251,13 +197,13 @@ fn print_process( write!( f, "{}", - WithTranslator::from_RSprocess(translator, child) + RSprocessDisplay::from(translator, child) )?; } else { write!( f, "{}, ", - WithTranslator::from_RSprocess(translator, child) + RSprocessDisplay::from(translator, child) )?; } } @@ -266,6 +212,12 @@ fn print_process( } } +translator_structure!(RSprocessDisplay, RSprocess, process, print_process); + + + +// RSchoices + fn print_choices( f: &mut fmt::Formatter, translator: &Translator, @@ -278,21 +230,27 @@ fn print_choices( write!( f, "[set: {}, process: {}]", - WithTranslator::from_RSset(translator, &el.0), - WithTranslator::from_RSprocess(translator, &el.1) + RSsetDisplay::from(translator, &el.0), + RSprocessDisplay::from(translator, &el.1) )?; } else { write!( f, "[set: {}, process: {}], ", - WithTranslator::from_RSset(translator, &el.0), - WithTranslator::from_RSprocess(translator, &el.1) + RSsetDisplay::from(translator, &el.0), + RSprocessDisplay::from(translator, &el.1) )?; } } write!(f, "]") } +translator_structure!(RSchoicesDisplay, RSchoices, choices, print_choices); + + + +// RSenvironment + fn print_environment( f: &mut fmt::Formatter, translator: &Translator, @@ -306,20 +264,26 @@ fn print_environment( f, "({} -> {})", translator.decode(*el.0).unwrap_or("Missing".into()), - WithTranslator::from_RSprocess(translator, el.1) + RSprocessDisplay::from(translator, el.1) )?; } else { write!( f, "({} -> {}), ", translator.decode(*el.0).unwrap_or("Missing".into()), - WithTranslator::from_RSprocess(translator, el.1) + RSprocessDisplay::from(translator, el.1) )?; } } write!(f, "}}") } +translator_structure!(RSenvironmentDisplay, RSenvironment, environment, print_environment); + + + +// RSsystem + fn print_system( f: &mut fmt::Formatter, translator: &Translator, @@ -328,21 +292,27 @@ fn print_system( write!( f, "[delta: {}, available_entities: {}, context_process: {}, reaction_rules: [", - WithTranslator::from_RSenvironment(translator, system.get_delta()), - WithTranslator::from_RSset(translator, system.get_available_entities()), - WithTranslator::from_RSprocess(translator, system.get_context_process()) + RSenvironmentDisplay::from(translator, system.get_delta()), + RSsetDisplay::from(translator, system.get_available_entities()), + RSprocessDisplay::from(translator, system.get_context_process()) )?; let mut it = system.get_reaction_rules().iter().peekable(); while let Some(el) = it.next() { if it.peek().is_none() { - write!(f, "{}", WithTranslator::from_RSreaction(translator, el))?; + write!(f, "{}", RSreactionDisplay::from(translator, el))?; } else { - write!(f, "{}, ", WithTranslator::from_RSreaction(translator, el))?; + write!(f, "{}, ", RSreactionDisplay::from(translator, el))?; } } write!(f, "] ]") } +translator_structure!(RSsystemDisplay, RSsystem, system, print_system); + + + +// RSlabel + fn print_label( f: &mut fmt::Formatter, translator: &Translator, @@ -351,17 +321,23 @@ fn print_label( write!( f, "{{available_entities: {}, context: {}, t: {}, reactants: {}, reactantsi: {}, inihibitors: {}, ireactants: {}, products: {}}}", - WithTranslator::from_RSset(translator, &label.available_entities), - WithTranslator::from_RSset(translator, &label.context), - WithTranslator::from_RSset(translator, &label.t), - WithTranslator::from_RSset(translator, &label.reactants), - WithTranslator::from_RSset(translator, &label.reactantsi), - WithTranslator::from_RSset(translator, &label.inihibitors), - WithTranslator::from_RSset(translator, &label.ireactants), - WithTranslator::from_RSset(translator, &label.products), + RSsetDisplay::from(translator, &label.available_entities), + RSsetDisplay::from(translator, &label.context), + RSsetDisplay::from(translator, &label.t), + RSsetDisplay::from(translator, &label.reactants), + RSsetDisplay::from(translator, &label.reactantsi), + RSsetDisplay::from(translator, &label.inihibitors), + RSsetDisplay::from(translator, &label.ireactants), + RSsetDisplay::from(translator, &label.products), ) } +translator_structure!(RSlabelDisplay, RSlabel, label, print_label); + + + +// RSassertOp + fn print_assert_op( f: &mut fmt::Formatter, _translator: &Translator, @@ -384,6 +360,12 @@ fn print_assert_op( } } +translator_structure!(RSassertOpDisplay, RSassertOp, assert_op, print_assert_op); + + + +// RSassert + #[allow(unused_variables)] fn print_assert( f: &mut fmt::Formatter, @@ -393,6 +375,12 @@ fn print_assert( todo!() } +translator_structure!(RSassertDisplay, RSassert, assert, print_assert); + + + +// RSBHML + #[allow(unused_variables)] fn print_bhml( f: &mut fmt::Formatter, @@ -402,6 +390,11 @@ fn print_bhml( todo!() } +translator_structure!(RSBHMLDisplay, RSBHML, bhml, print_bhml); + + +// Frequency + fn print_frequency( f: &mut fmt::Formatter, translator: &Translator, @@ -442,53 +435,5 @@ fn print_frequency( write!(f, "]") } -impl<'a> fmt::Display for WithTranslator<'a> { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match self { - WithTranslator::RSset { - translator, - set - } => print_set(f, translator, set), - WithTranslator::RSreaction { - translator, - reaction, - } => print_reaction(f, translator, reaction), - WithTranslator::RSprocess { - translator, - process, - } => print_process(f, translator, process), - WithTranslator::RSchoices { - translator, - choices, - } => print_choices(f, translator, choices), - WithTranslator::RSenvironment { - translator, - environment, - } => print_environment(f, translator, environment), - WithTranslator::RSsystem { - translator, - system - } => print_system(f, translator, system), - WithTranslator::RSlabel { - translator, - label - } => print_label(f, translator, label), - WithTranslator::RSassertOp { - translator, - assert_op, - } => print_assert_op(f, translator, assert_op), - WithTranslator::RSassert { - translator, - assert - } => print_assert(f, translator, assert), - WithTranslator::RSBHML { - translator, - bhml - } => print_bhml(f, translator, bhml), - WithTranslator::Frequency { - translator, - frequency, - } => print_frequency(f, translator, frequency), - } - } -} +translator_structure!(FrequencyDisplay, Frequency, frequency, print_frequency); +