Adding allTransitions

This commit is contained in:
elvis
2025-06-12 16:23:39 +02:00
parent 4507b0194f
commit 75c10dc313
4 changed files with 279 additions and 26 deletions

View File

@ -31,9 +31,79 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
let env = grammar::EnvironmentParser::new().parse("[x = {a}.{b}.x , y = ({a,c}.y + {b,c}.y)]").unwrap(); // let env = grammar::EnvironmentParser::new().parse("[x = {a}.{b}.x , y = ({a,c}.y + {b,c}.y)]").unwrap();
let process = grammar::ContextParser::new().parse("[({a}.nil + x + y)]").unwrap(); // let process = grammar::ContextParser::new().parse("[({a}.nil + x + y)]").unwrap();
println!("{:?}", rsprocess::transitions::unfold(&env, &process)); // println!("{:?}", rsprocess::transitions::unfold(&env, &process));
// println!("--------------------");
// println!("{:?}", env);
// -------------------------------------------------------------------------
// allTransitions(sys([],[a,b],[],[react([a],[c],[a]),react([b],[d],[b])]) , Moves).
// let env = grammar::EnvironmentParser::new().parse("[]").unwrap();
// let process = grammar::ContextParser::new().parse("[]").unwrap();
// let sys = rsprocess::structure::RSsystem::from(*env,
// rsprocess::structure::RSset::from(vec!["a", "b"]),
// *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)]",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));
// -------------------------------------------------------------------------
// 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(())
} }

View File

@ -38,8 +38,13 @@ Set_of_entities: RSset<'input> = {
// ----- ContextParser ----- // ----- ContextParser -----
pub Context: Box<RSprocess<'input>> = { pub Context: Box<RSprocess<'input>> = {
"[" "]" => Box::new(RSprocess::Nill), // "[" "]" => Box::new(RSprocess::Nill),
"[" <t: CTX_process> "]" => Box::new(t), // "[" <t: CTX_process> "]" => Box::new(t),
// "[" <t: Separeted<Boxed_CTX_process, ",">> "]" =>
// Box::new(RSprocess::NondeterministicChoice{ children: t })
"[" "]" => Box::new(RSprocess::NondeterministicChoice{ children: vec![] }),
"[" <t: CTX_process> "]" => Box::new(RSprocess::NondeterministicChoice{ children: vec![Rc::new(t)] }),
"[" <t: Separeted<Boxed_CTX_process, ",">> "]" => "[" <t: Separeted<Boxed_CTX_process, ",">> "]" =>
Box::new(RSprocess::NondeterministicChoice{ children: t }) Box::new(RSprocess::NondeterministicChoice{ children: t })
}; };

View File

