diff --git a/src/rsprocess/assert/dsl.rs b/src/rsprocess/assert/dsl.rs index 4323fbf..741edf2 100644 --- a/src/rsprocess/assert/dsl.rs +++ b/src/rsprocess/assert/dsl.rs @@ -1,6 +1,6 @@ use std::collections::HashMap; -use super::super::{structure, translator, graph}; +use super::super::{translator, graph, set, process, system, label}; /// If changing IntegerType in assert.rs, also change from Num to another /// similar parser with different return type in grammar.lalrpop in @@ -51,8 +51,8 @@ pub enum Expression { True, False, Integer(IntegerType), - Label(Box), - Set(structure::RSset), + Label(Box), + Set(set::Set), Element(translator::IdType), String(String), Var(Variable), @@ -173,14 +173,14 @@ pub enum AssertReturnValue { Boolean(bool), Integer(IntegerType), String(String), - Label(structure::RSlabel), - Set(structure::RSset), + Label(label::Label), + Set(set::Set), Element(translator::IdType), Node(petgraph::graph::NodeIndex), Edge(petgraph::graph::EdgeIndex), Neighbours(petgraph::graph::NodeIndex), - System(structure::RSsystem), - Context(structure::RSprocess), + System(system::System), + Context(process::Process), } // ----------------------------------------------------------------------------- @@ -190,8 +190,8 @@ pub enum AssertReturnValue { impl QualifierRestricted { pub(super) fn referenced_mut<'a>( &self, - label: &'a mut structure::RSlabel, - ) -> &'a mut structure::RSset { + label: &'a mut label::Label, + ) -> &'a mut set::Set { match self { Self::Entities => {&mut label.available_entities}, Self::Context => {&mut label.context}, @@ -205,8 +205,8 @@ impl QualifierRestricted { pub(super) fn referenced<'a>( &self, - label: &'a structure::RSlabel, - ) -> &'a structure::RSset { + label: &'a label::Label, + ) -> &'a set::Set { match self { Self::Entities => {&label.available_entities}, Self::Context => {&label.context}, @@ -220,7 +220,7 @@ impl QualifierRestricted { pub(super) fn get( &self, - label: &structure::RSlabel, + label: &label::Label, ) -> AssertReturnValue { AssertReturnValue::Set(self.referenced(label).clone()) } @@ -229,7 +229,7 @@ impl QualifierRestricted { impl QualifierLabel { pub(super) fn get( &self, - l: &structure::RSlabel, + l: &label::Label, ) -> AssertReturnValue { match self { QualifierLabel::AvailableEntities => { @@ -249,7 +249,7 @@ impl QualifierLabel { impl QualifierSystem { pub(super) fn get( &self, - l: &structure::RSsystem, + l: &system::System, ) -> AssertReturnValue { match self { Self::Context => { diff --git a/src/rsprocess/assert/rsassert.rs b/src/rsprocess/assert/rsassert.rs index e1a77f2..5be4dc0 100644 --- a/src/rsprocess/assert/rsassert.rs +++ b/src/rsprocess/assert/rsassert.rs @@ -1,5 +1,5 @@ use super::dsl::*; -use super::super::{structure, translator, graph}; +use super::super::{translator, graph, set, system, label}; use std::collections::HashMap; // ---------------------------------------------------------------------------- @@ -43,7 +43,7 @@ pub enum EdgeRelablerInput { #[derive(Debug, Clone)] enum EdgeRelablerInputValues { - Label(structure::RSlabel), + Label(label::Label), Edge(petgraph::graph::EdgeIndex), } @@ -157,7 +157,7 @@ pub enum NodeRelablerInput { #[derive(Debug, Clone)] enum NodeRelablerInputValues { - Entities(structure::RSset), + Entities(set::Set), Node(petgraph::graph::NodeIndex), } @@ -243,7 +243,7 @@ impl RSassert { node: &::NodeId, translator: &mut translator::Translator, ) -> Result { - let structure::RSsystem {available_entities: entities, ..} = + let system::System {available_entities: entities, ..} = graph.node_weight(*node).ok_or("Missing node {{debug: {node:?}}}")?; let mut input_vals = HashMap::new(); diff --git a/src/rsprocess/assert/tests.rs b/src/rsprocess/assert/tests.rs index ed8f341..319ddde 100644 --- a/src/rsprocess/assert/tests.rs +++ b/src/rsprocess/assert/tests.rs @@ -1,9 +1,10 @@ -// ----------------------------------------------------------------------------- -// Testing -// ----------------------------------------------------------------------------- - use super::dsl::*; -use super::super::{translator, structure}; +use super::super::{ environment, + process, + translator, + set, + system, + label }; use super::rsassert::*; type LocalAssert = RSassert; @@ -11,7 +12,8 @@ type LocalAssert = RSassert; #[test] fn return_true() { use translator::Translator; - use structure::{RSsystem, RSlabel}; + use system::System; + use label::Label; let tree = LocalAssert {tree: Tree::Return(Box::new(Expression::True))}; assert!(tree.typecheck().is_ok()); @@ -20,9 +22,9 @@ fn return_true() { let mut tr = Translator::new(); let mut graph = petgraph::Graph::new(); - let node_1 = graph.add_node(RSsystem::new()); - let node_2 = graph.add_node(RSsystem::new()); - let edge = graph.add_edge(node_1, node_2, RSlabel::new()); + let node_1 = graph.add_node(System::new()); + let node_2 = graph.add_node(System::new()); + let edge = graph.add_edge(node_1, node_2, Label::new()); assert!(matches!(tree.execute(&graph, &edge, &mut tr), Ok(AssertReturnValue::Boolean(true)))); @@ -31,7 +33,8 @@ fn return_true() { #[test] fn concat_1() { use translator::Translator; - use structure::{RSsystem, RSlabel}; + use system::System; + use label::Label; let tree = LocalAssert { tree: Tree::Concat( @@ -48,9 +51,9 @@ fn concat_1() { let mut tr = Translator::new(); let mut graph = petgraph::Graph::new(); - let node_1 = graph.add_node(RSsystem::new()); - let node_2 = graph.add_node(RSsystem::new()); - let edge = graph.add_edge(node_1, node_2, RSlabel::new()); + let node_1 = graph.add_node(System::new()); + let node_2 = graph.add_node(System::new()); + let edge = graph.add_edge(node_1, node_2, Label::new()); assert!(matches!(tree.execute(&graph, &edge, &mut tr), Ok(AssertReturnValue::Boolean(true)))); @@ -59,7 +62,8 @@ fn concat_1() { #[test] fn concat_2() { use translator::Translator; - use structure::{RSsystem, RSlabel}; + use system::System; + use label::Label; let tree = LocalAssert { tree: Tree::Concat( @@ -76,9 +80,9 @@ fn concat_2() { let mut tr = Translator::new(); let mut graph = petgraph::Graph::new(); - let node_1 = graph.add_node(RSsystem::new()); - let node_2 = graph.add_node(RSsystem::new()); - let edge = graph.add_edge(node_1, node_2, RSlabel::new()); + let node_1 = graph.add_node(System::new()); + let node_2 = graph.add_node(System::new()); + let edge = graph.add_edge(node_1, node_2, Label::new()); assert!(matches!(tree.execute(&graph, &edge, &mut tr), Ok(AssertReturnValue::Boolean(true)))); @@ -87,7 +91,8 @@ fn concat_2() { #[test] fn return_1() { use translator::Translator; - use structure::{RSsystem, RSlabel}; + use system::System; + use label::Label; let tree = LocalAssert { tree: Tree::Concat( @@ -101,9 +106,9 @@ fn return_1() { let mut tr = Translator::new(); let mut graph = petgraph::Graph::new(); - let node_1 = graph.add_node(RSsystem::new()); - let node_2 = graph.add_node(RSsystem::new()); - let edge = graph.add_edge(node_1, node_2, RSlabel::new()); + let node_1 = graph.add_node(System::new()); + let node_2 = graph.add_node(System::new()); + let edge = graph.add_edge(node_1, node_2, Label::new()); assert!(matches!(tree.execute(&graph, &edge, &mut tr), Ok(AssertReturnValue::Boolean(true)))); @@ -143,7 +148,8 @@ fn return_incompatible_2() { #[test] fn return_2() { use translator::Translator; - use structure::{RSsystem, RSlabel}; + use system::System; + use label::Label; let tree = LocalAssert { tree: Tree::Concat( @@ -166,9 +172,9 @@ fn return_2() { let mut tr = Translator::new(); let mut graph = petgraph::Graph::new(); - let node_1 = graph.add_node(RSsystem::new()); - let node_2 = graph.add_node(RSsystem::new()); - let edge = graph.add_edge(node_1, node_2, RSlabel::new()); + let node_1 = graph.add_node(System::new()); + let node_2 = graph.add_node(System::new()); + let edge = graph.add_edge(node_1, node_2, Label::new()); assert!(matches!(tree.execute(&graph, &edge, &mut tr), Ok(AssertReturnValue::Boolean(true)))); @@ -178,7 +184,8 @@ fn return_2() { #[test] fn return_3() { use translator::Translator; - use structure::{RSsystem, RSlabel}; + use system::System; + use label::Label; let tree = LocalAssert { tree: Tree::Concat( @@ -201,9 +208,9 @@ fn return_3() { let mut tr = Translator::new(); let mut graph = petgraph::Graph::new(); - let node_1 = graph.add_node(RSsystem::new()); - let node_2 = graph.add_node(RSsystem::new()); - let edge = graph.add_edge(node_1, node_2, RSlabel::new()); + let node_1 = graph.add_node(System::new()); + let node_2 = graph.add_node(System::new()); + let edge = graph.add_edge(node_1, node_2, Label::new()); assert!(matches!(tree.execute(&graph, &edge, &mut tr), Ok(AssertReturnValue::Boolean(false)))); @@ -212,7 +219,8 @@ fn return_3() { #[test] fn if_1() { use translator::Translator; - use structure::{RSsystem, RSlabel}; + use system::System; + use label::Label; let tree = LocalAssert { tree: Tree::Concat( @@ -237,9 +245,9 @@ fn if_1() { let mut tr = Translator::new(); let mut graph = petgraph::Graph::new(); - let node_1 = graph.add_node(RSsystem::new()); - let node_2 = graph.add_node(RSsystem::new()); - let edge = graph.add_edge(node_1, node_2, RSlabel::new()); + let node_1 = graph.add_node(System::new()); + let node_2 = graph.add_node(System::new()); + let edge = graph.add_edge(node_1, node_2, Label::new()); assert!(matches!(tree.execute(&graph, &edge, &mut tr), Ok(AssertReturnValue::Boolean(true)))); @@ -248,7 +256,8 @@ fn if_1() { #[test] fn if_2() { use translator::Translator; - use structure::{RSsystem, RSlabel}; + use system::System; + use label::Label; let tree = LocalAssert { tree: Tree::Concat( @@ -272,9 +281,9 @@ fn if_2() { let mut tr = Translator::new(); let mut graph = petgraph::Graph::new(); - let node_1 = graph.add_node(RSsystem::new()); - let node_2 = graph.add_node(RSsystem::new()); - let edge = graph.add_edge(node_1, node_2, RSlabel::new()); + let node_1 = graph.add_node(System::new()); + let node_2 = graph.add_node(System::new()); + let edge = graph.add_edge(node_1, node_2, Label::new()); assert!(matches!(tree.execute(&graph, &edge, &mut tr), Ok(AssertReturnValue::Boolean(false)))); @@ -283,7 +292,8 @@ fn if_2() { #[test] fn if_3() { use translator::Translator; - use structure::{RSsystem, RSlabel}; + use system::System; + use label::Label; let tree = LocalAssert { tree: Tree::Concat( @@ -308,9 +318,9 @@ fn if_3() { let mut tr = Translator::new(); let mut graph = petgraph::Graph::new(); - let node_1 = graph.add_node(RSsystem::new()); - let node_2 = graph.add_node(RSsystem::new()); - let edge = graph.add_edge(node_1, node_2, RSlabel::new()); + let node_1 = graph.add_node(System::new()); + let node_2 = graph.add_node(System::new()); + let edge = graph.add_edge(node_1, node_2, Label::new()); assert!(matches!(tree.execute(&graph, &edge, &mut tr), Ok(AssertReturnValue::Boolean(true)))); @@ -341,7 +351,8 @@ fn if_4() { #[test] fn if_else_1() { use translator::Translator; - use structure::{RSsystem, RSlabel}; + use system::System; + use label::Label; let tree = LocalAssert { tree: Tree::Concat( @@ -369,9 +380,9 @@ fn if_else_1() { let mut tr = Translator::new(); let mut graph = petgraph::Graph::new(); - let node_1 = graph.add_node(RSsystem::new()); - let node_2 = graph.add_node(RSsystem::new()); - let edge = graph.add_edge(node_1, node_2, RSlabel::new()); + let node_1 = graph.add_node(System::new()); + let node_2 = graph.add_node(System::new()); + let edge = graph.add_edge(node_1, node_2, Label::new()); assert!(matches!(tree.execute(&graph, &edge, &mut tr), Ok(AssertReturnValue::Boolean(true)))); @@ -380,7 +391,8 @@ fn if_else_1() { #[test] fn if_else_2() { use translator::Translator; - use structure::{RSsystem, RSlabel}; + use system::System; + use label::Label; let tree = LocalAssert { tree: Tree::Concat( @@ -408,9 +420,9 @@ fn if_else_2() { let mut tr = Translator::new(); let mut graph = petgraph::Graph::new(); - let node_1 = graph.add_node(RSsystem::new()); - let node_2 = graph.add_node(RSsystem::new()); - let edge = graph.add_edge(node_1, node_2, RSlabel::new()); + let node_1 = graph.add_node(System::new()); + let node_2 = graph.add_node(System::new()); + let edge = graph.add_edge(node_1, node_2, Label::new()); assert!(matches!(tree.execute(&graph, &edge, &mut tr), Ok(AssertReturnValue::Boolean(false)))); @@ -481,7 +493,8 @@ fn assignment_1() { #[test] fn assignment_2() { use translator::Translator; - use structure::{RSsystem, RSlabel}; + use system::System; + use label::Label; let tree = LocalAssert { tree: Tree::Concat( @@ -507,9 +520,9 @@ fn assignment_2() { let mut tr = Translator::new(); let mut graph = petgraph::Graph::new(); - let node_1 = graph.add_node(RSsystem::new()); - let node_2 = graph.add_node(RSsystem::new()); - let edge = graph.add_edge(node_1, node_2, RSlabel::new()); + let node_1 = graph.add_node(System::new()); + let node_2 = graph.add_node(System::new()); + let edge = graph.add_edge(node_1, node_2, Label::new()); assert!(matches!(tree.execute(&graph, &edge, &mut tr), Ok(AssertReturnValue::Boolean(true)))); @@ -518,7 +531,8 @@ fn assignment_2() { #[test] fn assignment_3() { use translator::Translator; - use structure::{RSsystem, RSlabel}; + use system::System; + use label::Label; let tree = LocalAssert { tree: Tree::Concat( @@ -546,9 +560,9 @@ fn assignment_3() { let mut tr = Translator::new(); let mut graph = petgraph::Graph::new(); - let node_1 = graph.add_node(RSsystem::new()); - let node_2 = graph.add_node(RSsystem::new()); - let edge = graph.add_edge(node_1, node_2, RSlabel::new()); + let node_1 = graph.add_node(System::new()); + let node_2 = graph.add_node(System::new()); + let edge = graph.add_edge(node_1, node_2, Label::new()); assert!(matches!(tree.execute(&graph, &edge, &mut tr), Ok(AssertReturnValue::Boolean(false)))); @@ -582,7 +596,8 @@ fn assignment_4() { #[test] fn assignment_5() { use translator::Translator; - use structure::{RSsystem, RSlabel}; + use system::System; + use label::Label; let tree = LocalAssert { tree: Tree::Concat( @@ -611,9 +626,9 @@ fn assignment_5() { let mut tr = Translator::new(); let mut graph = petgraph::Graph::new(); - let node_1 = graph.add_node(RSsystem::new()); - let node_2 = graph.add_node(RSsystem::new()); - let edge = graph.add_edge(node_1, node_2, RSlabel::new()); + let node_1 = graph.add_node(System::new()); + let node_2 = graph.add_node(System::new()); + let edge = graph.add_edge(node_1, node_2, Label::new()); assert!(matches!(tree.execute(&graph, &edge, &mut tr), Ok(AssertReturnValue::Integer(10)))); @@ -622,7 +637,8 @@ fn assignment_5() { #[test] fn assignment_6() { use translator::Translator; - use structure::{RSsystem, RSlabel}; + use system::System; + use label::Label; let tree = LocalAssert { tree: Tree::Concat( @@ -658,9 +674,9 @@ fn assignment_6() { let mut tr = Translator::new(); let mut graph = petgraph::Graph::new(); - let node_1 = graph.add_node(RSsystem::new()); - let node_2 = graph.add_node(RSsystem::new()); - let edge = graph.add_edge(node_1, node_2, RSlabel::new()); + let node_1 = graph.add_node(System::new()); + let node_2 = graph.add_node(System::new()); + let edge = graph.add_edge(node_1, node_2, Label::new()); assert!(matches!(tree.execute(&graph, &edge, &mut tr), Ok(AssertReturnValue::Integer(10)))); @@ -669,7 +685,8 @@ fn assignment_6() { #[test] fn assignment_7() { use translator::Translator; - use structure::{RSsystem, RSlabel}; + use system::System; + use label::Label; let tree = LocalAssert { tree: Tree::Concat( @@ -703,9 +720,9 @@ fn assignment_7() { let mut tr = Translator::new(); let mut graph = petgraph::Graph::new(); - let node_1 = graph.add_node(RSsystem::new()); - let node_2 = graph.add_node(RSsystem::new()); - let edge = graph.add_edge(node_1, node_2, RSlabel::new()); + let node_1 = graph.add_node(System::new()); + let node_2 = graph.add_node(System::new()); + let edge = graph.add_edge(node_1, node_2, Label::new()); assert!(matches!(tree.execute(&graph, &edge, &mut tr), Ok(AssertReturnValue::Boolean(true)))); @@ -714,7 +731,8 @@ fn assignment_7() { #[test] fn assignment_8() { use translator::Translator; - use structure::{RSsystem, RSlabel}; + use system::System; + use label::Label; let tree = LocalAssert { tree: Tree::Concat( @@ -752,9 +770,9 @@ fn assignment_8() { let mut tr = Translator::new(); let mut graph = petgraph::Graph::new(); - let node_1 = graph.add_node(RSsystem::new()); - let node_2 = graph.add_node(RSsystem::new()); - let edge = graph.add_edge(node_1, node_2, RSlabel::new()); + let node_1 = graph.add_node(System::new()); + let node_2 = graph.add_node(System::new()); + let edge = graph.add_edge(node_1, node_2, Label::new()); assert!(matches!(tree.execute(&graph, &edge, &mut tr), Ok(AssertReturnValue::Integer(10)))); @@ -763,7 +781,8 @@ fn assignment_8() { #[test] fn assignment_9() { use translator::Translator; - use structure::{RSsystem, RSlabel}; + use system::System; + use label::Label; let tree = LocalAssert { tree: Tree::Concat( @@ -799,9 +818,9 @@ fn assignment_9() { let mut tr = Translator::new(); let mut graph = petgraph::Graph::new(); - let node_1 = graph.add_node(RSsystem::new()); - let node_2 = graph.add_node(RSsystem::new()); - let edge = graph.add_edge(node_1, node_2, RSlabel::new()); + let node_1 = graph.add_node(System::new()); + let node_2 = graph.add_node(System::new()); + let edge = graph.add_edge(node_1, node_2, Label::new()); assert!(matches!(tree.execute(&graph, &edge, &mut tr), Ok(AssertReturnValue::Integer(10)))); @@ -810,7 +829,8 @@ fn assignment_9() { #[test] fn assignment_10() { use translator::Translator; - use structure::{RSsystem, RSlabel}; + use system::System; + use label::Label; let tree = LocalAssert { tree: Tree::Concat( @@ -848,9 +868,9 @@ fn assignment_10() { let mut tr = Translator::new(); let mut graph = petgraph::Graph::new(); - let node_1 = graph.add_node(RSsystem::new()); - let node_2 = graph.add_node(RSsystem::new()); - let edge = graph.add_edge(node_1, node_2, RSlabel::new()); + let node_1 = graph.add_node(System::new()); + let node_2 = graph.add_node(System::new()); + let edge = graph.add_edge(node_1, node_2, Label::new()); assert!(matches!(tree.execute(&graph, &edge, &mut tr), Ok(AssertReturnValue::Integer(10)))); @@ -860,7 +880,9 @@ fn assignment_10() { #[test] fn for_1() { use translator::Translator; - use structure::{RSsystem, RSlabel, RSset}; + use set::Set; + use system::System; + use label::Label; let tree = LocalAssert { tree: Tree::Concat( @@ -868,12 +890,12 @@ fn for_1() { Box::new(Tree::Assignment( Variable::Id("a".into()), None, - Box::new(Expression::Set(RSset::new())) + Box::new(Expression::Set(Set::new())) )), Box::new(Tree::Assignment( Variable::Id("b".into()), None, - Box::new(Expression::Set(RSset::new())) + Box::new(Expression::Set(Set::new())) )), )), Box::new(Tree::For( @@ -897,9 +919,9 @@ fn for_1() { let mut tr = Translator::new(); let mut graph = petgraph::Graph::new(); - let node_1 = graph.add_node(RSsystem::new()); - let node_2 = graph.add_node(RSsystem::new()); - let edge = graph.add_edge(node_1, node_2, RSlabel::new()); + let node_1 = graph.add_node(System::new()); + let node_2 = graph.add_node(System::new()); + let edge = graph.add_edge(node_1, node_2, Label::new()); assert!(tree.execute(&graph, &edge, &mut tr).is_err()); } @@ -908,7 +930,9 @@ fn for_1() { #[test] fn for_2() { use translator::Translator; - use structure::{RSsystem, RSlabel, RSset}; + use set::Set; + use system::System; + use label::Label; let tree = LocalAssert { tree: Tree::Concat( @@ -916,12 +940,12 @@ fn for_2() { Box::new(Tree::Assignment( Variable::Id("a".into()), None, - Box::new(Expression::Set(RSset::from([1, 2]))) + Box::new(Expression::Set(Set::from([1, 2]))) )), Box::new(Tree::Assignment( Variable::Id("b".into()), None, - Box::new(Expression::Set(RSset::new())) + Box::new(Expression::Set(Set::new())) )), )), Box::new(Tree::For( @@ -949,9 +973,9 @@ fn for_2() { tr.encode("two"); let mut graph = petgraph::Graph::new(); - let node_1 = graph.add_node(RSsystem::new()); - let node_2 = graph.add_node(RSsystem::new()); - let edge = graph.add_edge(node_1, node_2, RSlabel::new()); + let node_1 = graph.add_node(System::new()); + let node_2 = graph.add_node(System::new()); + let edge = graph.add_edge(node_1, node_2, Label::new()); assert!(matches!(tree.execute(&graph, &edge, &mut tr), Ok(AssertReturnValue::Set(_)))); @@ -960,7 +984,9 @@ fn for_2() { #[test] fn for_3() { use translator::Translator; - use structure::{RSsystem, RSlabel, RSset}; + use set::Set; + use system::System; + use label::Label; let tree = LocalAssert { tree: Tree::Concat( @@ -969,20 +995,20 @@ fn for_3() { Variable::Id("a".into()), None, Box::new(Expression::Label( - Box::new(RSlabel::from(RSset::from([1, 2]), - RSset::new(), - RSset::from([1, 2]), - RSset::new(), - RSset::new(), - RSset::new(), - RSset::new(), - RSset::new())) + Box::new(Label::from(Set::from([1, 2]), + Set::new(), + Set::from([1, 2]), + Set::new(), + Set::new(), + Set::new(), + Set::new(), + Set::new())) )) )), Box::new(Tree::Assignment( Variable::Id("b".into()), None, - Box::new(Expression::Set(RSset::new())) + Box::new(Expression::Set(Set::new())) )), )), Box::new(Tree::For( @@ -1015,9 +1041,9 @@ fn for_3() { tr.encode("two"); let mut graph = petgraph::Graph::new(); - let node_1 = graph.add_node(RSsystem::new()); - let node_2 = graph.add_node(RSsystem::new()); - let edge = graph.add_edge(node_1, node_2, RSlabel::new()); + let node_1 = graph.add_node(System::new()); + let node_2 = graph.add_node(System::new()); + let edge = graph.add_edge(node_1, node_2, Label::new()); assert!(matches!(tree.execute(&graph, &edge, &mut tr), Ok(AssertReturnValue::Set(_)))); @@ -1026,7 +1052,9 @@ fn for_3() { #[test] fn for_4() { use translator::Translator; - use structure::{RSsystem, RSlabel, RSset}; + use set::Set; + use system::System; + use label::Label; let tree = LocalAssert { tree: Tree::Concat( @@ -1035,20 +1063,20 @@ fn for_4() { Variable::Id("a".into()), None, Box::new(Expression::Label( - Box::new(RSlabel::from(RSset::from([1, 2]), - RSset::from([3]), - RSset::from([1, 2, 3]), - RSset::new(), - RSset::new(), - RSset::new(), - RSset::new(), - RSset::new())) + Box::new(Label::from(Set::from([1, 2]), + Set::from([3]), + Set::from([1, 2, 3]), + Set::new(), + Set::new(), + Set::new(), + Set::new(), + Set::new())) )) )), Box::new(Tree::Assignment( Variable::Id("b".into()), None, - Box::new(Expression::Set(RSset::new())) + Box::new(Expression::Set(Set::new())) )), )), Box::new(Tree::For( @@ -1082,9 +1110,9 @@ fn for_4() { tr.encode("three"); let mut graph = petgraph::Graph::new(); - let node_1 = graph.add_node(RSsystem::new()); - let node_2 = graph.add_node(RSsystem::new()); - let edge = graph.add_edge(node_1, node_2, RSlabel::new()); + let node_1 = graph.add_node(System::new()); + let node_2 = graph.add_node(System::new()); + let edge = graph.add_edge(node_1, node_2, Label::new()); assert!(matches!(tree.execute(&graph, &edge, &mut tr), Ok(AssertReturnValue::Element(_)))); @@ -1093,7 +1121,9 @@ fn for_4() { #[test] fn for_5() { use translator::Translator; - use structure::{RSsystem, RSlabel, RSset}; + use set::Set; + use system::System; + use label::Label; let tree = LocalAssert { tree: Tree::Concat( @@ -1102,14 +1132,14 @@ fn for_5() { Variable::Id("a".into()), None, Box::new(Expression::Label( - Box::new(RSlabel::from(RSset::from([1, 2]), - RSset::from([3]), - RSset::from([1, 2, 3]), - RSset::new(), - RSset::new(), - RSset::new(), - RSset::new(), - RSset::new())) + Box::new(Label::from(Set::from([1, 2]), + Set::from([3]), + Set::from([1, 2, 3]), + Set::new(), + Set::new(), + Set::new(), + Set::new(), + Set::new())) )) )), Box::new(Tree::Assignment( @@ -1162,9 +1192,9 @@ fn for_5() { tr.encode("three"); let mut graph = petgraph::Graph::new(); - let node_1 = graph.add_node(RSsystem::new()); - let node_2 = graph.add_node(RSsystem::new()); - let edge = graph.add_edge(node_1, node_2, RSlabel::new()); + let node_1 = graph.add_node(System::new()); + let node_2 = graph.add_node(System::new()); + let edge = graph.add_edge(node_1, node_2, Label::new()); assert!(matches!(tree.execute(&graph, &edge, &mut tr), Ok(AssertReturnValue::Integer(3)))); @@ -1173,7 +1203,9 @@ fn for_5() { #[test] fn for_6() { use translator::Translator; - use structure::{RSsystem, RSlabel, RSset}; + use set::Set; + use system::System; + use label::Label; let tree = LocalAssert { tree: Tree::Concat( @@ -1182,21 +1214,21 @@ fn for_6() { Variable::Id("a".into()), None, Box::new(Expression::Label( - Box::new(RSlabel::from(RSset::from([1, 2]), - RSset::from([3]), - RSset::from([1, 2, 3]), - RSset::new(), - RSset::new(), - RSset::new(), - RSset::new(), - RSset::new())) + Box::new(Label::from(Set::from([1, 2]), + Set::from([3]), + Set::from([1, 2, 3]), + Set::new(), + Set::new(), + Set::new(), + Set::new(), + Set::new())) )) )), Box::new(Tree::Concat( Box::new(Tree::Assignment( Variable::Id("b".into()), None, - Box::new(Expression::Set(RSset::from([2]))) + Box::new(Expression::Set(Set::from([2]))) )), Box::new(Tree::Assignment( Variable::Id("c".into()), @@ -1252,9 +1284,9 @@ fn for_6() { tr.encode("three"); let mut graph = petgraph::Graph::new(); - let node_1 = graph.add_node(RSsystem::new()); - let node_2 = graph.add_node(RSsystem::new()); - let edge = graph.add_edge(node_1, node_2, RSlabel::new()); + let node_1 = graph.add_node(System::new()); + let node_2 = graph.add_node(System::new()); + let edge = graph.add_edge(node_1, node_2, Label::new()); assert!(matches!(tree.execute(&graph, &edge, &mut tr), @@ -1264,7 +1296,8 @@ fn for_6() { #[test] fn for_7() { use translator::Translator; - use structure::{RSsystem, RSlabel}; + use system::System; + use label::Label; let tree = LocalAssert { tree: Tree::Concat( @@ -1304,9 +1337,9 @@ fn for_7() { let mut tr = Translator::new(); let mut graph = petgraph::Graph::new(); - let node_1 = graph.add_node(RSsystem::new()); - let node_2 = graph.add_node(RSsystem::new()); - let edge = graph.add_edge(node_1, node_2, RSlabel::new()); + let node_1 = graph.add_node(System::new()); + let node_2 = graph.add_node(System::new()); + let edge = graph.add_edge(node_1, node_2, Label::new()); assert!(matches!(tree.execute(&graph, &edge, &mut tr), @@ -1316,7 +1349,8 @@ fn for_7() { #[test] fn for_8() { use translator::Translator; - use structure::{RSsystem, RSlabel}; + use system::System; + use label::Label; let tree = LocalAssert { tree: Tree::Concat( @@ -1372,9 +1406,9 @@ fn for_8() { let mut tr = Translator::new(); let mut graph = petgraph::Graph::new(); - let node_1 = graph.add_node(RSsystem::new()); - let node_2 = graph.add_node(RSsystem::new()); - let edge = graph.add_edge(node_1, node_2, RSlabel::new()); + let node_1 = graph.add_node(System::new()); + let node_2 = graph.add_node(System::new()); + let edge = graph.add_edge(node_1, node_2, Label::new()); assert!(matches!(tree.execute(&graph, &edge, &mut tr), Ok(AssertReturnValue::Integer(1)))); @@ -1383,11 +1417,11 @@ fn for_8() { let mut tr = Translator::new(); let mut graph = petgraph::Graph::new(); - let node_1 = graph.add_node(RSsystem::new()); - let node_2 = graph.add_node(RSsystem::new()); - let edge = graph.add_edge(node_1, node_2, RSlabel::new()); - let node_3 = graph.add_node(RSsystem::new()); - graph.add_edge(node_1, node_3, RSlabel::new()); + let node_1 = graph.add_node(System::new()); + let node_2 = graph.add_node(System::new()); + let edge = graph.add_edge(node_1, node_2, Label::new()); + let node_3 = graph.add_node(System::new()); + graph.add_edge(node_1, node_3, Label::new()); assert!(matches!(tree.execute(&graph, &edge, &mut tr), Ok(AssertReturnValue::Integer(2)))); @@ -1396,7 +1430,11 @@ fn for_8() { #[test] fn nodes() { use translator::Translator; - use structure::{RSsystem, RSlabel, RSset, RSenvironment, RSprocess}; + use set::Set; + use process::Process; + use environment::Environment; + use system::System; + use label::Label; use std::rc::Rc; let tree = LocalAssert { @@ -1427,7 +1465,7 @@ fn nodes() { Box::new(Expression::Var( Variable::Id("a".into()) )), - Box::new(Expression::Set(RSset::from([1, 2]))) + Box::new(Expression::Set(Set::from([1, 2]))) )) )), ) @@ -1438,16 +1476,16 @@ fn nodes() { let mut tr = Translator::new(); let mut graph = petgraph::Graph::new(); - let node_1 = graph.add_node(RSsystem::new()); + let node_1 = graph.add_node(System::new()); let node_2 = graph.add_node( - RSsystem::from( - Rc::new(RSenvironment::new()), - RSset::from([2]), - RSprocess::Nill, + System::from( + Rc::new(Environment::new()), + Set::from([2]), + Process::Nill, Rc::new(vec![]) ) ); - let edge = graph.add_edge(node_1, node_2, RSlabel::new()); + let edge = graph.add_edge(node_1, node_2, Label::new()); println!("{:?}", tree.execute(&graph, &edge, &mut tr)); diff --git a/src/rsprocess/choices.rs b/src/rsprocess/choices.rs index 987725e..a1f2c37 100644 --- a/src/rsprocess/choices.rs +++ b/src/rsprocess/choices.rs @@ -1,7 +1,8 @@ use std::rc::Rc; -use super::set::Set; use super::process::Process; +use super::set::Set; +use super::translator::{Translator, PrintableWithTranslator, Formatter}; #[derive(Clone, Debug)] pub struct Choices { @@ -98,3 +99,29 @@ impl From, Rc)>> for Choices { Choices { context_moves: arr } } } + +impl PrintableWithTranslator for Choices { + fn print(&self, f: &mut std::fmt::Formatter, translator: &Translator) + -> std::fmt::Result { + write!(f, "[")?; + let mut it = self.iter().peekable(); + while let Some(el) = it.next() { + if it.peek().is_none() { + write!( + f, + "[set: {}, process: {}]", + Formatter::from(translator, &*el.0), + Formatter::from(translator, &*el.1) + )?; + } else { + write!( + f, + "[set: {}, process: {}], ", + Formatter::from(translator, &*el.0), + Formatter::from(translator, &*el.1) + )?; + } + } + write!(f, "]") + } +} diff --git a/src/rsprocess/environment.rs b/src/rsprocess/environment.rs index 4366cbf..1b91ede 100644 --- a/src/rsprocess/environment.rs +++ b/src/rsprocess/environment.rs @@ -7,7 +7,7 @@ use super::choices::Choices; use super::process::Process; use super::reaction::Reaction; use super::set::Set; -use super::translator::IdType; +use super::translator::{IdType, Translator, PrintableWithTranslator, Formatter}; #[derive(Clone, Debug, Serialize, Deserialize)] pub struct Environment { @@ -154,6 +154,33 @@ impl From> for Environment { } } +impl PrintableWithTranslator for Environment { + fn print(&self, f: &mut std::fmt::Formatter, translator: &Translator) + -> std::fmt::Result { + write!(f, "{{env:")?; + let mut it = self.iter().peekable(); + while let Some(el) = it.next() { + if it.peek().is_none() { + write!( + f, + "({} -> {})", + translator.decode(*el.0).unwrap_or("Missing".into()), + Formatter::from(translator, el.1) + )?; + } else { + write!( + f, + "({} -> {}), ", + translator.decode(*el.0).unwrap_or("Missing".into()), + Formatter::from(translator, el.1) + )?; + } + } + write!(f, "}}") + } +} + + // ----------------------------------------------------------------------------- // Loops // ----------------------------------------------------------------------------- diff --git a/src/rsprocess/format_helpers.rs b/src/rsprocess/format_helpers.rs index be967d6..b90872e 100644 --- a/src/rsprocess/format_helpers.rs +++ b/src/rsprocess/format_helpers.rs @@ -1,10 +1,11 @@ pub mod graph_map_nodes_ty_from { - use super::super::structure::{RSsystem, RSset}; + use super::super::set::Set; + use super::super::system::System; use super::super::translator; use std::rc::Rc; type GraphMapNodesFnTy = - dyn Fn(petgraph::prelude::NodeIndex, &RSsystem) -> String; + dyn Fn(petgraph::prelude::NodeIndex, &System) -> String; pub fn format_string( s: String @@ -22,23 +23,23 @@ pub mod graph_map_nodes_ty_from { translator: Rc ) -> Box { Box::new( - move |_, node: &RSsystem| + move |_, node: &System| format!("{}", - translator::RSsetDisplay::from(&translator, - &node.available_entities)) + translator::Formatter::from(&translator, + &node.available_entities)) ) } pub fn format_mask_entities( translator: Rc, - mask: RSset + mask: Set ) -> Box { Box::new( - move |_, node: &RSsystem| { + move |_, node: &System| { let masked_entities = node.available_entities.intersection(&mask); format!("{}", - translator::RSsetDisplay::from(&translator, + translator::Formatter::from(&translator, &masked_entities)) } ) @@ -46,15 +47,15 @@ pub mod graph_map_nodes_ty_from { pub fn format_exclude_entities( translator: Rc, - mask: RSset + mask: Set ) -> Box { Box::new( - move |_, node: &RSsystem| { + move |_, node: &System| { let masked_entities = node.available_entities.subtraction(&mask); format!("{}", - translator::RSsetDisplay::from(&translator, - &masked_entities)) + translator::Formatter::from(&translator, + &masked_entities)) } ) } @@ -63,22 +64,23 @@ pub mod graph_map_nodes_ty_from { translator: Rc ) -> Box { Box::new( - move |_, node: &RSsystem| + move |_, node: &System| format!("{}", - translator::RSprocessDisplay::from(&translator, - &node.context_process)) + translator::Formatter::from(&translator, + &node.context_process)) ) } } pub mod graph_map_edges_ty_from { - use super::super::structure::{RSlabel, RSset}; + use super::super::set::Set; + use super::super::label::Label; use super::super::translator; use std::rc::Rc; type GraphMapEdgesFnTy<'a> = - dyn Fn(petgraph::prelude::EdgeIndex, &'a RSlabel) -> String + 'a; + dyn Fn(petgraph::prelude::EdgeIndex, &'a Label) -> String + 'a; pub fn format_string<'a>( _translator: Rc, @@ -104,23 +106,23 @@ pub mod graph_map_edges_ty_from { { pub fn $name<'a>( translator: Rc, - $mask_name: Option, - $common_name: Option + $mask_name: Option, + $common_name: Option ) -> Box> { if let Some($mask_name) = $mask_name { if let Some($common_name) = $common_name { Box::new( - move |_, $edge_name: &RSlabel| + move |_, $edge_name: &Label| format!("{}", - translator::RSsetDisplay::from( + translator::Formatter::from( &translator, $mask_common )) ) } else { Box::new( - move |_, $edge_name: &RSlabel| + move |_, $edge_name: &Label| format!("{}", - translator::RSsetDisplay::from( + translator::Formatter::from( &translator, $mask )) ) @@ -128,17 +130,17 @@ pub mod graph_map_edges_ty_from { } else { if let Some($common_name) = $common_name { Box::new( - move |_, $edge_name: &RSlabel| + move |_, $edge_name: &Label| format!("{}", - translator::RSsetDisplay::from( + translator::Formatter::from( &translator, $common )) ) } else { Box::new( - move |_, $edge_name: &RSlabel| + move |_, $edge_name: &Label| format!("{}", - translator::RSsetDisplay::from( + translator::Formatter::from( &translator, $default )) ) @@ -217,14 +219,15 @@ pub mod graph_map_edges_ty_from { pub mod node_formatter { - use super::super::translator::IdType; - use super::super::graph::{SystemGraph, OperationType}; use std::rc::Rc; - use super::super::structure::{RSset, RSprocess}; - use petgraph::{Graph, Directed}; use petgraph::visit::IntoNodeReferences; + use super::super::translator::IdType; + use super::super::graph::{SystemGraph, OperationType}; + use super::super::set::Set; + use super::super::process::Process; + type RSdotGraph = Graph; type RSformatNodeTy = @@ -241,7 +244,7 @@ pub mod node_formatter { Box::new( move |_, n| { let rssystem = original_graph.node_weight(n.0).unwrap(); - if rssystem.context_process == RSprocess::Nill { + if rssystem.context_process == Process::Nill { Some(", fillcolor=".to_string() + &color) } else { None @@ -260,11 +263,11 @@ pub mod node_formatter { move |_, n| { let rssystem = original_graph.node_weight(n.0).unwrap(); match (Some(s) == star, &rssystem.context_process) { - (true, RSprocess::RecursiveIdentifier { identifier: _ }) + (true, Process::RecursiveIdentifier { identifier: _ }) => { Some(", fillcolor=".to_string() + &color) }, - (false, RSprocess::RecursiveIdentifier { identifier: id }) + (false, Process::RecursiveIdentifier { identifier: id }) if id == &s => { Some(", fillcolor=".to_string() + &color) }, @@ -279,13 +282,13 @@ pub mod node_formatter { color: String, _star: Option, ot: OperationType, - set: RSset + set: Set ) -> Box { Box::new( move |_, n| { let rssystem = original_graph.node_weight(n.0).unwrap(); match &rssystem.context_process { - RSprocess::EntitySet { entities, next_process: _ } + Process::EntitySet { entities, next_process: _ } if ot.evaluate(entities, &set) => { Some(", fillcolor=".to_string() + &color) }, @@ -304,7 +307,7 @@ pub mod node_formatter { Box::new( move |_, n| { let rssystem = original_graph.node_weight(n.0).unwrap(); - if let RSprocess::NondeterministicChoice { children: _ } = + if let Process::NondeterministicChoice { children: _ } = rssystem.context_process { Some(", fillcolor=".to_string() + &color) @@ -323,7 +326,7 @@ pub mod node_formatter { Box::new( move |_, n| { let rssystem = original_graph.node_weight(n.0).unwrap(); - if let RSprocess::Summation { children: _ } = + if let Process::Summation { children: _ } = rssystem.context_process { Some(", fillcolor=".to_string() + &color) @@ -343,7 +346,7 @@ pub mod node_formatter { Box::new( move |_, n| { let rssystem = original_graph.node_weight(n.0).unwrap(); - if let RSprocess::WaitEntity { repeat: _, + if let Process::WaitEntity { repeat: _, repeated_process: _, next_process: _ } = &rssystem.context_process @@ -361,7 +364,7 @@ pub mod node_formatter { color: String, _star: Option, ot: OperationType, - set: RSset + set: Set ) -> Box { Box::new( move |_, n| { @@ -377,13 +380,14 @@ pub mod node_formatter { } pub mod edge_formatter { - use super::super::graph::{SystemGraph, OperationType}; use std::rc::Rc; - use super::super::structure::RSset; - use petgraph::{Graph, Directed}; use petgraph::visit::{IntoEdgeReferences, EdgeRef}; + use super::super::graph::{SystemGraph, OperationType}; + use super::super::set::Set; + + type RSdotGraph = Graph; type RSformatEdgeTy = dyn Fn( @@ -396,7 +400,7 @@ pub mod edge_formatter { original_graph: Rc, color: String, ot: OperationType, - set: RSset + set: Set ) -> Box { Box::new( move |_, e| { @@ -414,7 +418,7 @@ pub mod edge_formatter { original_graph: Rc, color: String, ot: OperationType, - set: RSset + set: Set ) -> Box { Box::new( move |_, e| { @@ -432,7 +436,7 @@ pub mod edge_formatter { original_graph: Rc, color: String, ot: OperationType, - set: RSset + set: Set ) -> Box { Box::new( move |_, e| { @@ -450,7 +454,7 @@ pub mod edge_formatter { original_graph: Rc, color: String, ot: OperationType, - set: RSset + set: Set ) -> Box { Box::new( move |_, e| { @@ -468,7 +472,7 @@ pub mod edge_formatter { original_graph: Rc, color: String, ot: OperationType, - set: RSset + set: Set ) -> Box { Box::new( move |_, e| { @@ -486,7 +490,7 @@ pub mod edge_formatter { original_graph: Rc, color: String, ot: OperationType, - set: RSset + set: Set ) -> Box { Box::new( move |_, e| { @@ -504,7 +508,7 @@ pub mod edge_formatter { original_graph: Rc, color: String, ot: OperationType, - set: RSset + set: Set ) -> Box { Box::new( move |_, e| { @@ -522,7 +526,7 @@ pub mod edge_formatter { original_graph: Rc, color: String, ot: OperationType, - set: RSset + set: Set ) -> Box { Box::new( move |_, e| { diff --git a/src/rsprocess/frequency.rs b/src/rsprocess/frequency.rs index a366260..88da04b 100644 --- a/src/rsprocess/frequency.rs +++ b/src/rsprocess/frequency.rs @@ -5,7 +5,7 @@ use std::collections::HashMap; use super::reaction::Reaction; use super::set::Set; use super::system::System; -use super::translator::IdType; +use super::translator::{IdType, Translator, PrintableWithTranslator, PRECISION}; /// structure that holds the frequency of elements of a run or multiple runs, /// weighted. To print use ```translator::FrequencyDisplay```. @@ -56,6 +56,53 @@ impl Default for Frequency { } } +impl PrintableWithTranslator for Frequency { + fn print(&self, f: &mut std::fmt::Formatter, translator: &Translator) + -> std::fmt::Result { + use std::cmp::max; + + write!(f, "[")?; + let mut freq_it = self.frequency_map.iter().peekable(); + + let totals = &self.totals; + let weights = &self.weights; + + while let Some((e, freq)) = freq_it.next() { + write!(f, "{} -> ", translator.decode(*e).unwrap_or("Missing".into()))?; + + let mut total_freq = 0.; + + let end = max(freq.len(), max(totals.len(), weights.len())); + + for pos in 0..end { + let freq_e = freq.get(pos).copied().unwrap_or(0) as f32; + let weight = weights.get(pos).copied().unwrap_or(1) as f32; + let total = totals.get(pos).copied().unwrap_or(1) as f32; + + let weighted_freq = (freq_e * weight * 100.) / (total); + if pos == end-1 { + #[allow(clippy::uninlined_format_args)] + write!(f, "{weighted_freq:.*}", PRECISION)?; + } else { + #[allow(clippy::uninlined_format_args)] + write!(f, "{weighted_freq:.*}, ", PRECISION)?; + } + total_freq += weighted_freq; + } + + total_freq /= self.total_weights() as f32; + + #[allow(clippy::uninlined_format_args)] + write!(f, " (total: {total_freq:.*})", PRECISION)?; + + if freq_it.peek().is_some() { + writeln!(f, ",")?; + } + } + write!(f, "]") + } +} + // ----------------------------------------------------------------------------- diff --git a/src/rsprocess/grammar.lalrpop b/src/rsprocess/grammar.lalrpop index 105b739..a8b06f6 100644 --- a/src/rsprocess/grammar.lalrpop +++ b/src/rsprocess/grammar.lalrpop @@ -1,12 +1,7 @@ use std::rc::Rc; use std::str::FromStr; use lalrpop_util::ParseError; -use crate::rsprocess::structure::{ RSset, - RSprocess, - RSenvironment, - RSsystem, - RSlabel, - RSreaction }; +use crate::rsprocess::{set, reaction, process, environment, system, label}; use crate::rsprocess::structure::rsassert; use crate::rsprocess::translator::{ Translator, IdType }; use crate::rsprocess::presets::Instructions; @@ -183,14 +178,14 @@ Separated_Or: Vec = { // ----------------------------------------------------------------------------- // SetParser // ----------------------------------------------------------------------------- -pub Set: RSset = { +pub Set: set::Set = { => s }; -Set_of_entities: RSset = { - "{" "}" => RSset::from(vec![]), +Set_of_entities: set::Set = { + "{" "}" => set::Set::from(vec![]), "{" > "}" => - RSset::from(t.into_iter().map(|t| translator.encode(t)) + set::Set::from(t.into_iter().map(|t| translator.encode(t)) .collect::>()) }; @@ -199,57 +194,58 @@ Set_of_entities: RSset = { // ReactionParser // ----------------------------------------------------------------------------- -pub Reactions: Vec = { +pub Reactions: Vec = { "(" ")" => vec![], "(" > ")" => s } -Reaction: RSreaction = { +Reaction: reaction::Reaction = { #[precedence(level="1")] - "[" "," "," "]" => RSreaction::from(r, i, p), + "[" "," "," "]" => + reaction::Reaction::from(r, i, p), #[precedence(level="0")] "[" "r:" "," "i:" "," "p:" "]" => - RSreaction::from(r, i, p), + reaction::Reaction::from(r, i, p), } // ----------------------------------------------------------------------------- // ContextParser // ----------------------------------------------------------------------------- -pub Context: RSprocess = { - "[" "]" => RSprocess::NondeterministicChoice{ children: vec![] }, +pub Context: process::Process = { + "[" "]" => process::Process::NondeterministicChoice{ children: vec![] }, "[" > "]" => - RSprocess::NondeterministicChoice{ children: t } + process::Process::NondeterministicChoice{ children: t } }; -Boxed_CTX_process: Rc = { +Boxed_CTX_process: Rc = { => Rc::new(t) } -CTX_process: RSprocess = { - "nill" => RSprocess::Nill, +CTX_process: process::Process = { + "nill" => process::Process::Nill, "." => - RSprocess::EntitySet{ entities: c, next_process: Rc::new(k) }, + process::Process::EntitySet{ entities: c, next_process: Rc::new(k) }, "(" ")" => k, "(" > ")" => - RSprocess::Summation{ + process::Process::Summation{ children: k.into_iter().map(Rc::new).collect::>() }, "?" "?" "." => - RSprocess::Guarded{ reaction: r, next_process: Rc::new(k) }, + process::Process::Guarded{ reaction: r, next_process: Rc::new(k) }, "<" "," ">" "." => - RSprocess::WaitEntity{ repeat: n, + process::Process::WaitEntity{ repeat: n, repeated_process: Rc::new(k1), next_process: Rc::new(k) }, => - RSprocess::RecursiveIdentifier{ + process::Process::RecursiveIdentifier{ identifier: translator.encode(identifier) } }; @@ -258,12 +254,14 @@ CTX_process: RSprocess = { // ----------------------------------------------------------------------------- // EnvironmentParser // ----------------------------------------------------------------------------- -pub Environment: Box = { - "[" "]" => Box::new(RSenvironment::new()), - "[" > "]" => Box::new(RSenvironment::from(t)) +pub Environment: Box = { + "[" "]" => + Box::new(environment::Environment::new()), + "[" > "]" => + Box::new(environment::Environment::from(t)) }; -Env_term: (IdType, RSprocess) = { +Env_term: (IdType, process::Process) = { "=" => (translator.encode(identifier), k) }; @@ -450,7 +448,7 @@ AssertBinaryPrefix: rsassert::Binary = { } -AssertLabel: RSlabel = { +AssertLabel: label::Label = { "[" "Entities" ":" "," "Context" ":" "," @@ -459,7 +457,7 @@ AssertLabel: RSlabel = { "Inhibitors" ":" "," "InhibitorsPresent" ":" "," "Products" ":" "," - "]" => RSlabel::create(e, c, r, r_a, i, i_p, p) + "]" => label::Label::create(e, c, r, r_a, i, i_p, p) } // ----------------------------------------------------------------------------- @@ -488,21 +486,21 @@ AssertLabel: RSlabel = { // system // a system is an environment, a set of entities as initial state, a context and // a set of reaction rules. -pub System: RSsystem = { +pub System: system::System = { "Environment" ":" "Initial Entities" ":" "Context" ":" "Reactions" ":" - => RSsystem::from(delta.into(), - available_entities, - context_process, - Rc::new(reaction_rules)) + => system::System::from(delta.into(), + available_entities, + context_process, + Rc::new(reaction_rules)) } // experiment // an experiment is composed by a sequence of weights and a sequence of sets of // entities of equal length. -pub Experiment: (Vec, Vec) = { +pub Experiment: (Vec, Vec) = { "Weights" ":" > "Sets" ":" > => (w.into_iter().map(|x| x as u32).collect::>(), s), @@ -860,12 +858,12 @@ Instruction: presets::Instruction = { pub Run: presets::Instructions = { #[precedence(level="0")] > => - Instructions { system: presets::System::RSsystem { sys }, + Instructions { system: presets::System::System { sys }, instructions: instr }, #[precedence(level="1")] => - Instructions { system: presets::System::RSsystem { sys }, + Instructions { system: presets::System::System { sys }, instructions: vec![] }, #[precedence(level="2")] diff --git a/src/rsprocess/label.rs b/src/rsprocess/label.rs index 9c1860d..88b59e6 100644 --- a/src/rsprocess/label.rs +++ b/src/rsprocess/label.rs @@ -1,7 +1,8 @@ -use std::hash::Hash; use serde::{Deserialize, Serialize}; +use std::hash::Hash; use super::set::Set; +use super::translator::{Translator, PrintableWithTranslator, Formatter}; #[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialOrd)] pub struct Label { @@ -115,3 +116,21 @@ impl Hash for Label { self.products.hash(state); } } + +impl PrintableWithTranslator for Label { + fn print(&self, f: &mut std::fmt::Formatter, translator: &Translator) + -> std::fmt::Result { + write!( + f, + "{{available_entities: {}, context: {}, t: {}, reactants: {}, reactantsi: {}, inihibitors: {}, ireactants: {}, products: {}}}", + Formatter::from(translator, &self.available_entities), + Formatter::from(translator, &self.context), + Formatter::from(translator, &self.t), + Formatter::from(translator, &self.reactants), + Formatter::from(translator, &self.reactants_absent), + Formatter::from(translator, &self.inhibitors), + Formatter::from(translator, &self.inhibitors_present), + Formatter::from(translator, &self.products), + ) + } +} diff --git a/src/rsprocess/mod.rs b/src/rsprocess/mod.rs index d626648..71ff7b7 100644 --- a/src/rsprocess/mod.rs +++ b/src/rsprocess/mod.rs @@ -15,13 +15,13 @@ pub mod frequency; mod format_helpers; -mod set; -mod reaction; -mod process; -mod choices; -mod environment; -mod system; -mod label; +pub mod set; +pub mod reaction; +pub mod process; +pub mod choices; +pub mod environment; +pub mod system; +pub mod label; #[cfg(test)] mod system_test; diff --git a/src/rsprocess/presets.rs b/src/rsprocess/presets.rs index fa01afc..def7db1 100644 --- a/src/rsprocess/presets.rs +++ b/src/rsprocess/presets.rs @@ -1,23 +1,21 @@ //! Module that holds useful presets for interacting with other modules. -use std::env; -use std::fmt::Display; -use std::fs; -use std::io; -use std::io::prelude::*; -use std::rc::Rc; - use lalrpop_util::ParseError; use petgraph::Graph; +use std::env; +use std::fmt::Display; +use std::fs; +use std::io::prelude::*; +use std::io; +use std::rc::Rc; -// grammar is defined in lib.rs, calling lalrpop_mod! twice, generates twice -// the code -use crate::grammar; -use crate::rsprocess::graph::MapEdges; - -use super::structure::{RSset, RSsystem}; -use super::translator::Translator; use super::*; +use super::graph::MapEdges; +use super::set::Set; +use super::system; +use super::translator::Translator; + +use super::super::grammar; // ----------------------------------------------------------------------------- // Structures @@ -96,7 +94,7 @@ pub enum Instruction { /// Describes a system or a graph. pub enum System { Deserialize { path: String }, - RSsystem { sys: RSsystem }, + System { sys: system::System }, } impl System { @@ -107,7 +105,7 @@ impl System { ) -> Result { match self { - Self::RSsystem { sys } => Ok(EvaluatedSystem::System { + Self::System { sys } => Ok(EvaluatedSystem::System { sys: sys.to_owned(), translator, }), @@ -125,7 +123,7 @@ pub enum EvaluatedSystem { translator: Translator, }, System { - sys: RSsystem, + sys: system::System, translator: Translator, }, } @@ -285,7 +283,7 @@ where fn parser_experiment( translator: &mut Translator, contents: String, -) -> Result<(Vec, Vec), String> { +) -> Result<(Vec, Vec), String> { match grammar::ExperimentParser::new().parse(translator, &contents) { Ok(sys) => Ok(sys), Err(e) => reformat_error(e, &contents), @@ -360,7 +358,7 @@ pub fn target(system: &EvaluatedSystem) -> Result { Ok(format!( "After {} steps we arrive at state:\n{}", res.0, - translator::RSsetDisplay::from(translator, &res.1) + translator::Formatter::from(translator, &res.1) )) } @@ -387,7 +385,7 @@ pub fn traversed(system: &EvaluatedSystem) -> Result { for (e, _c, _t) in res { output.push_str(&format!( "{}", - translator::RSsetDisplay::from(translator, &e) + translator::Formatter::from(translator, &e) )); } Ok(output) @@ -427,7 +425,7 @@ pub fn hoop( for e in res { output.push_str(&format!( "{}", - translator::RSsetDisplay::from(translator, &e) + translator::Formatter::from(translator, &e) )); } @@ -452,7 +450,7 @@ pub fn freq(system: &EvaluatedSystem) -> Result { Ok(format!( "Frequency of encountered symbols:\n{}", - translator::FrequencyDisplay::from(translator, &res) + translator::Formatter::from(translator, &res) )) } @@ -463,7 +461,7 @@ pub fn limit_freq( system: &mut EvaluatedSystem, experiment: String ) -> Result { - let (sys, translator): (&RSsystem, &mut Translator) = match system { + let (sys, translator): (&system::System, &mut Translator) = match system { EvaluatedSystem::System { sys, translator } => (sys, translator), EvaluatedSystem::Graph { graph, translator } => { let Some(sys) = graph.node_weights().next() else { @@ -489,7 +487,7 @@ pub fn limit_freq( Ok(format!( "Frequency of encountered symbols:\n{}", - translator::FrequencyDisplay::from(translator, &res) + translator::Formatter::from(translator, &res) )) } @@ -503,7 +501,7 @@ pub fn fast_freq( experiment: String ) -> Result { - let (sys, translator): (&RSsystem, &mut Translator) = match system { + let (sys, translator): (&system::System, &mut Translator) = match system { EvaluatedSystem::System { sys, translator } => (sys, translator), EvaluatedSystem::Graph { graph, translator } => { let Some(sys) = graph.node_weights().next() else { @@ -529,7 +527,7 @@ pub fn fast_freq( Ok(format!( "Frequency of encountered symbols:\n{}", - translator::FrequencyDisplay::from(translator, &res) + translator::Formatter::from(translator, &res) )) } @@ -579,9 +577,9 @@ pub fn bisimilar( match (system_a, &mut system_b) { (EvaluatedSystem::Graph { graph: a, translator: _ }, EvaluatedSystem::Graph { graph: b, translator: translator_b }) => { - let a: Graph = + let a: Graph = a.map_edges(edge_relabeler, translator_b)?; - let b: Graph = + let b: Graph = b.map_edges(edge_relabeler, translator_b)?; Ok(format!( "{}", diff --git a/src/rsprocess/process.rs b/src/rsprocess/process.rs index 25cfdf7..65d0fba 100644 --- a/src/rsprocess/process.rs +++ b/src/rsprocess/process.rs @@ -1,11 +1,11 @@ -use super::translator::IdType; +use serde::{Deserialize, Serialize}; use std::collections::VecDeque; use std::hash::Hash; use std::rc::Rc; -use serde::{Deserialize, Serialize}; -use super::set::Set; use super::reaction::Reaction; +use super::set::Set; +use super::translator::{IdType, Translator, PrintableWithTranslator, Formatter}; #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] pub enum Process { @@ -120,3 +120,83 @@ impl Process { None } } + +impl PrintableWithTranslator for Process { + fn print(&self, f: &mut std::fmt::Formatter, translator: &Translator) + -> std::fmt::Result { + match self { + Self::Nill => { + write!(f, "Nill") + }, + Self::RecursiveIdentifier { identifier } => { + write!(f, + "[{}]", + translator + .decode(*identifier) + .unwrap_or("Missing".into())) + }, + Self::EntitySet { entities, next_process, } => { + write!( + f, + "{}.{}", + Formatter::from(translator, entities), + Formatter::from(translator, &**next_process) + ) + }, + Self::Guarded { reaction, next_process } => { + write!(f, + "?{}?.{}", + Formatter::from(translator, reaction), + Formatter::from(translator, &**next_process)) + }, + Self::WaitEntity { repeat, repeated_process, next_process, } => { + write!( + f, + "({})^{repeat}.{}", + Formatter::from(translator, &**repeated_process), + Formatter::from(translator, &**next_process) + ) + } + Self::Summation { children } => { + write!(f, "[")?; + let mut it = children.iter().peekable(); + while let Some(child) = it.next() { + if it.peek().is_none() { + write!( + f, + "{}", + Formatter::from(translator, &**child) + )?; + } else { + write!( + f, + "{} + ", + Formatter::from(translator, &**child) + )?; + } + } + write!(f, "]") + } + Self::NondeterministicChoice { children } => { + write!(f, "[")?; + let mut it = children.iter().peekable(); + while let Some(child) = it.next() { + if it.peek().is_none() { + write!( + f, + "{}", + Formatter::from(translator, &**child) + )?; + } else { + write!( + f, + "{}, ", + Formatter::from(translator, &**child) + )?; + } + } + write!(f, "]") + } + } + } +} diff --git a/src/rsprocess/reaction.rs b/src/rsprocess/reaction.rs index 6d22f7e..582319c 100644 --- a/src/rsprocess/reaction.rs +++ b/src/rsprocess/reaction.rs @@ -3,9 +3,11 @@ //! Allows to define the 'classical' mechanism to compute in a Reaction System //! (RS) Framework. -use std::hash::Hash; use serde::{Deserialize, Serialize}; +use std::hash::Hash; + use super::set::Set; +use super::translator::{Translator, PrintableWithTranslator, Formatter}; /// Basic structure for a reaction. #[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)] @@ -149,3 +151,16 @@ impl Default for Reaction { Reaction::new() } } + +impl PrintableWithTranslator for Reaction { + fn print(&self, f: &mut std::fmt::Formatter, translator: &Translator) + -> std::fmt::Result { + write!( + f, + "(r: {}, i: {}, p: {})", + Formatter::from(translator, &self.reactants), + Formatter::from(translator, &self.inhibitors), + Formatter::from(translator, &self.products) + ) + } +} diff --git a/src/rsprocess/set.rs b/src/rsprocess/set.rs index 7635b1c..40a4d95 100644 --- a/src/rsprocess/set.rs +++ b/src/rsprocess/set.rs @@ -1,7 +1,9 @@ -use super::translator::IdType; +use serde::{Deserialize, Serialize}; use std::collections::BTreeSet; use std::hash::Hash; -use serde::{Deserialize, Serialize}; +use std::fmt; + +use super::translator::{IdType, Translator, PrintableWithTranslator}; /// Basic set of entities. #[derive(Clone, Debug, PartialOrd, Eq, Ord, Serialize, Deserialize)] @@ -139,3 +141,26 @@ impl IntoIterator for Set { self.identifiers.into_iter() } } + +impl PrintableWithTranslator for Set { + fn print( + &self, + f: &mut fmt::Formatter, + translator: &Translator, + ) -> fmt::Result { + write!(f, "{{")?; + let mut it = self.iter().peekable(); + while let Some(el) = it.next() { + if it.peek().is_none() { + write!(f, + "{}", + translator.decode(*el).unwrap_or("Missing".into()))?; + } else { + write!(f, + "{}, ", + translator.decode(*el).unwrap_or("Missing".into()))?; + } + } + write!(f, "}}") + } +} diff --git a/src/rsprocess/structure.rs b/src/rsprocess/structure.rs index 6cf3910..3cfa6d6 100644 --- a/src/rsprocess/structure.rs +++ b/src/rsprocess/structure.rs @@ -1,47 +1,5 @@ //! Module for all basic structures. -// ----------------------------------------------------------------------------- -// RSset -// ----------------------------------------------------------------------------- - -pub type RSset = super::set::Set; - -// ----------------------------------------------------------------------------- -// RSreaction -// ----------------------------------------------------------------------------- - -pub type RSreaction = super::reaction::Reaction; - -// ----------------------------------------------------------------------------- -// RSprocess -// ----------------------------------------------------------------------------- - -pub type RSprocess = super::process::Process; - -// ----------------------------------------------------------------------------- -// RSchoices -// ----------------------------------------------------------------------------- - -pub type RSchoices = super::choices::Choices; - -// ----------------------------------------------------------------------------- -// RSenvironment -// ----------------------------------------------------------------------------- - -pub type RSenvironment = super::environment::Environment; - -// ----------------------------------------------------------------------------- -// RSsystem -// ----------------------------------------------------------------------------- - -pub type RSsystem = super::system::System; - -// ----------------------------------------------------------------------------- -// RSlabel -// ----------------------------------------------------------------------------- - -pub type RSlabel = super::label::Label; - // ----------------------------------------------------------------------------- // RSassert // ----------------------------------------------------------------------------- @@ -57,17 +15,3 @@ pub mod assert { pub mod rsassert { pub use crate::rsprocess::assert::rsassert::useful_types_edge_relabeler::*; } - -// ----------------------------------------------------------------------------- -// RSBHML -// ----------------------------------------------------------------------------- -#[derive(Clone, Debug)] -#[allow(clippy::upper_case_acronyms)] -pub enum RSBHML { - True, - False, - Or(Vec), - And(Vec), - // Diamond(Box, Box), - // Box(Box, Box), -} diff --git a/src/rsprocess/system.rs b/src/rsprocess/system.rs index f459b40..064e4a3 100644 --- a/src/rsprocess/system.rs +++ b/src/rsprocess/system.rs @@ -9,11 +9,9 @@ use super::label::Label; use super::process::Process; use super::reaction::Reaction; use super::set::Set; - -use super::translator::IdType; -use super::translator::Translator; - use super::transitions::TransitionsIterator; +use super::translator::{IdType, Translator, PrintableWithTranslator, Formatter}; + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct System { @@ -219,6 +217,29 @@ impl Default for System { } } +impl PrintableWithTranslator for System { + fn print(&self, f: &mut std::fmt::Formatter, translator: &Translator) + -> std::fmt::Result { + write!( + f, + "[delta: {}, available_entities: {}, context_process: {}, \ + reaction_rules: [", + Formatter::from(translator, &*self.delta), + Formatter::from(translator, &self.available_entities), + Formatter::from(translator, &self.context_process) + )?; + let mut it = self.reaction_rules.iter().peekable(); + while let Some(el) = it.next() { + if it.peek().is_none() { + write!(f, "{}", Formatter::from(translator, el))?; + } else { + write!(f, "{}, ", Formatter::from(translator, el))?; + } + } + write!(f, "] ]") + } +} + // ----------------------------------------------------------------------------- // Loops @@ -378,7 +399,7 @@ impl System { )); result.push_str(&format!( "{}\n", - translator::SetDisplay::from(translator, &self.available_entities) + translator::Formatter::from(translator, &self.available_entities) )); let reactants = self @@ -388,7 +409,7 @@ impl System { result.push_str(&format!( "The reactants are {}:\n{}\n", reactants.len(), - translator::SetDisplay::from(translator, &reactants) + translator::Formatter::from(translator, &reactants) )); let inhibitors = self @@ -398,7 +419,7 @@ impl System { result.push_str(&format!( "The inhibitors are {}:\n{}\n", inhibitors.len(), - translator::SetDisplay::from(translator, &inhibitors) + translator::Formatter::from(translator, &inhibitors) )); let products = self @@ -408,28 +429,28 @@ impl System { result.push_str(&format!( "The products are {}:\n{}\n", products.len(), - translator::SetDisplay::from(translator, &products) + translator::Formatter::from(translator, &products) )); let total = reactants.union(&inhibitors.union(&products)); result.push_str(&format!( "The reactions involve {} entities:\n{}\n", total.len(), - translator::SetDisplay::from(translator, &total) + translator::Formatter::from(translator, &total) )); let entities_env = self.delta.all_elements(); result.push_str(&format!( "The environment involves {} entities:\n{}\n", entities_env.len(), - translator::SetDisplay::from(translator, &entities_env) + translator::Formatter::from(translator, &entities_env) )); let entities_context = self.context_process.all_elements(); result.push_str(&format!( "The context involves {} entities:\n{}\n", entities_context.len(), - translator::SetDisplay::from(translator, &entities_context) + translator::Formatter::from(translator, &entities_context) )); let entities_all = total @@ -440,7 +461,7 @@ impl System { result.push_str(&format!( "The whole RS involves {} entities:\n{}\n", entities_all.len(), - translator::SetDisplay::from(translator, &entities_all) + translator::Formatter::from(translator, &entities_all) )); let possible_e = products @@ -450,7 +471,7 @@ impl System { result.push_str(&format!( "There are {} reactants that will never be available:\n{}\n", missing_e.len(), - translator::SetDisplay::from(translator, &missing_e) + translator::Formatter::from(translator, &missing_e) )); let entities_not_needed = entities_context.subtraction(&total); @@ -458,7 +479,7 @@ impl System { "The context can provide {} entities that will never be used:\ \n{}\n", entities_not_needed.len(), - translator::SetDisplay::from(translator, &entities_not_needed) + translator::Formatter::from(translator, &entities_not_needed) )); result.push_str(&format!( diff --git a/src/rsprocess/transitions.rs b/src/rsprocess/transitions.rs index 28897c8..2f79d3e 100644 --- a/src/rsprocess/transitions.rs +++ b/src/rsprocess/transitions.rs @@ -1,17 +1,21 @@ //! Module for helper structure for simulation -use super::structure::{RSlabel, RSprocess, RSset, RSsystem}; use std::rc::Rc; +use super::label::Label; +use super::process::Process; +use super::set::Set; +use super::system::System; + #[derive(Clone, Debug)] pub struct TransitionsIterator<'a> { - choices_iterator: std::vec::IntoIter<(Rc, Rc)>, - system: &'a RSsystem, + choices_iterator: std::vec::IntoIter<(Rc, Rc)>, + system: &'a System, } impl<'a> TransitionsIterator<'a> { pub fn from( - system: &'a RSsystem + system: &'a System ) -> Result, String> { match system.delta.unfold(&system.context_process, &system.available_entities) { @@ -25,10 +29,10 @@ impl<'a> TransitionsIterator<'a> { } impl<'a> Iterator for TransitionsIterator<'a> { - type Item = (RSlabel, RSsystem); + type Item = (Label, System); /// Creates the next arc from the current system. - fn next(&mut self) -> Option<(RSlabel, RSsystem)> { + fn next(&mut self) -> Option<(Label, System)> { let (c, k) = self.choices_iterator.next()?; let t = self.system.available_entities.union(c.as_ref()); let ( @@ -40,11 +44,11 @@ impl<'a> Iterator for TransitionsIterator<'a> { ) = self.system.reaction_rules.iter().fold( ( - RSset::new(), // reactants - RSset::new(), // reactants_absent - RSset::new(), // inhibitors - RSset::new(), // inhibitors_present - RSset::new(), // products + Set::new(), // reactants + Set::new(), // reactants_absent + Set::new(), // inhibitors + Set::new(), // inhibitors_present + Set::new(), // products ), |acc, reaction| { if reaction.enabled(&t) { @@ -67,7 +71,7 @@ impl<'a> Iterator for TransitionsIterator<'a> { }, ); - let label = RSlabel::from( + let label = Label::from( self.system.available_entities.clone(), (*c).clone(), t, @@ -77,7 +81,7 @@ impl<'a> Iterator for TransitionsIterator<'a> { inhibitors_present, products.clone(), ); - let new_system = RSsystem::from( + let new_system = System::from( Rc::clone(&self.system.delta), products, (*k).clone(), diff --git a/src/rsprocess/translator.rs b/src/rsprocess/translator.rs index 9ff6086..b8610e4 100644 --- a/src/rsprocess/translator.rs +++ b/src/rsprocess/translator.rs @@ -1,10 +1,10 @@ //! Module for translation and keeping track of strings. -use std::{cmp::max, collections::HashMap}; +use std::collections::HashMap; use serde::{Serialize, Deserialize}; /// precision for printing frequencies -static PRECISION: &usize = &2; +pub static PRECISION: &usize = &2; pub type IdType = u32; @@ -25,28 +25,7 @@ impl Translator { last_id: 1, } } -} -impl Default for Translator { - fn default() -> Self { - Translator::new() - } -} - -impl PartialEq for Translator { - fn eq(&self, other: &Self) -> bool { - for (s, id) in self.strings.iter() { - match other.strings.get(s) { - None => return false, - Some(id2) if id != id2 => return false, - _ => {} - } - } - true - } -} - -impl Translator { /// converts a string into an id pub fn encode(&mut self, s: impl Into) -> IdType { let s = s.into(); @@ -70,14 +49,66 @@ impl Translator { } } +impl Default for Translator { + fn default() -> Self { + Translator::new() + } +} + +impl PartialEq for Translator { + fn eq(&self, other: &Self) -> bool { + for (s, id) in self.strings.iter() { + match other.strings.get(s) { + None => return false, + Some(id2) if id != id2 => return false, + _ => {} + } + } + true + } +} + + // ----------------------------------------------------------------------------- // print structures // ----------------------------------------------------------------------------- + +pub trait PrintableWithTranslator { + fn print(&self, f: &mut fmt::Formatter, translator: &Translator) + -> fmt::Result; +} + +pub struct Formatter<'a, T> { + data: &'a T, + translator: &'a Translator, +} + +impl<'a, T> fmt::Display for Formatter<'a, T> +where + T: PrintableWithTranslator +{ + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.data.print(f, self.translator) + } +} + +impl<'a, T> Formatter<'a, T> { + pub fn from( + translator: &'a Translator, + data: &'a T + ) -> Self { + Self { data, translator } + } +} + + + + + + use super::{ - frequency::Frequency, structure::{ - RSBHML, RSassert, RSchoices, RSenvironment, RSlabel, - RSprocess, RSreaction, RSset, RSsystem, + RSassert }, }; use std::fmt; @@ -107,259 +138,6 @@ macro_rules! translator_structure { } - -// RSset - -fn print_set( - f: &mut fmt::Formatter, - translator: &Translator, - set: &RSset -) -> fmt::Result { - write!(f, "{{")?; - let mut it = set.iter().peekable(); - while let Some(el) = it.next() { - if it.peek().is_none() { - write!(f, - "{}", - translator.decode(*el).unwrap_or("Missing".into()))?; - } else { - write!(f, - "{}, ", - translator.decode(*el).unwrap_or("Missing".into()))?; - } - } - write!(f, "}}") -} - -translator_structure!(SetDisplay, RSset, set, print_set); -translator_structure!(RSsetDisplay, RSset, set, print_set); - - - -// RSreaction - -fn print_reaction( - f: &mut fmt::Formatter, - translator: &Translator, - reaction: &RSreaction, -) -> fmt::Result { - write!( - f, - "(r: {}, i: {}, p: {})", - RSsetDisplay::from(translator, &reaction.reactants), - RSsetDisplay::from(translator, &reaction.inhibitors), - RSsetDisplay::from(translator, &reaction.products) - ) -} - -translator_structure!(RSreactionDisplay, RSreaction, reaction, print_reaction); - - - -// RSprocess - -fn print_process( - f: &mut fmt::Formatter, - translator: &Translator, - process: &RSprocess, -) -> fmt::Result { - use super::process::Process::*; - match process { - Nill => { - write!(f, "Nill") - }, - RecursiveIdentifier { identifier } => { - write!(f, - "[{}]", - translator.decode(*identifier).unwrap_or("Missing".into())) - }, - EntitySet { entities, next_process, } => { - write!( - f, - "{}.{}", - RSsetDisplay::from(translator, entities), - RSprocessDisplay::from(translator, next_process) - ) - }, - Guarded { reaction, next_process } => { - write!(f, - "?{}?.{}", - RSreactionDisplay::from(translator, reaction), - RSprocessDisplay::from(translator, next_process)) - }, - WaitEntity { repeat, repeated_process, next_process, } => { - write!( - f, - "({})^{repeat}.{}", - RSprocessDisplay::from(translator, repeated_process), - RSprocessDisplay::from(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, - "{}", - RSprocessDisplay::from(translator, child) - )?; - } else { - write!( - f, - "{} + ", - RSprocessDisplay::from(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, - "{}", - RSprocessDisplay::from(translator, child) - )?; - } else { - write!( - f, - "{}, ", - RSprocessDisplay::from(translator, child) - )?; - } - } - write!(f, "]") - } - } -} - -translator_structure!(RSprocessDisplay, RSprocess, process, print_process); - - - -// RSchoices - -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: {}]", - RSsetDisplay::from(translator, &el.0), - RSprocessDisplay::from(translator, &el.1) - )?; - } else { - write!( - f, - "[set: {}, process: {}], ", - RSsetDisplay::from(translator, &el.0), - RSprocessDisplay::from(translator, &el.1) - )?; - } - } - write!(f, "]") -} - -translator_structure!(RSchoicesDisplay, RSchoices, choices, print_choices); - - - -// RSenvironment - -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).unwrap_or("Missing".into()), - RSprocessDisplay::from(translator, el.1) - )?; - } else { - write!( - f, - "({} -> {}), ", - translator.decode(*el.0).unwrap_or("Missing".into()), - RSprocessDisplay::from(translator, el.1) - )?; - } - } - write!(f, "}}") -} - -translator_structure!(RSenvironmentDisplay, RSenvironment, environment, print_environment); - - - -// RSsystem - -fn print_system( - f: &mut fmt::Formatter, - translator: &Translator, - system: &RSsystem -) -> fmt::Result { - write!( - f, - "[delta: {}, available_entities: {}, context_process: {}, reaction_rules: [", - RSenvironmentDisplay::from(translator, &system.delta), - RSsetDisplay::from(translator, &system.available_entities), - RSprocessDisplay::from(translator, &system.context_process) - )?; - let mut it = system.reaction_rules.iter().peekable(); - while let Some(el) = it.next() { - if it.peek().is_none() { - write!(f, "{}", RSreactionDisplay::from(translator, el))?; - } else { - write!(f, "{}, ", RSreactionDisplay::from(translator, el))?; - } - } - write!(f, "] ]") -} - -translator_structure!(RSsystemDisplay, RSsystem, system, print_system); - - - -// RSlabel - -fn print_label( - f: &mut fmt::Formatter, - translator: &Translator, - label: &RSlabel -) -> fmt::Result { - write!( - f, - "{{available_entities: {}, context: {}, t: {}, reactants: {}, reactantsi: {}, inihibitors: {}, ireactants: {}, products: {}}}", - RSsetDisplay::from(translator, &label.available_entities), - RSsetDisplay::from(translator, &label.context), - RSsetDisplay::from(translator, &label.t), - RSsetDisplay::from(translator, &label.reactants), - RSsetDisplay::from(translator, &label.reactants_absent), - RSsetDisplay::from(translator, &label.inhibitors), - RSsetDisplay::from(translator, &label.inhibitors_present), - RSsetDisplay::from(translator, &label.products), - ) -} - -translator_structure!(RSlabelDisplay, RSlabel, label, print_label); - // RSassert #[allow(unused_variables)] @@ -372,70 +150,3 @@ fn print_assert( } translator_structure!(RSassertDisplay, RSassert, assert, print_assert); - - - -// RSBHML - -#[allow(unused_variables)] -fn print_bhml( - f: &mut fmt::Formatter, - translator: &Translator, - bhml: &RSBHML -) -> fmt::Result { - todo!() -} - -translator_structure!(RSBHMLDisplay, RSBHML, bhml, print_bhml); - - -// Frequency - -fn print_frequency( - f: &mut fmt::Formatter, - translator: &Translator, - frequency: &Frequency, -) -> fmt::Result { - write!(f, "[")?; - let mut freq_it = frequency.frequency_map.iter().peekable(); - - let totals = &frequency.totals; - let weights = &frequency.weights; - - while let Some((e, freq)) = freq_it.next() { - write!(f, "{} -> ", translator.decode(*e).unwrap_or("Missing".into()))?; - - let mut total_freq = 0.; - - let end = max(freq.len(), max(totals.len(), weights.len())); - - for pos in 0..end { - let freq_e = freq.get(pos).copied().unwrap_or(0) as f32; - let weight = weights.get(pos).copied().unwrap_or(1) as f32; - let total = totals.get(pos).copied().unwrap_or(1) as f32; - - let weighted_freq = (freq_e * weight * 100.) / (total); - if pos == end-1 { - #[allow(clippy::uninlined_format_args)] - write!(f, "{weighted_freq:.*}", PRECISION)?; - } else { - #[allow(clippy::uninlined_format_args)] - write!(f, "{weighted_freq:.*}, ", PRECISION)?; - } - total_freq += weighted_freq; - } - - total_freq /= frequency.total_weights() as f32; - - #[allow(clippy::uninlined_format_args)] - write!(f, " (total: {total_freq:.*})", PRECISION)?; - - if freq_it.peek().is_some() { - writeln!(f, ",")?; - } - } - write!(f, "]") -} - -translator_structure!(FrequencyDisplay, Frequency, frequency, print_frequency); -