Files
ReactionSystems/src/rsprocess/graph.rs

553 lines
13 KiB
Rust
Raw Normal View History

2025-07-10 15:02:14 +02:00
//! Definitions for generating graphs from a simulation.
use petgraph::{Graph, Directed};
2025-07-05 14:54:43 +02:00
use std::collections::HashMap;
2025-07-26 16:46:48 +02:00
use super::structure::{RSlabel, RSsystem, RSset};
2025-07-05 14:54:43 +02:00
use super::support_structures::TransitionsIterator;
2025-07-13 17:28:13 +02:00
use super::translator::{self, IdType};
2025-07-07 01:25:38 +02:00
use std::rc::Rc;
2025-07-05 14:54:43 +02:00
2025-07-16 00:05:14 +02:00
pub type RSgraph = Graph<RSsystem, RSlabel, Directed, u32>;
2025-07-10 15:02:14 +02:00
/// Creates a graph starting from a system as root node
2025-07-05 14:54:43 +02:00
pub fn digraph(
system: RSsystem
) -> Result<RSgraph, String> {
let mut graph = Graph::default();
2025-07-05 14:54:43 +02:00
let node = graph.add_node(system.clone());
let mut association = HashMap::new();
association.insert(system.clone(), node);
let mut stack = vec![system];
while let Some(current) = stack.pop() {
2025-07-05 14:54:43 +02:00
// depth first
let current_node = *association.get(&current).unwrap();
for (label, next) in TransitionsIterator::from(&current)? {
2025-07-05 14:54:43 +02:00
// if not already visited
let next_node = association.entry(next.clone()).or_insert_with(|| {
stack.push(next.clone());
graph.add_node(next)
});
graph.add_edge(current_node, *next_node, label);
}
}
Ok(graph)
}
2025-07-07 01:25:38 +02:00
2025-07-26 21:22:30 +02:00
pub fn common_entities(graph: &RSgraph) -> RSset {
graph.node_references().fold(
None,
|acc, node|
match acc {
None => Some(node.1.available_entities.clone()),
Some(acc) => Some(node.1.available_entities.intersection(&acc))
}
).unwrap_or(RSset::new())
}
2025-07-07 01:25:38 +02:00
// -----------------------------------------------------------------------------
// helper functions
// -----------------------------------------------------------------------------
// Nodes -----------------------------------------------------------------------
/// Helper structure that specifies what information to display for nodes.
#[derive(Clone)]
pub enum NodeDisplayBase {
String { string: String },
2025-07-07 01:25:38 +02:00
Hide,
Entities,
MaskEntities { mask: RSset },
ExcludeEntities { mask: RSset },
2025-07-07 01:25:38 +02:00
Context,
UncommonEntities,
MaskUncommonEntities { mask: RSset }
}
pub struct NodeDisplay {
pub base: Vec<NodeDisplayBase>
2025-07-07 01:25:38 +02:00
}
2025-07-26 21:22:30 +02:00
type GraphMapNodesFnTy<'a> =
dyn Fn(petgraph::prelude::NodeIndex, &'a RSsystem) -> String + 'a;
2025-07-07 01:25:38 +02:00
fn match_node_display<'a>(
base: &NodeDisplayBase,
common_entities: Rc<RSset>,
translator: Rc<translator::Translator>
) -> Box<GraphMapNodesFnTy<'a>> {
use NodeDisplayBase::*;
use super::format_helpers::graph_map_nodes_ty_from::*;
match base {
String { string } => {
format_string(string.clone())
},
Hide => {
format_hide(translator)
},
Entities => {
format_entities(translator)
},
MaskEntities { mask } => {
format_mask_entities(translator, mask.clone())
},
ExcludeEntities { mask } => {
format_exclude_entities(translator, mask.clone())
},
Context => {
format_context(translator)
},
UncommonEntities => {
format_exclude_entities(translator, (*common_entities).clone())
},
MaskUncommonEntities { mask } => {
format_exclude_entities(translator,
mask.intersection(&common_entities))
}
}
}
2025-07-26 16:46:48 +02:00
impl NodeDisplay {
fn contains_uncommon(&self) -> bool {
self.base.iter().any(
|b|
matches!(b, NodeDisplayBase::UncommonEntities |
NodeDisplayBase::MaskUncommonEntities { mask: _ }))
}
pub fn generate<'a>(
self,
translator: Rc<translator::Translator>,
current_graph: &RSgraph
) -> Box<GraphMapNodesFnTy<'a>> {
let common_entities =
if self.contains_uncommon() {
Rc::new(common_entities(current_graph))
} else {
Rc::new(RSset::new())
2025-07-07 01:25:38 +02:00
};
Box::new(
move |i, n| {
let mut accumulator = String::new();
for b in &self.base {
let f = match_node_display(b,
Rc::clone(&common_entities),
Rc::clone(&translator));
2025-07-07 01:25:38 +02:00
accumulator.push_str(&(f)(i, n));
}
accumulator
}
)
2025-07-07 01:25:38 +02:00
}
}
2025-07-07 01:25:38 +02:00
// Edges -----------------------------------------------------------------------
/// Helper structure that specifies what information to display for edges
#[derive(Clone)]
pub enum EdgeDisplayBase {
2025-07-26 20:01:46 +02:00
String { string: String },
2025-07-07 01:25:38 +02:00
Hide,
Products,
MaskProducts { mask: RSset },
Entities,
MaskEntities { mask: RSset },
Context,
MaskContext { mask: RSset },
Union,
MaskUnion { mask: RSset },
Difference,
MaskDifference { mask: RSset },
EntitiesDeleted,
MaskEntitiesDeleted { mask: RSset },
EntitiesAdded,
MaskEntitiesAdded { mask: RSset },
}
pub struct EdgeDisplay {
pub base: Vec<EdgeDisplayBase>
2025-07-07 01:25:38 +02:00
}
type GraphMapEdgesFnTy<'a> =
dyn Fn(petgraph::prelude::EdgeIndex, &'a RSlabel) -> String + 'a;
2025-07-26 20:16:32 +02:00
2025-07-26 16:46:48 +02:00
fn match_edge_display<'a>(
base: &'a EdgeDisplayBase,
translator: Rc<translator::Translator>
) -> Box<GraphMapEdgesFnTy<'a>> {
use EdgeDisplayBase::*;
use super::format_helpers::graph_map_edges_ty_from::*;
2025-07-26 20:16:32 +02:00
match base {
String { string } => {
format_string(translator, string.clone())
2025-07-26 20:16:32 +02:00
}
Hide => {
format_hide(translator)
},
Products => {
format_products(translator)
},
MaskProducts { mask } => {
format_mask_products(translator, mask.clone())
},
Entities => {
format_entities(translator)
},
MaskEntities { mask } => {
format_mask_entities(translator, mask.clone())
},
Context => {
format_context(translator)
},
MaskContext { mask } => {
format_mask_context(translator, mask.clone())
},
Union => {
format_union(translator)
},
MaskUnion { mask } => {
format_mask_union(translator, mask.clone())
},
Difference => {
format_difference(translator)
},
MaskDifference { mask } => {
format_mask_difference(translator, mask.clone())
},
EntitiesDeleted => {
format_entities_deleted(translator)
},
MaskEntitiesDeleted { mask } => {
format_mask_entities_deleted(translator, mask.clone())
},
EntitiesAdded => {
format_entities_added(translator)
},
MaskEntitiesAdded { mask } => {
format_mask_entities_added(translator, mask.clone())
},
2025-07-07 01:25:38 +02:00
}
2025-07-26 20:16:32 +02:00
}
2025-07-07 01:25:38 +02:00
2025-07-26 20:16:32 +02:00
impl EdgeDisplay {
pub fn generate<'a>(
self,
translator: Rc<translator::Translator>,
_current_graph: &RSgraph
) -> Box<GraphMapEdgesFnTy<'a>> {
Box::new(
move |i, n| {
let mut accumulator = String::new();
for b in &self.base {
let f = match_edge_display(b,
Rc::clone(&translator));
accumulator.push_str(&(f)(i, n));
}
accumulator
}
)
2025-07-07 01:25:38 +02:00
}
}
// -----------------------------------------------------------------------------
2025-07-26 20:01:46 +02:00
// Color Nodes & Edges
// -----------------------------------------------------------------------------
2025-07-26 21:36:56 +02:00
// Node ------------------------------------------------------------------------
2025-07-26 16:46:48 +02:00
use petgraph::visit::{IntoEdgeReferences, IntoNodeReferences};
type RSdotGraph = Graph<String, String, Directed, u32>;
2025-07-26 21:22:30 +02:00
type RSformatNodeTy<'a> =
2025-07-13 17:28:13 +02:00
dyn Fn(
2025-07-26 21:22:30 +02:00
&'a RSdotGraph,
<&'a RSdotGraph as IntoNodeReferences>::NodeRef
) -> String + 'a;
type RSformatNodeTyOpt<'a> =
dyn Fn(
&'a RSdotGraph,
<&'a RSdotGraph as IntoNodeReferences>::NodeRef
) -> Option<String> + 'a;
2025-07-13 17:28:13 +02:00
#[derive(Clone, Copy)]
pub enum OperationType {
Equals,
Subset,
SubsetEqual,
Superset,
SupersetEqual
}
impl OperationType {
pub fn evaluate(&self, a: &RSset, b: &RSset) -> bool {
match self {
Self::Equals => {
a.is_subset(b) && b.is_subset(a)
},
Self::Subset => {
a.is_subset(b) && !b.is_subset(a)
},
Self::SubsetEqual => {
a.is_subset(b)
},
Self::Superset => {
b.is_subset(a) && !a.is_subset(b)
},
Self::SupersetEqual => {
b.is_subset(a)
}
}
}
}
#[derive(Clone)]
pub enum ContextColorConditional {
Nill,
RecursiveIdentifier(IdType),
EntitySet(OperationType, RSset),
NonDeterministicChoice,
Summation,
WaitEntity
}
#[derive(Clone)]
pub enum NodeColorConditional {
ContextConditional(ContextColorConditional),
EntitiesConditional(OperationType, RSset)
}
#[derive(Clone)]
pub struct NodeColor {
pub conditionals: Vec<(NodeColorConditional, String)>,
2025-07-26 21:22:30 +02:00
pub base_color: String,
2025-07-13 17:28:13 +02:00
}
2025-07-26 21:22:30 +02:00
#[inline(always)]
2025-07-26 21:36:56 +02:00
fn node_formatter_base_color(
2025-07-13 18:14:35 +02:00
base_color: String
) -> String
{
", fillcolor=".to_string() + &base_color
}
2025-07-26 21:22:30 +02:00
#[inline(always)]
fn match_node_color_conditional<'a>(
2025-07-26 21:36:56 +02:00
rule: &'a NodeColorConditional,
color: &'a String,
2025-07-13 17:28:13 +02:00
original_graph: Rc<RSgraph>,
2025-07-26 21:22:30 +02:00
star: Option<IdType>
) -> Box<RSformatNodeTyOpt<'a>> {
2025-07-26 16:46:48 +02:00
use super::format_helpers::node_formatter::*;
2025-07-13 17:28:13 +02:00
match rule {
NodeColorConditional::ContextConditional(ccc) => {
match ccc {
ContextColorConditional::Nill => {
2025-07-26 21:22:30 +02:00
format_nill(Rc::clone(&original_graph),
color.to_string(),
star)
2025-07-13 17:28:13 +02:00
},
ContextColorConditional::RecursiveIdentifier(s) => {
2025-07-26 21:22:30 +02:00
format_recursive_identifier(Rc::clone(&original_graph),
color.to_string(),
star,
*s)
2025-07-13 17:28:13 +02:00
},
ContextColorConditional::EntitySet(ot, set) => {
2025-07-26 21:22:30 +02:00
format_entity_set(Rc::clone(&original_graph),
color.to_string(),
star,
*ot,
set.clone())
2025-07-13 17:28:13 +02:00
},
ContextColorConditional::NonDeterministicChoice => {
2025-07-26 21:22:30 +02:00
format_non_deterministic_choice(Rc::clone(&original_graph),
color.to_string(),
star)
2025-07-13 17:28:13 +02:00
},
ContextColorConditional::Summation => {
2025-07-26 21:22:30 +02:00
format_summation(Rc::clone(&original_graph),
color.to_string(),
star)
2025-07-13 17:28:13 +02:00
},
ContextColorConditional::WaitEntity => {
2025-07-26 21:22:30 +02:00
format_wait_entity(Rc::clone(&original_graph),
color.to_string(),
star)
2025-07-13 17:28:13 +02:00
},
}
2025-07-13 17:28:13 +02:00
},
NodeColorConditional::EntitiesConditional(ot, set) => {
2025-07-26 21:22:30 +02:00
format_entities_conditional(Rc::clone(&original_graph),
color.to_string(),
star,
*ot,
set.clone())
2025-07-13 17:28:13 +02:00
},
}
}
2025-07-26 21:22:30 +02:00
impl NodeColor {
pub fn generate<'a>(
self,
original_graph: Rc<RSgraph>,
star: Option<IdType>
) -> Box<RSformatNodeTy<'a>> {
Box::new(
move |i, n| {
for (rule, color) in &self.conditionals {
let f = match_node_color_conditional(
rule,
color,
Rc::clone(&original_graph),
star
);
if let Some(s) = (f)(i, n) {
return s
}
}
node_formatter_base_color(self.base_color.clone())
}
)
}
}
2025-07-26 21:36:56 +02:00
// Edge ------------------------------------------------------------------------
type RSformatEdgeTy<'a> =
2025-07-13 19:08:39 +02:00
dyn Fn(
2025-07-26 21:36:56 +02:00
&'a RSdotGraph,
<&'a RSdotGraph as IntoEdgeReferences>::EdgeRef
) -> String + 'a;
type RSformatEdgeTyOpt<'a> =
dyn Fn(
&'a RSdotGraph,
<&'a RSdotGraph as IntoEdgeReferences>::EdgeRef
) -> Option<String> + 'a;
2025-07-13 19:08:39 +02:00
#[derive(Clone)]
pub enum EdgeColorConditional {
Entities(OperationType, RSset),
Context(OperationType, RSset),
T(OperationType, RSset),
Reactants(OperationType, RSset),
ReactantsAbsent(OperationType, RSset),
Inhibitors(OperationType, RSset),
InhibitorsPresent(OperationType, RSset),
Products(OperationType, RSset),
}
#[derive(Clone)]
pub struct EdgeColor {
pub conditionals: Vec<(EdgeColorConditional, String)>,
pub base_color: String
}
2025-07-26 21:36:56 +02:00
fn edge_formatter_base_color(
2025-07-13 19:08:39 +02:00
base_color: String
) -> String
{
2025-07-13 19:32:11 +02:00
", color=".to_string() + &base_color
2025-07-13 19:08:39 +02:00
}
2025-07-26 21:36:56 +02:00
fn match_edge_color_conditional<'a>(
rule: &'a EdgeColorConditional,
color: &'a String,
original_graph: Rc<RSgraph>
) -> Box<RSformatEdgeTyOpt<'a>> {
2025-07-26 16:46:48 +02:00
use super::format_helpers::edge_formatter::*;
2025-07-13 19:08:39 +02:00
match rule {
EdgeColorConditional::Entities(ot, set) => {
2025-07-26 21:36:56 +02:00
format_entities(Rc::clone(&original_graph),
color.to_string(),
*ot,
set.clone())
2025-07-13 19:08:39 +02:00
},
EdgeColorConditional::Context(ot, set) => {
2025-07-26 21:36:56 +02:00
format_context(Rc::clone(&original_graph),
color.to_string(),
*ot,
set.clone())
2025-07-13 19:08:39 +02:00
},
EdgeColorConditional::T(ot, set) => {
2025-07-26 21:36:56 +02:00
format_t(Rc::clone(&original_graph),
color.to_string(),
*ot,
set.clone())
2025-07-13 19:08:39 +02:00
},
EdgeColorConditional::Reactants(ot, set) => {
2025-07-26 21:36:56 +02:00
format_reactants(Rc::clone(&original_graph),
color.to_string(),
*ot,
set.clone())
2025-07-13 19:08:39 +02:00
},
EdgeColorConditional::ReactantsAbsent(ot, set) => {
2025-07-26 21:36:56 +02:00
format_reactants_absent(Rc::clone(&original_graph),
color.to_string(),
*ot,
set.clone())
2025-07-13 19:08:39 +02:00
},
EdgeColorConditional::Inhibitors(ot, set) => {
2025-07-26 21:36:56 +02:00
format_inhibitors(Rc::clone(&original_graph),
color.to_string(),
*ot,
set.clone())
2025-07-13 19:08:39 +02:00
},
EdgeColorConditional::InhibitorsPresent(ot, set) => {
2025-07-26 21:36:56 +02:00
format_inhibitors_present(Rc::clone(&original_graph),
color.to_string(),
*ot,
set.clone())
2025-07-13 19:08:39 +02:00
},
EdgeColorConditional::Products(ot, set) => {
2025-07-26 21:36:56 +02:00
format_products(Rc::clone(&original_graph),
color.to_string(),
*ot,
set.clone())
2025-07-13 19:08:39 +02:00
},
}
}
2025-07-26 21:36:56 +02:00
impl EdgeColor {
pub fn generate<'a>(
self,
original_graph: Rc<RSgraph>,
) -> Box<RSformatEdgeTy<'a>> {
Box::new(
move |i, n| {
for (rule, color) in &self.conditionals {
let f = match_edge_color_conditional(
rule,
color,
Rc::clone(&original_graph),
);
if let Some(s) = (f)(i, n) {
return s
}
}
edge_formatter_base_color(self.base_color.clone())
}
)
}
}