package Sequenic.T2ext.CoverageEngine.ga; import java.lang.reflect.Method; import java.util.List; import Sequenic.T2.Seq.Trace; import Sequenic.T2.Seq.TraceExecInfo; import Sequenic.T2ext.CoverageEngine.BaseEngine; import Sequenic.T2ext.Instrumenter.Sensor; /** * This evaluator can get the fitness from a function defined by the programmer * himself. It will look for a method name ending in _ga and uses that method as * a fitness function. The method signature should be something like: * private double <name>_ga() * * @author Christiaan Hees */ public class CustomEvaluator extends Evaluator { public static final String METHOD_EXTENSION = "_ga"; private Method gaMethod; public CustomEvaluator(BaseEngine engine) { super(engine); for(Method m : engine.t2covAnalyzer.getC().getDeclaredMethods()) { if(m.getName().endsWith(METHOD_EXTENSION)) { gaMethod = m; containingMethod = m.getName().substring(0, m.getName().length() - METHOD_EXTENSION.length()); // Only run the GA if it's the one for the method of this trace: //System.out.println("DEBUG CFG.getMethod: "+engine.CFG.getMethodName()); if(containingMethod.equals(engine.CFG.getOriginalMethodName())) { break; } containingMethod = null; } } } public double getFitness(Trace candidate, List population) { Sensor.ejectObservations(); TraceExecInfo info = engine.executeT2Trace(candidate); // TODO is this obj actually the one we need? Object obj = info.lastStepResult.receiverObj; gaMethod.setAccessible(true); try { return (Double) gaMethod.invoke(obj, (Object[])null); } catch (Exception e) { e.printStackTrace(); } return 0; } }