Better AllTransitions and implemented iterator over transitions

This commit is contained in:
elvis
2025-06-12 19:16:32 +02:00
parent 75c10dc313
commit a25316fa20
4 changed files with 104 additions and 61 deletions

View File

@ -67,8 +67,32 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// println!("all_transitions: {:?}", rsprocess::transitions::all_transitions(&sys));
// -------------------------------------------------------------------------
// parse_ctx("[({a}.nil + {b}.nil)]",Ks) , allTransitions(sys([],[],Ks,[react([a],[c],[a]),react([b],[d],[b])]),Moves).
// let env = grammar::EnvironmentParser::new().parse("[]").unwrap();
// let process = grammar::ContextParser::new().parse("[({a}.nil + {b}.nil)]").unwrap();
// let sys = rsprocess::structure::RSsystem::from(*env,
// rsprocess::structure::RSset::from(vec![]),
// *process,
// vec![
// rsprocess::structure::RSreaction::from(
// rsprocess::structure::RSset::from(vec!["a"]),
// rsprocess::structure::RSset::from(vec!["c"]),
// rsprocess::structure::RSset::from(vec!["a"])
// ),
// rsprocess::structure::RSreaction::from(
// rsprocess::structure::RSset::from(vec!["b"]),
// rsprocess::structure::RSset::from(vec!["d"]),
// rsprocess::structure::RSset::from(vec!["b"])
// )
// ]);
// println!("all_transitions: {:?}", rsprocess::transitions::all_transitions(&sys));
// -------------------------------------------------------------------------
// parse_ctx("[({a}.nil + {b}.nil),({c}.nil + {d}.nil)]",Ks) , allTransitions(sys([],[],Ks,[react([a],[c],[a]),react([b],[d],[b])]),Moves).
let env = grammar::EnvironmentParser::new().parse("[]").unwrap();
let process = grammar::ContextParser::new().parse("[({a}.nil + {b}.nil)]").unwrap();
let process = grammar::ContextParser::new().parse("[({a}.nil + {b}.nil),({c}.nil + {d}.nil)]").unwrap();
let sys = rsprocess::structure::RSsystem::from(*env,
rsprocess::structure::RSset::from(vec![]),
@ -86,24 +110,12 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
)
]);
let it = rsprocess::transitions::iterator_transitions(&sys)?;
println!("all_transitions: {:?}", rsprocess::transitions::all_transitions(&sys));
// -------------------------------------------------------------------------
// use std::rc::Rc;
// let mut a = rsprocess::structure::RSChoices::from(vec![
// (Rc::new(rsprocess::structure::RSset::from(vec!["a"])),
// Rc::new(rsprocess::structure::RSprocess::Nill)),
// ]);
// let b = rsprocess::structure::RSChoices::from(vec![
// (Rc::new(rsprocess::structure::RSset::from(vec!["b"])),
// Rc::new(rsprocess::structure::RSprocess::Nill)),
// ]);
// a.shuffle(b);
// println!("shuffle: {:?}", a);
for (n, i) in it.into_iter().enumerate() {
println!("next i - {n}: {:?}", i);
println!("--------------------");
}
Ok(())
}

View File

@ -1,3 +1,4 @@
pub mod structure;
pub mod support_structures;
pub mod classical;
pub mod transitions;

View File

