Synthia
Generic and flexible data structure generator
GraphRenderer.java
Go to the documentation of this file.
1 /*
2  Synthia, a data structure generator
3  Copyright (C) 2019-2021 Laboratoire d'informatique formelle
4  Université du Québec à Chicoutimi, Canada
5 
6  This program is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Lesser General Public License as published
8  by the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 package ca.uqac.lif.synthia.tree;
20 
21 import java.io.PrintStream;
22 import java.util.ArrayDeque;
23 import java.util.HashSet;
24 import java.util.Queue;
25 import java.util.Set;
26 
27 /**
28  * Renders a tree of labeled nodes as a <a href="https://graphviz.org">Graphviz</a>
29  * input file.
30  * @author Sylvain Hallé
31  * @ingroup API
32  */
33 public class GraphRenderer<T> extends GraphCrawler<T>
34 {
35 
36  protected boolean m_directed;
37 
38  protected String m_nodeString;
39 
40  public GraphRenderer(boolean directed)
41  {
42  super();
43  m_directed = directed;
44  m_nodeString = "[style=\"filled\",shape=\"circle\",label=\"\"]";
45  }
46 
48  {
49  m_nodeString = s;
50  return this;
51  }
52 
53  public void printToDot(PrintStream ps, Node<T> n)
54  {
55  if (m_directed)
56  {
57  ps.println("digraph G {");
58  }
59  else
60  {
61  ps.println("graph G {");
62  }
63  ps.println("node " + m_nodeString + ";");
64  Set<Node<T>> visited = new HashSet<Node<T>>();
65  Queue<Node<T>> to_visit = new ArrayDeque<Node<T>>();
66  to_visit.add(n);
67  while (!to_visit.isEmpty())
68  {
69  Node<T> current = to_visit.remove();
70  if (visited.contains(current))
71  {
72  continue;
73  }
74  visited.add(current);
75  int id = getId(current);
76  ps.println(id + " [color=\"" + getColor(current) + "\",label=\"" + getLabel(current) + "\"];");
77  for (Node<T> child : current.getChildren())
78  {
79  int t_id = getId(child);
80  if (m_directed)
81  {
82  ps.println(id + " -> " + t_id + ";");
83  }
84  else
85  {
86  if (!visited.contains(child))
87  {
88  ps.println(id + " -- " + t_id + ";");
89  }
90  }
91  if (!visited.contains(child) && !to_visit.contains(child))
92  {
93  to_visit.add(child);
94  }
95  }
96  }
97  ps.println("}");
98  }
99 
100  protected String getColor(Node<T> n)
101  {
102  return "grey";
103  }
104 
105  protected String getLabel(Node<T> n)
106  {
107  return n.toString();
108  }
109 
110  @SuppressWarnings("unchecked")
111  public static void toDot(PrintStream ps, Node<? extends Object> start)
112  {
114  gr.printToDot(ps, (Node<Object>) start);
115  }
116 }
ca.uqac.lif.synthia.tree.GraphRenderer.printToDot
void printToDot(PrintStream ps, Node< T > n)
Definition: GraphRenderer.java:53
ca.uqac.lif.synthia.tree.GraphRenderer.GraphRenderer
GraphRenderer(boolean directed)
Definition: GraphRenderer.java:40
ca.uqac.lif.synthia.tree.GraphRenderer.m_nodeString
String m_nodeString
Definition: GraphRenderer.java:38
ca.uqac.lif.synthia.tree.Node.toString
String toString()
Definition: Node.java:84
ca.uqac.lif.synthia.tree.Node.getChildren
List< Node< T > > getChildren()
Gets the children of this node.
Definition: Node.java:69
ca.uqac.lif.synthia.tree.GraphRenderer.m_directed
boolean m_directed
Definition: GraphRenderer.java:36
ca.uqac.lif.synthia.tree.GraphRenderer.setNodeString
GraphRenderer< T > setNodeString(String s)
Definition: GraphRenderer.java:47
ca.uqac.lif.synthia.tree.GraphRenderer
Renders a tree of labeled nodes as a Graphviz input file.
Definition: GraphRenderer.java:33
ca.uqac.lif.synthia.tree.GraphRenderer.getLabel
String getLabel(Node< T > n)
Definition: GraphRenderer.java:105
ca.uqac.lif.synthia.tree.GraphRenderer.toDot
static void toDot(PrintStream ps, Node<? extends Object > start)
Definition: GraphRenderer.java:111
ca.uqac.lif.synthia.tree.GraphCrawler.getId
int getId(Node< T > n)
Definition: GraphCrawler.java:20
ca.uqac.lif.synthia.tree.GraphCrawler
Definition: GraphCrawler.java:6
ca.uqac.lif.synthia.tree.Node
Simple implementation of a labeled nodel.
Definition: Node.java:31
ca.uqac.lif.synthia.tree.GraphRenderer.getColor
String getColor(Node< T > n)
Definition: GraphRenderer.java:100