Generate a DOT (Graphviz) representation of a subgraph.
(nodes: &[KgNode], edges: &[KgEdge])
| 509 | |
| 510 | /// Generate a DOT (Graphviz) representation of a subgraph. |
| 511 | pub(crate) fn emit_dot(nodes: &[KgNode], edges: &[KgEdge]) -> String { |
| 512 | let mut out = String::new(); |
| 513 | out.push_str("digraph query {\n"); |
| 514 | out.push_str(" rankdir=LR;\n"); |
| 515 | out.push_str( |
| 516 | " node [shape=box, style=\"filled,rounded\", fontname=\"Helvetica\", fontsize=10];\n", |
| 517 | ); |
| 518 | out.push_str(" edge [fontname=\"Helvetica\", fontsize=8, color=\"#888888\"];\n"); |
| 519 | out.push('\n'); |
| 520 | |
| 521 | // Nodes |
| 522 | for node in nodes { |
| 523 | let (fillcolor, shape) = node_style(node.primary_type()); |
| 524 | let label = node_label(node); |
| 525 | out.push_str(&format!( |
| 526 | " \"{}\" [label=\"{}\", fillcolor=\"{}\", shape={}];\n", |
| 527 | dot_escape_id(&node.id), |
| 528 | dot_escape_label(&label), |
| 529 | fillcolor, |
| 530 | shape, |
| 531 | )); |
| 532 | } |
| 533 | |
| 534 | out.push('\n'); |
| 535 | |
| 536 | // Edges |
| 537 | for edge in edges { |
| 538 | out.push_str(&format!( |
| 539 | " \"{}\" -> \"{}\" [label=\"{}\"];\n", |
| 540 | dot_escape_id(&edge.from_id), |
| 541 | dot_escape_id(&edge.to_id), |
| 542 | dot_escape_label(&compact(edge.kind.iri())), |
| 543 | )); |
| 544 | } |
| 545 | |
| 546 | out.push_str("}\n"); |
| 547 | out |
| 548 | } |
| 549 | |
| 550 | /// Returns `(fillcolor, shape)` for a given node kind. |
| 551 | fn node_style(kind: &str) -> (&'static str, &'static str) { |
no test coverage detected