implementing digraph generation
This commit is contained in:
39
src/rsprocess/graph.rs
Normal file
39
src/rsprocess/graph.rs
Normal file
@ -0,0 +1,39 @@
|
||||
#![allow(dead_code)]
|
||||
|
||||
use petgraph::Graph;
|
||||
use std::collections::HashMap;
|
||||
use super::structure::{RSlabel, RSsystem};
|
||||
use super::support_structures::TransitionsIterator;
|
||||
|
||||
pub fn digraph(
|
||||
system: RSsystem
|
||||
) -> Result<Graph<RSsystem, RSlabel>, String> {
|
||||
let mut graph: Graph<RSsystem, RSlabel> = Graph::new();
|
||||
let node = graph.add_node(system.clone());
|
||||
|
||||
let mut association = HashMap::new();
|
||||
association.insert(system.clone(), node);
|
||||
|
||||
let mut stack = vec![system];
|
||||
let mut current;
|
||||
|
||||
|
||||
while !stack.is_empty() {
|
||||
// depth first
|
||||
current = stack.pop().unwrap();
|
||||
let current_node = *association.get(¤t).unwrap();
|
||||
|
||||
// cycle through all next nodes
|
||||
let tr = TransitionsIterator::from(¤t)?;
|
||||
|
||||
for (label, next) in tr {
|
||||
// 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)
|
||||
}
|
||||
@ -7,3 +7,4 @@ pub mod structure;
|
||||
pub mod support_structures;
|
||||
pub mod transitions;
|
||||
pub mod translator;
|
||||
pub mod graph;
|
||||
|
||||
@ -182,7 +182,7 @@ impl Default for RSreaction {
|
||||
// -----------------------------------------------------------------------------
|
||||
// RSprocess
|
||||
// -----------------------------------------------------------------------------
|
||||
#[derive(Clone, Debug)]
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
||||
pub enum RSprocess {
|
||||
Nill,
|
||||
RecursiveIdentifier {
|
||||
@ -475,6 +475,24 @@ impl RSsystem {
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for RSsystem {
|
||||
// we ignore delta and reaction rules
|
||||
fn eq(&self, other: &RSsystem) -> bool {
|
||||
self.available_entities == other.available_entities &&
|
||||
self.context_process == other.context_process
|
||||
}
|
||||
}
|
||||
|
||||
impl Eq for RSsystem {}
|
||||
|
||||
impl Hash for RSsystem {
|
||||
// ignores delta and reaction rules
|
||||
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
|
||||
self.available_entities.hash(state);
|
||||
self.context_process.hash(state);
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for RSsystem {
|
||||
fn default() -> Self {
|
||||
RSsystem::new()
|
||||
|
||||
@ -141,7 +141,7 @@ fn print_process(
|
||||
use super::structure::RSprocess::*;
|
||||
match process {
|
||||
Nill => {
|
||||
write!(f, "[Nill]")
|
||||
write!(f, "Nill")
|
||||
}
|
||||
RecursiveIdentifier { identifier } => {
|
||||
write!(f,
|
||||
@ -154,7 +154,7 @@ fn print_process(
|
||||
} => {
|
||||
write!(
|
||||
f,
|
||||
"[entities: {}, next_process: {}]",
|
||||
"{}.{}",
|
||||
RSsetDisplay::from(translator, entities),
|
||||
RSprocessDisplay::from(translator, next_process)
|
||||
)
|
||||
@ -166,7 +166,7 @@ fn print_process(
|
||||
} => {
|
||||
write!(
|
||||
f,
|
||||
"[repeat: {repeat}, repeated_process: {}, next_process: {}]",
|
||||
"({})^{repeat}.{}",
|
||||
RSprocessDisplay::from(translator, repeated_process),
|
||||
RSprocessDisplay::from(translator, next_process)
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user