1 10 package org.jgap.gp.impl; 11 12 import java.util.*; 13 import javax.swing.tree.TreeNode ; 14 import org.jgap.gp.*; 15 16 22 public class JGAPTreeNode 23 implements TreeNode { 24 25 private final static String 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 getName() { 38 return m_chrom.getFunctions()[m_index].getClass().getName(); 39 } 40 41 45 public TreeNode getChildAt(int a_childIndex) { 46 int child = m_chrom.getChild(m_index, a_childIndex); 47 return new JGAPTreeNode(m_chrom, child); 48 } 49 50 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 rex) { 64 return count; 65 } 66 } 67 68 71 public TreeNode getParent() { 72 return new JGAPTreeNode(m_chrom, m_chrom.getParentNode(m_index)); 73 } 74 75 81 public int getIndex(TreeNode 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 93 public boolean getAllowsChildren() { 94 return getChildCount() > 0; 95 } 96 97 100 public boolean isLeaf() { 101 return getChildCount() == 0; 102 } 103 104 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 |