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

Revision 145, 8.9 KB checked in by sdanthin, 6 years ago (diff)

Move from Git to Svn (LARGE COMMIT)

Line 
1package scriptbuilder.structures;
2
3import java.awt.Color;
4import java.io.BufferedWriter;
5import java.io.File;
6import java.io.FileWriter;
7import java.util.ArrayList;
8import java.util.List;
9import java.util.Observable;
10import java.util.Vector;
11import javax.xml.parsers.SAXParserFactory;
12import scriptbuilder.structures.ScriptIncident.IncidentFocusedEvent;
13import scriptbuilder.structures.ScriptIncident.SliceChangedEvent;
14import scriptbuilder.structures.units.Unit;
15
16/**
17 * Representation of the script to be run by the TMC Simulator. Holds a list of
18 * incidents, which have start and end times and contain events.
19 *
20 * @author Greg Eddington <geddingt@calpoly.edu>
21 *
22 * @author Bryan McGuffin <bmcguffi@calpoly.edu>
23 * @author Sebastien Danthinne <sdanthin@calpoly.edu>
24 * @version 2017/06/22
25 */
26public class SimulationScript extends Observable implements I_XML_Writable
27{
28
29    /**
30     * All default options for GUI colorings of incidents.
31     */
32    public static final Color[] incidentColors =
33    {
34        Color.BLACK,
35        Color.BLUE,
36        Color.RED,
37        new Color(0x38, 0x5E, 0x0F),
38        new Color(128, 0, 128),
39        Color.MAGENTA,
40        new Color(0x23, 0x6B, 0x8E),
41        Color.ORANGE,
42        new Color(0x60, 0x33, 0x11),
43        Color.GRAY
44    };
45
46    /**
47     * The file to which this script will be saved.
48     */
49    public File saveFile = null;
50
51    /**
52     * The name of this script.
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    //todo: this incident fill count error
69    private final int INCIDENT_FILL_COUNT = 10;
70
71    /**
72     * Number of incidents currently displayed.
73     */
74    public int numberOfIncidents;
75
76    /**
77     * Script handler for parsing incoming XML files.
78     */
79    private MyScriptHandler sh;
80   
81    public boolean saved;
82
83    //TODO: Pretty much everything in this constructor is dummy data.
84    //Replace all of it.
85    /**
86     * Constructor. Backfill incident list with null objects, to be replaced
87     * later.
88     */
89    public SimulationScript()
90    {
91        sh = new MyScriptHandler(this);
92        incidents = new ArrayList<ScriptIncident>();
93        units = new ArrayList<Unit>();
94        numberOfIncidents = 0;
95        saved = true;
96
97        //Backfill with null incidents
98        for (int i = numberOfIncidents; i < INCIDENT_FILL_COUNT; i++)
99        {
100            incidents.add(null);
101        }
102    }
103   
104    /**
105     * checks and sees if this object has the unit passed.
106     * @param unitID
107     * @return does this SimulationScript have unitnum?
108     */
109    public boolean hasUnit(String unitID){
110        boolean indicator = false;
111        if(units.size()!=0){
112            for(Unit u : units){
113               if(unitID.equals(u.UnitNum)){
114                   indicator = true;
115               }
116            }
117        }
118           
119        return indicator;
120    }
121    /**
122     * creates a dummy unit that only has unit number
123     * @param unitNum
124     */
125    public void addDummyUnit(String unitNum){
126        Unit dummy = new Unit();
127        dummy.UnitNum = unitNum;
128        units.add(dummy);
129    }
130    /**
131     * Update the script's observers.
132     *
133     */
134    public void update()
135    {
136        // The script has changed, notify observers
137        //use to rewrite the save indicator
138        //System.out.println("Script changed");
139        setChanged();
140        notifyObservers(this);
141        saved = false;
142       
143       
144    }
145   
146
147    /**
148     * Tell this script's observers that there is a new slice event.
149     *
150     * @param e the slice focus event
151     */
152    public void broadcastEvent(SliceChangedEvent e)
153    {
154        // The slice focus has changed; pass the message
155        setChanged();
156        notifyObservers(e);
157    }
158
159    /**
160     * Tell this script's observers that there is a new slice event.
161     *
162     * @param e the incident focus event
163     */
164    public void broadcastEvent(IncidentFocusedEvent e)
165    {
166        // The slice focus has changed; pass the message
167        setChanged();
168        notifyObservers(e);
169    }
170
171    /**
172     * Load in an existing script from an XML file.
173     *
174     * @param f the file containing the script
175     */
176    public void loadScriptFromFile(File f)
177    {
178        try
179        {
180
181            SAXParserFactory.newInstance().newSAXParser().parse(f, sh);
182
183            Vector<ScriptIncident> inc = sh.getIncidents();
184            units = sh.getUnits();
185            for (ScriptIncident sci : inc)
186            {
187                addIncident(sci);
188            }
189        }
190        catch (Exception ex)
191        {
192            System.out.println("ERROR LOADING SCRIPT");
193            ex.printStackTrace();
194        }
195        System.out.println("H");
196        for(Unit testUnit : units){
197            System.out.println(testUnit.toXML());
198        }
199        this.update();
200    }
201   
202    /**
203     * Load in an existing list of units from an XML file.
204     * @param f the file containing the units
205     */
206    public void loadUnitsFromFile(File f){
207        try
208        {
209            SAXParserFactory.newInstance().newSAXParser().parse(f, sh);
210            units.addAll(sh.getUnits());
211        }
212        catch (Exception ex)
213        {
214            System.out.println("ERROR LOADING UNITS");
215            ex.printStackTrace();
216        }
217        this.update();
218    }
219
220    /**
221     * Add a new incident to the script.
222     *
223     * @param sci the incident to be added.
224     * @return true if there was enough room to add this incident.
225     */
226    public boolean addIncident(ScriptIncident sci)
227    {
228        if (numberOfIncidents < INCIDENT_FILL_COUNT)
229        {
230            incidents.set(numberOfIncidents++, sci);
231            return true;
232        }
233        return false;
234    }
235
236    /**
237     * Write this script, in proper XML format, to the file in question.
238     *
239     * @param f the destination savefile to be written.
240     */
241    public void saveScriptToFile(File f)
242    {
243        try
244        {
245            f.createNewFile();
246
247            BufferedWriter bw = new BufferedWriter(new FileWriter(f));
248            bw.write(this.toXML());
249            bw.flush();
250            bw.close();
251
252        }
253        catch (Exception ex)
254        {
255            System.out.println("ERROR SAVING SCRIPT");
256            ex.printStackTrace();
257        }
258        saved = true;
259    }
260
261    @Override
262    public String toXML()
263    {
264        ArrayList<TimeSlice> slices = arrangeAllSlices();
265        String output = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n";
266        output += XMLWriter.internalDTD();
267        output += XMLWriter.openTag(ELEMENT.TMC_SCRIPT.tag + " title=\"" + this.title + "\"");
268
269        if (units.size() > 0)
270        {
271            output += XMLWriter.openTag(ELEMENT.SCRIPT_DATA.tag);
272            for (Unit unit : units)
273            {
274                output += unit.toXML();
275            }
276            output += XMLWriter.closeTag(ELEMENT.SCRIPT_DATA.tag);
277        }
278        for (TimeSlice slice : slices)
279        {
280            output += slice.toXML();
281        }
282        output += XMLWriter.closeTag(ELEMENT.TMC_SCRIPT.tag);
283        return output;
284    }
285
286    /**
287     * Arranges all timeslices in this script in chronological order, then by
288     * incident number.
289     *
290     * @return a list of all timeslices in the simulation script
291     */
292    public ArrayList<TimeSlice> arrangeAllSlices()
293    {
294        ArrayList<TimeSlice> list = new ArrayList<TimeSlice>();
295        int length = absoluteLength();
296
297        for (int i = 0; i < length; i++)
298        {
299            for (ScriptIncident inc : incidents)
300            {
301
302                if (inc != null && inc.slices.get(i) != null)
303                {
304                    list.add(inc.slices.get(i));
305                }
306            }
307        }
308        return list;
309    }
310
311    /**
312     * Gets the total length of the simulation in seconds.
313     *
314     * @return
315     */
316    public int absoluteLength()
317    {
318        int length = 0;
319        for (ScriptIncident inc : incidents)
320        {
321            if (inc != null)
322            {
323                inc.updateLength();
324                int currentLength = inc.length + inc.offset;
325                if (currentLength > length)
326                {
327                    length = currentLength;
328                }
329            }
330        }
331        return length;
332    }
333
334    /**
335     * Counts the number of incidents currently running.
336     *
337     * @return the number of non-null incidents currently in the script. A
338     * number between 0 and INCIDENT_FILL_COUNT, inclusive.
339     */
340    public int incidentCount()
341    {
342        int count = 0;
343        for (ScriptIncident inc : incidents)
344        {
345            if (inc != null)
346            {
347                count++;
348            }
349        }
350        return count;
351    }
352
353}
Note: See TracBrowser for help on using the repository browser.