package Sequenic.T2ext.CoverageEngine.ga; import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Random; import org.uncommons.watchmaker.framework.factories.AbstractCandidateFactory; import Sequenic.T2.Obj.Cloner; import Sequenic.T2.Seq.Trace; /** * This class generates the initial population for the genetic algorithm. * * @author Christiaan Hees */ public class TraceFactory extends AbstractCandidateFactory { List traces; private int counter; public TraceFactory(Collection traces) { this.traces = new ArrayList(traces); counter = 0; } /** * Currently just returns one of the traces from the .tr file. * TODO: we might need more than 1000 traces so perhaps we need * a real generator? * Looks like this factory only gets used to create the * initial population, so this should be fine... */ @Override protected Trace generateRandomCandidate(Random rng) { //System.out.println("DEBUG candidate count: "+counter); if(counter>=traces.size()) { System.out.println("Warning: More than "+traces.size()+" candidates requested."); counter = 0; } Trace trace = null; try { // We want a clone so we can safely modify it with the GA operators // FIXME: Is this really how you're supposed to clone traces? trace = Cloner.clone(traces.get(counter)); } catch (Exception e) { System.out.println("Failed to clone trace "+trace); e.printStackTrace(); System.exit(1); } counter++; return trace; } }