📄 treebuild.java
字号:
/* * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *//* * Tree_build.java * Copyright (C) 1999 Malcolm Ware * */package weka.gui.treevisualizer;import java.util.*;import java.io.*;import java.awt.*;/** * This class will parse a dotty file and construct a tree structure from it * with Edge's and Node's * * @author Malcolm Ware (mfw4@cs.waikato.ac.nz) * @version $Revision: 1.1.1.1 $ */public class TreeBuild { //this class will parse the tree into relevant strings //into info objects //from there it will create the nodes and edges from the info objects /** The name of the tree, Not in use. */ private String m_graphName; /** An array with all the nodes initially constructed into it. */ private Vector m_aNodes; /** An array with all the edges initially constructed into it. */ private Vector m_aEdges; /** An array containing a structure that describes the node without * actually creating it. */ private Vector m_nodes; /** An arry containing a structure that describes the edge without * actually creating it. */ private Vector m_edges; /** An object setup to take graph data. */ private InfoObject m_grObj; /** An object setup to take node data. */ private InfoObject m_noObj; /** An object setup to take edge data. */ private InfoObject m_edObj; /** true if it is a digraph. (note that this can't build digraphs). */ private boolean m_digraph; /** The stream to parse. */ private StreamTokenizer m_st; /** A table containing all the colors. */ private Hashtable m_colorTable; /** * Upon construction this will only setup the color table for quick * reference of a color. */ public TreeBuild() { m_colorTable = new Hashtable(); Colors ab = new Colors(); for (int noa = 0;noa < ab.m_cols.length;noa++) { m_colorTable.put(ab.m_cols[noa].m_name,ab.m_cols[noa].m_col); } } /** * This will build A node structure from the dotty format passed. Don't * send a dotty format with multiple parents * per node, and ensure that there is 1 and only 1 node with no parent. * * @param t The reader with the dotty string to be read. * @return The top node of the tree structure (the last node with no parent). */ public Node create(Reader t) { m_nodes = new Vector(50,50); m_edges = new Vector(50,50); m_grObj = new InfoObject("graph"); m_noObj = new InfoObject("node"); m_edObj = new InfoObject("edge"); m_digraph = false; m_st = new StreamTokenizer(new BufferedReader(t)); setSyntax(); graph(); Node top = generateStructures(); return top; } /** * This will go through all the found Nodes and Edges build instances of * these * and link them together. * * @return The node with no parent (the top of the tree). */ private Node generateStructures() { String id,label,source,target; Integer shape,style; int fontsize; Color fontcolor,color; InfoObject t; m_aNodes = new Vector(50,50); m_aEdges = new Vector(50,50); for (int noa = 0;noa < m_nodes.size();noa++) { t = (InfoObject)m_nodes.elementAt(noa); id = t.m_id; if (t.m_label == null) { if (m_noObj.m_label == null) { label = ""; } else { label = m_noObj.m_label; } } else { label = t.m_label; } if (t.m_shape == null) { if (m_noObj.m_shape == null) { shape = new Integer(2); } else { shape = getShape(m_noObj.m_shape); } } else { shape = getShape(t.m_shape); } if (shape == null) { shape = new Integer(2); } if (t.m_style == null) { if (m_noObj.m_style == null) { style = new Integer(1); } else { style = getStyle(m_noObj.m_style); } } else { style = getStyle(t.m_style); } if (style == null) { style = new Integer(1); } if (t.m_fontSize == null) { if (m_noObj.m_fontSize == null) { fontsize = 12; } else { fontsize = Integer.valueOf(m_noObj.m_fontSize).intValue(); } } else { fontsize = Integer.valueOf(t.m_fontSize).intValue(); } if (t.m_fontColor == null) { if (m_noObj.m_fontColor == null) { fontcolor = Color.black; } else { fontcolor = (Color)m_colorTable .get(m_noObj.m_fontColor.toLowerCase()); } } else { fontcolor = (Color)m_colorTable.get(t.m_fontColor.toLowerCase()); } if (fontcolor == null) { fontcolor = Color.black; } if (t.m_color == null) { if (m_noObj.m_color == null) { color = Color.gray; } else { color = (Color)m_colorTable.get(m_noObj.m_color.toLowerCase()); } } else { color = (Color)m_colorTable.get(t.m_color.toLowerCase()); } if (color == null) { color = Color.gray; } m_aNodes.addElement(new Node(label,id,style.intValue(),shape.intValue(), color,t.m_data)); } for (int noa = 0;noa < m_edges.size();noa++) { t = (InfoObject)m_edges.elementAt(noa); id = t.m_id; if (t.m_label == null) { if (m_noObj.m_label == null) { label = ""; } else { label = m_noObj.m_label; } } else { label = t.m_label; } if (t.m_shape == null) { if (m_noObj.m_shape == null) { shape = new Integer(2); } else { shape = getShape(m_noObj.m_shape); } } else { shape = getShape(t.m_shape); } if (shape == null) { shape = new Integer(2); } if (t.m_style == null) { if (m_noObj.m_style == null) { style = new Integer(1); } else { style = getStyle(m_noObj.m_style); } } else { style = getStyle(t.m_style); } if (style == null) { style = new Integer(1); } if (t.m_fontSize == null) { if (m_noObj.m_fontSize == null) { fontsize = 12; } else { fontsize = Integer.valueOf(m_noObj.m_fontSize).intValue(); } } else { fontsize = Integer.valueOf(t.m_fontSize).intValue(); } if (t.m_fontColor == null) { if (m_noObj.m_fontColor == null) { fontcolor = Color.black; } else { fontcolor = (Color)m_colorTable .get(m_noObj.m_fontColor.toLowerCase()); } } else { fontcolor = (Color)m_colorTable.get(t.m_fontColor.toLowerCase()); } if (fontcolor == null) { fontcolor = Color.black; } if (t.m_color == null) { if (m_noObj.m_color == null) { color = Color.white; } else { color = (Color)m_colorTable.get(m_noObj.m_color.toLowerCase()); } } else { color = (Color)m_colorTable.get(t.m_color.toLowerCase()); } if (color == null) { color = Color.white; } m_aEdges.addElement(new Edge(label,t.m_source,t.m_target)); } boolean f_set,s_set; Node x,sour = null,targ = null; Edge y; for (int noa = 0;noa < m_aEdges.size();noa++) { f_set = false; s_set = false; y = (Edge)m_aEdges.elementAt(noa); for (int nob = 0;nob < m_aNodes.size();nob++) { x = (Node)m_aNodes.elementAt(nob); if (x.getRefer().equals(y.getRtarget())) { f_set = true; targ = x; } if (x.getRefer().equals(y.getRsource())) { s_set = true; sour = x; } if (f_set == true && s_set == true) { break; } } if (targ != sour) { y.setTarget(targ); y.setSource(sour); } else { System.out.println("logic error"); } } for (int noa = 0;noa < m_aNodes.size();noa++) { if (((Node)m_aNodes.elementAt(noa)).getParent(0) == null) { sour = (Node)m_aNodes.elementAt(noa); } } return sour; } /** * This will convert the shape string to an int representing that shape. * * @param sh The name of the shape. * @return An Integer representing the shape. */ private Integer getShape(String sh) { if (sh.equalsIgnoreCase("box") || sh.equalsIgnoreCase("rectangle")) { return new Integer(1); } else if (sh.equalsIgnoreCase("oval")) { return new Integer(2); } else if (sh.equalsIgnoreCase("diamond")) { return new Integer(3); } else { return null; } } /** * Converts the string representing the fill style int oa number * representing it. * * @param sty The name of the style. * @return An Integer representing the shape. */ private Integer getStyle(String sty) { if (sty.equalsIgnoreCase("filled")) { return new Integer(1); } else { return null; } } /** * This will setup the syntax for the tokenizer so that it parses the * string properly. * */ private void setSyntax() { m_st.resetSyntax(); m_st.eolIsSignificant(false); m_st.slashStarComments(true); m_st.slashSlashComments(true); //System.out.println("slash"); m_st.whitespaceChars(0,' '); m_st.wordChars(' '+1,'\u00ff'); m_st.ordinaryChar('['); m_st.ordinaryChar(']'); m_st.ordinaryChar('{'); m_st.ordinaryChar('}'); m_st.ordinaryChar('-'); m_st.ordinaryChar('>');
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -