| 1 | package scriptbuilder.structures; |
|---|
| 2 | |
|---|
| 3 | import java.awt.Color; |
|---|
| 4 | import java.io.File; |
|---|
| 5 | import java.util.ArrayList; |
|---|
| 6 | import java.util.List; |
|---|
| 7 | import java.util.Observable; |
|---|
| 8 | import java.util.Observer; |
|---|
| 9 | import java.util.Random; |
|---|
| 10 | import java.util.Vector; |
|---|
| 11 | import java.util.logging.Level; |
|---|
| 12 | import java.util.logging.Logger; |
|---|
| 13 | import javax.xml.parsers.ParserConfigurationException; |
|---|
| 14 | import javax.xml.parsers.SAXParserFactory; |
|---|
| 15 | import scriptbuilder.structures.ScriptEvent.ScriptEventType; |
|---|
| 16 | import scriptbuilder.structures.ScriptIncident.IncidentFocusedEvent; |
|---|
| 17 | import scriptbuilder.structures.ScriptIncident.SliceChangedEvent; |
|---|
| 18 | |
|---|
| 19 | /** |
|---|
| 20 | * Representation of the script to be run by the TMC Simulator. Holds a list of |
|---|
| 21 | * incidents, which have start and end times and contain events. |
|---|
| 22 | * |
|---|
| 23 | * @author Greg Eddington <geddingt@calpoly.edu> |
|---|
| 24 | * |
|---|
| 25 | * @author Bryan McGuffin <bmcguffi@calpoly.edu> |
|---|
| 26 | * @version 2017/06/22 |
|---|
| 27 | */ |
|---|
| 28 | public class SimulationScript extends Observable |
|---|
| 29 | { |
|---|
| 30 | |
|---|
| 31 | /** |
|---|
| 32 | * All default options for GUI colorings of incidents. |
|---|
| 33 | */ |
|---|
| 34 | public static final Color[] incidentColors = |
|---|
| 35 | { |
|---|
| 36 | Color.BLACK, |
|---|
| 37 | Color.BLUE, |
|---|
| 38 | Color.RED, |
|---|
| 39 | new Color(0x38, 0x5E, 0x0F), |
|---|
| 40 | new Color(128, 0, 128), |
|---|
| 41 | Color.MAGENTA, |
|---|
| 42 | new Color(0x23, 0x6B, 0x8E), |
|---|
| 43 | Color.ORANGE, |
|---|
| 44 | new Color(0x60, 0x33, 0x11), |
|---|
| 45 | Color.GRAY |
|---|
| 46 | }; |
|---|
| 47 | |
|---|
| 48 | /** |
|---|
| 49 | * The incidents displayed by the GUI. |
|---|
| 50 | */ |
|---|
| 51 | public List<ScriptIncident> incidents; |
|---|
| 52 | |
|---|
| 53 | //Somewhere in the code, something assumes that the list of incidents |
|---|
| 54 | //contains exactly 10 items. Until I can find and un-break that, this will do. |
|---|
| 55 | private final int INCIDENT_FILL_COUNT = 10; |
|---|
| 56 | |
|---|
| 57 | /** |
|---|
| 58 | * Number of incidents currently displayed. |
|---|
| 59 | */ |
|---|
| 60 | private int numberOfIncidents; |
|---|
| 61 | |
|---|
| 62 | /** |
|---|
| 63 | * Script handler for parsing incoming XML files. |
|---|
| 64 | */ |
|---|
| 65 | private MyScriptHandler sh; |
|---|
| 66 | |
|---|
| 67 | //TODO: Pretty much everything in this constructor is dummy data. |
|---|
| 68 | //Replace all of it. |
|---|
| 69 | /** |
|---|
| 70 | * Constructor. Backfill incident list with null objects, to be replaced |
|---|
| 71 | * later. |
|---|
| 72 | */ |
|---|
| 73 | public SimulationScript() |
|---|
| 74 | { |
|---|
| 75 | sh = new MyScriptHandler(this); |
|---|
| 76 | incidents = new ArrayList<ScriptIncident>(); |
|---|
| 77 | numberOfIncidents = 0; |
|---|
| 78 | |
|---|
| 79 | // Create the media event |
|---|
| 80 | /*incidents.add(new ScriptIncident(incidentColors[0], 100, "Media", |
|---|
| 81 | "An incident for the media in CAD.", 10800, this)); |
|---|
| 82 | numberOfIncidents++; |
|---|
| 83 | // Create the "other" event |
|---|
| 84 | incidents.add(new ScriptIncident(incidentColors[9], 999, "Other", |
|---|
| 85 | "An incident for small-scale events, false events, " |
|---|
| 86 | + "and noise. All events added to this incident will " |
|---|
| 87 | + "receive a randomly generated incident number.", |
|---|
| 88 | 10800, this)); |
|---|
| 89 | numberOfIncidents++;*/ |
|---|
| 90 | //Backfill with null incidents |
|---|
| 91 | for (int i = numberOfIncidents; i < INCIDENT_FILL_COUNT; i++) |
|---|
| 92 | { |
|---|
| 93 | incidents.add(null); |
|---|
| 94 | } |
|---|
| 95 | /* |
|---|
| 96 | // Add some demo events |
|---|
| 97 | incidents.add(new ScriptIncident(incidentColors[1], 174, "Blueberry Truck", |
|---|
| 98 | "Blueberry truck crashed on the freeway.", 8800, this)); |
|---|
| 99 | numberOfIncidents++; |
|---|
| 100 | incidents.add(new ScriptIncident(incidentColors[2], 175, "Construction Crash", |
|---|
| 101 | "Crash at construction site on Red Road.", 6800, this)); |
|---|
| 102 | numberOfIncidents++; |
|---|
| 103 | incidents.add(new ScriptIncident(incidentColors[3], 176, "Car Freeway Flip", |
|---|
| 104 | "Car flipped across the lane divider on the freeway.", 7200, this)); |
|---|
| 105 | numberOfIncidents++; |
|---|
| 106 | incidents.add(new ScriptIncident(incidentColors[4], 177, "Two Lane Crash", |
|---|
| 107 | "Crash taking two lanes on Tree Road.", 4200, this)); |
|---|
| 108 | numberOfIncidents++; |
|---|
| 109 | incidents.add(new ScriptIncident(incidentColors[5], 178, "Stalled Truck", |
|---|
| 110 | "Truck stalled on the freeway.", 2800, this)); |
|---|
| 111 | numberOfIncidents++; |
|---|
| 112 | //incidents.add(new ScriptIncident(incidentColors[6], 179, "Tomato Truck Spill", |
|---|
| 113 | // "Tomato trucked spilt tomatos all over the freeway.", 3200, this)); |
|---|
| 114 | //incidents.add(new ScriptIncident(incidentColors[7], 180, "Crash at Intersection", |
|---|
| 115 | // "Crash at the intersection of Tree Road and Red Road.", 4300, this)); |
|---|
| 116 | //incidents.add(new ScriptIncident(incidentColors[8], 181, "Bomb Threat", |
|---|
| 117 | // "Bomb threat and Road X.", 6000, this)); |
|---|
| 118 | incidents.add(null); |
|---|
| 119 | incidents.add(null); |
|---|
| 120 | incidents.add(null); |
|---|
| 121 | Random rng = new Random(); |
|---|
| 122 | ScriptEventType[] eventTypes = ScriptEventType.values(); |
|---|
| 123 | if (numberOfIncidents > 0) |
|---|
| 124 | { |
|---|
| 125 | for (int i = 0; i < 300; i++) |
|---|
| 126 | { |
|---|
| 127 | int n = rng.nextInt(); |
|---|
| 128 | int s = rng.nextInt(); |
|---|
| 129 | int e = rng.nextInt(); |
|---|
| 130 | if (n < 0) |
|---|
| 131 | { |
|---|
| 132 | n *= -1; |
|---|
| 133 | } |
|---|
| 134 | if (s < 0) |
|---|
| 135 | { |
|---|
| 136 | s *= -1; |
|---|
| 137 | } |
|---|
| 138 | if (e < 0) |
|---|
| 139 | { |
|---|
| 140 | e *= -1; |
|---|
| 141 | } |
|---|
| 142 | incidents.get(n % numberOfIncidents).slices.get(s % (incidents.get(n % numberOfIncidents).slices.size())).addEvent(new ScriptEvent(eventTypes[e % eventTypes.length])); |
|---|
| 143 | } |
|---|
| 144 | incidents.get(1).setOffset(200); |
|---|
| 145 | incidents.get(2).setOffset(3400); |
|---|
| 146 | incidents.get(3).setOffset(1400); |
|---|
| 147 | incidents.get(4).setOffset(4400); |
|---|
| 148 | incidents.get(5).setOffset(1400); |
|---|
| 149 | //incidents.get(6).setOffset(7600); |
|---|
| 150 | //incidents.get(7).setOffset(2400); |
|---|
| 151 | //incidents.get(8).setOffset(4800); |
|---|
| 152 | } |
|---|
| 153 | */ |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | /** |
|---|
| 157 | * Update the script's observers. |
|---|
| 158 | * |
|---|
| 159 | */ |
|---|
| 160 | public void update() |
|---|
| 161 | { |
|---|
| 162 | // The script has changed, notify observers |
|---|
| 163 | setChanged(); |
|---|
| 164 | notifyObservers(this); |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | /** |
|---|
| 168 | * Tell this script's observers that there is a new slice event. |
|---|
| 169 | * |
|---|
| 170 | * @param e the slice focus event |
|---|
| 171 | */ |
|---|
| 172 | public void broadcastEvent(SliceChangedEvent e) |
|---|
| 173 | { |
|---|
| 174 | // The slice focus has changed; pass the message |
|---|
| 175 | setChanged(); |
|---|
| 176 | notifyObservers(e); |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | /** |
|---|
| 180 | * Tell this script's observers that there is a new slice event. |
|---|
| 181 | * |
|---|
| 182 | * @param e the incident focus event |
|---|
| 183 | */ |
|---|
| 184 | public void broadcastEvent(IncidentFocusedEvent e) |
|---|
| 185 | { |
|---|
| 186 | // The slice focus has changed; pass the message |
|---|
| 187 | setChanged(); |
|---|
| 188 | notifyObservers(e); |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | /** |
|---|
| 192 | * Load in an existing script from an XML file. |
|---|
| 193 | * |
|---|
| 194 | * @param f the file containing the script |
|---|
| 195 | */ |
|---|
| 196 | public void loadScriptFromFile(File f) |
|---|
| 197 | { |
|---|
| 198 | try |
|---|
| 199 | { |
|---|
| 200 | |
|---|
| 201 | SAXParserFactory.newInstance().newSAXParser().parse(f, sh); |
|---|
| 202 | |
|---|
| 203 | Vector<ScriptIncident> inc = sh.getIncidents(); |
|---|
| 204 | |
|---|
| 205 | for (ScriptIncident sci : inc) |
|---|
| 206 | { |
|---|
| 207 | if (numberOfIncidents < INCIDENT_FILL_COUNT) |
|---|
| 208 | { |
|---|
| 209 | incidents.set(numberOfIncidents++, sci); |
|---|
| 210 | } |
|---|
| 211 | } |
|---|
| 212 | } |
|---|
| 213 | catch (Exception ex) |
|---|
| 214 | { |
|---|
| 215 | System.out.println("ERROR LOADING SCRIPT"); |
|---|
| 216 | ex.printStackTrace(); |
|---|
| 217 | } |
|---|
| 218 | this.update(); |
|---|
| 219 | } |
|---|
| 220 | } |
|---|