package jasi.sim.user; import jasi.sim.basic.control.SimListener; import jasi.sim.basic.element.Element; import jasi.sim.basic.event.Event; import jasi.sim.basic.value.Time; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.Date; /** * Simple output and error output for simulation into files. */ public class BasicOutput implements SimListener { /** The simulation's default output file name. */ private static final String OUT_FILE = "sim.out"; /** The simulation's default error-output file. */ private static final String ERR_FILE = "sim.err"; /** The writer for the simulation's default output. */ private PrintWriter out; /** The writer for the simulation's default error output. */ private PrintWriter err; /** * Factory method for SimpleOutput. * * @return An instance of this class */ public static BasicOutput create() { return create(OUT_FILE, ERR_FILE); } /** * Factory method for SimpleOutput. * * @param output * The file name of the simulation output. * @param error * The file name of the simulation error output. * @return An instance of this class */ public static BasicOutput create(String output, String error) { PrintWriter o = new PrintWriter(System.out); try { o = new PrintWriter(new FileWriter(output), true); } catch (IOException e1) { e1.printStackTrace(); } PrintWriter e = new PrintWriter(System.err); try { e = new PrintWriter(new FileWriter(error), true); } catch (IOException e1) { e1.printStackTrace(); } return new BasicOutput(o, e); } /** * Constructor for SimpleOutput. * * @param output * The file name of the simulation output. * @param error * The file name of the simulation error output. */ public BasicOutput(PrintWriter output, PrintWriter error) { out = output; err = error; Date now = new Date(); err.println(now); out.println(now); } /** * Destructor of simulation output: * * @throws Throwable * An error while finalizing. */ @Override protected void finalize() throws Throwable { err.flush(); out.flush(); super.finalize(); } /** * An event has been pushed to the simulation calendar. * * @param event * The pushed event. */ public void pushedEvent(Event event) { write("Added " + event.getClass().getName() + " for " + event.getTime() + " [" + event.getPriority() + "]"); } /** * An event has been popped from the calendar and executed by the * simulation. * * @param event * The executed event. */ public void poppedEvent(Event event) { write("Executed " + event.getClass().getName() + " for " + event.getTime() + " [" + event.getPriority() + "] " + event.getEventListeners()); } /** * An event has been removed from the simulation calendar. * * @param event * The removed event. */ public void removedEvent(Event event) { write("Removed " + event.getClass().getName() + " for " + event.getTime() + " [" + event.getPriority() + "]"); } /** * A simulation thread has been started. * * @param time * The current time. */ public void startedSimulation(Time time) { write("Simulation started with " + time); } /** * A simulation thread has been finished. * * @param time * The current time. */ public void finishedSimulation(Time time) { write("Simulation finished at " + time); } /** * A simulation element has been created. * * @param element * The newly created element. */ public void createdElement(Element element) { write("Created " + element.getClass().getName() + "(" + element.getId() + ")"); } /** * A simulation element has changed its state. * * @param element * The newly created element. * @param name * The attribute's name. * @param value * The new value of the attribute. */ public void changedElement(Element element, String name, Object value) { StringBuffer s = new StringBuffer("Changed " + element.getClass().getName() + "(" + element.getId() + ") " + name + "='"); if (value == null) { s.append("NULL"); } else if (value.getClass().isArray()) { Object[] v = (Object[]) value; for (int i = 0; i < v.length; i++) { s.append(v[i] + ","); } } else { s.append(value); } s.append("'"); write(s.toString()); } /** * The attribute value of a simulation element has been read. * * @param element * The simulation element. * @param name * The attribute's name. * @param value * The value of the attribute. */ public void requestedElement(Element element, String name, Object value) { StringBuffer s = new StringBuffer("Requested "); if (element == null) { s.append("NULL " + name + " " + value); } else { s.append(element.getClass().getName() + "(" + element.getId() + ") " + name + "='"); if (value != null) { if (value.getClass().isArray()) { Object[] v = (Object[]) value; for (int i = 0; i < v.length; i++) { s.append(v[i] + ","); } } else { s.append(value); } } else { s.append("NULL"); } s.append("'"); } write(s.toString()); } /** * A simulation element has been deleted. * * @param element * The deleted element. */ public void deletedElement(Element element) { write("Deleted " + element.getClass().getName() + "(" + element.getId() + ")"); } /** * A time advance has been granted. * * @param time * The granted time advance. * @param speed * The simulation speed in factors of real time. */ public void advancedTime(Time time, double speed) { String t = "Simulation time advanced to " + time + " (" + speed + ")"; write(t); System.out.println(t); } /** * The simulation archive is read. * * @param file * The file name of the archive. */ public void startReadingArchive(String file) { write("Reading archive from " + file); } /** * The simulation archive was read. */ public void finishedReadingArchive() { write("Finished reading archive"); } /** * The simulation archive is written. * * @param file * The file name of the archive. */ public void startWritingArchive(String file) { write("Writing archive to " + file); } /** * The simulation archive was written. */ public void finishedWritingArchive() { write("Finished writing archive"); } /** * An exception occured during simulation. * * @param e * The exception. */ public void gotException(Exception e) { System.err.print(e); e.printStackTrace(err); } /** * An message was created by the simulation. * * @param m * The message. */ public void gotMessage(String m) { write(m); System.out.println(m); } /** * Write output. * * @param output * The output. */ private void write(String output) { out.println(output); } }