source: src/main/java/weka/gui/treevisualizer/Node.java @ 20

Last change on this file since 20 was 4, checked in by gnappo, 14 years ago

Import di weka.

File size: 12.8 KB
Line 
1/*
2 *    This program is free software; you can redistribute it and/or modify
3 *    it under the terms of the GNU General Public License as published by
4 *    the Free Software Foundation; either version 2 of the License, or
5 *    (at your option) any later version.
6 *
7 *    This program is distributed in the hope that it will be useful,
8 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 *    GNU General Public License for more details.
11 *
12 *    You should have received a copy of the GNU General Public License
13 *    along with this program; if not, write to the Free Software
14 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
15 */
16
17/*
18 *    Node.java
19 *    Copyright (C) 1999 University of Waikato, Hamilton, New Zealand
20 *
21 */
22
23package weka.gui.treevisualizer;
24
25import java.awt.*;
26import java.util.*;
27import java.io.*;
28import weka.core.Instances;
29
30
31//this is a node structure that to be useful needs the Edge class as well
32
33//note i have done an unintentional naughty thing
34//getHeight() returns the pixel height of the node
35//getHeight(Node,int) returns how many levels down the tree goes
36//setHeight(int) is associated to the prior
37
38/**
39 * This class records all the data about a particular node for displaying.
40 *
41 * @author Malcolm Ware (mfw4@cs.waikato.ac.nz)
42 * @version $Revision: 1.4 $
43 */
44public class Node {
45 
46  /** The fill mode for the node (not in use). */
47  private int m_backstyle;       //how the back color will fill
48
49  /** The shape of the node. */
50  private int m_shape;
51
52  /** The color of the node. */
53  private Color m_color;
54
55  /** the text for the node. */
56  private String m_label;
57
58  /** the text broken up into lines */
59  private Vector m_lines;
60
61  //the coord of the left side .note all coords are
62  //between 1-0 for scaling per Stuart's suggestion
63  /** The center of the node (between 0 and 1). */
64  private double m_center;       //coord of the center . main x value used
65
66  /** The top of the node (between 0 and 1). */
67  private double m_top;          //main y coord to go by
68     
69  /** true if this nodes descendants are visible (not in use currently). */
70  private boolean m_cVisible;   //whether it's descendants are visible
71
72  /** true if this node is visible (not currently in use). */
73  private boolean m_visible;     //whether it's visible
74
75  /** true if this is the top of the tree. ie has no parent */
76  private boolean m_root;     //whether it is anscestor to all i.e top of tree
77
78  /** An array containing references to all the parent edges
79   * (only 1 currently). */
80  private Vector m_parent;      //the edge to its parent edges(or itself
81
82  //if true root)
83  /** An array containing references to all the child edges. */
84  private Vector m_children;     //a vector list of edges to the nodes children
85
86  /** The ID string for this node (used for construction purposes) */
87  private String m_refer;
88
89  /** A String containing extra information about the node. */
90  private String m_data;
91
92  /**
93   * An Instances variable generated from the data.
94   * Note that if this exists then the string shall be NULL to save space.
95   */
96  private Instances m_theData;
97
98  /**
99   * This will setup all the values of the node except for its top and center.
100   *
101   * @param label The text for the node.
102   * @param refer The ID string for this node.
103   * @param backstyle The backstyle of this node.
104   * @param shape The shape of this node.
105   * @param color The color of this node.
106   */
107  public Node(String label,String refer,int backstyle,int shape,
108              Color color,String d) {
109    m_label = label;
110    m_backstyle = backstyle;
111    m_shape = shape;
112    m_color = color;
113    m_refer = refer;
114   
115    m_center = 0;
116    m_top = 0;
117
118    m_cVisible = true;
119    m_visible = true;
120    m_root = false;
121    m_parent = new Vector(1,1);
122    m_children = new Vector(20,10);
123    m_lines = new Vector(4,2);
124    breakupLabel();
125    m_data = d;
126    m_theData = null;
127  }     
128 
129  /**
130   * This will return the Instances object related to this node.
131   * If it has not been allocated then that will be done also.
132   *
133   * @return The Instances object.
134   */
135  public Instances getInstances() {
136    if (m_theData == null && m_data != null) {
137      try {
138        m_theData = new Instances(new StringReader(m_data));
139      } catch(Exception e) {
140        System.out.println("Error : " + e);
141      }
142      m_data = null;
143    }
144    return m_theData;
145  }
146
147  /**
148   * Get If this node's childs are visible.
149   *
150   * @return True if the childs are visible.
151   */
152  public boolean getCVisible() {
153    return m_cVisible;
154  }
155
156  /**
157   * Recursively goes through the tree and sets all the children and the
158   * parent to visible.
159   *
160   * @param r The current node to set visible.
161   */
162  private void childVis(Node r) {
163    Edge e;
164    r.setVisible(true);
165    if (r.getCVisible()) {
166      for (int noa = 0;(e = r.getChild(noa)) != null;noa++) {
167        childVis(e.getTarget());
168      }
169    }
170  }
171
172  /**
173   * Sets all the children of this node either to visible or invisible
174   *
175   * @param v True if the children are to be visible
176   */
177  public void setCVisible(boolean v) {
178    m_cVisible = v;
179    if (v) {
180      childVis(this);
181    }
182    else if (!v) {
183      childInv(this);
184    }
185  }
186 
187  /**
188   * Recursively goes through the tree and sets all the children to invisible,
189   * Not the parent though.
190   *
191   * @param r The current node from whom the children are gonna be set
192   * invisible.
193   */
194  private void childInv(Node r) {
195    Edge e;
196    Node s;
197    for (int noa = 0;(e = r.getChild(noa)) != null;noa++) {
198      s = e.getTarget();
199      s.setVisible(false);
200      childInv(s);
201    }
202  }
203 
204
205 
206 
207 
208 
209
210
211  /**
212   * Get the value of refer.
213   *
214   * @return Value of refer.
215   */
216  public String getRefer() {
217   
218    return m_refer;
219  }
220 
221  /**
222   * Set the value of refer.
223   *
224   * @param v  Value to assign to refer.
225   */
226  public void setRefer(String v) {
227   
228    m_refer = v;
229  }
230 
231 
232 
233  /**
234   * Get the value of shape.
235   *
236   * @return Value of shape.
237   */
238  public int getShape() {
239   
240    return m_shape;
241  }
242 
243  /**
244   * Set the value of shape.
245   *
246   * @param v  Value to assign to shape.
247   */
248  public void setShape(int v) {
249   
250    m_shape = v;
251  }
252 
253 
254  /**
255   * Get the value of color.
256   *
257   * @return Value of color.
258   */
259  public Color getColor() {
260   
261    return m_color;
262  }
263 
264  /**
265   * Set the value of color.
266   *
267   * @param v  Value to assign to color.
268   */
269  public void setColor(Color v) {
270   
271    m_color = v;
272  }
273 
274 
275  /**
276   * Get the value of label.
277   *
278   * @return Value of label.
279   */
280  public String getLabel() {
281   
282    return m_label;
283  }
284 
285  /**
286   * This Will break the node's text up into lines.
287   *
288   */
289  private void breakupLabel() {
290    int prev = 0,noa;
291    for (noa = 0;noa < m_label.length();noa++) {
292      if (m_label.charAt(noa) == '\n') {
293        m_lines.addElement(m_label.substring(prev,noa));
294        prev = noa+1;
295      }
296    }
297    m_lines.addElement(m_label.substring(prev,noa));
298   
299  }
300 
301  /**
302   * This will return the width and height of the rectangle that the text
303   * will fit into.
304   *
305   * @param f The size info for the Font.
306   * @return A Dimension containing the size of the text.
307   */
308  public Dimension stringSize(FontMetrics f) {
309    Dimension d = new Dimension();
310    int old = 0;
311    String s;
312    int noa = 0;
313    while ((s = getLine(noa)) != null) {
314      noa++;
315      old = f.stringWidth(s);
316     
317      if (old > d.width) {
318        d.width = old;
319      }
320    }
321    d.height = noa * f.getHeight();
322    return d;
323   
324  }
325
326  /**
327   * Returns the text String for the specfied line.
328   *
329   * @param n The line wanted.
330   * @return The String corresponding to that line.
331   */
332  public String getLine(int n) {
333    if (n < m_lines.size()) {
334      return (String)m_lines.elementAt(n);
335    }
336    else {
337      return null;
338    }
339  }
340 
341 
342 
343
344 
345 
346 
347
348 
349 
350  /**
351   * Get the value of center.
352   *
353   * @return Value of center.
354   */
355  public double getCenter() {
356   
357    return m_center;
358  }
359 
360  /**
361   * Set the value of center.
362   *
363   * @param v  Value to assign to center.
364   */
365  public void setCenter(double v) {
366   
367    m_center = v;
368  }
369 
370  /**
371   * Will increase or decrease the postion of center.
372   *
373   * @param v The amount to increase or decrease center by.
374   */
375  public void adjustCenter(double v) {
376    m_center += v;
377  }
378 
379 
380  /**
381   * Get the value of top.
382   *
383   * @return Value of top.
384   */
385  public double getTop() {
386   
387    return m_top;
388  }
389 
390  /**
391   * Set the value of top.
392   *
393   * @param v  Value to assign to top.
394   */
395  public void setTop(double v) {
396   
397    m_top = v;
398  }
399 
400
401 
402 
403 
404  /**
405   * Get the value of visible.
406   *
407   * @return Value of visible.
408   */
409  public boolean getVisible() {
410   
411    return m_visible;
412  }
413 
414  /**
415   * Set the value of visible.
416   *
417   * @param v  Value to assign to visible.
418   */
419  private void setVisible(boolean v) {
420   
421    m_visible = v;
422  }
423 
424 
425 
426 
427  /**
428   * Get the value of root.
429   *
430   * @return True if has no parents.
431   */
432  public boolean getRoot() {
433   
434    return m_root;
435  }
436 
437  /**
438   * Set the value of root.
439   *
440   * @param v  Value to assign to root.
441   */
442  public void setRoot(boolean v) {
443   
444    m_root = v;
445  }
446 
447 
448 
449  /**
450   * Get the parent edge.
451   *
452   * @param i The parent number to get.
453   * @return The parent edge or NULL if it doesn't exist.
454   */
455  public Edge getParent(int i) {
456   
457    if (i < m_parent.size()) {
458      return (Edge)m_parent.elementAt(i);
459    }
460    else {
461      return null;
462    }
463
464  }
465 
466  /**
467   * Set the value of parent.
468   *
469   * @param v  Value to assign to parent.
470   */
471  public void setParent(Edge v) {
472   
473    m_parent.addElement(v);
474  }
475 
476 
477 
478  /**
479   * Get the Edge for the child number 'i'.
480   *
481   * @param i The child number to get.
482   * @return The child Edge or NULL if it doesn't exist.
483   */
484  public Edge getChild(int i) {
485   
486    if (i < m_children.size()) {
487      return (Edge)m_children.elementAt(i);
488    }
489    else {
490      return null;
491    }
492  }
493 
494  /**
495   * Set the value of children.
496   *
497   * @param v  Value to assign to children.
498   */
499  public void addChild(Edge v) {
500    m_children.addElement(v);
501  }
502 
503
504  /**
505   * Recursively finds the number of visible groups of siblings there are.
506   *
507   * @param r The current Node upto.
508   * @param n The current number of groups there are.
509   * @return The number of groups found so far.
510   */
511  public static int getGCount(Node r,int n) {
512    Edge e;
513   
514    if (r.getChild(0) != null && r.getCVisible()) {
515      n++;
516      for (int noa = 0;(e = r.getChild(noa)) != null;noa++) {
517        n = getGCount(e.getTarget(),n);
518      }
519    }
520    return n;
521  }
522
523  /**
524   * Recursively finds the total number of groups of siblings there are.
525   *
526   * @param r The current Node upto.
527   * @param n The current number of groups there are.
528   * @return The number of groups found so far.
529   */
530  public static int getTotalGCount(Node r,int n) {
531    Edge e;
532   
533    if (r.getChild(0) != null) {
534      n++;
535      for (int noa = 0;(e = r.getChild(noa)) != null;noa++) {
536        n = getTotalGCount(e.getTarget(),n);
537      }
538    }
539    return n;
540  }
541 
542
543
544
545
546  /**
547   * Recursively finds the number of visible nodes there are (this may
548   * accidentally count some of the invis nodes).
549   *
550   * @param r The current Node upto.
551   * @param n The current number nodes there are.
552   * @return The number of nodes found so far.
553   */
554  public static int getCount(Node r,int n) {
555    Edge e;
556    n++;
557    for (int noa = 0;(e = r.getChild(noa)) != null && r.getCVisible();noa++) {
558      n = getCount(e.getTarget(),n);
559    }
560    return n;
561   
562  }
563
564  /**
565   * Recursively finds the total number of nodes there are.
566   *
567   * @param r The current Node upto.
568   * @param n The current number nodes there are.
569   * @return The number of nodes found so far.
570   */
571  public static int getTotalCount(Node r,int n) {
572    Edge e;
573    n++;
574    for (int noa = 0;(e = r.getChild(noa)) != null;noa++) {
575      n = getTotalCount(e.getTarget(),n);
576    }
577    return n;
578  }
579 
580 
581  /**
582   * Recursively finds the number of visible levels there are.
583   *
584   * @param r The current Node upto.
585   * @param l The curent level.
586   * @return The max number of levels found so far.
587   */
588  public static int getHeight(Node r,int l) {
589    l++;
590    int lev = l,temp = 0;
591    Edge e;
592   
593    for (int noa = 0;(e = r.getChild(noa)) != null && r.getCVisible();noa++) {
594      temp = getHeight(e.getTarget(),l);
595      if (temp > lev) {
596        lev = temp;
597      }
598     
599    }
600   
601    return lev;
602
603
604  }
605
606  /**
607   * Recursively finds the total number of levels there are.
608   *
609   * @param r The current Node upto.
610   * @param l The curent level.
611   * @return The max number of levels found so far.
612   */
613  public static int getTotalHeight(Node r,int l) {
614    l++;
615    int lev = l,temp = 0;
616    Edge e;
617   
618    for (int noa = 0;(e = r.getChild(noa)) != null;noa++) {
619      temp = getTotalHeight(e.getTarget(),l);
620      if (temp > lev) {
621        lev = temp;
622      }
623    }
624    return lev;
625  }
626}
Note: See TracBrowser for help on using the repository browser.