2025-07-11 19:36:20 +02:00
|
|
|
//! Module that holds useful presets for interacting with other modules.
|
|
|
|
|
|
|
|
|
|
use std::env;
|
2025-07-11 23:49:59 +02:00
|
|
|
use std::fmt::Display;
|
2025-07-11 19:36:20 +02:00
|
|
|
use std::fs;
|
|
|
|
|
use std::io;
|
|
|
|
|
use std::io::prelude::*;
|
|
|
|
|
use std::rc::Rc;
|
|
|
|
|
|
|
|
|
|
use lalrpop_util::ParseError;
|
|
|
|
|
use petgraph::Graph;
|
|
|
|
|
|
|
|
|
|
// grammar is defined in lib.rs, calling lalrpop_mod! twice, generates twice
|
|
|
|
|
// the code
|
|
|
|
|
use crate::grammar;
|
|
|
|
|
|
|
|
|
|
use super::structure::RSlabel;
|
|
|
|
|
use super::structure::{RSset, RSsystem};
|
|
|
|
|
use super::translator::Translator;
|
|
|
|
|
use super::*;
|
|
|
|
|
|
2025-07-11 23:49:59 +02:00
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
// Structures
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub struct SaveOptions {
|
|
|
|
|
pub print: bool,
|
|
|
|
|
pub save: Option<Vec<String>>
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl SaveOptions {
|
|
|
|
|
pub fn combine(&mut self, other: &mut Self) {
|
|
|
|
|
self.print = self.print || other.print;
|
|
|
|
|
match (self.save.is_some(), other.save.is_some()) {
|
|
|
|
|
(false, false) |
|
|
|
|
|
(true, false) => {}
|
|
|
|
|
(false, true) => {
|
|
|
|
|
self.save = other.save.to_owned();
|
|
|
|
|
},
|
|
|
|
|
(true, true) => {
|
|
|
|
|
self.save
|
|
|
|
|
.as_mut()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.append(other.save.as_mut().unwrap());}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
pub fn new() -> Self {
|
|
|
|
|
SaveOptions { print: false, save: None }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Default for SaveOptions {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
SaveOptions::new()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub enum GraphSaveOptions {
|
|
|
|
|
Dot { so: SaveOptions },
|
|
|
|
|
GraphML { so: SaveOptions },
|
|
|
|
|
Serialize { path: String }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub enum Instruction {
|
|
|
|
|
Stats { so: SaveOptions },
|
|
|
|
|
Target { so: SaveOptions },
|
|
|
|
|
Run { so: SaveOptions },
|
|
|
|
|
Loop { symbol: String, so: SaveOptions },
|
2025-07-12 02:42:28 +02:00
|
|
|
Frequency { so: SaveOptions },
|
2025-07-11 23:49:59 +02:00
|
|
|
LimitFrequency { experiment: String, so: SaveOptions },
|
|
|
|
|
FastFrequency { experiment: String, so: SaveOptions },
|
|
|
|
|
Digraph { gso: Vec<GraphSaveOptions> },
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub enum System {
|
|
|
|
|
Deserialize { path: String },
|
|
|
|
|
RSsystem { sys: RSsystem }
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-12 02:42:28 +02:00
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub enum EvaluatedSystem {
|
|
|
|
|
Graph { graph: Graph<RSsystem, RSlabel>,
|
|
|
|
|
translator: Translator },
|
|
|
|
|
System { sys: RSsystem,
|
|
|
|
|
translator: Translator }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl System {
|
|
|
|
|
pub fn compute(
|
|
|
|
|
&self, translator: Translator
|
|
|
|
|
) -> Result<EvaluatedSystem, String>
|
|
|
|
|
{
|
|
|
|
|
match self {
|
|
|
|
|
Self::RSsystem { sys } => {
|
|
|
|
|
Ok(EvaluatedSystem::System { sys: sys.to_owned(), translator })
|
|
|
|
|
},
|
|
|
|
|
Self::Deserialize { path } => {
|
|
|
|
|
let (graph, translator) = deserialize(path.into())?;
|
|
|
|
|
Ok(EvaluatedSystem::Graph { graph, translator })
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-11 23:49:59 +02:00
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub struct Instructions {
|
|
|
|
|
pub system: System,
|
|
|
|
|
pub instructions: Vec<Instruction>
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-11 19:36:20 +02:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
// Helper Functions
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
fn read_file<T, F>(
|
|
|
|
|
translator: &mut Translator,
|
|
|
|
|
path_string: String,
|
|
|
|
|
parser: F
|
|
|
|
|
) -> Result<T, String>
|
|
|
|
|
where
|
|
|
|
|
F: Fn(&mut Translator, String) -> Result<T, String>
|
|
|
|
|
{
|
|
|
|
|
// relative path
|
|
|
|
|
let mut path = match env::current_dir() {
|
|
|
|
|
Ok(p) => p,
|
|
|
|
|
Err(_) => return Err("Error getting current directory.".into())
|
|
|
|
|
};
|
|
|
|
|
path = path.join(path_string);
|
|
|
|
|
|
|
|
|
|
// we read the file with a buffer
|
|
|
|
|
let f = match fs::File::open(path) {
|
|
|
|
|
Ok(f) => f,
|
|
|
|
|
Err(_) => return Err("Error opening file.".into())
|
|
|
|
|
};
|
|
|
|
|
let mut buf_reader = io::BufReader::new(f);
|
|
|
|
|
let mut contents = String::new();
|
|
|
|
|
match buf_reader.read_to_string(&mut contents) {
|
|
|
|
|
Ok(_) => {},
|
|
|
|
|
Err(_) => return Err("Error reading file.".into())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// parse
|
|
|
|
|
let result = parser(translator, contents)?;
|
|
|
|
|
|
|
|
|
|
Ok(result)
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-11 23:49:59 +02:00
|
|
|
fn reformat_error<T, S>(
|
|
|
|
|
e: ParseError<usize, T, &'static str>
|
|
|
|
|
) -> Result<S, String>
|
|
|
|
|
where
|
|
|
|
|
T: Display
|
2025-07-11 19:36:20 +02:00
|
|
|
{
|
2025-07-11 23:49:59 +02:00
|
|
|
match e {
|
|
|
|
|
ParseError::ExtraToken { token: (l, t, r) } => {
|
|
|
|
|
Err(format!(
|
|
|
|
|
"Unexpected token \"{t}\" \
|
|
|
|
|
between positions {l} and {r}."
|
|
|
|
|
))
|
|
|
|
|
},
|
|
|
|
|
ParseError::UnrecognizedEof { location: _, expected: _ } => {
|
|
|
|
|
Err("End of file encountered while parsing.".into())
|
|
|
|
|
},
|
|
|
|
|
ParseError::InvalidToken { location } => {
|
|
|
|
|
Err(format!("Invalid token at position {location}."))
|
|
|
|
|
},
|
|
|
|
|
ParseError::UnrecognizedToken { token: (l, t, r), expected }
|
|
|
|
|
=> {
|
|
|
|
|
Err(format!(
|
|
|
|
|
"Unrecognized token \"{t}\" \
|
|
|
|
|
between positions {l} and {r}. Expected: {expected:?}"
|
|
|
|
|
))
|
|
|
|
|
},
|
|
|
|
|
ParseError::User { error } => {
|
|
|
|
|
Err(error.to_string())
|
|
|
|
|
}
|
2025-07-11 19:36:20 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn parser_experiment(
|
|
|
|
|
translator: &mut Translator,
|
|
|
|
|
contents: String
|
|
|
|
|
) -> Result<(Vec<u32>, Vec<RSset>), String>
|
|
|
|
|
{
|
|
|
|
|
match grammar::ExperimentParser::new()
|
|
|
|
|
.parse(translator, &contents)
|
|
|
|
|
{
|
|
|
|
|
Ok(sys) => Ok(sys),
|
2025-07-11 23:49:59 +02:00
|
|
|
Err(e) => reformat_error(e)
|
2025-07-11 19:36:20 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-11 23:49:59 +02:00
|
|
|
fn parser_instructions(
|
|
|
|
|
translator: &mut Translator,
|
|
|
|
|
contents: String
|
|
|
|
|
) -> Result<Instructions, String>
|
|
|
|
|
{
|
|
|
|
|
match grammar::RunParser::new()
|
|
|
|
|
.parse(translator, &contents)
|
|
|
|
|
{
|
|
|
|
|
Ok(sys) => Ok(sys),
|
|
|
|
|
Err(e) => reformat_error(e)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn save_file(
|
2025-07-12 02:42:28 +02:00
|
|
|
contents: &String,
|
|
|
|
|
path_string: String
|
2025-07-11 23:49:59 +02:00
|
|
|
) -> Result<(), String>
|
|
|
|
|
{
|
|
|
|
|
// relative path
|
|
|
|
|
let mut path = match env::current_dir() {
|
|
|
|
|
Ok(p) => p,
|
|
|
|
|
Err(_) => return Err("Error getting current directory.".into())
|
|
|
|
|
};
|
|
|
|
|
path = path.join(path_string);
|
|
|
|
|
|
|
|
|
|
let mut f = match fs::File::create(&path) {
|
|
|
|
|
Ok(f) => f,
|
|
|
|
|
Err(_) => return Err(format!("Error creating file {}.",
|
|
|
|
|
path.to_str().unwrap()))
|
|
|
|
|
};
|
|
|
|
|
match write!(f, "{contents}") {
|
|
|
|
|
Ok(_) => {}
|
|
|
|
|
Err(_) => return Err("Error writing to file.".into())
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-11 19:36:20 +02:00
|
|
|
|
2025-07-12 02:42:28 +02:00
|
|
|
|
2025-07-11 19:36:20 +02:00
|
|
|
// -----------------------------------------------------------------------------
|
2025-07-11 23:49:59 +02:00
|
|
|
// main_do
|
2025-07-11 19:36:20 +02:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
/// Prints statistics of the system.
|
|
|
|
|
/// Equivalent main_do(stat) or main_do(stat, MissingE)
|
2025-07-12 02:42:28 +02:00
|
|
|
pub fn stats(system: &EvaluatedSystem) -> Result<String, String> {
|
|
|
|
|
match system {
|
|
|
|
|
EvaluatedSystem::System { sys, translator } => {
|
|
|
|
|
Ok(statistics::of_RSsystem(translator, sys))
|
|
|
|
|
},
|
|
|
|
|
EvaluatedSystem::Graph { graph, translator } => {
|
|
|
|
|
let Some(sys) = graph.node_weights().next()
|
|
|
|
|
else {
|
|
|
|
|
return Err("No node found in graph".into());
|
|
|
|
|
};
|
|
|
|
|
Ok(statistics::of_RSsystem(translator, sys))
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-07-11 19:36:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Prints a final set of entities in a terminating Reaction System.
|
2025-07-12 02:42:28 +02:00
|
|
|
/// The system needs to terminate to return.
|
2025-07-11 19:36:20 +02:00
|
|
|
/// Equivalent to main_do(target, E)
|
2025-07-12 02:42:28 +02:00
|
|
|
pub fn target(system: &EvaluatedSystem) -> Result<String, String> {
|
|
|
|
|
let (res, translator) = match system {
|
|
|
|
|
EvaluatedSystem::System { sys, translator } => {
|
|
|
|
|
(transitions::target(sys)?, translator)
|
|
|
|
|
},
|
|
|
|
|
EvaluatedSystem::Graph { graph, translator } => {
|
|
|
|
|
let Some(sys) = graph.node_weights().next()
|
|
|
|
|
else {
|
|
|
|
|
return Err("No node found in graph".into());
|
|
|
|
|
};
|
|
|
|
|
(transitions::target(sys)?, translator)
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
Ok(format!(
|
2025-07-11 19:36:20 +02:00
|
|
|
"After {} steps we arrive at state:\n{}",
|
|
|
|
|
res.0,
|
2025-07-12 02:42:28 +02:00
|
|
|
translator::RSsetDisplay::from(translator, &res.1)
|
|
|
|
|
))
|
2025-07-11 19:36:20 +02:00
|
|
|
}
|
|
|
|
|
|
2025-07-11 23:49:59 +02:00
|
|
|
|
2025-07-11 19:36:20 +02:00
|
|
|
/// Finds the list of traversed states in a (deterministic) terminating
|
|
|
|
|
/// reaction.
|
2025-07-12 02:42:28 +02:00
|
|
|
/// The system needs to terminate to return.
|
2025-07-11 19:36:20 +02:00
|
|
|
/// equivalent to main_do(run,Es)
|
2025-07-12 02:42:28 +02:00
|
|
|
pub fn traversed(system: &EvaluatedSystem) -> Result<String, String> {
|
|
|
|
|
let (res, translator) = match system {
|
|
|
|
|
EvaluatedSystem::System { sys, translator } => {
|
|
|
|
|
(transitions::run_separated(sys)?, translator)
|
|
|
|
|
},
|
|
|
|
|
EvaluatedSystem::Graph { graph, translator } => {
|
|
|
|
|
let Some(sys) = graph.node_weights().next()
|
|
|
|
|
else {
|
|
|
|
|
return Err("No node found in graph".into());
|
|
|
|
|
};
|
|
|
|
|
(transitions::run_separated(sys)?, translator)
|
|
|
|
|
}
|
|
|
|
|
};
|
2025-07-11 19:36:20 +02:00
|
|
|
|
2025-07-12 02:42:28 +02:00
|
|
|
let mut output = String::new();
|
2025-07-11 19:36:20 +02:00
|
|
|
|
2025-07-12 02:42:28 +02:00
|
|
|
output.push_str("The trace is composed by the set of entities:");
|
2025-07-11 19:36:20 +02:00
|
|
|
for (e, _c, _t) in res {
|
2025-07-12 02:42:28 +02:00
|
|
|
output.push_str(
|
|
|
|
|
&format!(
|
|
|
|
|
"{}",
|
|
|
|
|
translator::RSsetDisplay::from(translator, &e)
|
|
|
|
|
)
|
|
|
|
|
);
|
2025-07-11 19:36:20 +02:00
|
|
|
}
|
2025-07-12 02:42:28 +02:00
|
|
|
Ok(output)
|
2025-07-11 19:36:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Finds the looping list of states in a reaction system with a perpetual
|
2025-07-11 23:49:59 +02:00
|
|
|
/// context. IMPORTANT: for loops, we assume Delta defines the process constant
|
2025-07-11 19:36:20 +02:00
|
|
|
/// x = Q.x and the context process is x .
|
|
|
|
|
/// equivalent to main_do(loop,Es)
|
2025-07-12 02:42:28 +02:00
|
|
|
pub fn hoop(
|
|
|
|
|
system: &EvaluatedSystem, symbol: String
|
|
|
|
|
) -> Result<String, String>
|
|
|
|
|
{
|
|
|
|
|
let (res, translator) = match system {
|
|
|
|
|
EvaluatedSystem::System { sys, translator } => {
|
|
|
|
|
(sys, translator)
|
|
|
|
|
},
|
|
|
|
|
EvaluatedSystem::Graph { graph, translator } => {
|
|
|
|
|
let Some(sys) = graph.node_weights().next()
|
|
|
|
|
else {
|
|
|
|
|
return Err("No node found in graph".into());
|
|
|
|
|
};
|
|
|
|
|
(sys, translator)
|
2025-07-11 19:36:20 +02:00
|
|
|
}
|
|
|
|
|
};
|
2025-07-12 02:42:28 +02:00
|
|
|
// we retrieve the id for "x" and use it to find the corresponding loop
|
|
|
|
|
let Some(id) = translator.encode_not_mut(&symbol)
|
|
|
|
|
else {
|
|
|
|
|
return Err(format!("Symbol {symbol} not found"));
|
|
|
|
|
};
|
|
|
|
|
let res =
|
|
|
|
|
match perpetual::lollipops_only_loop_named(res, id) {
|
|
|
|
|
Some(o) => o,
|
|
|
|
|
None => {
|
|
|
|
|
return Err("No loop found.".into());
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let mut output = String::new();
|
2025-07-11 19:36:20 +02:00
|
|
|
|
2025-07-12 02:42:28 +02:00
|
|
|
output.push_str("The loop is composed by the sets:");
|
2025-07-11 19:36:20 +02:00
|
|
|
for e in res {
|
2025-07-12 02:42:28 +02:00
|
|
|
output.push_str(
|
|
|
|
|
&format!( "{}", translator::RSsetDisplay::from(translator, &e))
|
|
|
|
|
);
|
2025-07-11 19:36:20 +02:00
|
|
|
}
|
|
|
|
|
|
2025-07-12 02:42:28 +02:00
|
|
|
Ok(output)
|
2025-07-11 19:36:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Finds the frequency of each entity in the traversed states for a
|
|
|
|
|
/// (deterministic) terminating Reaction System.
|
|
|
|
|
/// equivalent to main_do(freq, PairList)
|
2025-07-12 02:42:28 +02:00
|
|
|
pub fn freq(
|
|
|
|
|
system: &EvaluatedSystem
|
|
|
|
|
) -> Result<String, String> {
|
|
|
|
|
let (sys, translator) = match system {
|
|
|
|
|
EvaluatedSystem::System { sys, translator } => {
|
|
|
|
|
(sys, translator)
|
|
|
|
|
},
|
|
|
|
|
EvaluatedSystem::Graph { graph, translator } => {
|
|
|
|
|
let Some(sys) = graph.node_weights().next()
|
|
|
|
|
else {
|
|
|
|
|
return Err("No node found in graph".into());
|
|
|
|
|
};
|
|
|
|
|
(sys, translator)
|
|
|
|
|
}
|
|
|
|
|
};
|
2025-07-11 19:36:20 +02:00
|
|
|
|
2025-07-12 02:42:28 +02:00
|
|
|
let res = frequency::naive_frequency(sys)?;
|
2025-07-11 19:36:20 +02:00
|
|
|
|
2025-07-12 02:42:28 +02:00
|
|
|
Ok(format!(
|
2025-07-11 19:36:20 +02:00
|
|
|
"Frequency of encountered symbols:\n{}",
|
2025-07-12 02:42:28 +02:00
|
|
|
translator::FrequencyDisplay::from(translator, &res)
|
|
|
|
|
))
|
2025-07-11 19:36:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Finds the frequency of each entity in the limit loop of a nonterminating
|
|
|
|
|
/// Reaction System whose context has the form Q1 ... Q1.Q2 ... Q2 ... Qn ...
|
|
|
|
|
/// equivalent to main_do(limitfreq, PairList)
|
|
|
|
|
pub fn limit_freq(
|
2025-07-12 02:42:28 +02:00
|
|
|
system: &mut EvaluatedSystem,
|
|
|
|
|
experiment: String
|
|
|
|
|
) -> Result<String, String>
|
2025-07-11 19:36:20 +02:00
|
|
|
{
|
2025-07-12 02:42:28 +02:00
|
|
|
let (sys, translator): (&RSsystem, &mut Translator) = match system {
|
|
|
|
|
EvaluatedSystem::System { sys, translator } => {
|
|
|
|
|
(sys, translator)
|
|
|
|
|
},
|
|
|
|
|
EvaluatedSystem::Graph { graph, translator } => {
|
|
|
|
|
let Some(sys) = graph.node_weights().next()
|
|
|
|
|
else {
|
|
|
|
|
return Err("No node found in graph".into());
|
|
|
|
|
};
|
|
|
|
|
(sys, translator)
|
|
|
|
|
}
|
|
|
|
|
};
|
2025-07-11 19:36:20 +02:00
|
|
|
|
2025-07-12 02:42:28 +02:00
|
|
|
let (_, sets) = read_file(translator,
|
|
|
|
|
experiment,
|
2025-07-11 19:36:20 +02:00
|
|
|
parser_experiment)?;
|
|
|
|
|
|
|
|
|
|
let res = match frequency::limit_frequency(&sets,
|
2025-07-12 02:42:28 +02:00
|
|
|
&sys.reaction_rules,
|
|
|
|
|
&sys.available_entities) {
|
2025-07-11 19:36:20 +02:00
|
|
|
Some(e) => e,
|
|
|
|
|
None => {return Err("Error calculating frequency.".into());}
|
|
|
|
|
};
|
|
|
|
|
|
2025-07-12 02:42:28 +02:00
|
|
|
Ok(format!(
|
2025-07-11 19:36:20 +02:00
|
|
|
"Frequency of encountered symbols:\n{}",
|
2025-07-12 02:42:28 +02:00
|
|
|
translator::FrequencyDisplay::from(translator, &res)
|
|
|
|
|
))
|
2025-07-11 19:36:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Finds the frequency of each entity in the traversed loops of a terminating
|
|
|
|
|
/// reaction system whose context has the form
|
|
|
|
|
/// Q1 ... Q1.Q2 ... Q2 ... Qn ... Qn.nil and each Qi is repeated Wi times
|
|
|
|
|
/// read from a corresponding file.
|
|
|
|
|
/// equivalent to main_do(fastfreq, PairList)
|
|
|
|
|
pub fn fast_freq(
|
2025-07-12 02:42:28 +02:00
|
|
|
system: &mut EvaluatedSystem,
|
|
|
|
|
experiment: String
|
|
|
|
|
) -> Result<String, String>
|
2025-07-11 19:36:20 +02:00
|
|
|
{
|
2025-07-12 02:42:28 +02:00
|
|
|
let (sys, translator): (&RSsystem, &mut Translator) = match system {
|
|
|
|
|
EvaluatedSystem::System { sys, translator } => {
|
|
|
|
|
(sys, translator)
|
|
|
|
|
},
|
|
|
|
|
EvaluatedSystem::Graph { graph, translator } => {
|
|
|
|
|
let Some(sys) = graph.node_weights().next()
|
|
|
|
|
else {
|
|
|
|
|
return Err("No node found in graph".into());
|
|
|
|
|
};
|
|
|
|
|
(sys, translator)
|
|
|
|
|
}
|
|
|
|
|
};
|
2025-07-11 19:36:20 +02:00
|
|
|
|
2025-07-12 02:42:28 +02:00
|
|
|
let (weights, sets) = read_file(translator,
|
|
|
|
|
experiment,
|
2025-07-11 19:36:20 +02:00
|
|
|
parser_experiment)?;
|
|
|
|
|
|
|
|
|
|
let res = match frequency::fast_frequency(&sets,
|
2025-07-12 02:42:28 +02:00
|
|
|
&sys.reaction_rules,
|
|
|
|
|
&sys.available_entities,
|
2025-07-11 19:36:20 +02:00
|
|
|
&weights) {
|
|
|
|
|
Some(e) => e,
|
|
|
|
|
None => {return Err("Error calculating frequency.".into());}
|
|
|
|
|
};
|
|
|
|
|
|
2025-07-12 02:42:28 +02:00
|
|
|
Ok(format!(
|
2025-07-11 19:36:20 +02:00
|
|
|
"Frequency of encountered symbols:\n{}",
|
2025-07-12 02:42:28 +02:00
|
|
|
translator::FrequencyDisplay::from(translator, &res)
|
|
|
|
|
))
|
2025-07-11 19:36:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Computes the LTS.
|
|
|
|
|
/// equivalent to main_do(digraph, Arcs) or to main_do(advdigraph, Arcs)
|
|
|
|
|
pub fn digraph(
|
2025-07-12 02:42:28 +02:00
|
|
|
system: &mut EvaluatedSystem
|
|
|
|
|
) -> Result<(), String> {
|
|
|
|
|
*system = if let EvaluatedSystem::System { sys, translator } = system {
|
|
|
|
|
EvaluatedSystem::Graph {
|
|
|
|
|
graph: graph::digraph(sys.clone())?,
|
|
|
|
|
translator: translator.to_owned()
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return Ok(());
|
|
|
|
|
};
|
|
|
|
|
Ok(())
|
2025-07-11 19:36:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2025-07-11 23:49:59 +02:00
|
|
|
// -----------------------------------------------------------------------------
|
2025-07-11 19:36:20 +02:00
|
|
|
// Output Functions
|
2025-07-11 23:49:59 +02:00
|
|
|
// -----------------------------------------------------------------------------
|
2025-07-11 19:36:20 +02:00
|
|
|
|
|
|
|
|
/// Writes the specified graph to a file in .dot format.
|
2025-07-12 02:42:28 +02:00
|
|
|
pub fn dot(system: &EvaluatedSystem) -> Result<String, String> {
|
|
|
|
|
match system {
|
|
|
|
|
EvaluatedSystem::System { sys:_, translator:_ } =>
|
|
|
|
|
Err("Supplied system is not a graph".into()),
|
|
|
|
|
EvaluatedSystem::Graph { graph, translator } => {
|
|
|
|
|
let rc_translator = Rc::new(translator.to_owned());
|
|
|
|
|
// map each value to the corresponding value we want to display
|
|
|
|
|
let modified_graph = graph.map(
|
|
|
|
|
|id, node|
|
|
|
|
|
graph::GraphMapNodesTy::from(
|
|
|
|
|
graph::GraphMapNodes::Entities,
|
|
|
|
|
Rc::clone(&rc_translator)
|
|
|
|
|
).get()(id, node)
|
|
|
|
|
+ "; " +
|
|
|
|
|
&graph::GraphMapNodesTy::from(
|
|
|
|
|
graph::GraphMapNodes::Context,
|
|
|
|
|
Rc::clone(&rc_translator)
|
|
|
|
|
).get()(id, node),
|
|
|
|
|
graph::GraphMapEdgesTy::from(
|
|
|
|
|
graph::GraphMapEdges::EntitiesAdded,
|
|
|
|
|
Rc::clone(&rc_translator)
|
|
|
|
|
).get()
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let graph = Rc::new(graph.to_owned());
|
|
|
|
|
|
|
|
|
|
let edge_formatter = graph::default_edge_formatter(
|
|
|
|
|
Rc::clone(&graph)
|
|
|
|
|
);
|
|
|
|
|
let node_formatter = graph::default_node_formatter(
|
|
|
|
|
Rc::clone(&graph)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let dot = rsdot::RSDot::with_attr_getters(
|
|
|
|
|
&modified_graph,
|
|
|
|
|
&[],
|
|
|
|
|
&edge_formatter,
|
|
|
|
|
&node_formatter,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
Ok(format!("{dot}"))
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-07-11 19:36:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Writes the specified graph to a file in .graphml format.
|
2025-07-12 02:42:28 +02:00
|
|
|
pub fn graphml(system: &EvaluatedSystem) -> Result<String, String> {
|
|
|
|
|
match system {
|
|
|
|
|
EvaluatedSystem::System { sys:_, translator:_ } =>
|
|
|
|
|
Err("Supplied system is not a graph".into()),
|
|
|
|
|
EvaluatedSystem::Graph { graph, translator } => {
|
|
|
|
|
let rc_translator = Rc::new(translator.to_owned());
|
|
|
|
|
|
|
|
|
|
// map each value to the corresponding value we want to display
|
|
|
|
|
let modified_graph = graph.map(
|
|
|
|
|
|id, node|
|
|
|
|
|
graph::GraphMapNodesTy::from(
|
|
|
|
|
graph::GraphMapNodes::Entities,
|
|
|
|
|
Rc::clone(&rc_translator)
|
|
|
|
|
).get()(id, node)
|
|
|
|
|
+ "; " +
|
|
|
|
|
&graph::GraphMapNodesTy::from(
|
|
|
|
|
graph::GraphMapNodes::Context,
|
|
|
|
|
Rc::clone(&rc_translator)
|
|
|
|
|
).get()(id, node),
|
|
|
|
|
graph::GraphMapEdgesTy::from(graph::GraphMapEdges::EntitiesAdded,
|
|
|
|
|
Rc::clone(&rc_translator)).get()
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
use petgraph_graphml::GraphMl;
|
|
|
|
|
let graphml = GraphMl::new(&modified_graph)
|
|
|
|
|
.pretty_print(true)
|
|
|
|
|
.export_node_weights_display()
|
|
|
|
|
.export_edge_weights_display();
|
|
|
|
|
|
|
|
|
|
Ok(format!("{graphml}"))
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-07-11 19:36:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Writes the specified graph, translator tuple to file.
|
|
|
|
|
/// N.B. graph size in memory might be much larger after serialization and
|
|
|
|
|
/// deserialization.
|
|
|
|
|
pub fn serialize(
|
2025-07-12 02:42:28 +02:00
|
|
|
system: &EvaluatedSystem,
|
|
|
|
|
path: String
|
2025-07-11 19:36:20 +02:00
|
|
|
) -> Result<(), String>
|
|
|
|
|
{
|
2025-07-12 02:42:28 +02:00
|
|
|
match system {
|
|
|
|
|
EvaluatedSystem::System { sys:_, translator:_ } =>
|
|
|
|
|
Err("Supplied system is not a graph".into()),
|
|
|
|
|
EvaluatedSystem::Graph { graph, translator } => {
|
|
|
|
|
// relative path
|
|
|
|
|
let mut path = std::path::PathBuf::from(path);
|
|
|
|
|
path.set_extension("cbor");
|
|
|
|
|
|
|
|
|
|
let f = match fs::File::create(&path) {
|
|
|
|
|
Ok(f) => f,
|
|
|
|
|
Err(_) => return Err(format!("Error creating file {}.",
|
|
|
|
|
path.to_str().unwrap()))
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
match serialize::ser(f, graph, translator) {
|
|
|
|
|
Ok(_) => Ok(()),
|
|
|
|
|
Err(_) => Err("Error during serialization.".into())
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-07-11 19:36:20 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Reads the specified serialized system from a file.
|
|
|
|
|
/// N.B. graph size in memory might be much larger after serialization and
|
|
|
|
|
/// deserialization
|
|
|
|
|
pub fn deserialize(
|
|
|
|
|
input_path: String
|
|
|
|
|
) -> Result<(Graph<RSsystem, RSlabel>, Translator), String>
|
|
|
|
|
{
|
|
|
|
|
// relative path
|
|
|
|
|
let mut path = match env::current_dir() {
|
|
|
|
|
Ok(p) => p,
|
|
|
|
|
Err(_) => return Err("Error getting current directory.".into())
|
|
|
|
|
};
|
|
|
|
|
path = path.join(input_path);
|
|
|
|
|
path.set_extension("cbor");
|
|
|
|
|
|
|
|
|
|
let f = match fs::File::open(&path) {
|
|
|
|
|
Ok(f) => f,
|
|
|
|
|
Err(_) => return Err(format!("Error opening file {}.",
|
|
|
|
|
path.to_str().unwrap()))
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
match serialize::de(f) {
|
|
|
|
|
Ok(a) => Ok(a),
|
|
|
|
|
Err(_) => Err("Error during deserialization.".into())
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-07-11 23:49:59 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
// Interpreting Instructions
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
2025-07-12 02:42:28 +02:00
|
|
|
macro_rules! save_options {
|
|
|
|
|
($assignment: expr, $so: ident) => {
|
|
|
|
|
let SaveOptions { print, save } = $so;
|
|
|
|
|
let output = $assignment;
|
|
|
|
|
if print {
|
|
|
|
|
println!("{output}");
|
|
|
|
|
}
|
|
|
|
|
if let Some(save) = save {
|
|
|
|
|
for file in save {
|
|
|
|
|
save_file(&output, file)?;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn execute(
|
|
|
|
|
instruction: Instruction,
|
|
|
|
|
system: &mut EvaluatedSystem
|
|
|
|
|
) -> Result<(), String> {
|
|
|
|
|
match instruction {
|
|
|
|
|
Instruction::Stats { so } => {
|
|
|
|
|
save_options!(stats(system)?, so);
|
|
|
|
|
}
|
|
|
|
|
Instruction::Target { so } => {
|
|
|
|
|
save_options!(target(system)?, so);
|
|
|
|
|
},
|
|
|
|
|
Instruction::Run { so } => {
|
|
|
|
|
save_options!(traversed(system)?, so);
|
|
|
|
|
},
|
|
|
|
|
Instruction::Loop { symbol, so } => {
|
|
|
|
|
save_options!(hoop(system, symbol)?, so);
|
|
|
|
|
},
|
|
|
|
|
Instruction::Frequency { so } => {
|
|
|
|
|
save_options!(freq(system)?, so);
|
|
|
|
|
},
|
|
|
|
|
Instruction::LimitFrequency { experiment, so } => {
|
|
|
|
|
save_options!(limit_freq(system, experiment)?, so);
|
|
|
|
|
},
|
|
|
|
|
Instruction::FastFrequency { experiment, so } => {
|
|
|
|
|
save_options!(fast_freq(system, experiment)?, so);
|
|
|
|
|
},
|
|
|
|
|
Instruction::Digraph { gso } => {
|
|
|
|
|
for save in gso {
|
|
|
|
|
digraph(system)?;
|
|
|
|
|
match save {
|
|
|
|
|
GraphSaveOptions::Dot { so } => {
|
|
|
|
|
save_options!(dot(system)?, so);
|
|
|
|
|
},
|
|
|
|
|
GraphSaveOptions::GraphML { so } => {
|
|
|
|
|
save_options!(graphml(system)?, so);
|
|
|
|
|
},
|
|
|
|
|
GraphSaveOptions::Serialize { path } => {
|
|
|
|
|
serialize(system, path)?;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-11 23:49:59 +02:00
|
|
|
pub fn run(path: String) -> Result<(), String> {
|
|
|
|
|
let mut translator = Translator::new();
|
|
|
|
|
|
2025-07-12 02:42:28 +02:00
|
|
|
let Instructions { system, instructions } =
|
|
|
|
|
read_file(&mut translator, path, parser_instructions)?;
|
2025-07-11 23:49:59 +02:00
|
|
|
|
2025-07-12 02:42:28 +02:00
|
|
|
let mut system = system.compute(translator)?;
|
|
|
|
|
|
|
|
|
|
for instr in instructions {
|
|
|
|
|
execute(instr, &mut system)?;
|
|
|
|
|
}
|
2025-07-11 23:49:59 +02:00
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|