Print methods
This commit is contained in:
88
src/main.rs
88
src/main.rs
@ -1,6 +1,7 @@
|
|||||||
mod rsprocess;
|
mod rsprocess;
|
||||||
use lalrpop_util::lalrpop_mod;
|
use lalrpop_util::lalrpop_mod;
|
||||||
use std::rc::Rc;
|
use rsprocess::translator::WithTranslator;
|
||||||
|
// use std::rc::Rc;
|
||||||
// use std::io;
|
// use std::io;
|
||||||
|
|
||||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
@ -51,19 +52,19 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
// let process = grammar::ContextParser::new().parse(&mut translator, "[]").unwrap();
|
// let process = grammar::ContextParser::new().parse(&mut translator, "[]").unwrap();
|
||||||
|
|
||||||
// let sys = rsprocess::structure::RSsystem::from(Rc::new(*env),
|
// let sys = rsprocess::structure::RSsystem::from(Rc::new(*env),
|
||||||
// rsprocess::structure::RSset::from(vec![translator.convert("a"),
|
// rsprocess::structure::RSset::from(vec![translator.encode("a"),
|
||||||
// translator.convert("b")]),
|
// translator.encode("b")]),
|
||||||
// *process,
|
// *process,
|
||||||
// Rc::new(vec![
|
// Rc::new(vec![
|
||||||
// rsprocess::structure::RSreaction::from(
|
// rsprocess::structure::RSreaction::from(
|
||||||
// rsprocess::structure::RSset::from(vec![translator.convert("a")]),
|
// rsprocess::structure::RSset::from(vec![translator.encode("a")]),
|
||||||
// rsprocess::structure::RSset::from(vec![translator.convert("c")]),
|
// rsprocess::structure::RSset::from(vec![translator.encode("c")]),
|
||||||
// rsprocess::structure::RSset::from(vec![translator.convert("a")])
|
// rsprocess::structure::RSset::from(vec![translator.encode("a")])
|
||||||
// ),
|
// ),
|
||||||
// rsprocess::structure::RSreaction::from(
|
// rsprocess::structure::RSreaction::from(
|
||||||
// rsprocess::structure::RSset::from(vec![translator.convert("b")]),
|
// rsprocess::structure::RSset::from(vec![translator.encode("b")]),
|
||||||
// rsprocess::structure::RSset::from(vec![translator.convert("d")]),
|
// rsprocess::structure::RSset::from(vec![translator.encode("d")]),
|
||||||
// rsprocess::structure::RSset::from(vec![translator.convert("b")])
|
// rsprocess::structure::RSset::from(vec![translator.encode("b")])
|
||||||
// )
|
// )
|
||||||
// ]));
|
// ]));
|
||||||
|
|
||||||
@ -79,14 +80,14 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
// *process,
|
// *process,
|
||||||
// Rc::new(vec![
|
// Rc::new(vec![
|
||||||
// rsprocess::structure::RSreaction::from(
|
// rsprocess::structure::RSreaction::from(
|
||||||
// rsprocess::structure::RSset::from(vec![translator.convert("a")]),
|
// rsprocess::structure::RSset::from(vec![translator.encode("a")]),
|
||||||
// rsprocess::structure::RSset::from(vec![translator.convert("c")]),
|
// rsprocess::structure::RSset::from(vec![translator.encode("c")]),
|
||||||
// rsprocess::structure::RSset::from(vec![translator.convert("a")])
|
// rsprocess::structure::RSset::from(vec![translator.encode("a")])
|
||||||
// ),
|
// ),
|
||||||
// rsprocess::structure::RSreaction::from(
|
// rsprocess::structure::RSreaction::from(
|
||||||
// rsprocess::structure::RSset::from(vec![translator.convert("b")]),
|
// rsprocess::structure::RSset::from(vec![translator.encode("b")]),
|
||||||
// rsprocess::structure::RSset::from(vec![translator.convert("d")]),
|
// rsprocess::structure::RSset::from(vec![translator.encode("d")]),
|
||||||
// rsprocess::structure::RSset::from(vec![translator.convert("b")])
|
// rsprocess::structure::RSset::from(vec![translator.encode("b")])
|
||||||
// )
|
// )
|
||||||
// ]));
|
// ]));
|
||||||
|
|
||||||
@ -103,14 +104,14 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
// *process,
|
// *process,
|
||||||
// Rc::new(vec![
|
// Rc::new(vec![
|
||||||
// rsprocess::structure::RSreaction::from(
|
// rsprocess::structure::RSreaction::from(
|
||||||
// rsprocess::structure::RSset::from(vec![translator.convert("a")]),
|
// rsprocess::structure::RSset::from(vec![translator.encode("a")]),
|
||||||
// rsprocess::structure::RSset::from(vec![translator.convert("c")]),
|
// rsprocess::structure::RSset::from(vec![translator.encode("c")]),
|
||||||
// rsprocess::structure::RSset::from(vec![translator.convert("a")])
|
// rsprocess::structure::RSset::from(vec![translator.encode("a")])
|
||||||
// ),
|
// ),
|
||||||
// rsprocess::structure::RSreaction::from(
|
// rsprocess::structure::RSreaction::from(
|
||||||
// rsprocess::structure::RSset::from(vec![translator.convert("b")]),
|
// rsprocess::structure::RSset::from(vec![translator.encode("b")]),
|
||||||
// rsprocess::structure::RSset::from(vec![translator.convert("d")]),
|
// rsprocess::structure::RSset::from(vec![translator.encode("d")]),
|
||||||
// rsprocess::structure::RSset::from(vec![translator.convert("b")])
|
// rsprocess::structure::RSset::from(vec![translator.encode("b")])
|
||||||
// )
|
// )
|
||||||
// ]));
|
// ]));
|
||||||
|
|
||||||
@ -122,24 +123,33 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
// }
|
// }
|
||||||
|
|
||||||
// -------------------------------------------------------------------------
|
// -------------------------------------------------------------------------
|
||||||
let env = grammar::EnvironmentParser::new().parse(&mut translator, "[]").unwrap();
|
// let env = grammar::EnvironmentParser::new().parse(&mut translator, "[]").unwrap();
|
||||||
let process = grammar::ContextParser::new().parse(&mut translator, "[({a}.nil + {b}.nil),({c}.nil + {d}.nil)]").unwrap();
|
// let process = grammar::ContextParser::new().parse(&mut translator, "[({a}.nil + {b}.nil),({c}.nil + {d}.nil)]").unwrap();
|
||||||
|
|
||||||
let sys = rsprocess::structure::RSsystem::from(Rc::new(*env),
|
// let sys = rsprocess::structure::RSsystem::from(Rc::new(*env),
|
||||||
rsprocess::structure::RSset::from(vec![]),
|
// rsprocess::structure::RSset::from(vec![]),
|
||||||
*process,
|
// *process,
|
||||||
Rc::new(vec![
|
// Rc::new(vec![
|
||||||
rsprocess::structure::RSreaction::from(
|
// rsprocess::structure::RSreaction::from(
|
||||||
rsprocess::structure::RSset::from(vec![translator.convert("a")]),
|
// rsprocess::structure::RSset::from(vec![translator.encode("a")]),
|
||||||
rsprocess::structure::RSset::from(vec![translator.convert("c")]),
|
// rsprocess::structure::RSset::from(vec![translator.encode("c")]),
|
||||||
rsprocess::structure::RSset::from(vec![translator.convert("a")])
|
// rsprocess::structure::RSset::from(vec![translator.encode("a")])
|
||||||
),
|
// ),
|
||||||
rsprocess::structure::RSreaction::from(
|
// rsprocess::structure::RSreaction::from(
|
||||||
rsprocess::structure::RSset::from(vec![translator.convert("b")]),
|
// rsprocess::structure::RSset::from(vec![translator.encode("b")]),
|
||||||
rsprocess::structure::RSset::from(vec![translator.convert("d")]),
|
// rsprocess::structure::RSset::from(vec![translator.encode("d")]),
|
||||||
rsprocess::structure::RSset::from(vec![translator.convert("b")])
|
// rsprocess::structure::RSset::from(vec![translator.encode("b")])
|
||||||
)
|
// )
|
||||||
]));
|
// ]));
|
||||||
println!("{:?}", rsprocess::transitions::run_separated(&sys));
|
// println!("{:?}", rsprocess::transitions::run_separated(&sys));
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
|
||||||
|
let tmp = rsprocess::structure::RSreaction::from(
|
||||||
|
rsprocess::structure::RSset::from(vec![translator.encode("a"), translator.encode("c")]),
|
||||||
|
rsprocess::structure::RSset::from(vec![translator.encode("c")]),
|
||||||
|
rsprocess::structure::RSset::from(vec![translator.encode("a")])
|
||||||
|
);
|
||||||
|
println!("{}", WithTranslator::from_RSreaction(&translator, &tmp));
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@ -33,9 +33,9 @@ pub Set: RSset = {
|
|||||||
|
|
||||||
Set_of_entities: RSset = {
|
Set_of_entities: RSset = {
|
||||||
"{" "}" => RSset::from(vec![]),
|
"{" "}" => RSset::from(vec![]),
|
||||||
"{" <t: Literal> "}" => RSset::from(vec![translator.convert(t)]),
|
"{" <t: Literal> "}" => RSset::from(vec![translator.encode(t)]),
|
||||||
"{" <t: Separeted<Literal, ",">> "}" =>
|
"{" <t: Separeted<Literal, ",">> "}" =>
|
||||||
RSset::from(t.into_iter().map(|t| translator.convert(t)).collect::<Vec<_>>())
|
RSset::from(t.into_iter().map(|t| translator.encode(t)).collect::<Vec<_>>())
|
||||||
};
|
};
|
||||||
|
|
||||||
// ----- ContextParser -----
|
// ----- ContextParser -----
|
||||||
@ -61,14 +61,14 @@ CTX_process: RSprocess = {
|
|||||||
RSprocess::EntitySet{ entities: c, next_process: Rc::new(k) },
|
RSprocess::EntitySet{ entities: c, next_process: Rc::new(k) },
|
||||||
"(" <k: CTX_process> ")" => k,
|
"(" <k: CTX_process> ")" => k,
|
||||||
"(" <k: Separeted<CTX_process, "+">> ")" =>
|
"(" <k: Separeted<CTX_process, "+">> ")" =>
|
||||||
RSprocess::Summation{ children: k.into_iter().map(|k| Rc::new(k)).collect::<Vec<_>>() },
|
RSprocess::Summation{ children: k.into_iter().map(Rc::new).collect::<Vec<_>>() },
|
||||||
"<" <n: Num> <k1: CTX_process> ">" "." <k: CTX_process> =>
|
"<" <n: Num> <k1: CTX_process> ">" "." <k: CTX_process> =>
|
||||||
RSprocess::WaitEntity{ repeat: n,
|
RSprocess::WaitEntity{ repeat: n,
|
||||||
repeated_process: Rc::new(k1),
|
repeated_process: Rc::new(k1),
|
||||||
next_process: Rc::new(k)},
|
next_process: Rc::new(k)},
|
||||||
"nil" => RSprocess::Nill,
|
"nil" => RSprocess::Nill,
|
||||||
<identifier: Literal> =>
|
<identifier: Literal> =>
|
||||||
RSprocess::RecursiveIdentifier{ identifier: translator.convert(identifier) }
|
RSprocess::RecursiveIdentifier{ identifier: translator.encode(identifier) }
|
||||||
};
|
};
|
||||||
|
|
||||||
// ----- EnvironmentParser -----
|
// ----- EnvironmentParser -----
|
||||||
@ -79,7 +79,7 @@ pub Environment: Box<RSenvironment> = {
|
|||||||
|
|
||||||
Env_term: (IdType, RSprocess) = {
|
Env_term: (IdType, RSprocess) = {
|
||||||
<identifier: Literal> "=" <k: CTX_process> =>
|
<identifier: Literal> "=" <k: CTX_process> =>
|
||||||
(translator.convert(identifier), k)
|
(translator.encode(identifier), k)
|
||||||
};
|
};
|
||||||
|
|
||||||
// ----- AssertParser -----
|
// ----- AssertParser -----
|
||||||
|
|||||||
@ -3,3 +3,4 @@ pub mod structure;
|
|||||||
pub mod support_structures;
|
pub mod support_structures;
|
||||||
pub mod classical;
|
pub mod classical;
|
||||||
pub mod transitions;
|
pub mod transitions;
|
||||||
|
pub mod perpetual;
|
||||||
|
|||||||
0
src/rsprocess/perpetual.rs
Normal file
0
src/rsprocess/perpetual.rs
Normal file
@ -73,6 +73,10 @@ impl RSset {
|
|||||||
.collect();
|
.collect();
|
||||||
RSset { identifiers: res }
|
RSset { identifiers: res }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn hashset(&self) -> &HashSet<IdType> {
|
||||||
|
&self.identifiers
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -167,21 +171,21 @@ impl RSprocess {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct RSChoices {
|
pub struct RSchoices {
|
||||||
context_moves: Vec<(Rc<RSset>, Rc<RSprocess>)>
|
context_moves: Vec<(Rc<RSset>, Rc<RSprocess>)>
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RSChoices {
|
impl RSchoices {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
RSChoices{ context_moves: vec![] }
|
RSchoices{ context_moves: vec![] }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_not_empty() -> Self {
|
pub fn new_not_empty() -> Self {
|
||||||
RSChoices{ context_moves: vec![(Rc::new(RSset::new()),
|
RSchoices{ context_moves: vec![(Rc::new(RSset::new()),
|
||||||
Rc::new(RSprocess::Nill))] }
|
Rc::new(RSprocess::Nill))] }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn append(&mut self, a: &mut RSChoices) {
|
pub fn append(&mut self, a: &mut RSchoices) {
|
||||||
self.context_moves.append(&mut a.context_moves);
|
self.context_moves.append(&mut a.context_moves);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -192,7 +196,7 @@ impl RSChoices {
|
|||||||
.map(|(c1, _)| (Rc::clone(c1), Rc::clone(&a))).collect::<Vec<_>>();
|
.map(|(c1, _)| (Rc::clone(c1), Rc::clone(&a))).collect::<Vec<_>>();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn shuffle(&mut self, choices: RSChoices) {
|
pub fn shuffle(&mut self, choices: RSchoices) {
|
||||||
match (self.context_moves.is_empty(), choices.context_moves.is_empty()){
|
match (self.context_moves.is_empty(), choices.context_moves.is_empty()){
|
||||||
(true, true) => {}
|
(true, true) => {}
|
||||||
(true, false) => { self.context_moves = choices.context_moves }
|
(true, false) => { self.context_moves = choices.context_moves }
|
||||||
@ -211,9 +215,13 @@ impl RSChoices {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn iter(&self) -> std::slice::Iter<'_, (Rc<RSset>, Rc<RSprocess>)> {
|
||||||
|
self.context_moves.iter()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl IntoIterator for RSChoices {
|
impl IntoIterator for RSchoices {
|
||||||
type Item = (Rc<RSset>, Rc<RSprocess>);
|
type Item = (Rc<RSset>, Rc<RSprocess>);
|
||||||
type IntoIter = std::vec::IntoIter<Self::Item>;
|
type IntoIter = std::vec::IntoIter<Self::Item>;
|
||||||
|
|
||||||
@ -222,21 +230,22 @@ impl IntoIterator for RSChoices {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<const N: usize> From<[(Rc<RSset>, Rc<RSprocess>); N]> for RSChoices {
|
|
||||||
|
impl<const N: usize> From<[(Rc<RSset>, Rc<RSprocess>); N]> for RSchoices {
|
||||||
fn from(arr: [(Rc<RSset>, Rc<RSprocess>); N]) -> Self {
|
fn from(arr: [(Rc<RSset>, Rc<RSprocess>); N]) -> Self {
|
||||||
RSChoices{context_moves: arr.to_vec()}
|
RSchoices{context_moves: arr.to_vec()}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<&[(Rc<RSset>, Rc<RSprocess>)]> for RSChoices {
|
impl From<&[(Rc<RSset>, Rc<RSprocess>)]> for RSchoices {
|
||||||
fn from(arr: &[(Rc<RSset>, Rc<RSprocess>)]) -> Self {
|
fn from(arr: &[(Rc<RSset>, Rc<RSprocess>)]) -> Self {
|
||||||
RSChoices{context_moves: arr.to_vec()}
|
RSchoices{context_moves: arr.to_vec()}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<Vec<(Rc<RSset>, Rc<RSprocess>)>> for RSChoices {
|
impl From<Vec<(Rc<RSset>, Rc<RSprocess>)>> for RSchoices {
|
||||||
fn from(arr: Vec<(Rc<RSset>, Rc<RSprocess>)>) -> Self {
|
fn from(arr: Vec<(Rc<RSset>, Rc<RSprocess>)>) -> Self {
|
||||||
RSChoices{context_moves: arr}
|
RSchoices{context_moves: arr}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -256,6 +265,10 @@ impl RSenvironment {
|
|||||||
pub fn get(&self, k: IdType) -> Option<&RSprocess> {
|
pub fn get(&self, k: IdType) -> Option<&RSprocess> {
|
||||||
self.definitions.get(&k)
|
self.definitions.get(&k)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn iter(&self) -> std::collections::hash_map::Iter<'_, u32, RSprocess> {
|
||||||
|
self.definitions.iter()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<const N: usize> From<[(IdType, RSprocess); N]> for RSenvironment {
|
impl<const N: usize> From<[(IdType, RSprocess); N]> for RSenvironment {
|
||||||
@ -329,14 +342,14 @@ impl RSsystem {
|
|||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct RSlabel {
|
pub struct RSlabel {
|
||||||
available_entities: RSset,
|
pub available_entities: RSset,
|
||||||
context: RSset,
|
pub context: RSset,
|
||||||
t: RSset, /// union of available_entities and context
|
pub t: RSset, /// union of available_entities and context
|
||||||
reactants: RSset,
|
pub reactants: RSset,
|
||||||
reactantsi: RSset,
|
pub reactantsi: RSset,
|
||||||
inihibitors: RSset,
|
pub inihibitors: RSset,
|
||||||
ireactants: RSset,
|
pub ireactants: RSset,
|
||||||
products: RSset,
|
pub products: RSset,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RSlabel {
|
impl RSlabel {
|
||||||
|
|||||||
@ -1,15 +1,15 @@
|
|||||||
#![allow(dead_code)]
|
#![allow(dead_code)]
|
||||||
|
|
||||||
use super::structure::{RSChoices, RSenvironment, RSlabel, RSprocess, RSset, RSsystem};
|
use super::structure::{RSchoices, RSenvironment, RSlabel, RSprocess, RSset, RSsystem};
|
||||||
use super::support_structures::TransitionsIterator;
|
use super::support_structures::TransitionsIterator;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
pub fn unfold(
|
pub fn unfold(
|
||||||
environment: &RSenvironment,
|
environment: &RSenvironment,
|
||||||
context_process: &RSprocess,
|
context_process: &RSprocess,
|
||||||
) -> Result<RSChoices, String> {
|
) -> Result<RSchoices, String> {
|
||||||
match context_process {
|
match context_process {
|
||||||
RSprocess::Nill => Ok(RSChoices::new()),
|
RSprocess::Nill => Ok(RSchoices::new()),
|
||||||
RSprocess::RecursiveIdentifier { identifier } => {
|
RSprocess::RecursiveIdentifier { identifier } => {
|
||||||
let newprocess = environment.get(*identifier);
|
let newprocess = environment.get(*identifier);
|
||||||
if let Some(newprocess) = newprocess {
|
if let Some(newprocess) = newprocess {
|
||||||
@ -21,7 +21,7 @@ pub fn unfold(
|
|||||||
RSprocess::EntitySet {
|
RSprocess::EntitySet {
|
||||||
entities,
|
entities,
|
||||||
next_process,
|
next_process,
|
||||||
} => Ok(RSChoices::from(vec![(
|
} => Ok(RSchoices::from(vec![(
|
||||||
Rc::new(entities.clone()),
|
Rc::new(entities.clone()),
|
||||||
Rc::clone(next_process),
|
Rc::clone(next_process),
|
||||||
)])),
|
)])),
|
||||||
@ -54,7 +54,7 @@ pub fn unfold(
|
|||||||
}
|
}
|
||||||
RSprocess::Summation { children } => {
|
RSprocess::Summation { children } => {
|
||||||
// short-circuits with try_fold.
|
// short-circuits with try_fold.
|
||||||
children.iter().try_fold(RSChoices::new(), |mut acc, x| {
|
children.iter().try_fold(RSchoices::new(), |mut acc, x| {
|
||||||
match unfold(environment, x) {
|
match unfold(environment, x) {
|
||||||
Ok(mut choices) => {
|
Ok(mut choices) => {
|
||||||
acc.append(&mut choices);
|
acc.append(&mut choices);
|
||||||
@ -67,12 +67,12 @@ pub fn unfold(
|
|||||||
RSprocess::NondeterministicChoice { children } => {
|
RSprocess::NondeterministicChoice { children } => {
|
||||||
// short-circuits with try_fold.
|
// short-circuits with try_fold.
|
||||||
if children.is_empty() {
|
if children.is_empty() {
|
||||||
Ok(RSChoices::from(vec![(
|
Ok(RSchoices::from(vec![(
|
||||||
Rc::new(RSset::new()),
|
Rc::new(RSset::new()),
|
||||||
Rc::new(RSprocess::Nill),
|
Rc::new(RSprocess::Nill),
|
||||||
)]))
|
)]))
|
||||||
} else {
|
} else {
|
||||||
children.iter().try_fold(RSChoices::new(), |mut acc, x| {
|
children.iter().try_fold(RSchoices::new(), |mut acc, x| {
|
||||||
acc.shuffle(unfold(environment, x)?);
|
acc.shuffle(unfold(environment, x)?);
|
||||||
Ok(acc)
|
Ok(acc)
|
||||||
})
|
})
|
||||||
|
|||||||
@ -1,5 +1,4 @@
|
|||||||
// translate and keeps track of strings
|
// translate and keeps track of strings
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
pub type IdType = u32;
|
pub type IdType = u32;
|
||||||
@ -7,23 +6,332 @@ pub type IdType = u32;
|
|||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct Translator {
|
pub struct Translator {
|
||||||
strings: HashMap<String, IdType>,
|
strings: HashMap<String, IdType>,
|
||||||
last_id: IdType
|
reverse: HashMap<IdType, String>,
|
||||||
|
last_id: IdType,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Translator {
|
impl Translator {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Translator { strings: HashMap::new(), last_id: 0 }
|
Translator {
|
||||||
|
strings: HashMap::new(),
|
||||||
|
reverse: HashMap::new(),
|
||||||
|
last_id: 0,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Translator {
|
impl Translator {
|
||||||
pub fn convert(&mut self, s: impl Into<String>) -> IdType {
|
pub fn encode(&mut self, s: impl Into<String>) -> IdType {
|
||||||
*(self.strings.entry(s.into()).or_insert(
|
let s = s.into();
|
||||||
{self.last_id += 1;
|
let id = *(self.strings.entry(s.clone()).or_insert({
|
||||||
|
self.last_id += 1;
|
||||||
self.last_id
|
self.last_id
|
||||||
|
}));
|
||||||
|
self.reverse.insert(id, s.clone());
|
||||||
|
id
|
||||||
}
|
}
|
||||||
))
|
|
||||||
|
pub fn decode(&self, el: IdType) -> String {
|
||||||
|
// TODO maybe find more efficient method??
|
||||||
|
self.reverse.get(&el)
|
||||||
|
.map(|x| x.to_string())
|
||||||
|
.unwrap_or(String::from("Not Found"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
use super::structure::{
|
||||||
|
RSassert, RSassertOp, RSchoices, RSenvironment, RSlabel, RSprocess, RSreaction, RSset,
|
||||||
|
RSsystem, RSBHML,
|
||||||
|
};
|
||||||
|
use std::fmt;
|
||||||
|
|
||||||
|
#[allow(clippy::large_enum_variant)]
|
||||||
|
#[allow(clippy::upper_case_acronyms)]
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub enum WithTranslator<'a> {
|
||||||
|
RSset {
|
||||||
|
translator: &'a Translator,
|
||||||
|
set: &'a RSset,
|
||||||
|
},
|
||||||
|
RSreaction {
|
||||||
|
translator: &'a Translator,
|
||||||
|
reaction: &'a RSreaction,
|
||||||
|
},
|
||||||
|
RSprocess {
|
||||||
|
translator: &'a Translator,
|
||||||
|
process: &'a RSprocess,
|
||||||
|
},
|
||||||
|
RSchoices {
|
||||||
|
translator: &'a Translator,
|
||||||
|
choices: &'a RSchoices,
|
||||||
|
},
|
||||||
|
RSenvironment {
|
||||||
|
translator: &'a Translator,
|
||||||
|
environment: &'a RSenvironment,
|
||||||
|
},
|
||||||
|
RSsystem {
|
||||||
|
translator: &'a Translator,
|
||||||
|
system: &'a RSsystem,
|
||||||
|
},
|
||||||
|
RSlabel {
|
||||||
|
translator: &'a Translator,
|
||||||
|
label: &'a RSlabel,
|
||||||
|
},
|
||||||
|
RSassertOp {
|
||||||
|
translator: &'a Translator,
|
||||||
|
assert_op: &'a RSassertOp,
|
||||||
|
},
|
||||||
|
RSassert {
|
||||||
|
translator: &'a Translator,
|
||||||
|
assert: &'a RSassert,
|
||||||
|
},
|
||||||
|
RSBHML {
|
||||||
|
translator: &'a Translator,
|
||||||
|
bhml: &'a RSBHML,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! from_RS {
|
||||||
|
($name:ident, $type:ty, $dataname:ident, $type2: ident) => {
|
||||||
|
pub fn $name(translator: &'a Translator, $dataname: &'a $type) -> Self {
|
||||||
|
WithTranslator::$type2 { translator, $dataname }
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(non_snake_case)]
|
||||||
|
#[allow(dead_code)]
|
||||||
|
impl<'a> WithTranslator<'a> {
|
||||||
|
from_RS!(from_RSset, RSset, set, RSset);
|
||||||
|
|
||||||
|
from_RS!(from_RSreaction, RSreaction, reaction, RSreaction);
|
||||||
|
|
||||||
|
from_RS!(from_RSprocess, RSprocess, process, RSprocess);
|
||||||
|
|
||||||
|
from_RS!(from_RSchoices, RSchoices, choices, RSchoices);
|
||||||
|
|
||||||
|
from_RS!(from_RSenvironment, RSenvironment, environment, RSenvironment);
|
||||||
|
|
||||||
|
from_RS!(from_RSsystem, RSsystem, system, RSsystem);
|
||||||
|
|
||||||
|
from_RS!(from_RSlabel, RSlabel, label, RSlabel);
|
||||||
|
|
||||||
|
from_RS!(from_RSassertOp, RSassertOp, assert_op, RSassertOp);
|
||||||
|
|
||||||
|
from_RS!(from_RSassert, RSassert, assert, RSassert);
|
||||||
|
|
||||||
|
from_RS!(from_RSBHML, RSBHML, bhml, RSBHML);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
// Printing functions
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
fn print_set(f: &mut fmt::Formatter, translator: &Translator, set: &RSset) -> fmt::Result {
|
||||||
|
write!(f, "{{")?;
|
||||||
|
let mut it = set.hashset().iter().peekable();
|
||||||
|
while let Some(el) = it.next() {
|
||||||
|
if it.peek().is_none() {
|
||||||
|
write!(f, "{}", translator.decode(*el))?;
|
||||||
|
} else {
|
||||||
|
write!(f, "{}, ", translator.decode(*el))?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
write!(f, "}}")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn print_reaction(f: &mut fmt::Formatter, translator: &Translator, reaction: &RSreaction) -> fmt::Result {
|
||||||
|
write!(f, "(r: {}, i: {}, p: {})",
|
||||||
|
WithTranslator::from_RSset(translator, reaction.reactants()),
|
||||||
|
WithTranslator::from_RSset(translator, reaction.inihibitors()),
|
||||||
|
WithTranslator::from_RSset(translator, reaction.products()))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn print_process(f: &mut fmt::Formatter, translator: &Translator, process: &RSprocess) -> fmt::Result {
|
||||||
|
use super::structure::RSprocess::*;
|
||||||
|
match process {
|
||||||
|
Nill => {
|
||||||
|
write!(f, "[Nill]")
|
||||||
|
},
|
||||||
|
RecursiveIdentifier{ identifier } => {
|
||||||
|
write!(f, "[{}]", translator.decode(*identifier))
|
||||||
|
},
|
||||||
|
EntitySet{ entities,
|
||||||
|
next_process } => {
|
||||||
|
write!(f, "[entities: {}, next_process: {}]",
|
||||||
|
WithTranslator::from_RSset(translator, entities),
|
||||||
|
WithTranslator::from_RSprocess(translator, next_process)
|
||||||
|
)
|
||||||
|
},
|
||||||
|
WaitEntity{ repeat,
|
||||||
|
repeated_process,
|
||||||
|
next_process } => {
|
||||||
|
write!(f, "[repeat: {repeat}, repeated_process: {}, next_process: {}]",
|
||||||
|
WithTranslator::from_RSprocess(translator, repeated_process),
|
||||||
|
WithTranslator::from_RSprocess(translator, next_process)
|
||||||
|
)
|
||||||
|
},
|
||||||
|
Summation{ children } => {
|
||||||
|
write!(f, "[")?;
|
||||||
|
let mut it = children.iter().peekable();
|
||||||
|
while let Some(child) = it.next() {
|
||||||
|
if it.peek().is_none() {
|
||||||
|
write!(f, "{}",
|
||||||
|
WithTranslator::from_RSprocess(translator, child)
|
||||||
|
)?;
|
||||||
|
} else {
|
||||||
|
write!(f, "{} + ",
|
||||||
|
WithTranslator::from_RSprocess(translator, child)
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
write!(f, "]")
|
||||||
|
},
|
||||||
|
NondeterministicChoice{ children } => {
|
||||||
|
write!(f, "[")?;
|
||||||
|
let mut it = children.iter().peekable();
|
||||||
|
while let Some(child) = it.next() {
|
||||||
|
if it.peek().is_none() {
|
||||||
|
write!(f, "{}",
|
||||||
|
WithTranslator::from_RSprocess(translator, child)
|
||||||
|
)?;
|
||||||
|
} else {
|
||||||
|
write!(f, "{}, ",
|
||||||
|
WithTranslator::from_RSprocess(translator, child)
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
write!(f, "]")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn print_choices(f: &mut fmt::Formatter, translator: &Translator, choices: &RSchoices) -> fmt::Result {
|
||||||
|
write!(f, "[")?;
|
||||||
|
let mut it = choices.iter().peekable();
|
||||||
|
while let Some(el) = it.next() {
|
||||||
|
if it.peek().is_none() {
|
||||||
|
write!(f, "[set: {}, process: {}]",
|
||||||
|
WithTranslator::from_RSset(translator, &el.0),
|
||||||
|
WithTranslator::from_RSprocess(translator, &el.1))?;
|
||||||
|
} else {
|
||||||
|
write!(f, "[set: {}, process: {}], ",
|
||||||
|
WithTranslator::from_RSset(translator, &el.0),
|
||||||
|
WithTranslator::from_RSprocess(translator, &el.1))?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
write!(f, "]")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn print_environment(f: &mut fmt::Formatter, translator: &Translator, environment: &RSenvironment) -> fmt::Result {
|
||||||
|
write!(f, "{{env:")?;
|
||||||
|
let mut it = environment.iter().peekable();
|
||||||
|
while let Some(el) = it.next() {
|
||||||
|
if it.peek().is_none() {
|
||||||
|
write!(f, "({} -> {})",
|
||||||
|
translator.decode(*el.0),
|
||||||
|
WithTranslator::from_RSprocess(translator, el.1)
|
||||||
|
)?;
|
||||||
|
} else {
|
||||||
|
write!(f, "({} -> {}), ",
|
||||||
|
translator.decode(*el.0),
|
||||||
|
WithTranslator::from_RSprocess(translator, el.1)
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
write!(f, "}}")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn print_system(f: &mut fmt::Formatter, translator: &Translator, system: &RSsystem) -> fmt::Result {
|
||||||
|
write!(f, "[delta: {}, available_entities: {}, context_process: {}, reaction_rules: [",
|
||||||
|
WithTranslator::from_RSenvironment(translator, system.get_delta()),
|
||||||
|
WithTranslator::from_RSset(translator, system.get_available_entities()),
|
||||||
|
WithTranslator::from_RSprocess(translator, system.get_context_process())
|
||||||
|
)?;
|
||||||
|
let mut it = system.get_reaction_rules().iter().peekable();
|
||||||
|
while let Some(el) = it.next() {
|
||||||
|
if it.peek().is_none() {
|
||||||
|
write!(f, "{}",
|
||||||
|
WithTranslator::from_RSreaction(translator, el)
|
||||||
|
)?;
|
||||||
|
} else {
|
||||||
|
write!(f, "{}, ",
|
||||||
|
WithTranslator::from_RSreaction(translator, el)
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
write!(f, "] ]")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn print_label(f: &mut fmt::Formatter, translator: &Translator, label: &RSlabel) -> fmt::Result {
|
||||||
|
write!(f, "{{available_entities: {}, context: {}, t: {}, reactants: {}, reactantsi: {}, inihibitors: {}, ireactants: {}, products: {}}}",
|
||||||
|
WithTranslator::from_RSset(translator, &label.available_entities),
|
||||||
|
WithTranslator::from_RSset(translator, &label.context),
|
||||||
|
WithTranslator::from_RSset(translator, &label.t),
|
||||||
|
WithTranslator::from_RSset(translator, &label.reactants),
|
||||||
|
WithTranslator::from_RSset(translator, &label.reactantsi),
|
||||||
|
WithTranslator::from_RSset(translator, &label.inihibitors),
|
||||||
|
WithTranslator::from_RSset(translator, &label.ireactants),
|
||||||
|
WithTranslator::from_RSset(translator, &label.products),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn print_assert_op(f: &mut fmt::Formatter, _translator: &Translator, assert_op: &RSassertOp) -> fmt::Result {
|
||||||
|
use super::structure::RSassertOp::*;
|
||||||
|
match assert_op {
|
||||||
|
InW => {write!(f, "InW")},
|
||||||
|
InR => {write!(f, "InR")},
|
||||||
|
InI => {write!(f, "InI")},
|
||||||
|
InP => {write!(f, "InP")},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(unused_variables)]
|
||||||
|
fn print_assert(f: &mut fmt::Formatter, translator: &Translator, assert: &RSassert) -> fmt::Result {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(unused_variables)]
|
||||||
|
fn print_bhml(f: &mut fmt::Formatter, translator: &Translator, bhml: &RSBHML) -> fmt::Result {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> fmt::Display for WithTranslator<'a> {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
match self {
|
||||||
|
WithTranslator::RSset{translator, set} => {
|
||||||
|
print_set(f, translator, set)
|
||||||
|
},
|
||||||
|
WithTranslator::RSreaction { translator, reaction } => {
|
||||||
|
print_reaction(f, translator, reaction)
|
||||||
|
},
|
||||||
|
WithTranslator::RSprocess { translator, process } => {
|
||||||
|
print_process(f, translator, process)
|
||||||
|
},
|
||||||
|
WithTranslator::RSchoices { translator, choices } => {
|
||||||
|
print_choices(f, translator, choices)
|
||||||
|
},
|
||||||
|
WithTranslator::RSenvironment { translator, environment } => {
|
||||||
|
print_environment(f, translator, environment)
|
||||||
|
},
|
||||||
|
WithTranslator::RSsystem { translator, system } => {
|
||||||
|
print_system(f, translator, system)
|
||||||
|
},
|
||||||
|
WithTranslator::RSlabel { translator, label } => {
|
||||||
|
print_label(f, translator, label)
|
||||||
|
},
|
||||||
|
WithTranslator::RSassertOp { translator, assert_op } => {
|
||||||
|
print_assert_op(f, translator, assert_op)
|
||||||
|
},
|
||||||
|
WithTranslator::RSassert { translator, assert } => {
|
||||||
|
print_assert(f, translator, assert)
|
||||||
|
},
|
||||||
|
WithTranslator::RSBHML { translator, bhml } => {
|
||||||
|
print_bhml(f, translator, bhml)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user