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

Revision 65, 6.9 KB checked in by bmcguffin, 9 years ago (diff)

Moved the "add incident" statement set into its own publicly accessible method.

Line 
1package scriptbuilder.structures;
2
3import java.awt.Color;
4import java.io.BufferedWriter;
5import java.io.File;
6import java.io.FileWriter;
7import java.io.OutputStream;
8import java.io.OutputStreamWriter;
9import java.util.ArrayList;
10import java.util.List;
11import java.util.Observable;
12import java.util.Random;
13import java.util.Vector;
14import java.util.logging.Level;
15import java.util.logging.Logger;
16import javax.xml.parsers.ParserConfigurationException;
17import javax.xml.parsers.SAXParserFactory;
18import scriptbuilder.structures.ScriptEvent.ScriptEventType;
19import scriptbuilder.structures.ScriptIncident.IncidentFocusedEvent;
20import scriptbuilder.structures.ScriptIncident.SliceChangedEvent;
21import 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 */
32public 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                addIncident(sci);
152            }
153        }
154        catch (Exception ex)
155        {
156            System.out.println("ERROR LOADING SCRIPT");
157            ex.printStackTrace();
158        }
159        this.update();
160    }
161
162    public boolean addIncident(ScriptIncident sci)
163    {
164        if (numberOfIncidents < INCIDENT_FILL_COUNT)
165        {
166            incidents.set(numberOfIncidents++, sci);
167            return true;
168        }
169        return false;
170    }
171
172    public void saveScriptToFile(File f)
173    {
174        try
175        {
176            f.createNewFile();
177
178            BufferedWriter bw = new BufferedWriter(new FileWriter(f));
179            bw.write(this.toXML());
180            bw.flush();
181            bw.close();
182
183        }
184        catch (Exception ex)
185        {
186            System.out.println("ERROR SAVING SCRIPT");
187            ex.printStackTrace();
188        }
189    }
190
191    @Override
192    public String toXML()
193    {
194        ArrayList<TimeSlice> slices = allSlices();
195        String output = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n";
196        output += "<!DOCTYPE TMC_SCRIPT SYSTEM \"script.dtd\">\n";
197        output += XMLWriter.openTag(ELEMENT.TMC_SCRIPT.tag + " title=\"" + this.title + "\"");
198
199        if (units.size() > 0)
200        {
201            output += XMLWriter.openTag(ELEMENT.SCRIPT_DATA.tag);
202            for (Unit unit : units)
203            {
204                output += unit.toXML();
205            }
206            output += XMLWriter.closeTag(ELEMENT.SCRIPT_DATA.tag);
207        }
208        for (TimeSlice slice : slices)
209        {
210            output += slice.toXML();
211        }
212        output += XMLWriter.closeTag(ELEMENT.TMC_SCRIPT.tag);
213        return output;
214    }
215
216    /**
217     * Arranges all timeslices in this script in chronological order, then by
218     * incident number.
219     *
220     * @return a list of all timeslices in the simulation script
221     */
222    public ArrayList<TimeSlice> allSlices()
223    {
224        ArrayList<TimeSlice> list = new ArrayList<TimeSlice>();
225        int length = absoluteLength();
226
227        for (int i = 0; i < length; i++)
228        {
229            for (ScriptIncident inc : incidents)
230            {
231
232                if (inc != null && inc.slices.get(i) != null)
233                {
234                    list.add(inc.slices.get(i));
235                }
236            }
237        }
238        return list;
239    }
240
241    /**
242     * Gets the total length of the simulation in seconds.
243     *
244     * @return
245     */
246    public int absoluteLength()
247    {
248        int length = 0;
249        for (ScriptIncident inc : incidents)
250        {
251            if (inc != null)
252            {
253                inc.updateLength();
254                int currentLength = inc.length + inc.offset;
255                if (currentLength > length)
256                {
257                    length = currentLength;
258                }
259            }
260        }
261        return length;
262    }
263
264    public int incidentCount()
265    {
266        int count = 0;
267        for (ScriptIncident inc : incidents)
268        {
269            if (inc != null)
270            {
271                count++;
272            }
273        }
274        return count;
275    }
276
277}
Note: See TracBrowser for help on using the repository browser.