| 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 | |
|---|
| 22 | /** |
|---|
| 23 | * Representation of the script to be run by the TMC Simulator. Holds a list of |
|---|
| 24 | * incidents, which have start and end times and contain events. |
|---|
| 25 | * |
|---|
| 26 | * @author Greg Eddington <geddingt@calpoly.edu> |
|---|
| 27 | * |
|---|
| 28 | * @author Bryan McGuffin <bmcguffi@calpoly.edu> |
|---|
| 29 | * @version 2017/06/22 |
|---|
| 30 | */ |
|---|
| 31 | public class SimulationScript extends Observable implements I_XML_Writable |
|---|
| 32 | { |
|---|
| 33 | |
|---|
| 34 | /** |
|---|
| 35 | * All default options for GUI colorings of incidents. |
|---|
| 36 | */ |
|---|
| 37 | public static final Color[] incidentColors = |
|---|
| 38 | { |
|---|
| 39 | Color.BLACK, |
|---|
| 40 | Color.BLUE, |
|---|
| 41 | Color.RED, |
|---|
| 42 | new Color(0x38, 0x5E, 0x0F), |
|---|
| 43 | new Color(128, 0, 128), |
|---|
| 44 | Color.MAGENTA, |
|---|
| 45 | new Color(0x23, 0x6B, 0x8E), |
|---|
| 46 | Color.ORANGE, |
|---|
| 47 | new Color(0x60, 0x33, 0x11), |
|---|
| 48 | Color.GRAY |
|---|
| 49 | }; |
|---|
| 50 | |
|---|
| 51 | public String title = ""; |
|---|
| 52 | |
|---|
| 53 | /** |
|---|
| 54 | * The incidents displayed by the GUI. |
|---|
| 55 | */ |
|---|
| 56 | public List<ScriptIncident> incidents; |
|---|
| 57 | |
|---|
| 58 | //Somewhere in the code, something assumes that the list of incidents |
|---|
| 59 | //contains exactly 10 items. Until I can find and un-break that, this will do. |
|---|
| 60 | private final int INCIDENT_FILL_COUNT = 10; |
|---|
| 61 | |
|---|
| 62 | /** |
|---|
| 63 | * Number of incidents currently displayed. |
|---|
| 64 | */ |
|---|
| 65 | private int numberOfIncidents; |
|---|
| 66 | |
|---|
| 67 | /** |
|---|
| 68 | * Script handler for parsing incoming XML files. |
|---|
| 69 | */ |
|---|
| 70 | private MyScriptHandler sh; |
|---|
| 71 | |
|---|
| 72 | //TODO: Pretty much everything in this constructor is dummy data. |
|---|
| 73 | //Replace all of it. |
|---|
| 74 | /** |
|---|
| 75 | * Constructor. Backfill incident list with null objects, to be replaced |
|---|
| 76 | * later. |
|---|
| 77 | */ |
|---|
| 78 | public SimulationScript() |
|---|
| 79 | { |
|---|
| 80 | sh = new MyScriptHandler(this); |
|---|
| 81 | incidents = new ArrayList<ScriptIncident>(); |
|---|
| 82 | numberOfIncidents = 0; |
|---|
| 83 | |
|---|
| 84 | // Create the media event |
|---|
| 85 | /*incidents.add(new ScriptIncident(incidentColors[0], 100, "Media", |
|---|
| 86 | "An incident for the media in CAD.", 10800, this)); |
|---|
| 87 | numberOfIncidents++; |
|---|
| 88 | // Create the "other" event |
|---|
| 89 | incidents.add(new ScriptIncident(incidentColors[9], 999, "Other", |
|---|
| 90 | "An incident for small-scale events, false events, " |
|---|
| 91 | + "and noise. All events added to this incident will " |
|---|
| 92 | + "receive a randomly generated incident number.", |
|---|
| 93 | 10800, this)); |
|---|
| 94 | numberOfIncidents++;*/ |
|---|
| 95 | //Backfill with null incidents |
|---|
| 96 | for (int i = numberOfIncidents; i < INCIDENT_FILL_COUNT; i++) |
|---|
| 97 | { |
|---|
| 98 | incidents.add(null); |
|---|
| 99 | } |
|---|
| 100 | /* |
|---|
| 101 | Add some demo events |
|---|
| 102 | incidents.add(new ScriptIncident(incidentColors[1], 174, "Blueberry Truck", |
|---|
| 103 | "Blueberry truck crashed on the freeway.", 8800, this)); |
|---|
| 104 | numberOfIncidents++; |
|---|
| 105 | incidents.add(new ScriptIncident(incidentColors[2], 175, "Construction Crash", |
|---|
| 106 | "Crash at construction site on Red Road.", 6800, this)); |
|---|
| 107 | numberOfIncidents++; |
|---|
| 108 | incidents.add(new ScriptIncident(incidentColors[3], 176, "Car Freeway Flip", |
|---|
| 109 | "Car flipped across the lane divider on the freeway.", 7200, this)); |
|---|
| 110 | numberOfIncidents++; |
|---|
| 111 | incidents.add(new ScriptIncident(incidentColors[4], 177, "Two Lane Crash", |
|---|
| 112 | "Crash taking two lanes on Tree Road.", 4200, this)); |
|---|
| 113 | numberOfIncidents++; |
|---|
| 114 | incidents.add(new ScriptIncident(incidentColors[5], 178, "Stalled Truck", |
|---|
| 115 | "Truck stalled on the freeway.", 2800, this)); |
|---|
| 116 | numberOfIncidents++; |
|---|
| 117 | incidents.add(new ScriptIncident(incidentColors[6], 179, "Tomato Truck Spill", |
|---|
| 118 | "Tomato trucked spilt tomatos all over the freeway.", 3200, this)); |
|---|
| 119 | incidents.add(new ScriptIncident(incidentColors[7], 180, "Crash at Intersection", |
|---|
| 120 | "Crash at the intersection of Tree Road and Red Road.", 4300, this)); |
|---|
| 121 | incidents.add(new ScriptIncident(incidentColors[8], 181, "Bomb Threat", |
|---|
| 122 | "Bomb threat and Road X.", 6000, this)); |
|---|
| 123 | incidents.add(null); |
|---|
| 124 | incidents.add(null); |
|---|
| 125 | incidents.add(null); |
|---|
| 126 | Random rng = new Random(); |
|---|
| 127 | ScriptEventType[] eventTypes = ScriptEventType.values(); |
|---|
| 128 | if (numberOfIncidents > 0) |
|---|
| 129 | { |
|---|
| 130 | for (int i = 0; i < 300; i++) |
|---|
| 131 | { |
|---|
| 132 | int n = rng.nextInt(); |
|---|
| 133 | int s = rng.nextInt(); |
|---|
| 134 | int e = rng.nextInt(); |
|---|
| 135 | if (n < 0) |
|---|
| 136 | { |
|---|
| 137 | n *= -1; |
|---|
| 138 | } |
|---|
| 139 | if (s < 0) |
|---|
| 140 | { |
|---|
| 141 | s *= -1; |
|---|
| 142 | } |
|---|
| 143 | if (e < 0) |
|---|
| 144 | { |
|---|
| 145 | e *= -1; |
|---|
| 146 | } |
|---|
| 147 | incidents.get(n % numberOfIncidents).slices.get(s % (incidents.get(n % numberOfIncidents).slices.size())).addEvent(new ScriptEvent(eventTypes[e % eventTypes.length])); |
|---|
| 148 | } |
|---|
| 149 | incidents.get(1).setOffset(200); |
|---|
| 150 | incidents.get(2).setOffset(3400); |
|---|
| 151 | incidents.get(3).setOffset(1400); |
|---|
| 152 | incidents.get(4).setOffset(4400); |
|---|
| 153 | incidents.get(5).setOffset(1400); |
|---|
| 154 | incidents.get(6).setOffset(7600); |
|---|
| 155 | incidents.get(7).setOffset(2400); |
|---|
| 156 | incidents.get(8).setOffset(4800); |
|---|
| 157 | } |
|---|
| 158 | */ |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | /** |
|---|
| 162 | * Update the script's observers. |
|---|
| 163 | * |
|---|
| 164 | */ |
|---|
| 165 | public void update() |
|---|
| 166 | { |
|---|
| 167 | // The script has changed, notify observers |
|---|
| 168 | setChanged(); |
|---|
| 169 | notifyObservers(this); |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | /** |
|---|
| 173 | * Tell this script's observers that there is a new slice event. |
|---|
| 174 | * |
|---|
| 175 | * @param e the slice focus event |
|---|
| 176 | */ |
|---|
| 177 | public void broadcastEvent(SliceChangedEvent e) |
|---|
| 178 | { |
|---|
| 179 | // The slice focus has changed; pass the message |
|---|
| 180 | setChanged(); |
|---|
| 181 | notifyObservers(e); |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | /** |
|---|
| 185 | * Tell this script's observers that there is a new slice event. |
|---|
| 186 | * |
|---|
| 187 | * @param e the incident focus event |
|---|
| 188 | */ |
|---|
| 189 | public void broadcastEvent(IncidentFocusedEvent e) |
|---|
| 190 | { |
|---|
| 191 | // The slice focus has changed; pass the message |
|---|
| 192 | setChanged(); |
|---|
| 193 | notifyObservers(e); |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | /** |
|---|
| 197 | * Load in an existing script from an XML file. |
|---|
| 198 | * |
|---|
| 199 | * @param f the file containing the script |
|---|
| 200 | */ |
|---|
| 201 | public void loadScriptFromFile(File f) |
|---|
| 202 | { |
|---|
| 203 | try |
|---|
| 204 | { |
|---|
| 205 | |
|---|
| 206 | SAXParserFactory.newInstance().newSAXParser().parse(f, sh); |
|---|
| 207 | |
|---|
| 208 | Vector<ScriptIncident> inc = sh.getIncidents(); |
|---|
| 209 | |
|---|
| 210 | for (ScriptIncident sci : inc) |
|---|
| 211 | { |
|---|
| 212 | if (numberOfIncidents < INCIDENT_FILL_COUNT) |
|---|
| 213 | { |
|---|
| 214 | incidents.set(numberOfIncidents++, sci); |
|---|
| 215 | } |
|---|
| 216 | } |
|---|
| 217 | } |
|---|
| 218 | catch (Exception ex) |
|---|
| 219 | { |
|---|
| 220 | System.out.println("ERROR LOADING SCRIPT"); |
|---|
| 221 | ex.printStackTrace(); |
|---|
| 222 | } |
|---|
| 223 | this.update(); |
|---|
| 224 | } |
|---|
| 225 | |
|---|
| 226 | public void saveScriptToFile(File f) |
|---|
| 227 | { |
|---|
| 228 | try |
|---|
| 229 | { |
|---|
| 230 | f.createNewFile(); |
|---|
| 231 | |
|---|
| 232 | BufferedWriter bw = new BufferedWriter(new FileWriter(f)); |
|---|
| 233 | bw.write(this.toXML()); |
|---|
| 234 | bw.flush(); |
|---|
| 235 | bw.close(); |
|---|
| 236 | |
|---|
| 237 | } |
|---|
| 238 | catch (Exception ex) |
|---|
| 239 | { |
|---|
| 240 | System.out.println("ERROR SAVING SCRIPT"); |
|---|
| 241 | ex.printStackTrace(); |
|---|
| 242 | } |
|---|
| 243 | } |
|---|
| 244 | |
|---|
| 245 | @Override |
|---|
| 246 | public String toXML() |
|---|
| 247 | { |
|---|
| 248 | String output = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"; |
|---|
| 249 | output += "<!DOCTYPE TMC_SCRIPT SYSTEM \"script.dtd\">"; |
|---|
| 250 | ArrayList<TimeSlice> slices = allSlices(); |
|---|
| 251 | output += openTag(ELEMENT.TMC_SCRIPT.tag + " title=\"" + this.title + "\""); |
|---|
| 252 | for (TimeSlice slice : slices) |
|---|
| 253 | { |
|---|
| 254 | output += slice.toXML(); |
|---|
| 255 | } |
|---|
| 256 | output += closeTag(ELEMENT.TMC_SCRIPT.tag); |
|---|
| 257 | return output; |
|---|
| 258 | } |
|---|
| 259 | |
|---|
| 260 | @Override |
|---|
| 261 | public String openTag(String s) |
|---|
| 262 | { |
|---|
| 263 | return "<" + s + ">\n"; |
|---|
| 264 | } |
|---|
| 265 | |
|---|
| 266 | @Override |
|---|
| 267 | public String closeTag(String s) |
|---|
| 268 | { |
|---|
| 269 | return "</" + s + ">\n"; |
|---|
| 270 | } |
|---|
| 271 | |
|---|
| 272 | @Override |
|---|
| 273 | public String emptyTag(String s) |
|---|
| 274 | { |
|---|
| 275 | return "<" + s + "/>\n"; |
|---|
| 276 | } |
|---|
| 277 | |
|---|
| 278 | /** |
|---|
| 279 | * Arranges all timeslices in this script in chronological order, then by |
|---|
| 280 | * incident number. |
|---|
| 281 | * |
|---|
| 282 | * @return a list of all timeslices in the simulation script |
|---|
| 283 | */ |
|---|
| 284 | public ArrayList<TimeSlice> allSlices() |
|---|
| 285 | { |
|---|
| 286 | ArrayList<TimeSlice> list = new ArrayList<TimeSlice>(); |
|---|
| 287 | int length = absoluteLength(); |
|---|
| 288 | |
|---|
| 289 | for (int i = 0; i < length; i++) |
|---|
| 290 | { |
|---|
| 291 | for (ScriptIncident inc : incidents) |
|---|
| 292 | { |
|---|
| 293 | |
|---|
| 294 | if (inc != null && inc.slices.get(i) != null) |
|---|
| 295 | { |
|---|
| 296 | list.add(inc.slices.get(i)); |
|---|
| 297 | } |
|---|
| 298 | } |
|---|
| 299 | } |
|---|
| 300 | return list; |
|---|
| 301 | } |
|---|
| 302 | |
|---|
| 303 | /** |
|---|
| 304 | * Gets the total length of the simulation in seconds. |
|---|
| 305 | * |
|---|
| 306 | * @return |
|---|
| 307 | */ |
|---|
| 308 | public int absoluteLength() |
|---|
| 309 | { |
|---|
| 310 | int length = 0; |
|---|
| 311 | for (ScriptIncident inc : incidents) |
|---|
| 312 | { |
|---|
| 313 | if (inc != null) |
|---|
| 314 | { |
|---|
| 315 | int currentLength = inc.length + inc.offset; |
|---|
| 316 | if (currentLength > length) |
|---|
| 317 | { |
|---|
| 318 | length = currentLength; |
|---|
| 319 | } |
|---|
| 320 | } |
|---|
| 321 | } |
|---|
| 322 | return length; |
|---|
| 323 | } |
|---|
| 324 | |
|---|
| 325 | } |
|---|