Replacing giant translator structure with lots of small structures

This commit is contained in:
elvis
2025-07-02 10:37:29 +02:00
parent 2e16e2b002
commit f3ba949b82
5 changed files with 135 additions and 189 deletions

View File

@ -1,7 +1,8 @@
#![allow(dead_code)] #![allow(dead_code)]
use crate::rsprocess::structure::RSsystem; 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 crate::rsprocess::{frequency, perpetual, statistics, transitions};
use std::env; use std::env;
@ -69,7 +70,7 @@ pub fn target() -> std::io::Result<()> {
println!( println!(
"After {} steps we arrive at state:\n{}", "After {} steps we arrive at state:\n{}",
res.0, res.0,
WithTranslator::from_RSset(&translator, &res.1) translator::RSsetDisplay::from(&translator, &res.1)
); );
Ok(()) 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 { for (e, _c, _t) in res {
println!("{}", WithTranslator::from_RSset(&translator, &e)); println!("{}", translator::RSsetDisplay::from(&translator, &e));
} }
Ok(()) Ok(())
@ -123,7 +124,7 @@ pub fn hoop() -> std::io::Result<()> {
println!("The loop is composed by the sets:"); println!("The loop is composed by the sets:");
for e in res { for e in res {
println!("{}", WithTranslator::from_RSset(&translator, &e)); println!("{}", translator::RSsetDisplay::from(&translator, &e));
} }
Ok(()) Ok(())
@ -147,7 +148,7 @@ pub fn freq() -> std::io::Result<()> {
println!( println!(
"Frequency of encountered symbols:\n{}", "Frequency of encountered symbols:\n{}",
WithTranslator::from_Frequency(&translator, &res) translator::FrequencyDisplay::from(&translator, &res)
); );
Ok(()) Ok(())

View File

@ -2,7 +2,8 @@
use super::structure::RSset; use super::structure::RSset;
use super::structure::RSsystem; use super::structure::RSsystem;
use super::translator::{Translator, WithTranslator}; use super::translator;
use super::translator::Translator;
#[allow(non_snake_case)] #[allow(non_snake_case)]
pub fn of_RSsystem<'a>(translator: &'a Translator, system: &'a RSsystem) -> String { 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!( result.push_str(&format!(
"{}\n", "{}\n",
WithTranslator::from_RSset(translator, system.get_available_entities()) translator::RSsetDisplay::from(translator, system.get_available_entities())
)); ));
let reactants = system let reactants = system
@ -26,7 +27,7 @@ pub fn of_RSsystem<'a>(translator: &'a Translator, system: &'a RSsystem) -> Stri
result.push_str(&format!( result.push_str(&format!(
"The reactants are {}:\n{}\n", "The reactants are {}:\n{}\n",
reactants.len(), reactants.len(),
WithTranslator::from_RSset(translator, &reactants) translator::RSsetDisplay::from(translator, &reactants)
)); ));
let inhibitors = system let inhibitors = system
@ -36,7 +37,7 @@ pub fn of_RSsystem<'a>(translator: &'a Translator, system: &'a RSsystem) -> Stri
result.push_str(&format!( result.push_str(&format!(
"The inhibitors are {}:\n{}\n", "The inhibitors are {}:\n{}\n",
inhibitors.len(), inhibitors.len(),
WithTranslator::from_RSset(translator, &inhibitors) translator::RSsetDisplay::from(translator, &inhibitors)
)); ));
let products = system let products = system
@ -46,28 +47,28 @@ pub fn of_RSsystem<'a>(translator: &'a Translator, system: &'a RSsystem) -> Stri
result.push_str(&format!( result.push_str(&format!(
"The products are {}:\n{}\n", "The products are {}:\n{}\n",
products.len(), products.len(),
WithTranslator::from_RSset(translator, &products) translator::RSsetDisplay::from(translator, &products)
)); ));
let total = reactants.union(&inhibitors.union(&products)); let total = reactants.union(&inhibitors.union(&products));
result.push_str(&format!( result.push_str(&format!(
"The reactions involve {} entities:\n{}\n", "The reactions involve {} entities:\n{}\n",
total.len(), total.len(),
WithTranslator::from_RSset(translator, &total) translator::RSsetDisplay::from(translator, &total)
)); ));
let entities_env = system.get_delta().all_elements(); let entities_env = system.get_delta().all_elements();
result.push_str(&format!( result.push_str(&format!(
"The environment involves {} entities:\n{}\n", "The environment involves {} entities:\n{}\n",
entities_env.len(), entities_env.len(),
WithTranslator::from_RSset(translator, &entities_env) translator::RSsetDisplay::from(translator, &entities_env)
)); ));
let entities_context = system.get_context_process().all_elements(); let entities_context = system.get_context_process().all_elements();
result.push_str(&format!( result.push_str(&format!(
"The context involves {} entities:\n{}\n", "The context involves {} entities:\n{}\n",
entities_context.len(), entities_context.len(),
WithTranslator::from_RSset(translator, &entities_context) translator::RSsetDisplay::from(translator, &entities_context)
)); ));
let entities_all = total let entities_all = total
@ -78,7 +79,7 @@ pub fn of_RSsystem<'a>(translator: &'a Translator, system: &'a RSsystem) -> Stri
result.push_str(&format!( result.push_str(&format!(
"The whole RS involves {} entities:\n{}\n", "The whole RS involves {} entities:\n{}\n",
entities_all.len(), entities_all.len(),
WithTranslator::from_RSset(translator, &entities_all) translator::RSsetDisplay::from(translator, &entities_all)
)); ));
let possible_e = products let possible_e = products
@ -88,14 +89,14 @@ pub fn of_RSsystem<'a>(translator: &'a Translator, system: &'a RSsystem) -> Stri
result.push_str(&format!( result.push_str(&format!(
"There are {} reactants that will never be available:\n{}\n", "There are {} reactants that will never be available:\n{}\n",
missing_e.len(), missing_e.len(),
WithTranslator::from_RSset(translator, &missing_e) translator::RSsetDisplay::from(translator, &missing_e)
)); ));
let entities_not_needed = entities_context.subtraction(&total); let entities_not_needed = entities_context.subtraction(&total);
result.push_str(&format!( result.push_str(&format!(
"The context can provide {} entities that will never be used:\n{}\n", "The context can provide {} entities that will never be used:\n{}\n",
entities_not_needed.len(), entities_not_needed.len(),
WithTranslator::from_RSset(translator, &entities_not_needed) translator::RSsetDisplay::from(translator, &entities_not_needed)
)); ));
result.push_str(&format!( result.push_str(&format!(

View File

@ -482,7 +482,7 @@ impl Default for RSsystem {
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// RSsystem // RSlabel
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct RSlabel { pub struct RSlabel {

View File

@ -147,7 +147,6 @@ pub fn run_separated(
let current = current.unwrap(); let current = current.unwrap();
res.push(current.0.get_context()); res.push(current.0.get_context());
let mut current = current.1; let mut current = current.1;
while let Some((label, next)) = one_transition(&current)? { while let Some((label, next)) = one_transition(&current)? {
current = next; current = next;
res.push(label.get_context()); res.push(label.get_context());

View File

@ -44,6 +44,8 @@ impl Translator {
} }
} }
// -----------------------------------------------------------------------------
// print structures
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
use super::{ use super::{
frequency::Frequency, frequency::Frequency,
@ -54,101 +56,33 @@ use super::{
}; };
use std::fmt; use std::fmt;
#[allow(clippy::large_enum_variant)] macro_rules! translator_structure {
#[allow(clippy::upper_case_acronyms)] ($name:ident, $type:ty, $dataname:ident, $print_func:ident) => {
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub enum WithTranslator<'a> { #[allow(dead_code)]
RSset { pub struct $name<'a> {
translator: &'a Translator, translator: &'a Translator,
set: &'a RSset, $dataname: &'a $type,
}, }
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 { #[allow(dead_code)]
($name:ident, $type:ty, $dataname:ident, $type2: ident) => { impl <'a>$name<'a> {
pub fn $name(translator: &'a Translator, $dataname: &'a $type) -> Self { pub fn from(translator: &'a Translator, $dataname: &'a $type) -> Self {
WithTranslator::$type2 { $name { translator, $dataname }
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); // RSset
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
// -----------------------------------------------------------------------------
fn print_set( fn print_set(
f: &mut fmt::Formatter, f: &mut fmt::Formatter,
@ -171,6 +105,12 @@ fn print_set(
write!(f, "}}") write!(f, "}}")
} }
translator_structure!(RSsetDisplay, RSset, set, print_set);
// RSreaction
fn print_reaction( fn print_reaction(
f: &mut fmt::Formatter, f: &mut fmt::Formatter,
translator: &Translator, translator: &Translator,
@ -179,12 +119,18 @@ fn print_reaction(
write!( write!(
f, f,
"(r: {}, i: {}, p: {})", "(r: {}, i: {}, p: {})",
WithTranslator::from_RSset(translator, reaction.reactants()), RSsetDisplay::from(translator, reaction.reactants()),
WithTranslator::from_RSset(translator, reaction.inihibitors()), RSsetDisplay::from(translator, reaction.inihibitors()),
WithTranslator::from_RSset(translator, reaction.products()) RSsetDisplay::from(translator, reaction.products())
) )
} }
translator_structure!(RSreactionDisplay, RSreaction, reaction, print_reaction);
// RSprocess
fn print_process( fn print_process(
f: &mut fmt::Formatter, f: &mut fmt::Formatter,
translator: &Translator, translator: &Translator,
@ -207,8 +153,8 @@ fn print_process(
write!( write!(
f, f,
"[entities: {}, next_process: {}]", "[entities: {}, next_process: {}]",
WithTranslator::from_RSset(translator, entities), RSsetDisplay::from(translator, entities),
WithTranslator::from_RSprocess(translator, next_process) RSprocessDisplay::from(translator, next_process)
) )
} }
WaitEntity { WaitEntity {
@ -219,8 +165,8 @@ fn print_process(
write!( write!(
f, f,
"[repeat: {repeat}, repeated_process: {}, next_process: {}]", "[repeat: {repeat}, repeated_process: {}, next_process: {}]",
WithTranslator::from_RSprocess(translator, repeated_process), RSprocessDisplay::from(translator, repeated_process),
WithTranslator::from_RSprocess(translator, next_process) RSprocessDisplay::from(translator, next_process)
) )
} }
Summation { children } => { Summation { children } => {
@ -231,13 +177,13 @@ fn print_process(
write!( write!(
f, f,
"{}", "{}",
WithTranslator::from_RSprocess(translator, child) RSprocessDisplay::from(translator, child)
)?; )?;
} else { } else {
write!( write!(
f, f,
"{} + ", "{} + ",
WithTranslator::from_RSprocess(translator, child) RSprocessDisplay::from(translator, child)
)?; )?;
} }
} }
@ -251,13 +197,13 @@ fn print_process(
write!( write!(
f, f,
"{}", "{}",
WithTranslator::from_RSprocess(translator, child) RSprocessDisplay::from(translator, child)
)?; )?;
} else { } else {
write!( write!(
f, 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( fn print_choices(
f: &mut fmt::Formatter, f: &mut fmt::Formatter,
translator: &Translator, translator: &Translator,
@ -278,21 +230,27 @@ fn print_choices(
write!( write!(
f, f,
"[set: {}, process: {}]", "[set: {}, process: {}]",
WithTranslator::from_RSset(translator, &el.0), RSsetDisplay::from(translator, &el.0),
WithTranslator::from_RSprocess(translator, &el.1) RSprocessDisplay::from(translator, &el.1)
)?; )?;
} else { } else {
write!( write!(
f, f,
"[set: {}, process: {}], ", "[set: {}, process: {}], ",
WithTranslator::from_RSset(translator, &el.0), RSsetDisplay::from(translator, &el.0),
WithTranslator::from_RSprocess(translator, &el.1) RSprocessDisplay::from(translator, &el.1)
)?; )?;
} }
} }
write!(f, "]") write!(f, "]")
} }
translator_structure!(RSchoicesDisplay, RSchoices, choices, print_choices);
// RSenvironment
fn print_environment( fn print_environment(
f: &mut fmt::Formatter, f: &mut fmt::Formatter,
translator: &Translator, translator: &Translator,
@ -306,20 +264,26 @@ fn print_environment(
f, f,
"({} -> {})", "({} -> {})",
translator.decode(*el.0).unwrap_or("Missing".into()), translator.decode(*el.0).unwrap_or("Missing".into()),
WithTranslator::from_RSprocess(translator, el.1) RSprocessDisplay::from(translator, el.1)
)?; )?;
} else { } else {
write!( write!(
f, f,
"({} -> {}), ", "({} -> {}), ",
translator.decode(*el.0).unwrap_or("Missing".into()), translator.decode(*el.0).unwrap_or("Missing".into()),
WithTranslator::from_RSprocess(translator, el.1) RSprocessDisplay::from(translator, el.1)
)?; )?;
} }
} }
write!(f, "}}") write!(f, "}}")
} }
translator_structure!(RSenvironmentDisplay, RSenvironment, environment, print_environment);
// RSsystem
fn print_system( fn print_system(
f: &mut fmt::Formatter, f: &mut fmt::Formatter,
translator: &Translator, translator: &Translator,
@ -328,21 +292,27 @@ fn print_system(
write!( write!(
f, f,
"[delta: {}, available_entities: {}, context_process: {}, reaction_rules: [", "[delta: {}, available_entities: {}, context_process: {}, reaction_rules: [",
WithTranslator::from_RSenvironment(translator, system.get_delta()), RSenvironmentDisplay::from(translator, system.get_delta()),
WithTranslator::from_RSset(translator, system.get_available_entities()), RSsetDisplay::from(translator, system.get_available_entities()),
WithTranslator::from_RSprocess(translator, system.get_context_process()) RSprocessDisplay::from(translator, system.get_context_process())
)?; )?;
let mut it = system.get_reaction_rules().iter().peekable(); let mut it = system.get_reaction_rules().iter().peekable();
while let Some(el) = it.next() { while let Some(el) = it.next() {
if it.peek().is_none() { if it.peek().is_none() {
write!(f, "{}", WithTranslator::from_RSreaction(translator, el))?; write!(f, "{}", RSreactionDisplay::from(translator, el))?;
} else { } else {
write!(f, "{}, ", WithTranslator::from_RSreaction(translator, el))?; write!(f, "{}, ", RSreactionDisplay::from(translator, el))?;
} }
} }
write!(f, "] ]") write!(f, "] ]")
} }
translator_structure!(RSsystemDisplay, RSsystem, system, print_system);
// RSlabel
fn print_label( fn print_label(
f: &mut fmt::Formatter, f: &mut fmt::Formatter,
translator: &Translator, translator: &Translator,
@ -351,17 +321,23 @@ fn print_label(
write!( write!(
f, f,
"{{available_entities: {}, context: {}, t: {}, reactants: {}, reactantsi: {}, inihibitors: {}, ireactants: {}, products: {}}}", "{{available_entities: {}, context: {}, t: {}, reactants: {}, reactantsi: {}, inihibitors: {}, ireactants: {}, products: {}}}",
WithTranslator::from_RSset(translator, &label.available_entities), RSsetDisplay::from(translator, &label.available_entities),
WithTranslator::from_RSset(translator, &label.context), RSsetDisplay::from(translator, &label.context),
WithTranslator::from_RSset(translator, &label.t), RSsetDisplay::from(translator, &label.t),
WithTranslator::from_RSset(translator, &label.reactants), RSsetDisplay::from(translator, &label.reactants),
WithTranslator::from_RSset(translator, &label.reactantsi), RSsetDisplay::from(translator, &label.reactantsi),
WithTranslator::from_RSset(translator, &label.inihibitors), RSsetDisplay::from(translator, &label.inihibitors),
WithTranslator::from_RSset(translator, &label.ireactants), RSsetDisplay::from(translator, &label.ireactants),
WithTranslator::from_RSset(translator, &label.products), RSsetDisplay::from(translator, &label.products),
) )
} }
translator_structure!(RSlabelDisplay, RSlabel, label, print_label);
// RSassertOp
fn print_assert_op( fn print_assert_op(
f: &mut fmt::Formatter, f: &mut fmt::Formatter,
_translator: &Translator, _translator: &Translator,
@ -384,6 +360,12 @@ fn print_assert_op(
} }
} }
translator_structure!(RSassertOpDisplay, RSassertOp, assert_op, print_assert_op);
// RSassert
#[allow(unused_variables)] #[allow(unused_variables)]
fn print_assert( fn print_assert(
f: &mut fmt::Formatter, f: &mut fmt::Formatter,
@ -393,6 +375,12 @@ fn print_assert(
todo!() todo!()
} }
translator_structure!(RSassertDisplay, RSassert, assert, print_assert);
// RSBHML
#[allow(unused_variables)] #[allow(unused_variables)]
fn print_bhml( fn print_bhml(
f: &mut fmt::Formatter, f: &mut fmt::Formatter,
@ -402,6 +390,11 @@ fn print_bhml(
todo!() todo!()
} }
translator_structure!(RSBHMLDisplay, RSBHML, bhml, print_bhml);
// Frequency
fn print_frequency( fn print_frequency(
f: &mut fmt::Formatter, f: &mut fmt::Formatter,
translator: &Translator, translator: &Translator,
@ -442,53 +435,5 @@ fn print_frequency(
write!(f, "]") write!(f, "]")
} }
impl<'a> fmt::Display for WithTranslator<'a> { translator_structure!(FrequencyDisplay, Frequency, frequency, print_frequency);
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),
}
}
}