| 1 | /* |
|---|
| 2 | * To change this license header, choose License Headers in Project Properties. |
|---|
| 3 | * To change this template file, choose Tools | Templates |
|---|
| 4 | * and open the template in the editor. |
|---|
| 5 | */ |
|---|
| 6 | package atmsdriver.trafficeventseditor; |
|---|
| 7 | |
|---|
| 8 | import atmsdriver.model.Highway; |
|---|
| 9 | import atmsdriver.model.Highways; |
|---|
| 10 | import atmsdriver.model.LoopDetector; |
|---|
| 11 | import atmsdriver.model.LoopDetector.DOTCOLOR; |
|---|
| 12 | import atmsdriver.model.Station; |
|---|
| 13 | import java.util.ArrayList; |
|---|
| 14 | import java.util.List; |
|---|
| 15 | import java.util.Observable; |
|---|
| 16 | import java.util.logging.Level; |
|---|
| 17 | import java.util.logging.Logger; |
|---|
| 18 | import tmcsim.common.SimulationException; |
|---|
| 19 | |
|---|
| 20 | /** |
|---|
| 21 | * |
|---|
| 22 | * @author jtorres |
|---|
| 23 | */ |
|---|
| 24 | public class TimeFrames extends Observable |
|---|
| 25 | { |
|---|
| 26 | public List<TimeFrame> frames; |
|---|
| 27 | final public Highways highways; |
|---|
| 28 | public Highway currentHighway; |
|---|
| 29 | public TimeFrame currentTimeFrame; |
|---|
| 30 | public Station currentStation; |
|---|
| 31 | public LoopDetector currentLoopDetector; |
|---|
| 32 | public TrafficLaneEvent currentTrafficLaneEvent; |
|---|
| 33 | |
|---|
| 34 | public TimeFrames() |
|---|
| 35 | { |
|---|
| 36 | frames = new ArrayList<>(); |
|---|
| 37 | highways = new Highways("./config/vds_data/highways_fullmap.txt", |
|---|
| 38 | "192.168.251.46", 8080); |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | public void addTimeFrame(String time) |
|---|
| 42 | { |
|---|
| 43 | TimeFrame frame = new TimeFrame(time); |
|---|
| 44 | frames.add(frame); |
|---|
| 45 | |
|---|
| 46 | setCurrentTimeFrame(this.frames.size() - 1); |
|---|
| 47 | |
|---|
| 48 | setChanged(); |
|---|
| 49 | notifyObservers("add frame"); |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | public void addEventsToTimeFrame(int[] rows, DOTCOLOR dotcolor) |
|---|
| 53 | { |
|---|
| 54 | for(int row : rows) |
|---|
| 55 | { |
|---|
| 56 | TrafficLaneEvent event = new TrafficLaneEvent(currentHighway.routeNumber, |
|---|
| 57 | currentStation, currentStation.loops.get(row), dotcolor); |
|---|
| 58 | currentTimeFrame.addEvent(event); |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | setChanged(); |
|---|
| 62 | notifyObservers("add event"); |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | public void setCurrentHighway(int index) |
|---|
| 66 | { |
|---|
| 67 | currentHighway = highways.highways.get(index); |
|---|
| 68 | currentStation = null; |
|---|
| 69 | currentLoopDetector = null; |
|---|
| 70 | |
|---|
| 71 | setChanged(); |
|---|
| 72 | notifyObservers("select hwy"); |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | public void setCurrentTimeFrame(int index) |
|---|
| 76 | { |
|---|
| 77 | currentTimeFrame = frames.get(index); |
|---|
| 78 | |
|---|
| 79 | setChanged(); |
|---|
| 80 | notifyObservers("select frame"); |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | public void setCurrentStation(int index) |
|---|
| 84 | { |
|---|
| 85 | currentStation = currentHighway.stations.get(index); |
|---|
| 86 | currentLoopDetector = null; |
|---|
| 87 | |
|---|
| 88 | setChanged(); |
|---|
| 89 | notifyObservers("select station"); |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | public void setCurrentLoopDetector(int index) |
|---|
| 93 | { |
|---|
| 94 | currentLoopDetector = currentStation.loops.get(index); |
|---|
| 95 | |
|---|
| 96 | setChanged(); |
|---|
| 97 | notifyObservers("select loop"); |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | public void setCurrentTrafficLaneEvent(int index) |
|---|
| 101 | { |
|---|
| 102 | currentTrafficLaneEvent = currentTimeFrame.events.get(index); |
|---|
| 103 | |
|---|
| 104 | setChanged(); |
|---|
| 105 | notifyObservers("select event"); |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | int getCurrentTrafficEventIndex() |
|---|
| 109 | { |
|---|
| 110 | return currentTimeFrame.events.indexOf(currentTrafficLaneEvent); |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | int getCurrentTimeFrameIndex() |
|---|
| 114 | { |
|---|
| 115 | return frames.indexOf(currentTimeFrame); |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | int getCurrentHighwayIndex() |
|---|
| 119 | { |
|---|
| 120 | return highways.highways.indexOf(currentHighway); |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | public void deleteTrafficLaneEvent(int index) |
|---|
| 124 | { |
|---|
| 125 | currentTimeFrame.events.remove(index); |
|---|
| 126 | |
|---|
| 127 | if(currentTimeFrame.events.size() > 0) |
|---|
| 128 | { |
|---|
| 129 | if(index == 0) |
|---|
| 130 | { |
|---|
| 131 | currentTrafficLaneEvent = currentTimeFrame.events.get(0); |
|---|
| 132 | } |
|---|
| 133 | else |
|---|
| 134 | { |
|---|
| 135 | currentTrafficLaneEvent = currentTimeFrame.events.get(index - 1); |
|---|
| 136 | } |
|---|
| 137 | } |
|---|
| 138 | else |
|---|
| 139 | { |
|---|
| 140 | currentTrafficLaneEvent = null; |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | setChanged(); |
|---|
| 144 | notifyObservers("delete event"); |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | void deleteTimeFrame(int selectedIndex) |
|---|
| 148 | { |
|---|
| 149 | frames.remove(selectedIndex); |
|---|
| 150 | |
|---|
| 151 | if(frames.size() > 0) |
|---|
| 152 | { |
|---|
| 153 | if(selectedIndex == 0) |
|---|
| 154 | { |
|---|
| 155 | currentTimeFrame = frames.get(0); |
|---|
| 156 | } |
|---|
| 157 | else |
|---|
| 158 | { |
|---|
| 159 | currentTimeFrame = frames.get(selectedIndex - 1); |
|---|
| 160 | } |
|---|
| 161 | } |
|---|
| 162 | else |
|---|
| 163 | { |
|---|
| 164 | currentTimeFrame = null; |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | setChanged(); |
|---|
| 168 | notifyObservers("delete frame"); |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | void singlePreviewStation() |
|---|
| 172 | { |
|---|
| 173 | highways.reset(); |
|---|
| 174 | for(TrafficLaneEvent event : currentTimeFrame.events) |
|---|
| 175 | { |
|---|
| 176 | if(event.station.equals(currentStation)) |
|---|
| 177 | { |
|---|
| 178 | highways.applyTrafficLaneEvent(event); |
|---|
| 179 | } |
|---|
| 180 | } |
|---|
| 181 | writeToFEP(); |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | void singlePreviewHighways() |
|---|
| 185 | { |
|---|
| 186 | highways.reset(); |
|---|
| 187 | for(TrafficLaneEvent event : currentTimeFrame.events) |
|---|
| 188 | { |
|---|
| 189 | highways.applyTrafficLaneEvent(event); |
|---|
| 190 | } |
|---|
| 191 | writeToFEP(); |
|---|
| 192 | } |
|---|
| 193 | |
|---|
| 194 | void writeToFEP() |
|---|
| 195 | { |
|---|
| 196 | try |
|---|
| 197 | { |
|---|
| 198 | highways.writeToFEP(); |
|---|
| 199 | } |
|---|
| 200 | catch (SimulationException ex) |
|---|
| 201 | { |
|---|
| 202 | System.out.println("writeToFEP() failed"); |
|---|
| 203 | } |
|---|
| 204 | } |
|---|
| 205 | |
|---|
| 206 | void cumulativePreviewStation() |
|---|
| 207 | { |
|---|
| 208 | highways.reset(); |
|---|
| 209 | for(TimeFrame tf : frames) |
|---|
| 210 | { |
|---|
| 211 | for(TrafficLaneEvent e : tf.events) |
|---|
| 212 | { |
|---|
| 213 | if(e.station.equals(currentStation)) |
|---|
| 214 | { |
|---|
| 215 | highways.applyTrafficLaneEvent(e); |
|---|
| 216 | } |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | if(tf.equals(currentTimeFrame)) |
|---|
| 220 | break; |
|---|
| 221 | } |
|---|
| 222 | writeToFEP(); |
|---|
| 223 | } |
|---|
| 224 | |
|---|
| 225 | void cumulativePreviewHighways() |
|---|
| 226 | { |
|---|
| 227 | highways.reset(); |
|---|
| 228 | for(TimeFrame tf : frames) |
|---|
| 229 | { |
|---|
| 230 | for(TrafficLaneEvent e : tf.events) |
|---|
| 231 | { |
|---|
| 232 | highways.applyTrafficLaneEvent(e); |
|---|
| 233 | } |
|---|
| 234 | |
|---|
| 235 | if(tf.equals(currentTimeFrame)) |
|---|
| 236 | break; |
|---|
| 237 | } |
|---|
| 238 | writeToFEP(); |
|---|
| 239 | } |
|---|
| 240 | } |
|---|