Files
ReactionSystems/src/rsprocess/structure.rs

584 lines
15 KiB
Rust
Raw Normal View History

2025-05-21 00:03:36 +02:00
#![allow(dead_code)]
2025-07-01 19:22:50 +02:00
use super::translator::IdType;
2025-07-01 18:00:27 +02:00
use std::collections::{BTreeSet, HashMap, VecDeque};
use std::hash::Hash;
2025-05-21 00:03:36 +02:00
use std::rc::Rc;
2025-05-19 00:10:23 +02:00
// -----------------------------------------------------------------------------
// RSset
// -----------------------------------------------------------------------------
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
2025-06-16 14:46:04 +02:00
pub struct RSset {
2025-07-01 19:22:50 +02:00
identifiers: BTreeSet<IdType>,
2025-05-19 00:10:23 +02:00
}
2025-06-16 14:46:04 +02:00
impl<const N: usize> From<[IdType; N]> for RSset {
fn from(arr: [IdType; N]) -> Self {
2025-07-01 19:22:50 +02:00
RSset {
identifiers: BTreeSet::from(arr),
}
2025-05-19 00:10:23 +02:00
}
}
2025-06-16 14:46:04 +02:00
impl From<&[IdType]> for RSset {
fn from(arr: &[IdType]) -> Self {
2025-07-01 19:22:50 +02:00
RSset {
identifiers: BTreeSet::from_iter(arr.to_vec()),
}
2025-05-19 00:10:23 +02:00
}
}
2025-06-16 14:46:04 +02:00
impl From<Vec<IdType>> for RSset {
fn from(arr: Vec<IdType>) -> Self {
2025-07-01 19:22:50 +02:00
RSset {
identifiers: BTreeSet::from_iter(arr),
}
2025-05-19 00:10:23 +02:00
}
}
2025-06-16 14:46:04 +02:00
impl RSset {
2025-05-19 00:10:23 +02:00
pub fn new() -> Self {
2025-07-01 19:22:50 +02:00
RSset {
identifiers: BTreeSet::new(),
}
2025-05-19 00:10:23 +02:00
}
2025-06-16 14:46:04 +02:00
pub fn is_subset(&self, b: &RSset) -> bool {
2025-07-01 19:22:50 +02:00
self.identifiers.is_subset(&b.identifiers)
2025-05-19 00:10:23 +02:00
}
2025-06-16 14:46:04 +02:00
pub fn is_disjoint(&self, b: &RSset) -> bool {
2025-07-01 19:22:50 +02:00
self.identifiers.is_disjoint(&b.identifiers)
2025-05-19 00:10:23 +02:00
}
2025-06-16 14:46:04 +02:00
pub fn union(&self, b: &RSset) -> RSset {
2025-07-01 19:22:50 +02:00
// TODO maybe find more efficient way without copy/clone
let mut ret: RSset = b.clone();
ret.identifiers.extend(self.identifiers.iter());
ret
2025-05-19 00:10:23 +02:00
}
2025-06-16 14:46:04 +02:00
pub fn union_option(&self, b: Option<&RSset>) -> RSset {
2025-07-01 19:22:50 +02:00
if let Some(b) = b {
self.union(b)
} else {
self.clone()
}
}
2025-06-12 16:23:39 +02:00
2025-06-16 14:46:04 +02:00
pub fn intersection(&self, b: &RSset) -> RSset {
2025-07-01 19:22:50 +02:00
// TODO maybe find more efficient way without copy/clone
let res: BTreeSet<_> = b
.identifiers
.intersection(&self.identifiers)
.copied()
.collect();
RSset { identifiers: res }
2025-06-12 16:23:39 +02:00
}
2025-06-16 14:46:04 +02:00
pub fn subtraction(&self, b: &RSset) -> RSset {
2025-07-01 19:22:50 +02:00
// TODO maybe find more efficient way without copy/clone
let res: BTreeSet<_> = self
.identifiers
.difference(&b.identifiers)
.copied()
.collect();
RSset { identifiers: res }
2025-06-12 16:23:39 +02:00
}
2025-06-18 11:28:04 +02:00
pub fn set(&self) -> &BTreeSet<IdType> {
2025-07-01 19:22:50 +02:00
&self.identifiers
2025-06-18 11:28:04 +02:00
}
2025-05-19 00:10:23 +02:00
pub fn iter(&self) -> std::collections::btree_set::Iter<'_, IdType> {
2025-07-01 19:22:50 +02:00
self.identifiers.iter()
2025-06-23 17:07:46 +02:00
}
pub fn len(&self) -> usize {
2025-07-01 19:22:50 +02:00
self.identifiers.len()
}
pub fn insert(&mut self, el: IdType) -> bool {
2025-07-01 19:22:50 +02:00
self.identifiers.insert(el)
}
pub fn push(&mut self, b: &RSset) {
2025-07-01 19:22:50 +02:00
self.identifiers.extend(b.iter())
}
}
impl Default for RSset {
fn default() -> Self {
2025-07-01 19:22:50 +02:00
RSset::new()
}
}
2025-06-23 17:07:46 +02:00
impl IntoIterator for RSset {
type Item = IdType;
type IntoIter = std::collections::btree_set::IntoIter<Self::Item>;
fn into_iter(self) -> Self::IntoIter {
2025-07-01 19:22:50 +02:00
self.identifiers.into_iter()
}
2025-06-23 17:07:46 +02:00
}
2025-05-19 00:10:23 +02:00
// -----------------------------------------------------------------------------
// RSreaction
// -----------------------------------------------------------------------------
#[derive(Clone, Debug)]
2025-06-16 14:46:04 +02:00
pub struct RSreaction {
reactants: RSset,
inihibitors: RSset,
2025-07-01 19:22:50 +02:00
products: RSset,
2025-05-19 00:10:23 +02:00
}
2025-06-16 14:46:04 +02:00
impl RSreaction {
2025-05-19 00:10:23 +02:00
pub fn new() -> Self {
2025-07-01 19:22:50 +02:00
RSreaction {
reactants: RSset::new(),
inihibitors: RSset::new(),
products: RSset::new(),
}
2025-05-19 00:10:23 +02:00
}
2025-06-16 14:46:04 +02:00
pub fn from(reactants: RSset, inihibitors: RSset, products: RSset) -> Self {
2025-07-01 19:22:50 +02:00
RSreaction {
reactants,
inihibitors,
products,
}
2025-05-19 00:10:23 +02:00
}
// see enable
2025-06-16 14:46:04 +02:00
pub fn enabled(&self, current_state: &RSset) -> bool {
2025-07-01 19:22:50 +02:00
self.reactants.is_subset(current_state)
2025-05-19 00:10:23 +02:00
&& self.inihibitors.is_disjoint(current_state)
}
2025-06-16 14:46:04 +02:00
pub fn products_clone(&self) -> RSset {
2025-07-01 19:22:50 +02:00
self.products.clone()
2025-05-19 00:10:23 +02:00
}
2025-06-16 14:46:04 +02:00
pub fn reactants(&self) -> &RSset {
2025-07-01 19:22:50 +02:00
&self.reactants
2025-06-12 16:23:39 +02:00
}
2025-06-16 14:46:04 +02:00
pub fn inihibitors(&self) -> &RSset {
2025-07-01 19:22:50 +02:00
&self.inihibitors
2025-06-12 16:23:39 +02:00
}
2025-06-16 14:46:04 +02:00
pub fn products(&self) -> &RSset {
2025-07-01 19:22:50 +02:00
&self.products
}
2025-05-19 00:10:23 +02:00
}
impl Default for RSreaction {
fn default() -> Self {
2025-07-01 19:22:50 +02:00
RSreaction::new()
}
}
2025-05-19 00:10:23 +02:00
// -----------------------------------------------------------------------------
// RSprocess
// -----------------------------------------------------------------------------
#[derive(Clone, Debug)]
2025-06-16 14:46:04 +02:00
pub enum RSprocess {
2025-05-19 00:10:23 +02:00
Nill,
2025-07-01 19:22:50 +02:00
RecursiveIdentifier {
2025-07-02 07:30:05 +02:00
identifier: IdType,
2025-07-01 19:22:50 +02:00
},
EntitySet {
entities: RSset,
next_process: Rc<RSprocess>,
},
WaitEntity {
repeat: i64,
repeated_process: Rc<RSprocess>,
next_process: Rc<RSprocess>,
},
Summation {
children: Vec<Rc<RSprocess>>,
},
NondeterministicChoice {
children: Vec<Rc<RSprocess>>,
},
2025-05-21 00:03:36 +02:00
}
2025-06-16 14:46:04 +02:00
impl RSprocess {
2025-05-21 00:03:36 +02:00
// TODO: remove all the clone()
2025-07-01 19:22:50 +02:00
pub fn concat(&self, new: &RSprocess) -> RSprocess {
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())],
},
}
2025-05-21 00:03:36 +02:00
}
pub fn all_elements(&self) -> RSset {
2025-07-01 19:22:50 +02:00
let mut queue = VecDeque::from([self]);
let mut elements = RSset::new();
while let Some(el) = queue.pop_front() {
match el {
Self::Nill => {}
Self::RecursiveIdentifier { identifier: _ } => {}
Self::EntitySet {
entities,
next_process,
} => {
elements.push(entities);
queue.push_back(next_process);
}
Self::WaitEntity {
repeat: _,
repeated_process,
next_process,
} => {
queue.push_back(repeated_process);
queue.push_back(next_process);
}
Self::Summation { children } => {
for c in children {
queue.push_back(c);
}
}
Self::NondeterministicChoice { children } => {
for c in children {
queue.push_back(c);
}
}
}
}
elements
}
2025-05-21 00:03:36 +02:00
}
// -----------------------------------------------------------------------------
// RSchoices
// -----------------------------------------------------------------------------
2025-05-21 00:03:36 +02:00
#[derive(Clone, Debug)]
2025-06-18 11:28:04 +02:00
pub struct RSchoices {
2025-07-01 19:22:50 +02:00
context_moves: Vec<(Rc<RSset>, Rc<RSprocess>)>,
2025-05-21 00:03:36 +02:00
}
2025-06-18 11:28:04 +02:00
impl RSchoices {
2025-05-21 00:03:36 +02:00
pub fn new() -> Self {
2025-07-01 19:22:50 +02:00
RSchoices {
context_moves: vec![],
}
2025-05-21 00:03:36 +02:00
}
2025-06-12 16:23:39 +02:00
pub fn new_not_empty() -> Self {
2025-07-01 19:22:50 +02:00
RSchoices {
context_moves: vec![(Rc::new(RSset::new()),
Rc::new(RSprocess::Nill))],
}
2025-06-12 16:23:39 +02:00
}
2025-06-18 11:28:04 +02:00
pub fn append(&mut self, a: &mut RSchoices) {
2025-07-01 19:22:50 +02:00
self.context_moves.append(&mut a.context_moves);
2025-05-21 00:03:36 +02:00
}
2025-06-16 14:46:04 +02:00
pub fn replace(&mut self, a: Rc<RSprocess>) {
2025-07-01 19:22:50 +02:00
self.context_moves = self
.context_moves
.iter_mut()
.map(|(c1, _)| (Rc::clone(c1), Rc::clone(&a)))
.collect::<Vec<_>>();
2025-05-21 00:03:36 +02:00
}
2025-06-18 11:28:04 +02:00
pub fn shuffle(&mut self, choices: RSchoices) {
2025-07-01 19:22:50 +02:00
match (
self.context_moves.is_empty(),
choices.context_moves.is_empty(),
) {
(true, true) => {}
(true, false) => self.context_moves = choices.context_moves,
(false, true) => {}
(false, false) => {
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;
}
}
2025-05-21 00:03:36 +02:00
}
2025-06-18 11:28:04 +02:00
pub fn iter(&self) -> std::slice::Iter<'_, (Rc<RSset>, Rc<RSprocess>)> {
2025-07-01 19:22:50 +02:00
self.context_moves.iter()
2025-06-18 11:28:04 +02:00
}
2025-05-19 00:10:23 +02:00
}
2025-06-18 11:28:04 +02:00
impl IntoIterator for RSchoices {
2025-06-16 14:46:04 +02:00
type Item = (Rc<RSset>, Rc<RSprocess>);
2025-06-12 16:23:39 +02:00
type IntoIter = std::vec::IntoIter<Self::Item>;
fn into_iter(self) -> Self::IntoIter {
2025-07-01 19:22:50 +02:00
self.context_moves.into_iter()
2025-06-12 16:23:39 +02:00
}
}
2025-06-18 11:28:04 +02:00
impl<const N: usize> From<[(Rc<RSset>, Rc<RSprocess>); N]> for RSchoices {
2025-06-16 14:46:04 +02:00
fn from(arr: [(Rc<RSset>, Rc<RSprocess>); N]) -> Self {
2025-07-01 19:22:50 +02:00
RSchoices {
context_moves: arr.to_vec(),
}
2025-05-21 00:03:36 +02:00
}
}
2025-06-18 11:28:04 +02:00
impl From<&[(Rc<RSset>, Rc<RSprocess>)]> for RSchoices {
2025-06-16 14:46:04 +02:00
fn from(arr: &[(Rc<RSset>, Rc<RSprocess>)]) -> Self {
2025-07-01 19:22:50 +02:00
RSchoices {
context_moves: arr.to_vec(),
}
2025-05-21 00:03:36 +02:00
}
}
2025-06-18 11:28:04 +02:00
impl From<Vec<(Rc<RSset>, Rc<RSprocess>)>> for RSchoices {
2025-06-16 14:46:04 +02:00
fn from(arr: Vec<(Rc<RSset>, Rc<RSprocess>)>) -> Self {
2025-07-01 19:22:50 +02:00
RSchoices { context_moves: arr }
2025-05-21 00:03:36 +02:00
}
}
2025-05-19 00:10:23 +02:00
// -----------------------------------------------------------------------------
// RSenvironment
// -----------------------------------------------------------------------------
#[derive(Clone, Debug)]
2025-06-16 14:46:04 +02:00
pub struct RSenvironment {
definitions: HashMap<IdType, RSprocess>,
2025-05-19 00:10:23 +02:00
}
2025-06-16 14:46:04 +02:00
impl RSenvironment {
pub fn new() -> RSenvironment {
2025-07-01 19:22:50 +02:00
RSenvironment {
definitions: HashMap::new(),
}
2025-05-21 00:03:36 +02:00
}
2025-06-16 14:46:04 +02:00
pub fn get(&self, k: IdType) -> Option<&RSprocess> {
2025-07-01 19:22:50 +02:00
self.definitions.get(&k)
2025-05-19 00:10:23 +02:00
}
2025-06-18 11:28:04 +02:00
pub fn iter(&self) -> std::collections::hash_map::Iter<'_, u32, RSprocess> {
2025-07-01 19:22:50 +02:00
self.definitions.iter()
2025-06-18 11:28:04 +02:00
}
pub fn all_elements(&self) -> RSset {
2025-07-01 19:22:50 +02:00
let mut acc = RSset::new();
for (_, process) in self.definitions.iter() {
acc.push(&process.all_elements());
}
acc
}
}
impl Default for RSenvironment {
fn default() -> Self {
2025-07-01 19:22:50 +02:00
RSenvironment::new()
}
2025-05-19 00:10:23 +02:00
}
2025-06-16 14:46:04 +02:00
impl<const N: usize> From<[(IdType, RSprocess); N]> for RSenvironment {
fn from(arr: [(IdType, RSprocess); N]) -> Self {
2025-07-01 19:22:50 +02:00
RSenvironment {
definitions: HashMap::from(arr),
}
2025-05-19 00:10:23 +02:00
}
}
2025-06-16 14:46:04 +02:00
impl From<&[(IdType, RSprocess)]> for RSenvironment {
fn from(arr: &[(IdType, RSprocess)]) -> Self {
2025-07-01 19:22:50 +02:00
RSenvironment {
definitions: HashMap::from_iter(arr.to_vec()),
}
2025-05-19 00:10:23 +02:00
}
}
2025-06-16 14:46:04 +02:00
impl From<Vec<(IdType, RSprocess)>> for RSenvironment {
fn from(arr: Vec<(IdType, RSprocess)>) -> Self {
2025-07-01 19:22:50 +02:00
RSenvironment {
definitions: HashMap::from_iter(arr),
}
2025-05-19 00:10:23 +02:00
}
}
2025-06-12 16:23:39 +02:00
// -----------------------------------------------------------------------------
// RSsystem
// -----------------------------------------------------------------------------
#[derive(Clone, Debug)]
2025-06-16 14:46:04 +02:00
pub struct RSsystem {
delta: Rc<RSenvironment>,
available_entities: RSset,
context_process: RSprocess,
reaction_rules: Rc<Vec<RSreaction>>,
2025-06-12 16:23:39 +02:00
}
2025-06-16 14:46:04 +02:00
impl RSsystem {
pub fn new() -> RSsystem {
2025-07-01 19:22:50 +02:00
RSsystem {
delta: Rc::new(RSenvironment::new()),
available_entities: RSset::new(),
context_process: RSprocess::Nill,
reaction_rules: Rc::new(vec![]),
}
}
pub fn from(
delta: Rc<RSenvironment>,
available_entities: RSset,
context_process: RSprocess,
reaction_rules: Rc<Vec<RSreaction>>,
) -> RSsystem {
RSsystem {
delta: Rc::clone(&delta),
available_entities,
context_process,
reaction_rules: Rc::clone(&reaction_rules),
}
2025-06-12 16:23:39 +02:00
}
2025-06-16 14:46:04 +02:00
pub fn get_delta(&self) -> &Rc<RSenvironment> {
2025-07-01 19:22:50 +02:00
&self.delta
2025-06-12 16:23:39 +02:00
}
2025-06-16 14:46:04 +02:00
pub fn get_available_entities(&self) -> &RSset {
2025-07-01 19:22:50 +02:00
&self.available_entities
2025-06-12 16:23:39 +02:00
}
2025-06-16 14:46:04 +02:00
pub fn get_context_process(&self) -> &RSprocess {
2025-07-01 19:22:50 +02:00
&self.context_process
2025-06-12 16:23:39 +02:00
}
2025-06-16 14:46:04 +02:00
pub fn get_reaction_rules(&self) -> &Rc<Vec<RSreaction>> {
2025-07-01 19:22:50 +02:00
&self.reaction_rules
2025-06-12 16:23:39 +02:00
}
}
impl Default for RSsystem {
fn default() -> Self {
2025-07-01 19:22:50 +02:00
RSsystem::new()
}
}
2025-06-12 16:23:39 +02:00
// -----------------------------------------------------------------------------
// RSsystem
// -----------------------------------------------------------------------------
#[derive(Clone, Debug)]
2025-06-16 14:46:04 +02:00
pub struct RSlabel {
2025-06-18 11:28:04 +02:00
pub available_entities: RSset,
pub context: RSset,
2025-07-01 19:22:50 +02:00
pub t: RSset,
/// union of available_entities and context
2025-06-18 11:28:04 +02:00
pub reactants: RSset,
pub reactantsi: RSset, // reactants absent
2025-06-18 11:28:04 +02:00
pub inihibitors: RSset,
pub ireactants: RSset, // inhibitors present
2025-06-18 11:28:04 +02:00
pub products: RSset,
2025-06-12 16:23:39 +02:00
}
2025-06-16 14:46:04 +02:00
impl RSlabel {
2025-06-12 16:23:39 +02:00
pub fn new() -> Self {
2025-07-01 19:22:50 +02:00
RSlabel {
available_entities: RSset::new(),
context: RSset::new(),
t: RSset::new(),
reactants: RSset::new(),
reactantsi: RSset::new(),
inihibitors: RSset::new(),
ireactants: RSset::new(),
products: RSset::new(),
}
2025-06-12 16:23:39 +02:00
}
#[allow(clippy::too_many_arguments)]
2025-07-01 19:22:50 +02:00
pub fn from(
available_entities: RSset,
context: RSset,
t: RSset,
reactants: RSset,
reactantsi: RSset,
inihibitors: RSset,
ireactants: RSset,
products: RSset,
) -> Self {
RSlabel {
available_entities,
context,
t,
reactants,
reactantsi,
inihibitors,
ireactants,
products,
}
2025-06-12 16:23:39 +02:00
}
2025-06-17 13:45:35 +02:00
pub fn get_context(&self) -> (RSset, RSset, RSset) {
2025-07-01 19:22:50 +02:00
// TODO remove clone?
(
self.available_entities.clone(),
self.context.clone(),
self.t.clone(),
)
2025-06-17 13:45:35 +02:00
}
2025-06-12 16:23:39 +02:00
}
2025-05-19 00:10:23 +02:00
// -----------------------------------------------------------------------------
// RSassertOp
// -----------------------------------------------------------------------------
#[derive(Clone, Debug)]
pub enum RSassertOp {
InW,
InR,
InI,
2025-07-01 19:22:50 +02:00
InP,
2025-05-19 00:10:23 +02:00
}
// -----------------------------------------------------------------------------
// RSassert
// -----------------------------------------------------------------------------
#[derive(Clone, Debug)]
2025-06-16 14:46:04 +02:00
pub enum RSassert {
Not(Box<RSassert>),
Xor(Box<RSassert>, Box<RSassert>),
Or(Vec<RSassert>),
And(Vec<RSassert>),
Sub(RSset, RSassertOp),
2025-07-01 19:22:50 +02:00
NonEmpty(RSassertOp),
2025-05-19 00:10:23 +02:00
}
// -----------------------------------------------------------------------------
// RSBHML
// -----------------------------------------------------------------------------
#[derive(Clone, Debug)]
#[allow(clippy::upper_case_acronyms)]
2025-06-16 14:46:04 +02:00
pub enum RSBHML {
2025-05-19 00:10:23 +02:00
True,
False,
2025-06-16 14:46:04 +02:00
Or(Vec<RSBHML>),
And(Vec<RSBHML>),
Diamond(Box<RSassert>, Box<RSBHML>),
2025-07-01 19:22:50 +02:00
Box(Box<RSassert>, Box<RSBHML>),
2025-05-19 00:10:23 +02:00
}