Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Move dot_string to HugrView #271

Merged
merged 1 commit into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ path = "src/lib.rs"

[dependencies]
thiserror = "1.0.28"
portgraph = { version = "0.7.0", features = ["serde", "petgraph"] }
portgraph = { version = "0.7.1", features = ["serde", "petgraph"] }
pyo3 = { version = "0.19.0", optional = true, features = [
"multiple-pymethods",
] }
Expand Down
53 changes: 2 additions & 51 deletions src/hugr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,12 @@ pub use self::validate::ValidationError;
use derive_more::From;
pub use rewrite::{Rewrite, SimpleReplacement, SimpleReplacementError};

use portgraph::dot::{DotFormat, EdgeStyle, NodeStyle, PortStyle};
use portgraph::multiportgraph::MultiPortGraph;
use portgraph::{Hierarchy, LinkView, PortMut, PortView, UnmanagedDenseMap};
use portgraph::{Hierarchy, PortMut, UnmanagedDenseMap};
use thiserror::Error;

pub use self::view::HugrView;
use crate::ops::{OpName, OpType};
use crate::types::EdgeKind;
use crate::ops::OpType;

/// The Hugr data structure.
#[derive(Clone, Debug, PartialEq)]
Expand Down Expand Up @@ -103,53 +101,6 @@ impl Hugr {
pub fn apply_rewrite<E>(&mut self, rw: impl Rewrite<Error = E>) -> Result<(), E> {
rw.apply(self)
}

/// Return dot string showing underlying graph and hierarchy side by side.
pub fn dot_string(&self) -> String {
self.graph
.dot_format()
.with_hierarchy(&self.hierarchy)
.with_node_style(|n| {
NodeStyle::Box(format!(
"({ni}) {name}",
ni = n.index(),
name = self.op_types[n].name()
))
})
.with_port_style(|port| {
let node = self.graph.port_node(port).unwrap();
let optype = self.op_types.get(node);
let offset = self.graph.port_offset(port).unwrap();
match optype.port_kind(offset).unwrap() {
EdgeKind::Static(ty) => {
PortStyle::new(html_escape::encode_text(&format!("{}", ty)))
}
EdgeKind::Value(ty) => {
PortStyle::new(html_escape::encode_text(&format!("{}", ty)))
}
EdgeKind::StateOrder => match self.graph.port_links(port).count() > 0 {
true => PortStyle::text("", false),
false => PortStyle::Hidden,
},
_ => PortStyle::text("", true),
}
})
.with_edge_style(|src, tgt| {
let src_node = self.graph.port_node(src).unwrap();
let src_optype = self.op_types.get(src_node);
let src_offset = self.graph.port_offset(src).unwrap();
let tgt_node = self.graph.port_node(tgt).unwrap();

if self.hierarchy.parent(src_node) != self.hierarchy.parent(tgt_node) {
EdgeStyle::Dashed
} else if src_optype.port_kind(src_offset) == Some(EdgeKind::StateOrder) {
EdgeStyle::Dotted
} else {
EdgeStyle::Solid
}
})
.finish()
}
}

/// Arbitrary metadata for a node.
Expand Down
53 changes: 52 additions & 1 deletion src/hugr/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ use std::ops::Deref;

use context_iterators::{ContextIterator, IntoContextIterator, MapCtx, MapWithCtx, WithCtx};
use itertools::{Itertools, MapInto};
use portgraph::dot::{DotFormat, EdgeStyle, NodeStyle, PortStyle};
use portgraph::{multiportgraph, LinkView, MultiPortGraph, PortView};

use super::{Hugr, NodeMetadata};
use super::{Node, Port};
use crate::ops::OpType;
use crate::ops::{OpName, OpType};
use crate::types::EdgeKind;
use crate::Direction;

/// A trait for inspecting HUGRs.
Expand Down Expand Up @@ -135,6 +137,55 @@ pub trait HugrView: sealed::HugrInternals {

/// Iterates over the input and output neighbours of the `node` in sequence.
fn all_neighbours(&self, node: Node) -> Self::Neighbours<'_>;

/// Return dot string showing underlying graph and hierarchy side by side.
fn dot_string(&self) -> String {
let hugr = self.base_hugr();
let graph = self.portgraph();
graph
.dot_format()
.with_hierarchy(&hugr.hierarchy)
.with_node_style(|n| {
NodeStyle::Box(format!(
"({ni}) {name}",
ni = n.index(),
name = self.get_optype(n.into()).name()
))
})
.with_port_style(|port| {
let node = graph.port_node(port).unwrap();
let optype = self.get_optype(node.into());
let offset = graph.port_offset(port).unwrap();
match optype.port_kind(offset).unwrap() {
EdgeKind::Static(ty) => {
PortStyle::new(html_escape::encode_text(&format!("{}", ty)))
}
EdgeKind::Value(ty) => {
PortStyle::new(html_escape::encode_text(&format!("{}", ty)))
}
EdgeKind::StateOrder => match graph.port_links(port).count() > 0 {
true => PortStyle::text("", false),
false => PortStyle::Hidden,
},
_ => PortStyle::text("", true),
}
})
.with_edge_style(|src, tgt| {
let src_node = graph.port_node(src).unwrap();
let src_optype = self.get_optype(src_node.into());
let src_offset = graph.port_offset(src).unwrap();
let tgt_node = graph.port_node(tgt).unwrap();

if hugr.hierarchy.parent(src_node) != hugr.hierarchy.parent(tgt_node) {
EdgeStyle::Dashed
} else if src_optype.port_kind(src_offset) == Some(EdgeKind::StateOrder) {
EdgeStyle::Dotted
} else {
EdgeStyle::Solid
}
})
.finish()
}
}

impl<T> HugrView for T
Expand Down
Loading