📄 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.awt.Color;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.io.StreamTokenizer;
import java.util.Hashtable;
import java.util.Vector;
/**
* 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$
*/
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<Node> m_aNodes;
/** An array with all the edges initially constructed into it. */
private Vector<Edge> m_aEdges;
/** An array containing a structure that describes the node without
* actually creating it. */
private Vector<InfoObject> m_nodes;
/** An arry containing a structure that describes the edge without
* actually creating it. */
private Vector<InfoObject> 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;
/** The stream to parse. */
private StreamTokenizer m_st;
/** A table containing all the colors. */
private Hashtable<String, Color> m_colorTable;
/**
* Upon construction this will only setup the color table for quick
* reference of a color.
*/
public TreeBuild() {
m_colorTable = new Hashtable<String, Color>();
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<InfoObject>(50,50);
m_edges = new Vector<InfoObject>(50,50);
m_grObj = new InfoObject("graph");
m_noObj = new InfoObject("node");
m_edObj = new InfoObject("edge");
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;
Integer shape,style;
int fontsize;
Color fontcolor,color;
InfoObject t;
m_aNodes = new Vector<Node>(50,50);
m_aEdges = new Vector<Edge>(50,50);
for (int noa = 0;noa < m_nodes.size();noa++) {
t = 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 = m_colorTable
.get(m_noObj.m_fontColor.toLowerCase());
}
}
else {
fontcolor = 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 = getDefaultColor(shape);
}
else {
color = m_colorTable.get(m_noObj.m_color.toLowerCase());
}
}
else {
color = m_colorTable.get(t.m_color.toLowerCase());
}
if (color == null) {
color = getDefaultColor(shape);
}
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 = 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 = m_colorTable
.get(m_noObj.m_fontColor.toLowerCase());
}
}
else {
fontcolor = 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.black;
}
else {
color = m_colorTable.get(m_noObj.m_color.toLowerCase());
}
}
else {
color = m_colorTable.get(t.m_color.toLowerCase());
}
if (color == null) {
color = Color.black;
}
m_aEdges.addElement(new Edge(label,t.m_source,t.m_target,color));
}
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 = m_aEdges.elementAt(noa);
for (int nob = 0;nob < m_aNodes.size();nob++) {
x = 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 (m_aNodes.elementAt(noa).getParent(0) == null) {
sour = m_aNodes.elementAt(noa);
}
}
return sour;
}
private Color getDefaultColor(Integer shape) {
if (shape==1) {
return new Color(255,153,51);
}
else if (shape==2) {
return new Color(200,200,0);
}
else if (shape==3) {
return new Color(153,255,153);
}
else {
return new Color(149,172,174);
}
}
/**
* 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);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -