Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/port80/graph/dot/impl/TestUtil.java


1   //
2   // Copyright(c) 2002, Chris Leung
3   //
4   
5   package com.port80.graph.dot.impl;
6   
7   import java.awt.Graphics2D;
8   import java.awt.image.BufferedImage;
9   import java.io.File;
10  import java.io.FileInputStream;
11  import java.io.StringReader;
12  
13  import javax.imageio.ImageIO;
14  import javax.imageio.ImageWriter;
15  import javax.imageio.stream.FileImageOutputStream;
16  
17  import com.port80.graph.IGraph;
18  import com.port80.graph.dot.parser.DotParser;
19  import com.port80.graph.impl.DirectedGraphRenderer;
20  import com.port80.graph.impl.GraphPanel;
21  import com.port80.util.Debug;
22  import com.port80.util.msg;
23  import com.port80.util.sprint;
24  
25  /**
26   * Utilities for tests.
27   * 
28   * @author chrisl
29   */
30  public class TestUtil {
31  
32    ////////////////////////////////////////////////////////////////////////
33  
34    private static final String NAME = "TestUtil";
35    private static boolean VERBOSE = Debug.isVerbose();
36  
37    ////////////////////////////////////////////////////////////////////////
38  
39    /** 
40     * Create an IGraph from the given text description and initialized vertex dimensions and ranking. 
41     */
42    public static IGraph createGraph(String text) {
43      DotParser parser = new DotParser(new StringReader(text)).parse();
44      IGraph graph = parser.getGraph();
45      DotGraph dotgraph = new DotGraph(graph);
46      NetworkSimplex.dotRank(dotgraph, graph.getAttrDouble("nslimit", 1.0), true);
47      dotgraph.saveRanks();
48      // Update vertex and edge shapes for VirtualGraph creation.
49      updateShape(graph);
50      return graph;
51    }
52  
53    public static IGraph createGraph(File file) {
54      DotParser parser = null;
55      try {
56        parser = new DotParser(new FileInputStream(file), file.getCanonicalPath());
57        parser.parse();
58      } catch (Exception ex) {
59        msg.err(NAME + ".createGraph(File)", ex);
60        return null;
61      }
62      IGraph graph = parser.getGraph();
63      DotGraph dotgraph = new DotGraph(graph);
64      NetworkSimplex.dotRank(dotgraph, graph.getAttrDouble("nslimit", 1.0), true);
65      dotgraph.saveRanks();
66      // Update vertex and edge shapes for VirtualGraph creation.
67      updateShape(graph);
68      return graph;
69    }
70  
71    /**
72     * Annotate vertex and edge shapes with dimension information.
73     * updateShape() need to be called before creating VirtualGraph to annotate the vertex dimensions.
74     * updateShape() need to be called before Route() to annotate the vertex positions information.
75     */
76    public static void updateShape(IGraph graph) {
77      BufferedImage bimg = new BufferedImage(10, 10, BufferedImage.TYPE_BYTE_GRAY);
78      Graphics2D g2d = (Graphics2D) bimg.getGraphics();
79      DirectedGraphRenderer renderer = new DirectedGraphRenderer();
80      renderer.updateShape(g2d, graph);
81    }
82  
83    public static void saveImage(String fname, float scale, IGraph graph) {
84      GraphPanel gpanel = new GraphPanel(graph, scale);
85      BufferedImage buf = gpanel.getImage();
86      saveImage(buf, fname);
87    }
88  
89    public static void saveImage(BufferedImage buf, String fname) {
90      final String EXT = "png";
91      fname += "." + EXT;
92      try {
93        File ifile = msg.createFile(fname);
94        ImageWriter writer = (ImageWriter) ImageIO.getImageWritersByFormatName(EXT).next();
95        FileImageOutputStream of = new FileImageOutputStream(ifile);
96        writer.setOutput(of);
97        writer.write(buf);
98        of.close();
99        writer.dispose();
100       if (VERBOSE)
101         msg.println(NAME + ".saveImage(): " + fname + " saved.");
102     } catch (Exception e) {
103       msg.err(NAME + ".saveImage(): Can't open output file: " + fname, e);
104     }
105   }
106 
107   ////////////////////////////////////////////////////////////////////////
108 
109   /**
110    * Check that values are monotonic increasing.
111    */
112   public static boolean checkMonotonic(int[] a, String message) {
113     boolean ok = true;
114     for (int i = 0, prev = -1; i < a.length; ++i) {
115       if (a[i] < prev) {
116         msg.err(
117           "spaces not monotonic increasing: "
118             + message
119             + ", i="
120             + i
121             + ", prev="
122             + prev
123             + ", spaces[i]="
124             + a[i]
125             + ", spaces[]="
126             + sprint.array("", "%6d ", a));
127         a[i] = prev;
128         ok = false;
129       }
130       prev = a[i];
131     }
132     return ok;
133   }
134 
135   ////////////////////////////////////////////////////////////////////////
136 }