package uk.ac.leeds.sog.moses.agent; import java.io.File; import java.io.IOException; import org.apache.log4j.Appender; import org.apache.log4j.BasicConfigurator; import org.apache.log4j.Layout; import org.apache.log4j.Logger; import org.apache.log4j.PatternLayout; import org.apache.log4j.PropertyConfigurator; import org.apache.log4j.RollingFileAppender; import org.apache.log4j.xml.DOMConfigurator; /** * @author BELINDA * This class simulates the steps of the household lifecycel on a yearly basis. */ public class Simulation { // The current model simulates 100 years private static int MAX_SIMULATION_YEAR = 100; private static String s_censusFile = null; //output the results in log static { String logPropertyFile = System.getProperty("log4j.configuration"); if (null != logPropertyFile) { if (logPropertyFile.endsWith("xml")) { DOMConfigurator.configure(logPropertyFile); } else if (logPropertyFile.endsWith("properties")) { PropertyConfigurator.configure(logPropertyFile); } } else { BasicConfigurator.configure(); Logger root = Logger.getRootLogger(); try { Layout layout = new PatternLayout("%d{HH:mm:ssSSS} %-5p [%-15t]: %c{1} - %m%n"); ((Appender)root.getAllAppenders().nextElement()).setLayout(layout); RollingFileAppender appender = new RollingFileAppender(layout, "toymodellog.txt", false); appender.setMaxBackupIndex(10); appender.setMaxFileSize("50MB"); root.addAppender(appender); } catch (IOException e) { e.printStackTrace(); } } } private static Logger s_logger = Logger.getLogger(Simulation.class); private Model i_model; private ModelBuilder i_modelBuilder; //the constructor public Simulation() { i_modelBuilder = new ModelBuilder(); if(s_logger.isDebugEnabled()) { s_logger.debug("" + s_censusFile); } i_model = i_modelBuilder.buildModel(s_censusFile); // initialise probability model here ProbabilityModel.initialise(); } //Method to call the method to run the simulation if we have got the input file public static void main(String args[]) { // @author Andy Turner if(args.length == 0) { s_censusFile = "C:/Work/Projects/MoSeS/Workspace/ToyModelOutfile3.csv"; } else { s_censusFile = args[0]; } Simulation simulation = new Simulation(); simulation.run(); } //method to run the simulation private void run() { int step = 1; while(true) { s_logger.info("Simulation step: " + step); i_model.step(); if(i_model.simulationCompleted()) { break; } /*if(step == 53) { int stop = 1; stop++; }*/ step += 1; } s_logger.info("Simulation ends at step " + step); } }