Changeset 180 in tmcsimulator for trunk/src/atmsdriver


Ignore:
Timestamp:
10/27/2017 01:38:13 PM (9 years ago)
Author:
jdalbey
Message:

ATMSDriver.java Refactored for cleaner design. DotColor? enum moved to LoopDetector? class. ConsoleDriver? renamed ConsoleTrafficDriver? (and associated project configurations updated), build.xml package-jars target updated, ConsoleTrafficDriver? logic improved to allow more user control, runtraffic.bash script created for automating traffic display tests.

Location:
trunk/src/atmsdriver
Files:
4 edited
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/src/atmsdriver/ATMSDriver.java

    r128 r180  
    145145                 
    146146                // run the console driver, pass it the atmsDriver highways model 
    147                 ConsoleDriver driver = new ConsoleDriver(atmsDriver.highways); 
     147                ConsoleTrafficDriver driver = new ConsoleTrafficDriver(atmsDriver.highways); 
    148148                 
    149149            } else { 
  • trunk/src/atmsdriver/ConsoleTrafficDriver.java

    r176 r180  
    55import atmsdriver.model.Highway; 
    66import atmsdriver.model.Station; 
     7import atmsdriver.model.LoopDetector.DOTCOLOR; 
    78import java.io.FileInputStream; 
    89import java.util.ArrayList; 
     
    2122 * @version 10/11/2017 
    2223 */ 
    23 public final class ConsoleDriver { 
     24public final class ConsoleTrafficDriver { 
    2425    // highways model 
    2526    private final Highways highways; 
     
    2728    // lists used for user input validation 
    2829    private final List<Integer> routeNumInputList; 
    29     private final List<String> dotColorInputList; 
    30     /** 
    31      * Properties for the ConsoleDriver 
     30    /** 
     31     * Properties for the ConsoleTrafficDriver 
    3232     */ 
    3333    private static Properties ConsoleDriverProperties; 
     
    5757 
    5858                // Construct the console driver using the highways model 
    59                 ConsoleDriver driver = new ConsoleDriver(highways); 
     59                ConsoleTrafficDriver driver = new ConsoleTrafficDriver(highways); 
    6060                driver.runConsole();     
    6161            } else { 
     
    9292     * @param highways  
    9393     */ 
    94     public ConsoleDriver(Highways highways) { 
     94    public ConsoleTrafficDriver(Highways highways) { 
    9595        // set highways model 
    9696        this.highways = highways; 
     
    9898        // set input validation lists 
    9999        routeNumInputList = generateRouteNumInputList(); 
    100         dotColorInputList = new ArrayList<>(Arrays.asList("R", "Y", "G"));         
    101100    } 
    102101     
     
    121120    public void runConsole() { 
    122121        Scanner sc = new Scanner(System.in); 
     122        char choice = 'A'; 
    123123        // Run continuously 
    124         while (true) { 
     124        while (choice != 'Q') 
     125        { 
    125126            // Get necessary values for colorization of highways 
    126127            Integer routeNumber = getRouteNumber(sc); 
     
    129130            Double range = getRange(sc, postmile); 
    130131            DOTCOLOR dotcolor = getDotColor(sc); 
    131              
     132 
    132133            // apply colorization to highways 
    133             applyColorToHighwayStretch(routeNumber, direction, postmile, range, dotcolor); 
    134         } 
    135     } 
    136      
    137     /** 
    138      * Applies specified color to the specified highway stretch. Route number and 
    139      * direction specify the highway. Postmile and range specify the stretch of 
    140      * specified highway. Dot color is the color to be applied to the stretch. 
    141      *  
    142      * @param routeNumber highway route number 
    143      * @param direction highway direction 
    144      * @param postmile origin postmile value 
    145      * @param range range from origin postmile 
    146      * @param dotColor the color to be applied to specified highway stretch 
    147      */ 
    148     public void applyColorToHighwayStretch(Integer routeNumber, DIRECTION direction,  
    149             Double postmile, Double range, DOTCOLOR dotColor) { 
    150         System.out.println("Applying " + dotColor.name() + " dots to highway "  
    151                 + routeNumber + " " + direction.name() + " at postmile "  
    152                 + postmile + " with a range of " + range + " miles..."); 
    153          
    154         // Get the highway by route number 
    155         Highway highway = highways.getHighwayByRouteNumber(routeNumber); 
    156          
    157         // start value for highway section, and end value for highway section 
    158         // by postmile 
    159         Double startPost; 
    160         Double endPost; 
    161          
    162         // postmiles increase from s to n and w to e 
    163          
    164         // if the direction is south or west 
    165         if(direction.equals(DIRECTION.SOUTH) || direction.equals(DIRECTION.WEST)) 
    166         { 
    167             // add range value to startPost to get 
    168             // the end postmile value of the highway section 
    169             startPost = postmile; 
    170             endPost = postmile + range; 
    171              
    172             // iterate through the stations, if within the specified highway 
    173             // stretch, update the station by direction and apply dot color 
    174             for(Station station : highway.stations) 
    175             { 
    176                 if(station.postmile >= startPost && station.postmile <= endPost) 
    177                 { 
    178                     station.updateByDirection(direction, dotColor); 
     134            highways.applyColorToHighwayStretch(routeNumber, direction, postmile, range, dotcolor); 
     135 
     136            System.out.println("Add another entry or Send now? (A/S)"); 
     137            choice = sc.next().toUpperCase().trim().charAt(0); 
     138            System.out.println(""); 
     139            if (choice == 'S') 
     140            { 
     141                // Send highway model to FEP for transmit to ATMS 
     142                try { 
     143                    highways.writeToFEP(); 
     144                } catch (SimulationException ex) { 
     145                    System.out.println("Skipping writeToFEP..."); 
    179146                } 
    180             } 
    181         } 
    182         // if the direction is north or east  
    183         else 
    184         { 
    185             //subtract range value from startPost 
    186             // to get the end postmile value of the highway section 
    187             startPost = postmile; 
    188             endPost = postmile - range; 
    189              
    190             // iterate through the stations, if within the specified highway 
    191             // section, update the station by direction and apply dot color 
    192             for(Station station : highway.stations) 
    193             { 
    194                 if(station.postmile <= startPost && station.postmile >= endPost) 
    195                 { 
    196                     station.updateByDirection(direction, dotColor); 
    197                 } 
    198             } 
    199         } 
    200         System.out.println(""); 
    201       //  Omit this since it's now running as a task in ATMSBatchDriver 
    202 //        try { 
    203 //            highways.writeToFEP(); 
    204 //        } catch (SimulationException ex) { 
    205 //            System.out.println("Skipping writeToFEP..."); 
    206 //        } 
    207     } 
     147                System.out.println("Add another entry or Quit? (A/Q)"); 
     148                choice = sc.next().toUpperCase().trim().charAt(0); 
     149            } 
     150        } 
     151    } 
     152     
    208153     
    209154    /** 
     
    398343            // prompt user for color 
    399344            System.out.println("Enter a dot color (G/Y/R):"); 
    400             dotColorInput = sc.next(); 
     345            dotColorInput = sc.next().toUpperCase(); 
    401346            System.out.println(""); 
    402347            // validate user's input 
    403             if(dotColorInputList.contains(dotColorInput)) 
     348            if(DOTCOLOR.allLetters.contains(dotColorInput)) 
    404349            { 
    405350                verified = true; 
     
    414359    } 
    415360     
    416     /** 
    417      * Enum for highway status dot colors. Each color has associated volume 
    418      * and occupancy constants. 
    419      * 
    420      * @author John A. Torres, jdalbey 
    421      * @version 10/11/2017 
    422      */ 
    423     public static enum DOTCOLOR { 
    424  
    425         RED(1, 0.06f), 
    426         YELLOW(3,0.059f), // speed = 26 
    427         GREEN(0,0); 
    428          
    429         // All the first letters of the values, in order. 
    430         private static String allLetters = "RYG"; 
    431          
    432         private int vol;  /* volume */ 
    433         private float occ;  /* occupancy */       
    434          
    435         private DOTCOLOR(int v, float o) 
    436         { 
    437             vol = v; 
    438             occ = o; 
    439         } 
    440         /** 
    441          * Return the first letter of this enum. 
    442          * 
    443          * @return String first letter of this enum. 
    444          */ 
    445         public String getLetter() { 
    446             return this.toString().substring(0, 1); 
    447         } 
    448  
    449         public int volume() 
    450         { 
    451             return vol; 
    452         } 
    453         public float occupancy() 
    454         { 
    455             return occ; 
    456         } 
    457         /** 
    458          * Returns a dot color given its first character. 
    459          * 
    460          * @param letter the first character of a dot color 
    461          * @return dot color corresponding to letter 
    462          * @pre letter must be one of allLetters 
    463          */ 
    464         public static DOTCOLOR toDotColor(String letter) { 
    465             return values()[allLetters.indexOf(letter.charAt(0))]; 
    466         } 
    467     }   
    468361} 
  • trunk/src/atmsdriver/model/Highways.java

    r176 r180  
    109109        return highways; 
    110110    } 
    111      
     111        /** 
     112     * Applies specified color to the specified highway stretch. Route number and 
     113     * direction specify the highway. Postmile and range specify the stretch of 
     114     * specified highway. Dot color is the color to be applied to the stretch. 
     115     *  
     116     * @param routeNumber highway route number 
     117     * @param direction highway direction 
     118     * @param postmile origin postmile value 
     119     * @param range range from origin postmile 
     120     * @param dotColor the color to be applied to specified highway stretch 
     121     */ 
     122    public void applyColorToHighwayStretch(Integer routeNumber, Station.DIRECTION direction,  
     123            Double postmile, Double range, LoopDetector.DOTCOLOR dotColor) { 
     124        System.out.println("Applying " + dotColor.name() + " dots to highway "  
     125                + routeNumber + " " + direction.name() + " at postmile "  
     126                + postmile + " with a range of " + range + " miles..."); 
     127         
     128        // Get the highway by route number 
     129        Highway highway = getHighwayByRouteNumber(routeNumber); 
     130         
     131        // start value for highway section, and end value for highway section 
     132        // by postmile 
     133        Double startPost; 
     134        Double endPost; 
     135         
     136        // postmiles increase from s to n and w to e 
     137         
     138        // if the direction is south or west 
     139        if(direction.equals(Station.DIRECTION.SOUTH) || direction.equals(Station.DIRECTION.WEST)) 
     140        { 
     141            // add range value to startPost to get 
     142            // the end postmile value of the highway section 
     143            startPost = postmile; 
     144            endPost = postmile + range; 
     145             
     146            // iterate through the stations, if within the specified highway 
     147            // stretch, update the station by direction and apply dot color 
     148            for(Station station : highway.stations) 
     149            { 
     150                if(station.postmile >= startPost && station.postmile <= endPost) 
     151                { 
     152                    station.updateByDirection(direction, dotColor); 
     153                } 
     154            } 
     155        } 
     156        // if the direction is north or east  
     157        else 
     158        { 
     159            //subtract range value from startPost 
     160            // to get the end postmile value of the highway section 
     161            startPost = postmile; 
     162            endPost = postmile - range; 
     163             
     164            // iterate through the stations, if within the specified highway 
     165            // section, update the station by direction and apply dot color 
     166            for(Station station : highway.stations) 
     167            { 
     168                if(station.postmile <= startPost && station.postmile >= endPost) 
     169                { 
     170                    station.updateByDirection(direction, dotColor); 
     171                } 
     172            } 
     173        } 
     174        System.out.println(""); 
     175    } 
     176 
    112177    /* 
    113178     private ArrayList<FEPLine> loadLines(String highwayMetaFileName) { 
  • trunk/src/atmsdriver/model/LoopDetector.java

    r176 r180  
    121121        loopElement.appendChild(spdElement); 
    122122    } 
     123     
     124    /** 
     125     * Enum for highway status dot colors. Each color has associated volume 
     126     * and occupancy constants. 
     127     * 
     128     * @author John A. Torres, jdalbey 
     129     * @version 10/11/2017 
     130     */ 
     131    public static enum DOTCOLOR { 
     132 
     133        RED(1, 0.06f),    // "Stopped" is less than 25mph 
     134        YELLOW(3,0.059f), // speed = 26 
     135        GREEN(0,0); 
     136         
     137        // All the first letters of the values, in order. 
     138        public static String allLetters = "RYG"; 
     139         
     140        private int vol;  /* volume */ 
     141        private float occ;  /* occupancy */       
     142         
     143        private DOTCOLOR(int v, float o) 
     144        { 
     145            vol = v; 
     146            occ = o; 
     147        } 
     148        /** 
     149         * Return the first letter of this enum. 
     150         * 
     151         * @return String first letter of this enum. 
     152         */ 
     153        public String getLetter() { 
     154            return this.toString().substring(0, 1); 
     155        } 
     156 
     157        public int volume() 
     158        { 
     159            return vol; 
     160        } 
     161        public float occupancy() 
     162        { 
     163            return occ; 
     164        } 
     165        /** 
     166         * Returns a dot color given its first character. 
     167         * 
     168         * @param letter the first character of a dot color 
     169         * @return dot color corresponding to letter 
     170         * @pre letter must be one of allLetters 
     171         */ 
     172        public static DOTCOLOR toDotColor(String letter) { 
     173            return values()[allLetters.indexOf(letter.charAt(0))]; 
     174        } 
     175    }   
     176     
    123177} 
  • trunk/src/atmsdriver/model/Station.java

    r168 r180  
    11package atmsdriver.model; 
    22 
    3 import atmsdriver.ConsoleDriver; 
    4 import atmsdriver.ConsoleDriver.DOTCOLOR; 
     3import atmsdriver.ConsoleTrafficDriver; 
     4import atmsdriver.model.LoopDetector.DOTCOLOR; 
    55import java.util.List; 
    66import org.w3c.dom.Document; 
     
    174174    private void outputUpdateMessage(DOTCOLOR dotcolor, String OPP_ML) 
    175175    { 
    176         System.out.printf("Updating route %-3.3s %-5.5s %-3.3s lanes\t %-12.12s " 
     176        System.out.printf("Updating %-3.3s %-5.5s %-3.3s lanes\t %-12.12s " 
    177177                + "at postmile %-6.6s to %-7.7s\n",  
    178178                Integer.toString(this.routeNumber), this.direction.name(),  
Note: See TracChangeset for help on using the changeset viewer.