Adding transitions support

This commit is contained in:
elvis
2025-05-21 00:03:36 +02:00
parent 32c2fc2701
commit 4507b0194f
6 changed files with 187 additions and 44 deletions

View File

@ -1,10 +1,12 @@
use std::collections::{BTreeMap, HashSet};
#![allow(dead_code)]
use std::collections::{HashMap, HashSet};
use std::rc::Rc;
// -----------------------------------------------------------------------------
// RSset
// -----------------------------------------------------------------------------
#[derive(Clone, Debug)]
#[allow(dead_code)]
pub struct RSset<'a> {
identifiers: HashSet<&'a str>
}
@ -40,7 +42,6 @@ impl<'a> RSset<'a> {
self.identifiers.is_disjoint(&b.identifiers)
}
#[allow(dead_code)]
pub fn union(&self, b: &RSset<'a>) -> RSset<'a> {
// TODO maybe find more efficient way
let mut ret: RSset = b.clone();
@ -65,7 +66,6 @@ impl<'a> RSset<'a> {
// RSreaction
// -----------------------------------------------------------------------------
#[derive(Clone, Debug)]
#[allow(dead_code)]
pub struct RSreaction<'a> {
reactants: RSset<'a>,
inihibitors: RSset<'a>,
@ -73,7 +73,6 @@ pub struct RSreaction<'a> {
}
impl<'a> RSreaction<'a> {
#[allow(dead_code)]
pub fn new() -> Self {
RSreaction{ reactants: RSset::new(),
inihibitors: RSset::new(),
@ -91,7 +90,6 @@ impl<'a> RSreaction<'a> {
&& self.inihibitors.is_disjoint(current_state)
}
#[allow(dead_code)]
pub fn products_clone(&self) -> RSset<'a> {
self.products.clone()
}
@ -107,47 +105,123 @@ impl<'a> RSreaction<'a> {
// RSprocess
// -----------------------------------------------------------------------------
#[derive(Clone, Debug)]
#[allow(dead_code)]
pub enum RSprocess<'a> {
Nill,
ConstantIdentifier{identifier: &'a str},
EntitySet{entities: RSset<'a>, next_process: Box<RSprocess<'a>>},
WaitEntity{repeat: i64, repeated_process: Box<RSprocess<'a>>, next_process: Box<RSprocess<'a>>},
NondeterministicChoice{children: Vec<RSprocess<'a>>},
Summation{children: Vec<RSprocess<'a>>}
RecursiveIdentifier{identifier: &'a str},
EntitySet{entities: RSset<'a>, next_process: Rc<RSprocess<'a>>},
WaitEntity{repeat: i64, repeated_process: Rc<RSprocess<'a>>, next_process: Rc<RSprocess<'a>>},
Summation{children: Vec<RSprocess<'a>>},
NondeterministicChoice{children: Vec<Rc<RSprocess<'a>>>}
}
impl<'a> RSprocess<'a> {
// TODO: remove all the clone()
pub fn concat(&self, new: &RSprocess<'a>) -> RSprocess<'a>{
match (self, new) {
(RSprocess::NondeterministicChoice{children: c1}, RSprocess::NondeterministicChoice{children: c2}) => {
RSprocess::NondeterministicChoice { children: [c1.clone(), c2.clone()].concat() }
},
(RSprocess::NondeterministicChoice{children}, new) |
(new, RSprocess::NondeterministicChoice{children}) => {
let mut new_children = children.clone();
new_children.push(Rc::new(new.clone()));
RSprocess::NondeterministicChoice{ children: new_children }
},
(_, _) => {
RSprocess::NondeterministicChoice { children: vec![Rc::new(self.clone()), Rc::new(new.clone())] }
}
}
}
}
#[derive(Clone, Debug)]
pub struct RSChoices<'a> {
context_moves: Vec<(Rc<RSset<'a>>, Rc<RSprocess<'a>>)>
}
impl<'a> RSChoices<'a> {
pub fn new() -> Self {
RSChoices{ context_moves: vec![] }
}
pub fn append(&mut self, a: &mut RSChoices<'a>) {
self.context_moves.append(&mut a.context_moves);
}
pub fn replace(&mut self, a: Rc<RSprocess<'a>>) {
self.context_moves =
self.context_moves
.iter_mut()
.map(|(c1, _)| (Rc::clone(c1), Rc::clone(&a))).collect::<Vec<_>>();
}
pub fn shuffle(&mut self, choices: RSChoices<'a>) {
if self.context_moves.is_empty() || choices.context_moves.is_empty() {
self.context_moves = vec![];
} else {
let mut new_self = vec![];
for item_self in &self.context_moves {
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;
}
}
}
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 {
RSChoices{context_moves: arr.to_vec()}
}
}
impl<'a> From<&[(Rc<RSset<'a>>, Rc<RSprocess<'a>>)]> for RSChoices<'a> {
fn from(arr: &[(Rc<RSset<'a>>, Rc<RSprocess<'a>>)]) -> Self {
RSChoices{context_moves: arr.to_vec()}
}
}
impl<'a> From<Vec<(Rc<RSset<'a>>, Rc<RSprocess<'a>>)>> for RSChoices<'a> {
fn from(arr: Vec<(Rc<RSset<'a>>, Rc<RSprocess<'a>>)>) -> Self {
RSChoices{context_moves: arr}
}
}
// -----------------------------------------------------------------------------
// RSenvironment
// -----------------------------------------------------------------------------
#[derive(Clone, Debug)]
#[allow(dead_code)]
pub struct RSenvironment<'a> {
definitions: BTreeMap<&'a str, Box<RSprocess<'a>>>,
definitions: HashMap<&'a str, RSprocess<'a>>,
}
impl<'a> RSenvironment<'a> {
pub fn new() -> RSenvironment<'a> {
RSenvironment{definitions: BTreeMap::new()}
RSenvironment{definitions: HashMap::new()}
}
pub fn get(&self, k: &'a str) -> Option<&RSprocess<'a>> {
self.definitions.get(k)
}
}
impl<'a, const N: usize> From<[(&'a str, Box<RSprocess<'a>>); N]> for RSenvironment<'a> {
fn from(arr: [(&'a str, Box<RSprocess<'a>>); N]) -> Self {
RSenvironment{definitions: BTreeMap::from(arr)}
impl<'a, const N: usize> From<[(&'a str, RSprocess<'a>); N]> for RSenvironment<'a> {
fn from(arr: [(&'a str, RSprocess<'a>); N]) -> Self {
RSenvironment{definitions: HashMap::from(arr)}
}
}
impl<'a> From<&[(&'a str, Box<RSprocess<'a>>)]> for RSenvironment<'a> {
fn from(arr: &[(&'a str, Box<RSprocess<'a>>)]) -> Self {
RSenvironment{definitions: BTreeMap::from_iter(arr.to_vec())}
impl<'a> From<&[(&'a str, RSprocess<'a>)]> for RSenvironment<'a> {
fn from(arr: &[(&'a str, RSprocess<'a>)]) -> Self {
RSenvironment{definitions: HashMap::from_iter(arr.to_vec())}
}
}
impl<'a> From<Vec<(&'a str, Box<RSprocess<'a>>)>> for RSenvironment<'a> {
fn from(arr: Vec<(&'a str, Box<RSprocess<'a>>)>) -> Self {
RSenvironment{definitions: BTreeMap::from_iter(arr)}
impl<'a> From<Vec<(&'a str, RSprocess<'a>)>> for RSenvironment<'a> {
fn from(arr: Vec<(&'a str, RSprocess<'a>)>) -> Self {
RSenvironment{definitions: HashMap::from_iter(arr)}
}
}
@ -155,7 +229,6 @@ impl<'a> From<Vec<(&'a str, Box<RSprocess<'a>>)>> for RSenvironment<'a> {
// RSassertOp
// -----------------------------------------------------------------------------
#[derive(Clone, Debug)]
#[allow(dead_code)]
pub enum RSassertOp {
InW,
InR,
@ -167,7 +240,6 @@ pub enum RSassertOp {
// RSassert
// -----------------------------------------------------------------------------
#[derive(Clone, Debug)]
#[allow(dead_code)]
pub enum RSassert<'a> {
Not(Box<RSassert<'a>>),
Xor(Box<RSassert<'a>>, Box<RSassert<'a>>),
@ -181,7 +253,6 @@ pub enum RSassert<'a> {
// RSBHML
// -----------------------------------------------------------------------------
#[derive(Clone, Debug)]
#[allow(dead_code)]
#[allow(clippy::upper_case_acronyms)]
pub enum RSBHML<'a> {
True,