@ -0,0 +1,65 @@
#![allow(dead_code)]
use std::rc::Rc;
use super::structure::{RSsystem, RSlabel, RSset, RSprocess};
use super::transitions::unfold;
#[derive(Clone, Debug)]
pub struct TransitionsIterator<'a> {
choices_iterator: std::vec::IntoIter<(Rc<RSset<'a>>, Rc<RSprocess<'a>>)>,
system: &'a RSsystem<'a>
}
impl<'a> TransitionsIterator<'a> {
pub fn from(system: &'a RSsystem<'a>) -> Result<TransitionsIterator<'a>, String> {
match unfold(system.get_delta(), system.get_context_process()) {
Ok(o) => Ok(
TransitionsIterator {
choices_iterator: o.into_iter(),
system
}
),
Err(e) => Err(e)
}
}
}
impl<'a> Iterator for TransitionsIterator<'a> {
type Item = (RSlabel<'a>, RSsystem<'a>);
fn next(&mut self) -> Option<(RSlabel<'a>, RSsystem<'a>)> {
let choice = self.choices_iterator.next()?;
let t = self.system.get_available_entities().union(choice.0.as_ref());
let (reactants, reactantsi, inihibitors, ireactants, products)
= self.system.get_reaction_rules().iter()
.fold((RSset::new(), RSset::new(), RSset::new(), RSset::new(), RSset::new()),
|acc, reaction| if reaction.enabled(&t) {
(acc.0.union(reaction.reactants()),
acc.1,
acc.2.union(reaction.inihibitors()),
acc.3,
acc.4.union(reaction.products()))
} else {
(acc.0,
acc.1.union(&reaction.inihibitors().intersection(&t)),
acc.2,
acc.3.union(&reaction.reactants().subtraction(&t)),
acc.4)
}
);
let label = RSlabel::from(self.system.get_available_entities().clone(),
(*choice.0).clone(),
t,
reactants,
reactantsi,
inihibitors,
ireactants,
products.clone());
let new_system = RSsystem::from(self.system.get_delta().clone(),
products,
(*choice.1).clone(),
self.system.get_reaction_rules().clone());
Some((label, new_system))
}
}

View File

@ -2,6 +2,7 @@
use std::rc::Rc;
use super::structure::{RSset, RSChoices, RSenvironment, RSprocess, RSsystem, RSlabel};
use super::support_structures::{TransitionsIterator};
pub fn unfold<'a>(environment: &'a RSenvironment<'a>, context_process: &'a RSprocess<'a>) -> Result<RSChoices<'a>, String> {
@ -60,47 +61,11 @@ pub fn unfold<'a>(environment: &'a RSenvironment<'a>, context_process: &'a RSpro
}
}
pub fn all_transitions<'a>(system: &'a RSsystem<'a>) -> Result<Vec<(RSlabel<'a>, RSsystem<'a>)>, String> {
let choices = unfold(system.get_delta(), system.get_context_process())?;
println!("choices: {:?}", choices);
println!("\n\n");
let mut results = vec![];
for choice in choices {
let t = system.get_available_entities().union(choice.0.as_ref());
let (reactants, reactantsi, inihibitors, ireactants, products)
= system.get_reaction_rules().iter()
.fold((RSset::new(), RSset::new(), RSset::new(), RSset::new(), RSset::new()),
|acc, reaction| if reaction.enabled(&t) {
(acc.0.union(reaction.reactants()),
acc.1,
acc.2.union(reaction.inihibitors()),
acc.3,
acc.4.union(reaction.products()))
} else {
(acc.0,
acc.1.union(&reaction.inihibitors().intersection(&t)),
acc.2,
acc.3.union(&reaction.reactants().subtraction(&t)),
acc.4)
}
);
let label = RSlabel::from(system.get_available_entities().clone(),
(*choice.0).clone(),
t,
reactants,
reactantsi,
inihibitors,
ireactants,
products.clone());
let new_system = RSsystem::from(system.get_delta().clone(),
products,
(*choice.1).clone(),
system.get_reaction_rules().clone());
results.push((label, new_system))
}
Ok(results)
pub fn iterator_transitions<'a>(system: &'a RSsystem<'a>) -> Result<TransitionsIterator<'a>, String> {
TransitionsIterator::from(system)
}
pub fn all_transitions<'a>(system: &'a RSsystem<'a>) -> Result<Vec<(RSlabel<'a>, RSsystem<'a>)>, String> {
let tr = TransitionsIterator::from(system)?;
Ok(tr.collect::<Vec<_>>())
}