| 1 | package scriptbuilder.structures; |
|---|
| 2 | |
|---|
| 3 | import java.awt.Color; |
|---|
| 4 | import java.io.BufferedWriter; |
|---|
| 5 | import java.io.File; |
|---|
| 6 | import java.io.FileWriter; |
|---|
| 7 | import java.io.OutputStream; |
|---|
| 8 | import java.io.OutputStreamWriter; |
|---|
| 9 | import java.util.ArrayList; |
|---|
| 10 | import java.util.List; |
|---|
| 11 | import java.util.Observable; |
|---|
| 12 | import java.util.Random; |
|---|
| 13 | import java.util.Vector; |
|---|
| 14 | import java.util.logging.Level; |
|---|
| 15 | import java.util.logging.Logger; |
|---|
| 16 | import javax.xml.parsers.ParserConfigurationException; |
|---|
| 17 | import javax.xml.parsers.SAXParserFactory; |
|---|
| 18 | import scriptbuilder.structures.ScriptEvent.ScriptEventType; |
|---|
| 19 | import scriptbuilder.structures.ScriptIncident.IncidentFocusedEvent; |
|---|
| 20 | import scriptbuilder.structures.ScriptIncident.SliceChangedEvent; |
|---|
| 21 | import scriptbuilder.structures.units.Unit; |
|---|
| 22 | |
|---|
| 23 | /** |
|---|
| 24 | * Representation of the script to be run by the TMC Simulator. Holds a list of |
|---|
| 25 | * incidents, which have start and end times and contain events. |
|---|
| 26 | * |
|---|
| 27 | * @author Greg Eddington <geddingt@calpoly.edu> |
|---|
| 28 | * |
|---|
| 29 | * @author Bryan McGuffin <bmcguffi@calpoly.edu> |
|---|
| 30 | * @version 2017/06/22 |
|---|
| 31 | */ |
|---|
| 32 | public class SimulationScript extends Observable implements I_XML_Writable |
|---|
| 33 | { |
|---|
| 34 | |
|---|
| 35 | /** |
|---|
| 36 | * All default options for GUI colorings of incidents. |
|---|
| 37 | */ |
|---|
| 38 | public static final Color[] incidentColors = |
|---|
| 39 | { |
|---|
| 40 | Color.BLACK, |
|---|
| 41 | Color.BLUE, |
|---|
| 42 | Color.RED, |
|---|
| 43 | new Color(0x38, 0x5E, 0x0F), |
|---|
| 44 | new Color(128, 0, 128), |
|---|
| 45 | Color.MAGENTA, |
|---|
| 46 | new Color(0x23, 0x6B, 0x8E), |
|---|
| 47 | Color.ORANGE, |
|---|
| 48 | new Color(0x60, 0x33, 0x11), |
|---|
| 49 | Color.GRAY |
|---|
| 50 | }; |
|---|
| 51 | |
|---|
| 52 | public File saveFile = null; |
|---|
| 53 | |
|---|
| 54 | public String title = ""; |
|---|
| 55 | |
|---|
| 56 | /** |
|---|
| 57 | * The incidents displayed by the GUI. |
|---|
| 58 | */ |
|---|
| 59 | public List<ScriptIncident> incidents; |
|---|
| 60 | |
|---|
| 61 | /** |
|---|
| 62 | * The units which participate in Unit events. |
|---|
| 63 | */ |
|---|
| 64 | public List<Unit> units; |
|---|
| 65 | |
|---|
| 66 | //Somewhere in the code, something assumes that the list of incidents |
|---|
| 67 | //contains exactly 10 items. Until I can find and un-break that, this will do. |
|---|
| 68 | private final int INCIDENT_FILL_COUNT = 10; |
|---|
| 69 | |
|---|
| 70 | /** |
|---|
| 71 | * Number of incidents currently displayed. |
|---|
| 72 | */ |
|---|
| 73 | private int numberOfIncidents; |
|---|
| 74 | |
|---|
| 75 | /** |
|---|
| 76 | * Script handler for parsing incoming XML files. |
|---|
| 77 | */ |
|---|
| 78 | private MyScriptHandler sh; |
|---|
| 79 | |
|---|
| 80 | //TODO: Pretty much everything in this constructor is dummy data. |
|---|
| 81 | //Replace all of it. |
|---|
| 82 | /** |
|---|
| 83 | * Constructor. Backfill incident list with null objects, to be replaced |
|---|
| 84 | * later. |
|---|
| 85 | */ |
|---|
| 86 | public SimulationScript() |
|---|
| 87 | { |
|---|
| 88 | sh = new MyScriptHandler(this); |
|---|
| 89 | incidents = new ArrayList<ScriptIncident>(); |
|---|
| 90 | units = new ArrayList<Unit>(); |
|---|
| 91 | numberOfIncidents = 0; |
|---|
| 92 | |
|---|
| 93 | //Backfill with null incidents |
|---|
| 94 | for (int i = numberOfIncidents; i < INCIDENT_FILL_COUNT; i++) |
|---|
| 95 | { |
|---|
| 96 | incidents.add(null); |
|---|
| 97 | } |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | /** |
|---|
| 101 | * Update the script's observers. |
|---|
| 102 | * |
|---|
| 103 | */ |
|---|
| 104 | public void update() |
|---|
| 105 | { |
|---|
| 106 | // The script has changed, notify observers |
|---|
| 107 | setChanged(); |
|---|
| 108 | notifyObservers(this); |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | /** |
|---|
| 112 | * Tell this script's observers that there is a new slice event. |
|---|
| 113 | * |
|---|
| 114 | * @param e the slice focus event |
|---|
| 115 | */ |
|---|
| 116 | public void broadcastEvent(SliceChangedEvent e) |
|---|
| 117 | { |
|---|
| 118 | // The slice focus has changed; pass the message |
|---|
| 119 | setChanged(); |
|---|
| 120 | notifyObservers(e); |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | /** |
|---|
| 124 | * Tell this script's observers that there is a new slice event. |
|---|
| 125 | * |
|---|
| 126 | * @param e the incident focus event |
|---|
| 127 | */ |
|---|
| 128 | public void broadcastEvent(IncidentFocusedEvent e) |
|---|
| 129 | { |
|---|
| 130 | // The slice focus has changed; pass the message |
|---|
| 131 | setChanged(); |
|---|
| 132 | notifyObservers(e); |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | /** |
|---|
| 136 | * Load in an existing script from an XML file. |
|---|
| 137 | * |
|---|
| 138 | * @param f the file containing the script |
|---|
| 139 | */ |
|---|
| 140 | public void loadScriptFromFile(File f) |
|---|
| 141 | { |
|---|
| 142 | try |
|---|
| 143 | { |
|---|
| 144 | |
|---|
| 145 | SAXParserFactory.newInstance().newSAXParser().parse(f, sh); |
|---|
| 146 | |
|---|
| 147 | Vector<ScriptIncident> inc = sh.getIncidents(); |
|---|
| 148 | units = sh.getUnits(); |
|---|
| 149 | for (ScriptIncident sci : inc) |
|---|
| 150 | { |
|---|
| 151 | if (numberOfIncidents < INCIDENT_FILL_COUNT) |
|---|
| 152 | { |
|---|
| 153 | incidents.set(numberOfIncidents++, sci); |
|---|
| 154 | } |
|---|
| 155 | } |
|---|
| 156 | } |
|---|
| 157 | catch (Exception ex) |
|---|
| 158 | { |
|---|
| 159 | System.out.println("ERROR LOADING SCRIPT"); |
|---|
| 160 | ex.printStackTrace(); |
|---|
| 161 | } |
|---|
| 162 | this.update(); |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | public void saveScriptToFile(File f) |
|---|
| 166 | { |
|---|
| 167 | try |
|---|
| 168 | { |
|---|
| 169 | f.createNewFile(); |
|---|
| 170 | |
|---|
| 171 | BufferedWriter bw = new BufferedWriter(new FileWriter(f)); |
|---|
| 172 | bw.write(this.toXML()); |
|---|
| 173 | bw.flush(); |
|---|
| 174 | bw.close(); |
|---|
| 175 | |
|---|
| 176 | } |
|---|
| 177 | catch (Exception ex) |
|---|
| 178 | { |
|---|
| 179 | System.out.println("ERROR SAVING SCRIPT"); |
|---|
| 180 | ex.printStackTrace(); |
|---|
| 181 | } |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | @Override |
|---|
| 185 | public String toXML() |
|---|
| 186 | { |
|---|
| 187 | ArrayList<TimeSlice> slices = allSlices(); |
|---|
| 188 | String output = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"; |
|---|
| 189 | output += "<!DOCTYPE TMC_SCRIPT SYSTEM \"script.dtd\">\n"; |
|---|
| 190 | output += XMLWriter.openTag(ELEMENT.TMC_SCRIPT.tag + " title=\"" + this.title + "\""); |
|---|
| 191 | |
|---|
| 192 | if (units.size() > 0) |
|---|
| 193 | { |
|---|
| 194 | output += XMLWriter.openTag(ELEMENT.SCRIPT_DATA.tag); |
|---|
| 195 | for (Unit unit : units) |
|---|
| 196 | { |
|---|
| 197 | output += unit.toXML(); |
|---|
| 198 | } |
|---|
| 199 | output += XMLWriter.closeTag(ELEMENT.SCRIPT_DATA.tag); |
|---|
| 200 | } |
|---|
| 201 | for (TimeSlice slice : slices) |
|---|
| 202 | { |
|---|
| 203 | output += slice.toXML(); |
|---|
| 204 | } |
|---|
| 205 | output += XMLWriter.closeTag(ELEMENT.TMC_SCRIPT.tag); |
|---|
| 206 | return output; |
|---|
| 207 | } |
|---|
| 208 | /** |
|---|
| 209 | * Arranges all timeslices in this script in chronological order, then by |
|---|
| 210 | * incident number. |
|---|
| 211 | * |
|---|
| 212 | * @return a list of all timeslices in the simulation script |
|---|
| 213 | */ |
|---|
| 214 | public ArrayList<TimeSlice> allSlices() |
|---|
| 215 | { |
|---|
| 216 | ArrayList<TimeSlice> list = new ArrayList<TimeSlice>(); |
|---|
| 217 | int length = absoluteLength(); |
|---|
| 218 | |
|---|
| 219 | for (int i = 0; i < length; i++) |
|---|
| 220 | { |
|---|
| 221 | for (ScriptIncident inc : incidents) |
|---|
| 222 | { |
|---|
| 223 | |
|---|
| 224 | if (inc != null && inc.slices.get(i) != null) |
|---|
| 225 | { |
|---|
| 226 | list.add(inc.slices.get(i)); |
|---|
| 227 | } |
|---|
| 228 | } |
|---|
| 229 | } |
|---|
| 230 | return list; |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | /** |
|---|
| 234 | * Gets the total length of the simulation in seconds. |
|---|
| 235 | * |
|---|
| 236 | * @return |
|---|
| 237 | */ |
|---|
| 238 | public int absoluteLength() |
|---|
| 239 | { |
|---|
| 240 | int length = 0; |
|---|
| 241 | for (ScriptIncident inc : incidents) |
|---|
| 242 | { |
|---|
| 243 | if (inc != null) |
|---|
| 244 | { |
|---|
| 245 | int currentLength = inc.length + inc.offset; |
|---|
| 246 | if (currentLength > length) |
|---|
| 247 | { |
|---|
| 248 | length = currentLength; |
|---|
| 249 | } |
|---|
| 250 | } |
|---|
| 251 | } |
|---|
| 252 | return length; |
|---|
| 253 | } |
|---|
| 254 | |
|---|
| 255 | } |
|---|