Warning: Can't use blame annotator:
svn blame failed on trunk/src/scriptbuilder/structures/SimulationScript.java: ("Can't find a temporary directory: Internal error", 20014)

source: tmcsimulator-scriptbuilder/trunk/src/scriptbuilder/structures/SimulationScript.java @ 24

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