KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > gp > impl > JGAPTreeNode


1 /*
2  * This file is part of JGAP.
3  *
4  * JGAP offers a dual license model containing the LGPL as well as the MPL.
5  *
6  * For licencing information please see the file license.txt included with JGAP
7  * or have a look at the top of class org.jgap.Chromosome which representatively
8  * includes the JGAP license policy applicable for any file delivered with JGAP.
9  */

10 package org.jgap.gp.impl;
11
12 import java.util.*;
13 import javax.swing.tree.TreeNode JavaDoc;
14 import org.jgap.gp.*;
15
16 /**
17  * A CommandGene represented as a tree node.
18  *
19  * @author Klaus Meffert
20  * @since 3.0
21  */

22 public class JGAPTreeNode
23     implements TreeNode JavaDoc {
24   /** String containing the CVS revision. Read out via reflection!*/
25   private final static String JavaDoc CVS_REVISION = "$Revision: 1.2 $";
26
27   private ProgramChromosome m_chrom;
28
29   private int m_index;
30
31   public JGAPTreeNode(ProgramChromosome a_chrom, int a_index) {
32     m_chrom = a_chrom;
33     m_chrom.redepth();
34     m_index = a_index;
35   }
36
37   public String JavaDoc getName() {
38     return m_chrom.getFunctions()[m_index].getClass().getName();
39   }
40
41   /**
42    * @param a_childIndex index of the child to get
43    * @return the child TreeNode at index a_childIndex
44    */

45   public TreeNode JavaDoc getChildAt(int a_childIndex) {
46     int child = m_chrom.getChild(m_index, a_childIndex);
47     return new JGAPTreeNode(m_chrom, child);
48   }
49
50   /**
51    * @return the number of children <code>TreeNode</code>s the receiver
52    * contains
53    */

54   public int getChildCount() {
55     int count = 0;
56     try {
57       do {
58         if (m_chrom.getChild(m_index, count) < 0) {
59           return count;
60         }
61         count++;
62       } while (true);
63     } catch (RuntimeException JavaDoc rex) {
64       return count;
65     }
66   }
67
68   /**
69    * @return the parent <code>TreeNode</code> of the receiver
70    */

71   public TreeNode JavaDoc getParent() {
72     return new JGAPTreeNode(m_chrom, m_chrom.getParentNode(m_index));
73   }
74
75   /**
76    * @param a_node the node to retrieve the index for
77    * @return the index of <code>node</code> in the receivers children.
78    * If the receiver does not contain <code>node</code>, -1 will be
79    * returned
80    */

81   public int getIndex(TreeNode JavaDoc a_node) {
82     for (int i = 0; i < getChildCount(); i++) {
83       if (getChildAt(i).equals(a_node)) {
84         return i;
85       }
86     }
87     return -1;
88   }
89
90   /**
91    * @return true if the receiver allows children
92    */

93   public boolean getAllowsChildren() {
94     return getChildCount() > 0;
95   }
96
97   /**
98    * @return true if the receiver is a leaf
99    */

100   public boolean isLeaf() {
101     return getChildCount() == 0;
102   }
103
104   /**
105    * @return the children of the receiver as an <code>Enumeration</code>
106    */

107   public Enumeration children() {
108     Vector l = new Vector();
109     for (int i = 0; i < getChildCount(); i++) {
110       l.add(getChildAt(i));
111     }
112     return l.elements();
113   }
114 }
115
Popular Tags