@ -43,7 +43,7 @@ impl<'a> RSset<'a> {
} }
pub fn union(&self, b: &RSset<'a>) -> RSset<'a> { pub fn union(&self, b: &RSset<'a>) -> RSset<'a> {
// TODO maybe find more efficient way // TODO maybe find more efficient way without copy/clone
let mut ret: RSset = b.clone(); let mut ret: RSset = b.clone();
ret.identifiers.extend(self.identifiers.iter()); ret.identifiers.extend(self.identifiers.iter());
ret ret
@ -51,14 +51,27 @@ impl<'a> RSset<'a> {
pub fn union_option(&self, b: Option<&RSset<'a>>) -> RSset<'a> { pub fn union_option(&self, b: Option<&RSset<'a>>) -> RSset<'a> {
if let Some(b) = b { if let Some(b) = b {
// TODO maybe find more efficient way self.union(b)
let mut ret: RSset = b.clone();
ret.identifiers.extend(self.identifiers.iter());
ret
} else { } else {
self.clone() self.clone()
} }
} }
pub fn intersection(&self, b: &RSset<'a>) -> RSset<'a> {
// TODO maybe find more efficient way without copy/clone
let res: HashSet<_> = b.identifiers.intersection(&self.identifiers)
.copied()
.collect();
RSset { identifiers: res }
}
pub fn subtraction(&self, b: &RSset<'a>) -> RSset<'a> {
// TODO maybe find more efficient way without copy/clone
let res: HashSet<_> = self.identifiers.difference(&b.identifiers)
.copied()
.collect();
RSset { identifiers: res }
}
} }
@ -94,6 +107,14 @@ impl<'a> RSreaction<'a> {
self.products.clone() self.products.clone()
} }
pub fn reactants(&self) -> &RSset<'a> {
&self.reactants
}
pub fn inihibitors(&self) -> &RSset<'a> {
&self.inihibitors
}
pub fn products(&self) -> &RSset<'a> { pub fn products(&self) -> &RSset<'a> {
&self.products &self.products
} }
@ -144,6 +165,10 @@ impl<'a> RSChoices<'a> {
RSChoices{ context_moves: vec![] } RSChoices{ context_moves: vec![] }
} }
pub fn new_not_empty() -> Self {
RSChoices{ context_moves: vec![(Rc::new(RSset::new()), Rc::new(RSprocess::Nill))] }
}
pub fn append(&mut self, a: &mut RSChoices<'a>) { pub fn append(&mut self, a: &mut RSChoices<'a>) {
self.context_moves.append(&mut a.context_moves); self.context_moves.append(&mut a.context_moves);
} }
@ -156,21 +181,33 @@ impl<'a> RSChoices<'a> {
} }
pub fn shuffle(&mut self, choices: RSChoices<'a>) { pub fn shuffle(&mut self, choices: RSChoices<'a>) {
if self.context_moves.is_empty() || choices.context_moves.is_empty() { match (self.context_moves.is_empty(), choices.context_moves.is_empty()) {
self.context_moves = vec![]; (true, true) => {}
} else { (true, false) => { self.context_moves = choices.context_moves }
let mut new_self = vec![]; (false, true) => {}
for item_self in &self.context_moves { (false, false) => {
for item_choices in &choices.context_moves { let mut new_self = vec![];
new_self.push((Rc::new(item_self.0.union(&item_choices.0)), for item_self in &self.context_moves {
Rc::new(item_self.1.concat(&item_choices.1)))); for item_choices in &choices.context_moves {
new_self.push((Rc::new(item_self.0.union(&item_choices.0)),
Rc::new(item_self.1.concat(&item_choices.1))));
}
} }
self.context_moves = new_self;
} }
self.context_moves = new_self;
} }
} }
} }
impl<'a> IntoIterator for RSChoices<'a> {
type Item = (Rc<RSset<'a>>, Rc<RSprocess<'a>>);
type IntoIter = std::vec::IntoIter<Self::Item>;
fn into_iter(self) -> Self::IntoIter {
self.context_moves.into_iter()
}
}
impl<'a, const N: usize> From<[(Rc<RSset<'a>>, Rc<RSprocess<'a>>); N]> for RSChoices<'a> { impl<'a, const N: usize> From<[(Rc<RSset<'a>>, Rc<RSprocess<'a>>); N]> for RSChoices<'a> {
fn from(arr: [(Rc<RSset<'a>>, Rc<RSprocess<'a>>); N]) -> Self { fn from(arr: [(Rc<RSset<'a>>, Rc<RSprocess<'a>>); N]) -> Self {
RSChoices{context_moves: arr.to_vec()} RSChoices{context_moves: arr.to_vec()}
@ -225,6 +262,99 @@ impl<'a> From<Vec<(&'a str, RSprocess<'a>)>> for RSenvironment<'a> {
} }
} }
// -----------------------------------------------------------------------------
// RSsystem
// -----------------------------------------------------------------------------
#[derive(Clone, Debug)]
pub struct RSsystem<'a> {
delta: RSenvironment<'a>,
available_entities: RSset<'a>,
context_process: RSprocess<'a>,
reaction_rules: Vec<RSreaction<'a>>,
}
impl<'a> RSsystem<'a> {
pub fn new() -> RSsystem<'a> {
RSsystem {
delta: RSenvironment::new(),
available_entities: RSset::new(),
context_process: RSprocess::Nill,
reaction_rules: vec![],
}
}
pub fn from(delta: RSenvironment<'a>,
available_entities: RSset<'a>,
context_process: RSprocess<'a>,
reaction_rules: Vec<RSreaction<'a>>) -> RSsystem<'a> {
RSsystem { delta, available_entities, context_process, reaction_rules }
}
pub fn get_delta(&self) -> &RSenvironment<'a> {
&self.delta
}
pub fn get_available_entities(&self) -> &RSset<'a> {
&self.available_entities
}
pub fn get_context_process(&self) -> &RSprocess<'a> {
&self.context_process
}
pub fn get_reaction_rules(&self) -> &Vec<RSreaction<'a>> {
&self.reaction_rules
}
}
// -----------------------------------------------------------------------------
// RSsystem
// -----------------------------------------------------------------------------
#[derive(Clone, Debug)]
pub struct RSlabel<'a> {
available_entities: RSset<'a>,
c: RSset<'a>, /// TODO: what is c? what is t? (c comes from choices, t is
t: RSset<'a>, /// the union of available_entities and c)
reactants: RSset<'a>,
reactantsi: RSset<'a>,
inihibitors: RSset<'a>,
ireactants: RSset<'a>,
products: RSset<'a>,
}
impl<'a> RSlabel<'a> {
pub fn new() -> Self {
RSlabel { available_entities: RSset::new(),
c: RSset::new(),
t: RSset::new(),
reactants: RSset::new(),
reactantsi: RSset::new(),
inihibitors: RSset::new(),
ireactants: RSset::new(),
products: RSset::new() }
}
#[allow(clippy::too_many_arguments)]
pub fn from(available_entities: RSset<'a>,
c: RSset<'a>,
t: RSset<'a>,
reactants: RSset<'a>,
reactantsi: RSset<'a>,
inihibitors: RSset<'a>,
ireactants: RSset<'a>,
products: RSset<'a>,) -> Self {
RSlabel { available_entities,
c,
t,
reactants,
reactantsi,
inihibitors,
ireactants,
products }
}
}
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// RSassertOp // RSassertOp
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------

View File

@ -1,7 +1,8 @@
#![allow(dead_code)] #![allow(dead_code)]
use std::rc::Rc; use std::rc::Rc;
use super::structure::{RSChoices, RSenvironment, RSprocess}; use super::structure::{RSset, RSChoices, RSenvironment, RSprocess, RSsystem, RSlabel};
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> {
match context_process { match context_process {
@ -47,12 +48,59 @@ pub fn unfold<'a>(environment: &'a RSenvironment<'a>, context_process: &'a RSpro
}, },
RSprocess::NondeterministicChoice{children} => { RSprocess::NondeterministicChoice{children} => {
// short-circuits with try_fold. // short-circuits with try_fold.
children.iter().try_fold(RSChoices::new(), |mut acc, x| { if children.is_empty() {
match unfold(environment, x) { Ok(RSChoices::from(vec![(Rc::new(RSset::new()), Rc::new(RSprocess::Nill))]))
Ok(choices) => {acc.shuffle(choices); Ok(acc)}, } else {
Err(e) => Err(e) children.iter().try_fold(RSChoices::new(), |mut acc, x| {
} acc.shuffle(unfold(environment, x)?);
}) Ok(acc)
})
}
} }
} }
} }
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)
}