/*
* Copyright (c) 2006 Thomas Weise
*
* E-Mail : tweise@gmx.de
* Creation Date : 2006-07-25 11:19:56
* Original Filename: org.dgpf.gp.regression.expressions.parameterless.Variable.java
* Version : 1.0.0
* Last modification: 2006-07-25
* by: Thomas Weise
*
* License : GNU LESSER GENERAL PUBLIC LICENSE
* Version 2.1, February 1999
* You should have received a copy of this license along
* with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307, USA or download the license under
* http://www.gnu.org/copyleft/lesser.html.
*
* Warranty : This software is provided "as is" without any
* warranty; without even the implied warranty of
* merchantability or fitness for a particular purpose.
* See the Gnu Lesser General Public License for more
* details.
*/
package org.dgpf.gp.regression.expressions.parameterless;
import org.dgpf.gp.regression.base.Calculator;
import org.dgpf.gp.regression.base.Expression;
import org.dgpf.gp.regression.base.IExpressionHandler;
import org.dgpf.gp.regression.base.AggregationUtils;
import org.dgpf.gp.regression.expressions.Costs;
/**
* This expression returns the value of a variable.
*
* @author Thomas Weise
*/
public final class Variable extends Expression
{
/**
* The serial version uid.
*/
private static final long serialVersionUID = 1;
/**
* The variable index.
*/
final int m_variable;
/**
* The internal variable array.
*/
private static volatile Variable[] s_vars ;
static
{
int l_i;
s_vars = new Variable[32];
for(l_i = (s_vars.length-1); l_i >= 0; l_i--)
{
s_vars[l_i] = new Variable(l_i);
}
}
/**
* Create a new variable expression.
* @param p_index The index of the variable to read.
*/
private Variable (final int p_index)
{
super();
this.m_variable = p_index;
}
/**
* Create a variable that accesses the specified index.
* @param p_index The index the variable accesses.
* @return The variable.
*/
public static final Variable create (final int p_index)
{
Variable[] l_v, l_v2;
int l_i;
l_v = s_vars;
if((l_i=l_v.length) <= p_index)
{
l_v2 = new Variable[p_index << 1];
System.arraycopy(l_v, 0, l_v2, 0, l_i);
for(; l_i < l_v2.length; l_i++)
{
l_v2[l_i] = new Variable(l_i);
}
s_vars = l_v = l_v2;
}
return l_v[p_index];
}
/**
* Returns the costs that will arise when executing this expression.
* This is defined as the costs of exactly this expression plus the costs
* of all sub-expressions.
* @return The costs of executing this expression.
*/
@Override
public final double get_costs ()
{
return Costs.VARIABLE_ACCESS_COSTS;
}
/**
* Compute the expression value.
* @param p_calculator The calculator this expression should be evaluated
* on.
* @return The computed expression value.
*/
@Override
public final double compute (final Calculator p_calculator)
{
return p_calculator.get_variable(this.m_variable);
}
/**
* Obtain the expression handler used to handle this expression.
* @return The expression handler used to handle this expression.
*/
@Override
public final IExpressionHandler get_handler()
{
return VariableHandler.INSTANCE;
}
/**
* Store this objects content into a string builder.
* @param p_sb The string builder to store stuff into.
*/
@Override
public final void to_string (final StringBuilder p_sb)
{
AggregationUtils.get_variable_name(this.m_variable, p_sb);
}
/**
* Check if another object is equal to this one.
* @param p_o The object to check.
* @return true
if and only if p_o
is exactly
* the same object than this one.
*/
@Override
public final boolean equals (final Object p_o)
{
return ( (p_o == this) ||
( (p_o instanceof Variable) &&
(this.m_variable == ((Variable)p_o).m_variable)));
}
/**
* Get a string that can be copy-pasted into a java file an that creates
* this construct.
* @param p_b The string builder to add with.
*/
@Override
public final void to_creation_string (final StringBuilder p_b)
{
p_b.append("Variable.create(");
p_b.append(this.m_variable);
p_b.append(')');
}
/**
* Check whether an expression is constant or not.
* @return true
if and only if the specified expression is
* constant.
*/
@Override
public boolean is_constant()
{
return false;
}
/**
* Perform a write replace of this object.
* @return The object to store.
*/
private final Object writeReplace()
{
return create(this.m_variable);
}
/**
* Deserialize to the proper instance.
* @return The globally shared instance.
*/
private final Object readResolve()
{
return create(this.m_variable);
}
/**
* Check how the specified variable is used.
* @param p_index The index of the variable.
* @return An or-combination of 1
if the variable is read and
* 2
if it is written to.
*/
@Override
public final int get_variable_usage (final int p_index)
{
return ((p_index == this.m_variable) ? 1 : 0);
}
/**
* Returns the count of variable used.
* @return The count of variable used.
*/
@Override
public int get_variable_count ()
{
return (this.m_variable+1);
}
/**
* Obtain the depth of the construct.
* @return The depth of the construct.
*/
@Override
public int get_depth ()
{
return 1;
}
}