001 package org.jaga.reproduction.booleanFormulas.nodes;
002
003 /**
004 * TODO: Complete these comments.
005 *
006 * <p><u>Project:</u> JAGA - Java API for Genetic Algorithms.</p>
007 *
008 * <p><u>Company:</u> University College London and JAGA.Org
009 * (<a href="http://www.jaga.org" target="_blank">http://www.jaga.org</a>).
010 * </p>
011 *
012 * <p><u>Copyright:</u> (c) 2004 by G. Paperin.<br/>
013 * This program is free software; you can redistribute it and/or modify
014 * it under the terms of the GNU General Public License as published by
015 * the Free Software Foundation, ONLY if you include a note of the original
016 * author(s) in any redistributed/modified copy.<br/>
017 * This program is distributed in the hope that it will be useful,
018 * but WITHOUT ANY WARRANTY; without even the implied warranty of
019 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
020 * GNU General Public License for more details.<br/>
021 * You should have received a copy of the GNU General Public License
022 * along with this program; if not, write to the Free Software
023 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024 * or see http://www.gnu.org/licenses/gpl.html</p>
025 *
026 * @author Greg Paperin (greg@jaga.org)
027 *
028 * @version JAGA public release 1.0 beta
029 */
030
031 public class TerminalNode extends BooleanFormulaTreeNode {
032
033 private int terminalIndex = -1;
034
035 public TerminalNode() {
036 terminalIndex = -1;
037 }
038
039 public TerminalNode(int terminalIndex) {
040 this.terminalIndex = terminalIndex;
041 }
042
043 public Object clone() {
044 TerminalNode copy = new TerminalNode(this.terminalIndex);
045 return copy;
046 }
047
048 public int recalcHeight() {
049 final int height = 1;
050 setHeight(height);
051 OperatorNode parent = getParent();
052 if (null != parent)
053 parent.propagateNewHeight();
054 return height;
055 }
056
057 public void setTerminalIndex(int terminalIndex) {
058 if (0 <= this.terminalIndex)
059 throw new UnsupportedOperationException("Terminal index is read-only");
060 this.terminalIndex = terminalIndex;
061 }
062
063 public int getRequiredChildrenNumber() {
064 return 0;
065 }
066
067 public boolean evaluate(boolean [] parameters) {
068 if (terminalIndex < 0 || terminalIndex >= parameters.length)
069 throw new IndexOutOfBoundsException("terminalIndex is " + terminalIndex
070 + " but parameters are from 0 to "
071 + parameters.length);
072 return parameters[terminalIndex];
073 }
074
075 public StringBuffer toStringBuffer(boolean infix) {
076 StringBuffer s = new StringBuffer("p");
077 s.append(terminalIndex);
078 return s;
079 }
080
081 }