/*
* Copyright (c) 2006 Thomas Weise
*
* E-Mail : tweise@gmx.de
* Creation Date : 2006-07-03 09:40:46
* Original Filename: org.dgpf.gp.vm.instructions.arith.SetZeroFlagHandler.java
* Version : 1.0.0
* Last modification: 2006-07-03
* 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.vm.instructions.arith;
import org.dgpf.gp.vm.base.IInstructionHandler;
import org.dgpf.gp.vm.base.Instruction;
import org.dgpf.gp.vm.base.VMContext;
import org.dgpf.gp.vm.instructions.base.NoMemInstrHandler;
import org.dgpf.gp.vm.mutation.MutationInfo;
import org.sfc.math.stochastic.Randomizer;
/**
* The instruction handler for the set-zero-flag instruction.
*
* @author Thomas Weise
*/
public final class SetZeroFlagHandler extends NoMemInstrHandler
{
/**
* The serial version uid.
*/
private static final long serialVersionUID = 1;
/**
* The different set zero flag instructions.
*/
private static final Instruction[] SZF = new Instruction[]
{SetZeroFlag.SET_FLAG, SetZeroFlag.CLEAR_FLAG,
SetZeroFlag.TOGGLE_FLAG};
/**
* The globally shared instance of the set zero flag handler.
*/
public static final IInstructionHandler INSTANCE =
new SetZeroFlagHandler();
/**
* Create a new set zero flag handler - you cannot do this, use the shared
* instance.
* @see #INSTANCE
*/
private SetZeroFlagHandler()
{
super();
}
/**
* Create a new randomized instance of this instruction.
* @param p_context The vm context to be used.
* @param p_info The information record holding the infos on the
* instruction to be created.
* @return The new instruction instance.
*/
public final Instruction create (final VMContext p_context,
final MutationInfo p_info)
{
return SZF[p_context.get_randomizer().nextInt(SZF.length)];
}
/**
* Perform the mutation.
* @param p_source The source object to create a randomizedly altered
* copy of.
* @param p_context The context hosting the operation.
* @param p_info The information record.
* @return The new, randomizedly altered copy of the object
* p_source
or p_source
itself if no
* mutation was possible.
*/
public final Instruction mutate (final Instruction p_source,
final VMContext p_context,
final MutationInfo p_info)
{
Randomizer l_r;
int l_i;
Instruction l_c;
l_i = SZF.length;
l_r = p_context.get_randomizer();
do
{
l_c = SZF[l_r.nextInt(l_i)];
} while(l_c == p_source);
return l_c;
}
/**
* This method tells if this instruction can be useful at the end of a
* procedure. A comparison, for example, will never be useful, since
* its result will always get lost.
* @return true
if and only if an instruction is useful at the
* end of a procedure.
*/
@Override
public boolean is_useful_at_end()
{
return false;
}
/**
* Try to melt two instructions together. The second instruction is of
* the type this instruction handler handles.
* @param p_first The first instruction.
* @param p_second The second instruction.
* @return An instruction able to replace the sequence of these both
* instructions, or null
if no valid replacement
* coult be found.
*/
@Override
public final Instruction try_melt (final Instruction p_first,
final Instruction p_second)
{
if( (p_first instanceof SetZeroFlag) || (p_first instanceof Compare) )
{
return p_second;
}
return super.try_melt(p_first, p_second);
}
}