package main; import java.awt.event.*; import genegui.GeneNetInterface; import parameters.ParameterSetArray; import stat.Stat; import iterators.ModelIterator; import iterators.MatingIterator; class ScriptRunner implements Runnable { BetterTokenizer script = null; GeneNet program = null; String outfileName = "ScriptOut.txt"; ScriptRunner(BetterTokenizer tokenizer, GeneNet program) { script = tokenizer; this.program = program; try { // Make the output file String command = GeneralInput.findNextIDToken(script); if( !command.toUpperCase().equals("OUTFILE")) { GlobalIO.printlnGeneral("The first thing specified in a script file must be the output filename using &Outfile "); return; } GeneralInput.nextToken(script); outfileName = script.sval; GlobalIO.setGlobalOutfile(outfileName); } catch(Exception e) { GlobalIO.printlnGeneral("Error making script output file - this will probably doom this script: " + e.toString()); } } public void run() { Globals.setScriptRunning(true); System.out.println("started running script"); processInstructions(); GlobalIO.closeGlobalOutfile(); Globals.setScriptRunning(false); } void processInstructions() { try { String command = GeneralInput.findNextIDToken(script).toUpperCase(); while( !command.toUpperCase().equals("ENDSCRIPT")) { System.out.println(command); if(command.equals("LOAD")) { GeneralInput.nextToken(script); program.actor.actionPerformed(new ActionEvent(this, GeneNetInterface.LOAD_MODEL, script.sval)); } else if(command.equals("CLOSE")) { // For now, just allow closing of wheel plots GeneralInput.nextToken(script); ParameterSetArray psa = Globals.getParamSetArray(script.sval); if(psa != null) Globals.removeParamSetArray(psa); } else if(command.equals("SET")) { doSet(); } else if(command.equals("STATISTIC")) { GeneralInput.nextToken(script); doStatistic(script.sval); } else if(command.equals("DOMATINGFORALLWHEELCOMBOS")) { MatingIterator iterator; if(program.ItsIterator != null) iterator = (MatingIterator)program.ItsIterator; else { iterator = new MatingIterator(); GeneralInput.nextToken(script); iterator.init(program.model.net, program.model); iterator.setNumMatings( (int)script.nval ); } iterator.setSaveMatings(false); for(int i = 0; i < Globals.getNumParamSetArrays(); i++) { for(int j = i; j < Globals.getNumParamSetArrays(); j++) { iterator.setFirstWheel(Globals.getParamSetArray(i)); iterator.setSecondWheel(Globals.getParamSetArray(j)); iterator.doRun(); } } } else if(command.equals("DOSTATISTICFORALLWHEELS")) { // Get the statistic name GeneralInput.nextToken(script); for(int i = 0; i < Globals.getNumParamSetArrays(); i++) { Globals.setCurParamSetArray(Globals.getParamSetArray(i)); doStatistic(script.sval); } } else if(command.equals("DOSTATISTICFORALLWHEELCOMBOS")) { // Get the statistic name GeneralInput.nextToken(script); for(int i = 0; i < Globals.getNumParamSetArrays(); i++) { for(int j = i; j < Globals.getNumParamSetArrays(); j++) { Globals.setCurParamSetArray(Globals.getParamSetArray(i)); Globals.setCompareParamSetArray(Globals.getParamSetArray(j)); doStatistic(script.sval); } } } command = GeneralInput.findNextIDToken(script).toUpperCase(); } } catch(Exception e) { GlobalIO.printlnGeneral(e.toString()); GlobalIO.closeGlobalOutfile(); } } void doSet() throws Exception { String thing = script.sval.toUpperCase(); if(thing.equals("OUTFILE")) { GeneralInput.nextToken(script); outfileName = script.sval; GlobalIO.closeGlobalOutfile(); // Close the old one GlobalIO.setGlobalOutfile(outfileName); } else if(thing.equals("WHEELPLOT") || thing.equals("PARAMSETARRAY")) { GeneralInput.nextToken(script); Globals.setCurParamSetArray(script.sval); } } void doStatistic(String stat_name) throws Exception { // GlobalIO.printlnScript(""); // GlobalIO.printlnScript("Doing Statistic " + stat_name + " on parameter set array " + Globals.getCurParamSetArray().getFilename()); try { Class c = Class.forName("stat." + stat_name + "Stat"); Stat stat = (Stat)c.newInstance(); stat.init(Globals.getCurParamSetArray()); stat.calculateStat(); } catch(Exception e) { GlobalIO.printlnGeneral("Statistic <" + stat_name + "> was not found or had some other problem"); } } }