Better AllTransitions and implemented iterator over transitions
This commit is contained in:
48
src/main.rs
48
src/main.rs
@ -67,8 +67,32 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
// println!("all_transitions: {:?}", rsprocess::transitions::all_transitions(&sys));
|
// 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).
|
// 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 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,
|
let sys = rsprocess::structure::RSsystem::from(*env,
|
||||||
rsprocess::structure::RSset::from(vec![]),
|
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));
|
for (n, i) in it.into_iter().enumerate() {
|
||||||
|
println!("next i - {n}: {:?}", i);
|
||||||
|
println!("--------------------");
|
||||||
|
}
|
||||||
|
|
||||||
// -------------------------------------------------------------------------
|
|
||||||
// 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);
|
|
||||||
|
|
||||||
println!("--------------------");
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,3 +1,4 @@
|
|||||||
pub mod structure;
|
pub mod structure;
|
||||||
|
pub mod support_structures;
|
||||||
pub mod classical;
|
pub mod classical;
|
||||||
pub mod transitions;
|
pub mod transitions;
|
||||||
|
|||||||
65
src/rsprocess/support_structures.rs
Normal file
65
src/rsprocess/support_structures.rs
Normal 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))
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use super::structure::{RSset, RSChoices, RSenvironment, RSprocess, RSsystem, RSlabel};
|
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> {
|
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> {
|
pub fn iterator_transitions<'a>(system: &'a RSsystem<'a>) -> Result<TransitionsIterator<'a>, String> {
|
||||||
let choices = unfold(system.get_delta(), system.get_context_process())?;
|
TransitionsIterator::from(system)
|
||||||
println!("choices: {:?}", choices);
|
}
|
||||||
println!("\n\n");
|
|
||||||
let mut results = vec![];
|
pub fn all_transitions<'a>(system: &'a RSsystem<'a>) -> Result<Vec<(RSlabel<'a>, RSsystem<'a>)>, String> {
|
||||||
|
let tr = TransitionsIterator::from(system)?;
|
||||||
for choice in choices {
|
Ok(tr.collect::<Vec<_>>())
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user