Changeset 30 in tmcsimulator-scriptbuilder for trunk/src/scriptbuilder/structures


Ignore:
Timestamp:
08/02/2017 12:48:06 PM (9 years ago)
Author:
bmcguffin
Message:

Made TimeSlice?, ScriptIncident?, and SimulationScript? all implement I_XML_Writable. Simulationscript also now implements a saveScriptToFile() method.

Location:
trunk/src/scriptbuilder/structures
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/scriptbuilder/structures/ScriptIncident.java

    r24 r30  
    1919 * @version 2017/06/29 
    2020 */ 
    21 public class ScriptIncident 
     21public class ScriptIncident implements I_XML_Writable 
    2222{ 
    2323 
     
    212212        } 
    213213        return arr; 
     214    } 
     215 
     216    @Override 
     217    public String toXML() 
     218    { 
     219        String output = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"; 
     220        output += "<!DOCTYPE TMC_SCRIPT SYSTEM \"script.dtd\">"; 
     221        output += openTag(ELEMENT.TMC_SCRIPT.tag + " title=\"" + this.script.title + "\""); 
     222         
     223        ArrayList<TimeSlice> s = (ArrayList) slices.values(); 
     224         
     225        for (TimeSlice slice : s) 
     226        { 
     227            output += slice.toXML(); 
     228        } 
     229        output += closeTag(ELEMENT.TMC_SCRIPT.tag); 
     230        return output; 
     231    } 
     232 
     233 
     234    @Override 
     235    public String openTag(String s) 
     236    { 
     237        return "<" + s + ">\n"; 
     238    } 
     239 
     240    @Override 
     241    public String closeTag(String s) 
     242    { 
     243        return "</" + s + ">\n"; 
     244    } 
     245 
     246    @Override 
     247    public String emptyTag(String s) 
     248    { 
     249        return "<" + s + "/>\n"; 
    214250    } 
    215251 
  • trunk/src/scriptbuilder/structures/SimulationScript.java

    r24 r30  
    22 
    33import java.awt.Color; 
     4import java.io.BufferedWriter; 
    45import java.io.File; 
     6import java.io.FileWriter; 
     7import java.io.OutputStream; 
     8import java.io.OutputStreamWriter; 
    59import java.util.ArrayList; 
    610import java.util.List; 
     
    2529 * @version 2017/06/22 
    2630 */ 
    27 public class SimulationScript extends Observable 
     31public class SimulationScript extends Observable implements I_XML_Writable 
    2832{ 
    2933 
     
    4549    }; 
    4650 
     51    public String title = ""; 
     52 
    4753    /** 
    4854     * The incidents displayed by the GUI. 
     
    9399        } 
    94100        /* 
    95          // Add some demo events 
     101         Add some demo events 
    96102         incidents.add(new ScriptIncident(incidentColors[1], 174, "Blueberry Truck", 
    97103         "Blueberry truck crashed on the freeway.", 8800, this)); 
     
    109115         "Truck stalled on the freeway.", 2800, this)); 
    110116         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(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)); 
    117123         incidents.add(null); 
    118124         incidents.add(null); 
     
    146152         incidents.get(4).setOffset(4400); 
    147153         incidents.get(5).setOffset(1400); 
    148          //incidents.get(6).setOffset(7600); 
    149          //incidents.get(7).setOffset(2400); 
    150          //incidents.get(8).setOffset(4800);  
     154         incidents.get(6).setOffset(7600); 
     155         incidents.get(7).setOffset(2400); 
     156         incidents.get(8).setOffset(4800);  
    151157         } 
    152158         */ 
     
    217223        this.update(); 
    218224    } 
     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 
    219325} 
  • trunk/src/scriptbuilder/structures/TimeSlice.java

    r21 r30  
    88import java.util.TimeZone; 
    99import scriptbuilder.gui.ScriptBuilderGuiConstants; 
     10import scriptbuilder.structures.events.*; 
    1011import scriptbuilder.structures.events.I_ScriptEvent; 
    1112 
     
    194195 
    195196    /** 
    196      * Converts the contents of this timeslice to a correctly 
    197      * formatted <ScriptEvent> XML element. 
     197     * Converts the contents of this timeslice to a correctly formatted 
     198     * <ScriptEvent> XML element. 
    198199     * 
    199200     * @return XML conversion of this timeslice. 
     
    202203    public String toXML() 
    203204    { 
     205        ArrayList<I_ScriptEvent> eventsCopy = new ArrayList<I_ScriptEvent>(); 
     206 
     207        for (I_ScriptEvent e : events) 
     208        { 
     209            eventsCopy.add(e); 
     210        } 
     211 
    204212        System.out.println("Seconds:: " + seconds); 
    205213        String output = openTag(ELEMENT.SCRIPT_EVENT.tag); 
     
    212220        output += thisIncident.name + closeTag(ELEMENT.INCIDENT.tag); 
    213221 
    214         for (I_ScriptEvent ev : events) 
     222        output += openTag(ELEMENT.CAD_DATA.tag); 
     223        if (containsCADIncidentEvent()) 
     224        { 
     225            output += openTag(ELEMENT.CAD_INCIDENT_EVENT.tag); 
     226            for (I_ScriptEvent ev : eventsCopy) 
     227            { 
     228                if (ev instanceof I_XML_Writable && isCADIncidentEvent(ev)) 
     229                { 
     230                    I_XML_Writable ex = (I_XML_Writable) ev; 
     231                    output += ex.toXML(); 
     232                    eventsCopy.remove(ev); 
     233                } 
     234            } 
     235            output += closeTag(ELEMENT.CAD_INCIDENT_EVENT.tag); 
     236        } 
     237        output += closeTag(ELEMENT.CAD_DATA.tag); 
     238 
     239        for (I_ScriptEvent ev : eventsCopy) 
    215240        { 
    216241            if (ev instanceof I_XML_Writable) 
     
    242267        return "<" + s + "/>\n"; 
    243268    } 
     269 
     270    private boolean containsCADIncidentEvent() 
     271    { 
     272        for (I_ScriptEvent ev : events) 
     273        { 
     274            if (isCADIncidentEvent(ev)) 
     275            { 
     276                return true; 
     277            } 
     278        } 
     279        return false; 
     280    } 
     281 
     282    private boolean isCADIncidentEvent(I_ScriptEvent ev) 
     283    { 
     284        return ev instanceof AudioEvent || ev instanceof UnitEvent 
     285                || ev instanceof ParamicsEvent || ev instanceof TowEvent 
     286                || ev instanceof WitnessEvent; 
     287    } 
    244288} 
Note: See TracChangeset for help on using the changeset viewer.