Advertisement
Guest User

Untitled

a guest
May 27th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.46 KB | None | 0 0
  1. public Intersection buildIntersection()throws IOException{
  2. //defining some stuff aye
  3. String ts_name;
  4. String theLine = br.readLine();
  5. String expectedTag = Tag.INTERSECTION;
  6. int indexCounter = 0;
  7. ArrayList<TrafficStream> TS_array = new ArrayList<>();
  8. ArrayList<String> TS_names = new ArrayList<>();
  9. Intersection inter;
  10. ArrayList<PretimedPhasePlan> ppArray = new ArrayList<>();
  11. ArrayList<Phase> phaseArray = new ArrayList<>();
  12. String states;
  13. String trafString;
  14. TrafficDirection trafDir;
  15. TrafficDirection trafOri;
  16. int sigIndex;
  17. int sigFace;
  18. String trafString2;
  19. int trafIndex;
  20. int k;
  21. int j;
  22. Scanner sc;
  23.  
  24. try {
  25. while ((theLine = br.readLine()) != null){
  26. //skip empty lines and comments
  27. if (theLine.startsWith("//")||theLine.isEmpty()){
  28. continue;
  29. }
  30. if (theLine == Tag.INTERSECTION && expectedTag == Tag.INTERSECTION){
  31. theLine = br.readLine();
  32. if(theLine.startsWith("//")||theLine.isEmpty()){
  33. continue;
  34. }
  35. else{
  36. sc = new Scanner(theLine);
  37. sc.useDelimiter("\t");
  38. inter = new Intersection(sc.next(), sc.next());
  39. }
  40. }
  41. expectedTag = Tag.TRAFFIC_STREAMS;
  42. continue;
  43.  
  44.  
  45. //Traffic Stream
  46. if (theLine == Tag.TRAFFIC_STREAMS && expectedTag == Tag.TRAFFIC_STREAMS){
  47. theLine = br.readLine();
  48. if(theLine.startsWith("//")||theLine.isEmpty()){
  49. continue;
  50. }else{
  51. while(!(theLine == Tag.END_TRAFFIC_STREAMS)){
  52. sc = new Scanner(theLine);
  53. sc.useDelimiter("\t");
  54. ts_name = sc.next();
  55. TS_names.add(ts_name);
  56. TS_array.add(new TrafficStream(ts_name,sc.next()));
  57. theLine = br.readLine();
  58. }
  59. }
  60. }
  61. expectedTag = Tag.PHASEPLAN;
  62. continue;
  63.  
  64.  
  65. //PhasePlan
  66. if (theLine == Tag.PHASEPLAN && expectedTag == Tag.PHASEPLAN){
  67. expectedTag = Tag.PHASES;
  68. theLine = br.readLine();
  69. ppArray.add(new PretimedPhasePlan());
  70. if(theLine.startsWith("//")||theLine.isEmpty()){
  71. continue;
  72. }
  73. }
  74. //Phases
  75. if (theLine == Tag.PHASES && expectedTag == Tag.PHASES){
  76. theLine = br.readLine();
  77. if(theLine.startsWith("//")||theLine.isEmpty()){
  78. continue;
  79. }
  80. else{
  81. while(!(theLine == Tag.END_PHASES)){
  82. sc = new Scanner(theLine);
  83. sc.useDelimiter("\t");
  84. phaseArray.add(new Phase(sc.next(),sc.next()));
  85. states = sc.next();
  86. for (int i = 0; i<states.length(); i++){
  87. switch (states.charAt(i)){
  88. case 'R': (phaseArray.get(indexCounter)).addStream(TS_array.get(i), State.RED); break;
  89. case 'Y': (phaseArray.get(indexCounter)).addStream(TS_array.get(i), State.YELLOW); break;
  90. case 'G': (phaseArray.get(indexCounter)).addStream(TS_array.get(i), State.GREEN); break;
  91. case 'X': (phaseArray.get(indexCounter)).addStream(TS_array.get(i), State.OFF); break;
  92. }
  93. }
  94. phaseArray.get(indexCounter).setMinGreenInterval(sc.nextInt());
  95. indexCounter++;
  96. theLine = br.readLine();
  97. }
  98. expectedTag = Tag.END_PHASEPLAN;
  99. for (k = 0; k<ppArray.size()+1; k++){
  100. for (j = 0; j<phaseArray.size()+1; j++){
  101. ppArray.get(k).add(phaseArray.get(j));
  102. }
  103. }
  104. theLine = br.readLine();
  105. indexCounter = 0;
  106. }
  107. }
  108. //PhasePlan-End
  109. if (theLine == Tag.END_PHASEPLAN && expectedTag == Tag.END_PHASEPLAN){
  110. expectedTag = Tag.SIGNAL_FACES;
  111. theLine = br.readLine();
  112. }
  113.  
  114.  
  115. //Signal Faces
  116. if (theLine == Tag.SIGNAL_FACES && expectedTag == Tag.SIGNAL_FACES){
  117. theLine = br.readLine();
  118. if(theLine.startsWith("//")||theLine.isEmpty()){
  119. continue;
  120. }else{
  121. while(!(theLine == Tag.END_SIGNAL_FACES)){
  122. if(theLine.startsWith("//")||theLine.isEmpty()){
  123. continue;}
  124. sc = new Scanner(theLine);
  125. sc.useDelimiter("\t");
  126. trafString = sc.next();
  127. if (trafString.length() == 1){
  128. switch (trafString.charAt(0)){
  129. case 'N': trafDir = TrafficDirection.NORTH; break;
  130. case 'S': trafDir = TrafficDirection.SOUTH; break;
  131. case 'W': trafDir = TrafficDirection.WEST; break;
  132. case 'E': trafDir = TrafficDirection.EAST; break;
  133. }
  134. }else{
  135. if (trafString.equals("NE")){
  136. trafDir = TrafficDirection.NORTHEAST;}
  137. if (trafString.equals("NW")){
  138. trafDir = TrafficDirection.NORTHWEST;}
  139. if (trafString.equals("SE")){
  140. trafDir = TrafficDirection.SOUTHEAST;}
  141. if (trafString.equals("SW")){
  142. trafDir = TrafficDirection.SOUTHWEST;}
  143. }
  144. switch (sc.next().charAt(0)){
  145. case 'N': trafOri = TrafficDirection.NORTH; break;
  146. case 'S': trafOri = TrafficDirection.SOUTH; break;
  147. case 'W': trafOri = TrafficDirection.WEST; break;
  148. case 'E': trafOri = TrafficDirection.EAST; break;
  149. }
  150. switch (sc.next().charAt(0)){
  151. case 'S': sigFace = SignalFace.STANDARD; break;
  152. case 'L': sigFace = SignalFace.LEFT_ARROW; break;
  153. case 'R': sigFace = SignalFace.RIGHT_ARROW; break;
  154. }
  155. trafString2 = sc.next();
  156. for(k=0;k<TS_names.size()+1;k++){
  157. if(trafString2 == TS_names.get(k)){
  158. trafIndex = k;
  159. }
  160. }
  161. TS_array.get(trafIndex).addObserver(inter.addSignalFace(trafDir,trafOri,sigFace));
  162. theLine = br.readLine();
  163. }
  164. }
  165. }
  166. return inter;
  167. }
  168. }catch(TrafficSyntaxException e){
  169. System.err.println("Traffic Syntax Exception: "+ e.getMessage());
  170. }catch (TrafficIOException e){
  171. System.err.println("IOException: "+ e.getMessage());
  172. }catch (TrafficException e){
  173. System.err.println("Exception at: "+ expectedTag);
  174. }
  175. }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement