2025-07-11 19:36:20 +02:00
|
|
|
//! Module that holds useful presets for interacting with other modules.
|
|
|
|
|
|
2025-09-10 22:41:40 +02:00
|
|
|
use std::collections::HashMap;
|
2025-07-11 23:49:59 +02:00
|
|
|
use std::fmt::Display;
|
2025-09-07 17:55:53 +02:00
|
|
|
use std::io::prelude::*;
|
2025-07-11 19:36:20 +02:00
|
|
|
use std::rc::Rc;
|
2025-09-11 02:49:14 +02:00
|
|
|
use std::{env, fs, io};
|
2025-07-11 19:36:20 +02:00
|
|
|
|
2025-09-11 02:49:14 +02:00
|
|
|
use lalrpop_util::ParseError;
|
|
|
|
|
use petgraph::Graph;
|
|
|
|
|
|
|
|
|
|
use super::super::grammar;
|
2025-08-24 02:01:24 +02:00
|
|
|
use super::graph::MapEdges;
|
|
|
|
|
use super::set::Set;
|
2025-09-07 17:55:53 +02:00
|
|
|
use super::system::ExtensionsSystem;
|
2025-08-24 02:01:24 +02:00
|
|
|
use super::translator::Translator;
|
2025-09-07 17:55:53 +02:00
|
|
|
use super::*;
|
2025-08-24 02:01:24 +02:00
|
|
|
|
2025-07-11 23:49:59 +02:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
// Structures
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
2025-07-16 18:03:31 +02:00
|
|
|
/// Describes how the result of some computation has to be saved.
|
2025-07-11 23:49:59 +02:00
|
|
|
pub struct SaveOptions {
|
|
|
|
|
pub print: bool,
|
2025-09-11 02:49:14 +02:00
|
|
|
pub save: Option<Vec<String>>,
|
2025-07-11 23:49:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl SaveOptions {
|
|
|
|
|
pub fn combine(&mut self, other: &mut Self) {
|
2025-07-12 15:41:10 +02:00
|
|
|
self.print = self.print || other.print;
|
|
|
|
|
match (self.save.is_some(), other.save.is_some()) {
|
2025-09-11 02:49:14 +02:00
|
|
|
| (false, false) | (true, false) => {},
|
|
|
|
|
| (false, true) => {
|
2025-07-12 15:41:10 +02:00
|
|
|
self.save = other.save.to_owned();
|
2025-09-11 02:49:14 +02:00
|
|
|
},
|
|
|
|
|
| (true, true) => {
|
2025-07-12 15:41:10 +02:00
|
|
|
self.save
|
|
|
|
|
.as_mut()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.append(other.save.as_mut().unwrap());
|
2025-09-11 02:49:14 +02:00
|
|
|
},
|
2025-07-12 15:41:10 +02:00
|
|
|
}
|
2025-07-11 23:49:59 +02:00
|
|
|
}
|
|
|
|
|
pub fn new() -> Self {
|
2025-07-12 15:41:10 +02:00
|
|
|
SaveOptions {
|
|
|
|
|
print: false,
|
2025-09-11 02:49:14 +02:00
|
|
|
save: None,
|
2025-07-12 15:41:10 +02:00
|
|
|
}
|
2025-07-11 23:49:59 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Default for SaveOptions {
|
|
|
|
|
fn default() -> Self {
|
2025-07-12 15:41:10 +02:00
|
|
|
SaveOptions::new()
|
2025-07-11 23:49:59 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-16 18:03:31 +02:00
|
|
|
// Describes output options for a graph.
|
2025-07-11 23:49:59 +02:00
|
|
|
pub enum GraphSaveOptions {
|
2025-07-12 15:41:10 +02:00
|
|
|
Dot {
|
2025-07-26 22:46:10 +02:00
|
|
|
node_display: graph::NodeDisplay,
|
2025-07-27 14:59:35 +02:00
|
|
|
edge_display: graph::EdgeDisplay,
|
2025-09-07 17:55:53 +02:00
|
|
|
node_color: graph::NodeColor,
|
|
|
|
|
edge_color: graph::EdgeColor,
|
2025-07-12 15:41:10 +02:00
|
|
|
so: SaveOptions,
|
|
|
|
|
},
|
|
|
|
|
GraphML {
|
2025-07-26 22:46:10 +02:00
|
|
|
node_display: graph::NodeDisplay,
|
2025-07-27 14:59:35 +02:00
|
|
|
edge_display: graph::EdgeDisplay,
|
2025-07-12 15:41:10 +02:00
|
|
|
so: SaveOptions,
|
|
|
|
|
},
|
|
|
|
|
Serialize {
|
|
|
|
|
path: String,
|
|
|
|
|
},
|
2025-07-11 23:49:59 +02:00
|
|
|
}
|
|
|
|
|
|
2025-07-16 18:03:31 +02:00
|
|
|
/// Describes the computation to apply to the input system or graph.
|
2025-07-11 23:49:59 +02:00
|
|
|
pub enum Instruction {
|
2025-09-07 17:55:53 +02:00
|
|
|
Stats {
|
|
|
|
|
so: SaveOptions,
|
|
|
|
|
},
|
|
|
|
|
Target {
|
|
|
|
|
so: SaveOptions,
|
|
|
|
|
},
|
|
|
|
|
Run {
|
|
|
|
|
so: SaveOptions,
|
|
|
|
|
},
|
|
|
|
|
Loop {
|
|
|
|
|
symbol: String,
|
2025-09-11 02:49:14 +02:00
|
|
|
so: SaveOptions,
|
2025-09-07 17:55:53 +02:00
|
|
|
},
|
|
|
|
|
Frequency {
|
|
|
|
|
so: SaveOptions,
|
|
|
|
|
},
|
|
|
|
|
LimitFrequency {
|
|
|
|
|
experiment: String,
|
|
|
|
|
so: SaveOptions,
|
|
|
|
|
},
|
|
|
|
|
FastFrequency {
|
|
|
|
|
experiment: String,
|
|
|
|
|
so: SaveOptions,
|
|
|
|
|
},
|
|
|
|
|
Digraph {
|
2025-09-10 22:41:40 +02:00
|
|
|
group: Option<Box<assert::grouping::Assert>>,
|
2025-09-11 02:49:14 +02:00
|
|
|
gso: Vec<GraphSaveOptions>,
|
2025-09-07 17:55:53 +02:00
|
|
|
},
|
|
|
|
|
Bisimilarity {
|
|
|
|
|
system_b: String,
|
2025-09-10 22:41:40 +02:00
|
|
|
edge_relabeler: Box<assert::relabel::Assert>,
|
2025-09-07 17:55:53 +02:00
|
|
|
so: SaveOptions,
|
|
|
|
|
},
|
2025-07-11 23:49:59 +02:00
|
|
|
}
|
|
|
|
|
|
2025-07-16 18:03:31 +02:00
|
|
|
/// Describes a system or a graph.
|
2025-09-10 22:41:40 +02:00
|
|
|
#[derive(Clone)]
|
2025-07-11 23:49:59 +02:00
|
|
|
pub enum System {
|
|
|
|
|
Deserialize { path: String },
|
2025-08-24 02:01:24 +02:00
|
|
|
System { sys: system::System },
|
2025-07-11 23:49:59 +02:00
|
|
|
}
|
|
|
|
|
|
2025-07-12 02:42:28 +02:00
|
|
|
impl System {
|
2025-07-16 18:03:31 +02:00
|
|
|
/// Deserialize the graph if applicable.
|
2025-09-10 22:41:40 +02:00
|
|
|
pub fn compute(
|
|
|
|
|
&self,
|
|
|
|
|
translator: Translator,
|
|
|
|
|
) -> Result<EvaluatedSystem, String> {
|
2025-07-12 15:41:10 +02:00
|
|
|
match self {
|
2025-09-11 02:49:14 +02:00
|
|
|
| Self::System { sys } => Ok(EvaluatedSystem::System {
|
2025-07-12 15:41:10 +02:00
|
|
|
sys: sys.to_owned(),
|
|
|
|
|
translator,
|
|
|
|
|
}),
|
2025-09-11 02:49:14 +02:00
|
|
|
| Self::Deserialize { path } => {
|
2025-07-12 15:41:10 +02:00
|
|
|
let (graph, translator) = deserialize(path.into())?;
|
|
|
|
|
Ok(EvaluatedSystem::Graph { graph, translator })
|
2025-09-11 02:49:14 +02:00
|
|
|
},
|
2025-07-12 15:41:10 +02:00
|
|
|
}
|
2025-07-12 02:42:28 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-10 22:41:40 +02:00
|
|
|
#[derive(Clone)]
|
2025-07-16 00:05:14 +02:00
|
|
|
pub enum EvaluatedSystem {
|
|
|
|
|
Graph {
|
2025-09-11 02:49:14 +02:00
|
|
|
graph: graph::SystemGraph,
|
2025-07-16 00:05:14 +02:00
|
|
|
translator: Translator,
|
|
|
|
|
},
|
|
|
|
|
System {
|
2025-08-24 02:01:24 +02:00
|
|
|
sys: system::System,
|
2025-07-16 00:05:14 +02:00
|
|
|
translator: Translator,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl EvaluatedSystem {
|
|
|
|
|
pub fn get_translator(&mut self) -> &mut Translator {
|
2025-09-07 17:55:53 +02:00
|
|
|
match self {
|
2025-09-11 02:49:14 +02:00
|
|
|
| EvaluatedSystem::Graph {
|
2025-09-07 17:55:53 +02:00
|
|
|
graph: _,
|
|
|
|
|
translator,
|
|
|
|
|
} => translator,
|
2025-09-11 02:49:14 +02:00
|
|
|
| EvaluatedSystem::System { sys: _, translator } => translator,
|
2025-09-07 17:55:53 +02:00
|
|
|
}
|
2025-07-16 00:05:14 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-16 18:03:31 +02:00
|
|
|
/// Holds all the computations to be done on the system
|
2025-07-11 23:49:59 +02:00
|
|
|
pub struct Instructions {
|
|
|
|
|
pub system: System,
|
2025-07-12 15:41:10 +02:00
|
|
|
pub instructions: Vec<Instruction>,
|
2025-07-11 23:49:59 +02:00
|
|
|
}
|
|
|
|
|
|
2025-07-11 19:36:20 +02:00
|
|
|
// -----------------------------------------------------------------------------
|
2025-07-26 19:43:20 +02:00
|
|
|
// IO Helper Functions
|
2025-07-11 19:36:20 +02:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
2025-09-10 22:41:40 +02:00
|
|
|
fn read_file<T, F>(
|
|
|
|
|
translator: &mut Translator,
|
|
|
|
|
path_string: String,
|
|
|
|
|
parser: F,
|
|
|
|
|
) -> Result<T, String>
|
2025-07-11 19:36:20 +02:00
|
|
|
where
|
2025-07-12 15:41:10 +02:00
|
|
|
F: Fn(&mut Translator, String) -> Result<T, String>,
|
2025-07-11 19:36:20 +02:00
|
|
|
{
|
|
|
|
|
// relative path
|
|
|
|
|
let mut path = match env::current_dir() {
|
2025-09-11 02:49:14 +02:00
|
|
|
| Ok(p) => p,
|
|
|
|
|
| Err(e) => {
|
|
|
|
|
return Err(format!("Error getting current directory: {e}"));
|
|
|
|
|
},
|
2025-07-11 19:36:20 +02:00
|
|
|
};
|
|
|
|
|
path = path.join(path_string);
|
|
|
|
|
|
|
|
|
|
// we read the file with a buffer
|
|
|
|
|
let f = match fs::File::open(path) {
|
2025-09-11 02:49:14 +02:00
|
|
|
| Ok(f) => f,
|
|
|
|
|
| Err(e) => return Err(format!("Error opening file: {e}.")),
|
2025-07-11 19:36:20 +02:00
|
|
|
};
|
|
|
|
|
let mut buf_reader = io::BufReader::new(f);
|
|
|
|
|
let mut contents = String::new();
|
|
|
|
|
match buf_reader.read_to_string(&mut contents) {
|
2025-09-11 02:49:14 +02:00
|
|
|
| Ok(_) => {},
|
|
|
|
|
| Err(e) => return Err(format!("Error reading file: {e}")),
|
2025-07-11 19:36:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// parse
|
|
|
|
|
let result = parser(translator, contents)?;
|
|
|
|
|
|
|
|
|
|
Ok(result)
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-10 22:41:40 +02:00
|
|
|
fn reformat_error<T, S>(
|
|
|
|
|
e: ParseError<usize, T, &'static str>,
|
|
|
|
|
input_str: &str,
|
|
|
|
|
) -> Result<S, String>
|
2025-07-11 23:49:59 +02:00
|
|
|
where
|
2025-07-12 15:41:10 +02:00
|
|
|
T: Display,
|
2025-07-11 19:36:20 +02:00
|
|
|
{
|
2025-07-11 23:49:59 +02:00
|
|
|
match e {
|
2025-09-11 02:49:14 +02:00
|
|
|
| ParseError::ExtraToken { token: (l, t, r) } => Err(format!(
|
2025-09-07 17:55:53 +02:00
|
|
|
"Unexpected token \"{t}\" \
|
2025-09-10 22:41:40 +02:00
|
|
|
between positions {l} and {r}."
|
2025-09-07 17:55:53 +02:00
|
|
|
)),
|
2025-09-11 02:49:14 +02:00
|
|
|
| ParseError::UnrecognizedEof {
|
2025-07-12 15:41:10 +02:00
|
|
|
location: _,
|
|
|
|
|
expected: _,
|
2025-09-07 17:55:53 +02:00
|
|
|
} => Err("End of file encountered while parsing.".into()),
|
2025-09-11 02:49:14 +02:00
|
|
|
| ParseError::InvalidToken { location } =>
|
|
|
|
|
Err(format!("Invalid token at position {location}.")),
|
|
|
|
|
| ParseError::UnrecognizedToken {
|
2025-07-12 15:41:10 +02:00
|
|
|
token: (l, t, r),
|
2025-09-10 22:41:40 +02:00
|
|
|
expected,
|
2025-07-13 17:28:13 +02:00
|
|
|
} => {
|
2025-09-07 17:55:53 +02:00
|
|
|
use colored::Colorize;
|
2025-08-20 19:51:03 +02:00
|
|
|
|
2025-09-07 17:55:53 +02:00
|
|
|
let mut err = format!(
|
|
|
|
|
"Unrecognized token {}{}{} \
|
2025-09-10 22:41:40 +02:00
|
|
|
between positions {l} and {r}.",
|
2025-09-07 17:55:53 +02:00
|
|
|
"\"".red(),
|
|
|
|
|
t.to_string().red(),
|
|
|
|
|
"\"".red(),
|
|
|
|
|
);
|
|
|
|
|
|
2025-09-10 22:41:40 +02:00
|
|
|
// Temporary debug.
|
2025-09-11 02:17:40 +02:00
|
|
|
err.push_str("\nExpected: ");
|
2025-09-10 22:41:40 +02:00
|
|
|
let mut it = expected.iter().peekable();
|
|
|
|
|
while let Some(s) = it.next() {
|
|
|
|
|
err.push('(');
|
|
|
|
|
err.push_str(&format!("{}", s.green()));
|
|
|
|
|
err.push(')');
|
|
|
|
|
if it.peek().is_some() {
|
|
|
|
|
err.push(',');
|
|
|
|
|
err.push(' ');
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-09-07 17:55:53 +02:00
|
|
|
let right_new_line = input_str[l..]
|
|
|
|
|
.find("\n")
|
|
|
|
|
.map(|pos| pos + l)
|
|
|
|
|
.unwrap_or(input_str.len());
|
|
|
|
|
let left_new_line = input_str[..r]
|
|
|
|
|
.rfind("\n")
|
|
|
|
|
.map(|pos| pos + 1)
|
|
|
|
|
.unwrap_or_default();
|
|
|
|
|
|
|
|
|
|
let line_number = input_str[..l].match_indices('\n').count() + 1;
|
|
|
|
|
let pre_no_color = format!("{line_number} |");
|
|
|
|
|
let pre = format!("{}", pre_no_color.blue());
|
|
|
|
|
|
|
|
|
|
let line_pos_l = l - left_new_line;
|
|
|
|
|
let line_pos_r = r - left_new_line;
|
|
|
|
|
|
|
|
|
|
err.push_str(&format!(
|
|
|
|
|
"\nLine {} position {} to {}:\n{}{}{}{}",
|
|
|
|
|
line_number,
|
|
|
|
|
line_pos_l,
|
|
|
|
|
line_pos_r,
|
|
|
|
|
&pre,
|
|
|
|
|
&input_str[left_new_line..l].green(),
|
|
|
|
|
&input_str[l..r].red(),
|
|
|
|
|
&input_str[r..right_new_line],
|
|
|
|
|
));
|
|
|
|
|
err.push('\n');
|
|
|
|
|
err.push_str(&" ".repeat(pre_no_color.len() - 1));
|
|
|
|
|
err.push_str(&format!("{}", "|".blue()));
|
|
|
|
|
err.push_str(&" ".repeat(l - left_new_line));
|
|
|
|
|
err.push_str(&format!("{}", &"↑".red()));
|
|
|
|
|
if r - l > 2 {
|
|
|
|
|
err.push_str(&" ".repeat(r - l - 2));
|
|
|
|
|
err.push_str(&format!("{}", &"↑".red()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Err(err)
|
2025-09-11 02:49:14 +02:00
|
|
|
},
|
|
|
|
|
| ParseError::User { error } => Err(error.to_string()),
|
2025-07-11 19:36:20 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn parser_experiment(
|
|
|
|
|
translator: &mut Translator,
|
2025-07-12 15:41:10 +02:00
|
|
|
contents: String,
|
2025-08-24 02:01:24 +02:00
|
|
|
) -> Result<(Vec<u32>, Vec<Set>), String> {
|
2025-07-12 15:41:10 +02:00
|
|
|
match grammar::ExperimentParser::new().parse(translator, &contents) {
|
2025-09-11 02:49:14 +02:00
|
|
|
| Ok(sys) => Ok(sys),
|
|
|
|
|
| Err(e) => reformat_error(e, &contents),
|
2025-07-11 19:36:20 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-11 23:49:59 +02:00
|
|
|
fn parser_instructions(
|
|
|
|
|
translator: &mut Translator,
|
2025-07-12 15:41:10 +02:00
|
|
|
contents: String,
|
|
|
|
|
) -> Result<Instructions, String> {
|
|
|
|
|
match grammar::RunParser::new().parse(translator, &contents) {
|
2025-09-11 02:49:14 +02:00
|
|
|
| Ok(sys) => Ok(sys),
|
|
|
|
|
| Err(e) => reformat_error(e, &contents),
|
2025-07-11 23:49:59 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-12 15:41:10 +02:00
|
|
|
fn save_file(contents: &String, path_string: String) -> Result<(), String> {
|
2025-07-11 23:49:59 +02:00
|
|
|
// relative path
|
|
|
|
|
let mut path = match env::current_dir() {
|
2025-09-11 02:49:14 +02:00
|
|
|
| Ok(p) => p,
|
|
|
|
|
| Err(_) => return Err("Error getting current directory.".into()),
|
2025-07-11 23:49:59 +02:00
|
|
|
};
|
|
|
|
|
path = path.join(path_string);
|
|
|
|
|
|
|
|
|
|
let mut f = match fs::File::create(&path) {
|
2025-09-11 02:49:14 +02:00
|
|
|
| Ok(f) => f,
|
|
|
|
|
| Err(_) => {
|
2025-09-10 22:41:40 +02:00
|
|
|
return Err(format!(
|
|
|
|
|
"Error creating file {}.",
|
|
|
|
|
path.to_str().unwrap()
|
|
|
|
|
));
|
2025-09-11 02:49:14 +02:00
|
|
|
},
|
2025-07-11 23:49:59 +02:00
|
|
|
};
|
|
|
|
|
match write!(f, "{contents}") {
|
2025-09-11 02:49:14 +02:00
|
|
|
| Ok(_) => {},
|
|
|
|
|
| Err(_) => return Err("Error writing to file.".into()),
|
2025-07-11 23:49:59 +02:00
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
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 {
|
2025-09-11 02:49:14 +02:00
|
|
|
| EvaluatedSystem::System { sys, translator } =>
|
|
|
|
|
Ok(sys.statistics(translator)),
|
|
|
|
|
| EvaluatedSystem::Graph { graph, translator } => {
|
2025-07-12 15:41:10 +02:00
|
|
|
let Some(sys) = graph.node_weights().next() else {
|
2025-09-02 01:34:01 +02:00
|
|
|
return Err("No node found in graph.".into());
|
2025-07-12 15:41:10 +02:00
|
|
|
};
|
2025-08-23 23:40:19 +02:00
|
|
|
Ok(sys.statistics(translator))
|
2025-09-11 02:49:14 +02:00
|
|
|
},
|
2025-07-12 02:42:28 +02:00
|
|
|
}
|
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 {
|
2025-09-11 02:49:14 +02:00
|
|
|
| EvaluatedSystem::System { sys, translator } =>
|
|
|
|
|
(sys.target()?, translator),
|
|
|
|
|
| EvaluatedSystem::Graph { graph, translator } => {
|
2025-07-12 15:41:10 +02:00
|
|
|
let Some(sys) = graph.node_weights().next() else {
|
2025-09-02 01:34:01 +02:00
|
|
|
return Err("No node found in graph.".into());
|
2025-07-12 15:41:10 +02:00
|
|
|
};
|
2025-08-23 23:40:19 +02:00
|
|
|
(sys.target()?, translator)
|
2025-09-11 02:49:14 +02:00
|
|
|
},
|
2025-07-12 02:42:28 +02:00
|
|
|
};
|
|
|
|
|
Ok(format!(
|
2025-07-12 15:41:10 +02:00
|
|
|
"After {} steps we arrive at state:\n{}",
|
|
|
|
|
res.0,
|
2025-08-24 02:01:24 +02:00
|
|
|
translator::Formatter::from(translator, &res.1)
|
2025-07-12 02:42:28 +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 {
|
2025-09-11 02:49:14 +02:00
|
|
|
| EvaluatedSystem::System { sys, translator } =>
|
|
|
|
|
(sys.run_separated()?, translator),
|
|
|
|
|
| EvaluatedSystem::Graph { graph, translator } => {
|
2025-07-12 15:41:10 +02:00
|
|
|
let Some(sys) = graph.node_weights().next() else {
|
2025-09-02 01:34:01 +02:00
|
|
|
return Err("No node found in graph.".into());
|
2025-07-12 15:41:10 +02:00
|
|
|
};
|
2025-08-23 23:40:19 +02:00
|
|
|
(sys.run_separated()?, translator)
|
2025-09-11 02:49:14 +02:00
|
|
|
},
|
2025-07-12 02:42:28 +02:00
|
|
|
};
|
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-09-02 01:34:01 +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-09-10 22:41:40 +02:00
|
|
|
output.push_str(&format!(
|
|
|
|
|
"{}",
|
|
|
|
|
translator::Formatter::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 .
|
2025-09-07 17:55:53 +02:00
|
|
|
/// equivalent to main_do(loop,Es)
|
2025-09-10 22:41:40 +02:00
|
|
|
pub fn hoop(
|
|
|
|
|
system: &EvaluatedSystem,
|
|
|
|
|
symbol: String,
|
|
|
|
|
) -> Result<String, String> {
|
2025-09-05 13:13:35 +02:00
|
|
|
use system::LoopSystem;
|
|
|
|
|
|
2025-07-12 02:42:28 +02:00
|
|
|
let (res, translator) = match system {
|
2025-09-11 02:49:14 +02:00
|
|
|
| EvaluatedSystem::System { sys, translator } => (sys, translator),
|
|
|
|
|
| EvaluatedSystem::Graph { graph, translator } => {
|
2025-07-12 15:41:10 +02:00
|
|
|
let Some(sys) = graph.node_weights().next() else {
|
2025-09-02 01:34:01 +02:00
|
|
|
return Err("No node found in graph.".into());
|
2025-07-12 15:41:10 +02:00
|
|
|
};
|
|
|
|
|
(sys, translator)
|
2025-09-11 02:49:14 +02:00
|
|
|
},
|
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
|
2025-07-12 15:41:10 +02:00
|
|
|
let Some(id) = translator.encode_not_mut(&symbol) else {
|
2025-09-02 01:34:01 +02:00
|
|
|
return Err(format!("Symbol {symbol} not found."));
|
2025-07-12 15:41:10 +02:00
|
|
|
};
|
2025-08-23 23:40:19 +02:00
|
|
|
let res = match res.lollipops_only_loop_named(id) {
|
2025-09-11 02:49:14 +02:00
|
|
|
| Some(o) => o,
|
|
|
|
|
| None => {
|
2025-07-12 15:41:10 +02:00
|
|
|
return Err("No loop found.".into());
|
2025-09-11 02:49:14 +02:00
|
|
|
},
|
2025-07-12 02:42:28 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let mut output = String::new();
|
2025-07-11 19:36:20 +02:00
|
|
|
|
2025-09-02 01:34:01 +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-09-10 22:41:40 +02:00
|
|
|
output.push_str(&format!(
|
|
|
|
|
"{}",
|
|
|
|
|
translator::Formatter::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 15:41:10 +02:00
|
|
|
pub fn freq(system: &EvaluatedSystem) -> Result<String, String> {
|
2025-09-05 13:13:35 +02:00
|
|
|
use frequency::BasicFrequency;
|
2025-09-07 17:55:53 +02:00
|
|
|
|
2025-07-12 02:42:28 +02:00
|
|
|
let (sys, translator) = match system {
|
2025-09-11 02:49:14 +02:00
|
|
|
| EvaluatedSystem::System { sys, translator } => (sys, translator),
|
|
|
|
|
| EvaluatedSystem::Graph { graph, translator } => {
|
2025-07-12 15:41:10 +02:00
|
|
|
let Some(sys) = graph.node_weights().next() else {
|
2025-09-02 01:34:01 +02:00
|
|
|
return Err("No node found in graph.".into());
|
2025-07-12 15:41:10 +02:00
|
|
|
};
|
|
|
|
|
(sys, translator)
|
2025-09-11 02:49:14 +02:00
|
|
|
},
|
2025-07-12 02:42:28 +02:00
|
|
|
};
|
2025-07-11 19:36:20 +02:00
|
|
|
|
2025-08-23 23:40:19 +02:00
|
|
|
let res = frequency::Frequency::naive_frequency(sys)?;
|
2025-07-11 19:36:20 +02:00
|
|
|
|
2025-07-12 02:42:28 +02:00
|
|
|
Ok(format!(
|
2025-07-12 15:41:10 +02:00
|
|
|
"Frequency of encountered symbols:\n{}",
|
2025-08-24 02:01:24 +02:00
|
|
|
translator::Formatter::from(translator, &res)
|
2025-07-12 02:42:28 +02:00
|
|
|
))
|
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)
|
2025-09-10 22:41:40 +02:00
|
|
|
pub fn limit_freq(
|
|
|
|
|
system: &mut EvaluatedSystem,
|
|
|
|
|
experiment: String,
|
|
|
|
|
) -> Result<String, String> {
|
2025-09-05 13:13:35 +02:00
|
|
|
use frequency::BasicFrequency;
|
2025-09-07 17:55:53 +02:00
|
|
|
|
2025-08-24 02:01:24 +02:00
|
|
|
let (sys, translator): (&system::System, &mut Translator) = match system {
|
2025-09-11 02:49:14 +02:00
|
|
|
| EvaluatedSystem::System { sys, translator } => (sys, translator),
|
|
|
|
|
| EvaluatedSystem::Graph { graph, translator } => {
|
2025-07-12 15:41:10 +02:00
|
|
|
let Some(sys) = graph.node_weights().next() else {
|
2025-09-02 01:34:01 +02:00
|
|
|
return Err("No node found in graph.".into());
|
2025-07-12 15:41:10 +02:00
|
|
|
};
|
|
|
|
|
(sys, translator)
|
2025-09-11 02:49:14 +02:00
|
|
|
},
|
2025-07-12 02:42:28 +02:00
|
|
|
};
|
2025-07-11 19:36:20 +02:00
|
|
|
|
2025-07-12 15:41:10 +02:00
|
|
|
let (_, sets) = read_file(translator, experiment, parser_experiment)?;
|
2025-07-11 19:36:20 +02:00
|
|
|
|
2025-09-07 17:55:53 +02:00
|
|
|
let res = match frequency::Frequency::limit_frequency(
|
|
|
|
|
&sets,
|
|
|
|
|
&sys.reaction_rules,
|
|
|
|
|
&sys.available_entities,
|
|
|
|
|
) {
|
2025-09-11 02:49:14 +02:00
|
|
|
| Some(e) => e,
|
|
|
|
|
| None => {
|
2025-07-12 15:41:10 +02:00
|
|
|
return Err("Error calculating frequency.".into());
|
2025-09-11 02:49:14 +02:00
|
|
|
},
|
2025-07-11 19:36:20 +02:00
|
|
|
};
|
|
|
|
|
|
2025-07-12 02:42:28 +02:00
|
|
|
Ok(format!(
|
2025-07-12 15:41:10 +02:00
|
|
|
"Frequency of encountered symbols:\n{}",
|
2025-08-24 02:01:24 +02:00
|
|
|
translator::Formatter::from(translator, &res)
|
2025-07-12 02:42:28 +02:00
|
|
|
))
|
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)
|
2025-09-10 22:41:40 +02:00
|
|
|
pub fn fast_freq(
|
|
|
|
|
system: &mut EvaluatedSystem,
|
|
|
|
|
experiment: String,
|
|
|
|
|
) -> Result<String, String> {
|
2025-09-05 13:13:35 +02:00
|
|
|
use frequency::BasicFrequency;
|
|
|
|
|
|
2025-08-24 02:01:24 +02:00
|
|
|
let (sys, translator): (&system::System, &mut Translator) = match system {
|
2025-09-11 02:49:14 +02:00
|
|
|
| EvaluatedSystem::System { sys, translator } => (sys, translator),
|
|
|
|
|
| EvaluatedSystem::Graph { graph, translator } => {
|
2025-07-12 15:41:10 +02:00
|
|
|
let Some(sys) = graph.node_weights().next() else {
|
|
|
|
|
return Err("No node found in graph".into());
|
|
|
|
|
};
|
|
|
|
|
(sys, translator)
|
2025-09-11 02:49:14 +02:00
|
|
|
},
|
2025-07-12 02:42:28 +02:00
|
|
|
};
|
2025-07-11 19:36:20 +02:00
|
|
|
|
2025-07-12 15:41:10 +02:00
|
|
|
let (weights, sets) = read_file(translator, experiment, parser_experiment)?;
|
|
|
|
|
|
2025-09-07 17:55:53 +02:00
|
|
|
let res = match frequency::Frequency::fast_frequency(
|
|
|
|
|
&sets,
|
|
|
|
|
&sys.reaction_rules,
|
|
|
|
|
&sys.available_entities,
|
|
|
|
|
&weights,
|
|
|
|
|
) {
|
2025-09-11 02:49:14 +02:00
|
|
|
| Some(e) => e,
|
|
|
|
|
| None => {
|
2025-09-07 17:55:53 +02:00
|
|
|
return Err("Error calculating frequency.".into());
|
2025-09-11 02:49:14 +02:00
|
|
|
},
|
2025-09-07 17:55:53 +02:00
|
|
|
};
|
2025-07-11 19:36:20 +02:00
|
|
|
|
2025-07-12 02:42:28 +02:00
|
|
|
Ok(format!(
|
2025-07-12 15:41:10 +02:00
|
|
|
"Frequency of encountered symbols:\n{}",
|
2025-08-24 02:01:24 +02:00
|
|
|
translator::Formatter::from(translator, &res)
|
2025-07-12 02:42:28 +02:00
|
|
|
))
|
2025-07-11 19:36:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Computes the LTS.
|
|
|
|
|
/// equivalent to main_do(digraph, Arcs) or to main_do(advdigraph, Arcs)
|
2025-07-12 15:41:10 +02:00
|
|
|
pub fn digraph(system: &mut EvaluatedSystem) -> Result<(), String> {
|
2025-07-16 00:05:14 +02:00
|
|
|
if let EvaluatedSystem::System { sys, translator } = system {
|
2025-09-07 17:55:53 +02:00
|
|
|
*system = EvaluatedSystem::Graph {
|
2025-09-11 02:49:14 +02:00
|
|
|
graph: sys.digraph()?,
|
2025-09-07 17:55:53 +02:00
|
|
|
translator: translator.to_owned(),
|
|
|
|
|
};
|
2025-07-16 00:05:14 +02:00
|
|
|
}
|
2025-07-12 02:42:28 +02:00
|
|
|
Ok(())
|
2025-07-11 19:36:20 +02:00
|
|
|
}
|
|
|
|
|
|
2025-09-10 22:41:40 +02:00
|
|
|
pub fn grouping(
|
|
|
|
|
system: &mut EvaluatedSystem,
|
|
|
|
|
group: &assert::grouping::Assert,
|
|
|
|
|
) -> Result<(), String> {
|
|
|
|
|
let mut buckets = HashMap::new();
|
|
|
|
|
let mut leader: HashMap<petgraph::prelude::NodeIndex, _> = HashMap::new();
|
|
|
|
|
|
|
|
|
|
if let EvaluatedSystem::Graph { graph, translator } = system {
|
|
|
|
|
for node in graph.node_indices() {
|
|
|
|
|
let val = group.execute(graph, &node, translator)?;
|
2025-09-11 02:17:40 +02:00
|
|
|
println!("node: {node:?} -> val: {val:?}");
|
2025-09-10 22:41:40 +02:00
|
|
|
buckets.entry(val.clone()).or_insert(vec![]).push(node);
|
|
|
|
|
let l = buckets.get(&val).unwrap().first().unwrap();
|
|
|
|
|
leader.insert(node, (*l, val));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for node in graph.node_indices().rev() {
|
|
|
|
|
let (origin, val) = leader.get(&node).unwrap();
|
|
|
|
|
if *origin == node {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if buckets.get(val).unwrap().len() <= 1 {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mut edges =
|
|
|
|
|
graph.neighbors_directed(node, petgraph::Outgoing).detach();
|
|
|
|
|
while let Some(edge) = edges.next_edge(graph) {
|
|
|
|
|
graph.add_edge(
|
|
|
|
|
*origin,
|
|
|
|
|
graph.edge_endpoints(edge).unwrap().1,
|
|
|
|
|
graph.edge_weight(edge).unwrap().clone(),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
let mut edges =
|
|
|
|
|
graph.neighbors_directed(node, petgraph::Incoming).detach();
|
|
|
|
|
while let Some(edge) = edges.next_edge(graph) {
|
|
|
|
|
graph.add_edge(
|
|
|
|
|
graph.edge_endpoints(edge).unwrap().0,
|
|
|
|
|
*origin,
|
|
|
|
|
graph.edge_weight(edge).unwrap().clone(),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
graph
|
|
|
|
|
.remove_node(node)
|
|
|
|
|
.ok_or(format!("Could not remove node {node:?}"))?;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
} else {
|
|
|
|
|
Err("Grouping can be done only on graphs.".into())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-16 18:03:31 +02:00
|
|
|
/// Computes bisimularity of two provided systems
|
2025-07-16 00:05:14 +02:00
|
|
|
pub fn bisimilar(
|
|
|
|
|
system_a: &mut EvaluatedSystem,
|
2025-09-10 22:41:40 +02:00
|
|
|
edge_relabeler: &assert::relabel::Assert,
|
2025-09-07 17:55:53 +02:00
|
|
|
system_b: String,
|
|
|
|
|
) -> Result<String, String> {
|
2025-09-10 22:41:40 +02:00
|
|
|
use assert::relabel::AssertReturnValue;
|
2025-08-15 00:32:58 +02:00
|
|
|
|
2025-09-07 17:55:53 +02:00
|
|
|
let system_b = read_file(
|
|
|
|
|
system_a.get_translator(),
|
|
|
|
|
system_b.to_string(),
|
|
|
|
|
parser_instructions,
|
|
|
|
|
)?;
|
2025-09-10 22:41:40 +02:00
|
|
|
let mut system_b = match system_b
|
|
|
|
|
.system
|
|
|
|
|
.compute(system_a.get_translator().clone())?
|
|
|
|
|
{
|
2025-09-11 02:49:14 +02:00
|
|
|
| EvaluatedSystem::System { sys, translator } =>
|
|
|
|
|
EvaluatedSystem::System { sys, translator },
|
|
|
|
|
| EvaluatedSystem::Graph { graph, translator } => {
|
2025-09-07 17:55:53 +02:00
|
|
|
if translator != *system_a.get_translator() {
|
|
|
|
|
return Err("Bisimilarity not implemented for systems with \
|
2025-09-10 22:41:40 +02:00
|
|
|
different encodings. Serialize the systems \
|
|
|
|
|
with the same translator."
|
2025-09-07 17:55:53 +02:00
|
|
|
.into());
|
|
|
|
|
}
|
|
|
|
|
EvaluatedSystem::Graph { graph, translator }
|
2025-09-11 02:49:14 +02:00
|
|
|
},
|
2025-09-07 17:55:53 +02:00
|
|
|
};
|
2025-07-16 00:05:14 +02:00
|
|
|
|
2025-07-16 18:34:33 +02:00
|
|
|
digraph(system_a)?;
|
2025-07-16 00:05:14 +02:00
|
|
|
digraph(&mut system_b)?;
|
|
|
|
|
|
2025-07-16 18:34:33 +02:00
|
|
|
// since we ran digraph on both they have to be graphs
|
2025-08-15 00:32:58 +02:00
|
|
|
match (system_a, &mut system_b) {
|
2025-09-11 02:49:14 +02:00
|
|
|
| (
|
2025-09-07 17:55:53 +02:00
|
|
|
EvaluatedSystem::Graph {
|
|
|
|
|
graph: a,
|
|
|
|
|
translator: _,
|
|
|
|
|
},
|
|
|
|
|
EvaluatedSystem::Graph {
|
|
|
|
|
graph: b,
|
|
|
|
|
translator: translator_b,
|
|
|
|
|
},
|
|
|
|
|
) => {
|
|
|
|
|
let a: Graph<system::System, AssertReturnValue> =
|
|
|
|
|
a.map_edges(edge_relabeler, translator_b)?;
|
|
|
|
|
let b: Graph<system::System, AssertReturnValue> =
|
|
|
|
|
b.map_edges(edge_relabeler, translator_b)?;
|
|
|
|
|
Ok(format!(
|
|
|
|
|
"{}",
|
2025-09-11 02:49:14 +02:00
|
|
|
// bisimilarity::bisimilarity_kanellakis_smolka::bisimilarity(&
|
|
|
|
|
// &a, &&b)
|
2025-09-10 22:41:40 +02:00
|
|
|
// bisimilarity::bisimilarity_paige_tarjan::bisimilarity_ignore_labels(&&a, &&b)
|
|
|
|
|
bisimilarity::bisimilarity_paige_tarkan::bisimilarity(&&a, &&b)
|
2025-09-07 17:55:53 +02:00
|
|
|
))
|
2025-09-11 02:49:14 +02:00
|
|
|
},
|
|
|
|
|
| _ => {
|
2025-09-07 17:55:53 +02:00
|
|
|
unreachable!()
|
2025-09-11 02:49:14 +02:00
|
|
|
},
|
2025-07-16 00:05:14 +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 15:32:21 +02:00
|
|
|
pub fn dot(
|
|
|
|
|
system: &EvaluatedSystem,
|
2025-07-26 22:46:10 +02:00
|
|
|
node_display: graph::NodeDisplay,
|
2025-07-27 14:59:35 +02:00
|
|
|
edge_display: graph::EdgeDisplay,
|
2025-07-26 21:22:30 +02:00
|
|
|
node_color: graph::NodeColor,
|
2025-09-07 17:55:53 +02:00
|
|
|
edge_color: graph::EdgeColor,
|
2025-07-12 15:41:10 +02:00
|
|
|
) -> Result<String, String> {
|
2025-07-12 02:42:28 +02:00
|
|
|
match system {
|
2025-09-11 02:49:14 +02:00
|
|
|
| EvaluatedSystem::System {
|
2025-07-12 15:41:10 +02:00
|
|
|
sys: _,
|
|
|
|
|
translator: _,
|
|
|
|
|
} => Err("Supplied system is not a graph".into()),
|
2025-09-11 02:49:14 +02:00
|
|
|
| EvaluatedSystem::Graph { graph, translator } => {
|
2025-07-12 15:41:10 +02:00
|
|
|
let rc_translator = Rc::new(translator.clone());
|
|
|
|
|
let modified_graph = graph.map(
|
2025-07-26 22:46:10 +02:00
|
|
|
node_display.generate(Rc::clone(&rc_translator), graph),
|
2025-07-27 14:59:35 +02:00
|
|
|
edge_display.generate(Rc::clone(&rc_translator), graph),
|
2025-07-12 15:41:10 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let graph = Rc::new(graph.to_owned());
|
|
|
|
|
|
2025-09-10 22:41:40 +02:00
|
|
|
let node_formatter = node_color
|
|
|
|
|
.generate(Rc::clone(&graph), translator.encode_not_mut("*"));
|
2025-09-07 17:55:53 +02:00
|
|
|
let edge_formatter = edge_color.generate(Rc::clone(&graph));
|
|
|
|
|
|
2025-09-10 22:41:40 +02:00
|
|
|
let dot = dot::Dot::with_attr_getters(
|
|
|
|
|
&modified_graph,
|
|
|
|
|
&[],
|
|
|
|
|
&edge_formatter,
|
|
|
|
|
&node_formatter,
|
|
|
|
|
);
|
2025-07-12 15:41:10 +02:00
|
|
|
|
|
|
|
|
Ok(format!("{dot}"))
|
2025-09-11 02:49:14 +02:00
|
|
|
},
|
2025-07-12 02:42:28 +02:00
|
|
|
}
|
2025-07-11 19:36:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Writes the specified graph to a file in .graphml format.
|
2025-07-13 17:28:13 +02:00
|
|
|
pub fn graphml(
|
|
|
|
|
system: &EvaluatedSystem,
|
2025-07-26 22:46:10 +02:00
|
|
|
node_display: graph::NodeDisplay,
|
2025-07-27 14:59:35 +02:00
|
|
|
edge_display: graph::EdgeDisplay,
|
2025-07-13 17:28:13 +02:00
|
|
|
) -> Result<String, String> {
|
2025-07-12 02:42:28 +02:00
|
|
|
match system {
|
2025-09-11 02:49:14 +02:00
|
|
|
| EvaluatedSystem::System {
|
2025-07-12 15:41:10 +02:00
|
|
|
sys: _,
|
|
|
|
|
translator: _,
|
|
|
|
|
} => Err("Supplied system is not a graph".into()),
|
2025-09-11 02:49:14 +02:00
|
|
|
| EvaluatedSystem::Graph { graph, translator } => {
|
2025-07-12 15:41:10 +02:00
|
|
|
let rc_translator = Rc::new(translator.to_owned());
|
|
|
|
|
|
|
|
|
|
// map each value to the corresponding value we want to display
|
|
|
|
|
let modified_graph = graph.map(
|
2025-09-07 17:55:53 +02:00
|
|
|
node_display.generate(Rc::clone(&rc_translator), graph),
|
|
|
|
|
edge_display.generate(rc_translator, graph),
|
2025-07-12 15:41:10 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
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-09-11 02:49:14 +02:00
|
|
|
},
|
2025-07-12 02:42:28 +02:00
|
|
|
}
|
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.
|
2025-07-12 15:41:10 +02:00
|
|
|
pub fn serialize(system: &EvaluatedSystem, path: String) -> Result<(), String> {
|
2025-07-12 02:42:28 +02:00
|
|
|
match system {
|
2025-09-11 02:49:14 +02:00
|
|
|
| EvaluatedSystem::System {
|
2025-07-12 15:41:10 +02:00
|
|
|
sys: _,
|
|
|
|
|
translator: _,
|
|
|
|
|
} => Err("Supplied system is not a graph".into()),
|
2025-09-11 02:49:14 +02:00
|
|
|
| EvaluatedSystem::Graph { graph, translator } => {
|
2025-07-12 15:41:10 +02:00
|
|
|
// relative path
|
|
|
|
|
let mut path = std::path::PathBuf::from(path);
|
|
|
|
|
path.set_extension("cbor");
|
|
|
|
|
|
|
|
|
|
let f = match fs::File::create(&path) {
|
2025-09-11 02:49:14 +02:00
|
|
|
| Ok(f) => f,
|
|
|
|
|
| Err(_) => {
|
2025-09-10 22:41:40 +02:00
|
|
|
return Err(format!(
|
|
|
|
|
"Error creating file {}.",
|
|
|
|
|
path.to_str().unwrap()
|
|
|
|
|
));
|
2025-09-11 02:49:14 +02:00
|
|
|
},
|
2025-07-12 15:41:10 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
match serialize::ser(f, graph, translator) {
|
2025-09-11 02:49:14 +02:00
|
|
|
| Ok(_) => Ok(()),
|
|
|
|
|
| Err(_) => Err("Error during serialization.".into()),
|
2025-07-12 15:41:10 +02:00
|
|
|
}
|
2025-09-11 02:49:14 +02:00
|
|
|
},
|
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
|
2025-09-10 22:41:40 +02:00
|
|
|
pub fn deserialize(
|
|
|
|
|
input_path: String,
|
|
|
|
|
) -> Result<(graph::SystemGraph, Translator), String> {
|
2025-07-11 19:36:20 +02:00
|
|
|
// relative path
|
|
|
|
|
let mut path = match env::current_dir() {
|
2025-09-11 02:49:14 +02:00
|
|
|
| Ok(p) => p,
|
|
|
|
|
| Err(_) => return Err("Error getting current directory.".into()),
|
2025-07-11 19:36:20 +02:00
|
|
|
};
|
|
|
|
|
path = path.join(input_path);
|
|
|
|
|
path.set_extension("cbor");
|
|
|
|
|
|
|
|
|
|
let f = match fs::File::open(&path) {
|
2025-09-11 02:49:14 +02:00
|
|
|
| Ok(f) => f,
|
|
|
|
|
| Err(_) => {
|
2025-09-10 22:41:40 +02:00
|
|
|
return Err(format!(
|
|
|
|
|
"Error opening file {}.",
|
|
|
|
|
path.to_str().unwrap()
|
|
|
|
|
));
|
2025-09-11 02:49:14 +02:00
|
|
|
},
|
2025-07-11 19:36:20 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
match serialize::de(f) {
|
2025-09-11 02:49:14 +02:00
|
|
|
| Ok(a) => Ok(a),
|
|
|
|
|
| Err(_) => Err("Error during deserialization.".into()),
|
2025-07-11 19:36:20 +02:00
|
|
|
}
|
|
|
|
|
}
|
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) => {
|
2025-07-12 15:41:10 +02:00
|
|
|
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)?;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-07-12 02:42:28 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-10 22:41:40 +02:00
|
|
|
fn execute(
|
|
|
|
|
instruction: Instruction,
|
|
|
|
|
system: &mut EvaluatedSystem,
|
|
|
|
|
) -> Result<(), String> {
|
2025-07-12 02:42:28 +02:00
|
|
|
match instruction {
|
2025-09-11 02:49:14 +02:00
|
|
|
| Instruction::Stats { so } => {
|
2025-07-12 15:41:10 +02:00
|
|
|
save_options!(stats(system)?, so);
|
2025-09-11 02:49:14 +02:00
|
|
|
},
|
|
|
|
|
| Instruction::Target { so } => {
|
2025-07-12 15:41:10 +02:00
|
|
|
save_options!(target(system)?, so);
|
2025-09-11 02:49:14 +02:00
|
|
|
},
|
|
|
|
|
| Instruction::Run { so } => {
|
2025-07-12 15:41:10 +02:00
|
|
|
save_options!(traversed(system)?, so);
|
2025-09-11 02:49:14 +02:00
|
|
|
},
|
|
|
|
|
| Instruction::Loop { symbol, so } => {
|
2025-07-12 15:41:10 +02:00
|
|
|
save_options!(hoop(system, symbol)?, so);
|
2025-09-11 02:49:14 +02:00
|
|
|
},
|
|
|
|
|
| Instruction::Frequency { so } => {
|
2025-07-12 15:41:10 +02:00
|
|
|
save_options!(freq(system)?, so);
|
2025-09-11 02:49:14 +02:00
|
|
|
},
|
|
|
|
|
| Instruction::LimitFrequency { experiment, so } => {
|
2025-07-12 15:41:10 +02:00
|
|
|
save_options!(limit_freq(system, experiment)?, so);
|
2025-09-11 02:49:14 +02:00
|
|
|
},
|
|
|
|
|
| Instruction::FastFrequency { experiment, so } => {
|
2025-07-12 15:41:10 +02:00
|
|
|
save_options!(fast_freq(system, experiment)?, so);
|
2025-09-11 02:49:14 +02:00
|
|
|
},
|
|
|
|
|
| Instruction::Digraph { group, gso } => {
|
2025-09-10 22:41:40 +02:00
|
|
|
let mut graph = system.clone();
|
|
|
|
|
digraph(&mut graph)?;
|
|
|
|
|
if let Some(group) = group {
|
|
|
|
|
group.typecheck()?;
|
|
|
|
|
grouping(&mut graph, &group)?;
|
|
|
|
|
}
|
2025-07-12 15:41:10 +02:00
|
|
|
for save in gso {
|
|
|
|
|
match save {
|
2025-09-11 02:49:14 +02:00
|
|
|
| GraphSaveOptions::Dot {
|
2025-07-12 15:41:10 +02:00
|
|
|
node_display: nd,
|
|
|
|
|
edge_display: ed,
|
2025-09-07 17:55:53 +02:00
|
|
|
node_color: nc,
|
|
|
|
|
edge_color: ec,
|
2025-07-12 15:41:10 +02:00
|
|
|
so,
|
|
|
|
|
} => {
|
2025-09-10 22:41:40 +02:00
|
|
|
save_options!(dot(&graph, nd, ed, nc, ec)?, so);
|
2025-09-11 02:49:14 +02:00
|
|
|
},
|
|
|
|
|
| GraphSaveOptions::GraphML {
|
2025-07-13 17:28:13 +02:00
|
|
|
node_display: nd,
|
|
|
|
|
edge_display: ed,
|
2025-07-12 15:41:10 +02:00
|
|
|
so,
|
|
|
|
|
} => {
|
2025-09-10 22:41:40 +02:00
|
|
|
save_options!(graphml(&graph, nd, ed)?, so);
|
2025-09-11 02:49:14 +02:00
|
|
|
},
|
|
|
|
|
| GraphSaveOptions::Serialize { path } => {
|
2025-09-10 22:41:40 +02:00
|
|
|
serialize(&graph, path)?;
|
2025-09-11 02:49:14 +02:00
|
|
|
},
|
2025-07-12 15:41:10 +02:00
|
|
|
}
|
|
|
|
|
}
|
2025-09-11 02:49:14 +02:00
|
|
|
},
|
|
|
|
|
| Instruction::Bisimilarity {
|
2025-09-07 17:55:53 +02:00
|
|
|
system_b,
|
|
|
|
|
edge_relabeler,
|
|
|
|
|
so,
|
|
|
|
|
} => {
|
|
|
|
|
edge_relabeler.typecheck()?;
|
|
|
|
|
save_options!(bisimilar(system, &edge_relabeler, system_b)?, so);
|
2025-09-11 02:49:14 +02:00
|
|
|
},
|
2025-07-12 02:42:28 +02:00
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-26 19:43:20 +02:00
|
|
|
/// Interprets file at supplied path, then executes the code specified as
|
|
|
|
|
/// instructions inside the file.
|
2025-07-11 23:49:59 +02:00
|
|
|
pub fn run(path: String) -> Result<(), String> {
|
|
|
|
|
let mut translator = Translator::new();
|
|
|
|
|
|
2025-07-12 15:41:10 +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 {
|
2025-07-12 15:41:10 +02:00
|
|
|
execute(instr, &mut system)?;
|
2025-07-12 02:42:28 +02:00
|
|
|
}
|
2025-07-11 23:49:59 +02:00
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|