1 | package vnsim.vehicular.routePlan.cityRouting; |
---|
2 | |
---|
3 | import java.util.ArrayList; |
---|
4 | |
---|
5 | public class CrossRoadCongestion { |
---|
6 | |
---|
7 | private IntersectionNode intersectionNode; |
---|
8 | // for every segments in the intersection |
---|
9 | private ArrayList<WeekCongestion> lastWeek; |
---|
10 | // for every segments in the intersection |
---|
11 | private ArrayList<ArrayList<Double>> realTimeCongestion; |
---|
12 | |
---|
13 | public CrossRoadCongestion(IntersectionNode inode) { |
---|
14 | realTimeCongestion = new ArrayList<ArrayList<Double>>(); |
---|
15 | lastWeek = new ArrayList<WeekCongestion>(); |
---|
16 | for (int i = 0; i < inode.getSegmentsNb(); i++) { |
---|
17 | realTimeCongestion.add(new ArrayList<Double>()); |
---|
18 | lastWeek.add(new WeekCongestion()); |
---|
19 | } |
---|
20 | intersectionNode = inode; |
---|
21 | } |
---|
22 | |
---|
23 | public IntersectionNode getIntersectionNode() { |
---|
24 | return intersectionNode; |
---|
25 | } |
---|
26 | |
---|
27 | // get last congestion on the segment |
---|
28 | public Double getCongestion(int segmentIndex, int day, int hour) { |
---|
29 | ArrayList<Double> data = realTimeCongestion.get(segmentIndex); |
---|
30 | if (data.size() == 0) { |
---|
31 | return null; |
---|
32 | } |
---|
33 | return data.get(data.size() - 1); |
---|
34 | } |
---|
35 | |
---|
36 | public Double getLastWeekCongestion(int segmentIndex, int day, int hour) { |
---|
37 | return lastWeek.get(segmentIndex).getCongestion(day, hour); |
---|
38 | } |
---|
39 | |
---|
40 | public void setCongestion(int segmentIndex, Double congestion) { |
---|
41 | realTimeCongestion.get(segmentIndex).add(congestion); |
---|
42 | } |
---|
43 | |
---|
44 | public void setLastWeekCongestion(int segmentIndex, int day, int hour, |
---|
45 | Double congestion) { |
---|
46 | lastWeek.get(segmentIndex).setCongestion(day, hour, congestion); |
---|
47 | } |
---|
48 | |
---|
49 | // for all segments mediate the real time data and set the value to lastWeek |
---|
50 | // clears the real-time list; |
---|
51 | public void updateLastWeekCongestion(int day, int hour) { |
---|
52 | Double congestion; |
---|
53 | for (int i = 0; i < lastWeek.size(); i++) { |
---|
54 | congestion = mediate(realTimeCongestion.get(i)); |
---|
55 | lastWeek.get(i).setCongestion(day, hour, congestion); |
---|
56 | realTimeCongestion.get(i).clear(); |
---|
57 | } |
---|
58 | } |
---|
59 | |
---|
60 | private static Double mediate(ArrayList<Double> list) { |
---|
61 | if (list == null) { |
---|
62 | return null; |
---|
63 | } |
---|
64 | if (list.size() == 0) { |
---|
65 | return new Double(0); |
---|
66 | } |
---|
67 | Double m = new Double(0); |
---|
68 | for (int i = 0; i < list.size(); i++) { |
---|
69 | m = m + list.get(i); |
---|
70 | } |
---|
71 | return m / list.size(); |
---|
72 | } |
---|
73 | |
---|
74 | public ArrayList<WeekCongestion> cloneLastWeek() { |
---|
75 | ArrayList<WeekCongestion> cloned = new ArrayList<WeekCongestion>(); |
---|
76 | for (int i = 0; i < lastWeek.size(); i++) { |
---|
77 | cloned.add(lastWeek.get(i).cloneWeekCongestion()); |
---|
78 | } |
---|
79 | return cloned; |
---|
80 | } |
---|
81 | |
---|
82 | // last week congestion |
---|
83 | public String toString() { |
---|
84 | String str = "segments_nb " + lastWeek.size() + "\n"; |
---|
85 | for (int i = 0; i < lastWeek.size(); i++) { |
---|
86 | str = str + "segment " + i + "\n"; |
---|
87 | str = str + lastWeek.get(i).toString() + "\n"; |
---|
88 | } |
---|
89 | return str; |
---|
90 | } |
---|
91 | } |
---|