Advertisement
Guest User

Java_AI_Champion

a guest
Mar 29th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.43 KB | None | 0 0
  1. public Object[] test_Read(String filename) {
  2.  
  3. try {
  4. // helpful variables
  5. int i=0;
  6. int j=0;
  7. int num_of_days=80;
  8. String source_text="";
  9. String destination_text="";
  10. String Road_Text="";
  11. String Prediction_Text="";
  12. String Actual_Traffic_Text="";
  13. String[] prediction_per_day = new String[num_of_days];
  14. String[] actual_traffic_per_day = new String[num_of_days];
  15.  
  16.  
  17.  
  18. // read the whole text
  19.  
  20. String input_string = new String(Files.readAllBytes(Paths.get(filename)));
  21. input_string=input_string.replaceAll("[\r\n]+", " ; ");
  22.  
  23.  
  24. // get source and destination
  25.  
  26. String regexString0 = Pattern.quote("<Source> ") + "(.*?)" + Pattern.quote(" </Source>");
  27. Pattern pattern0 = Pattern.compile(regexString0);
  28. Matcher matcher0 = pattern0.matcher(input_string);
  29. while (matcher0.find()) {
  30. source_text = matcher0.group(1);
  31. }
  32.  
  33. String regexString1 = Pattern.quote("<Destination> ") + "(.*?)" + Pattern.quote(" </Destination>");
  34. Pattern pattern1 = Pattern.compile(regexString1);
  35. Matcher matcher1 = pattern1.matcher(input_string);
  36. while (matcher1.find()) {
  37. destination_text = matcher1.group(1);
  38. }
  39.  
  40. System.out.println("Source is: "+source_text); // SOURCE
  41. System.out.println("Destination is: "+destination_text); // DESTINATION
  42.  
  43.  
  44.  
  45. // get roads and vertexes
  46.  
  47. String regexString2 = Pattern.quote("<Roads>") + "(.*?)" + Pattern.quote("</Roads>");
  48. Pattern pattern2 = Pattern.compile(regexString2);
  49. Matcher matcher2 = pattern2.matcher(input_string);
  50. while (matcher2.find()) {
  51. Road_Text = matcher2.group(1);
  52. }
  53.  
  54. // System.out.println(Road_Text);
  55. //System.out.println(Road_Text);
  56. int count=(Road_Text.length() -Road_Text.replace(";","").length()-1)/4;
  57.  
  58. System.out.println(count);
  59. String [] Road=new String[count];
  60. String [] NodeA=new String[count];
  61. String [] NodeB=new String[count];
  62. int [] cost=new int [count];
  63.  
  64.  
  65. Scanner road_s = new Scanner(Road_Text);
  66. road_s.useDelimiter("\\s*;\\s*");
  67.  
  68. while(road_s.hasNext()) {
  69. /*
  70. if(i%4!=0) {
  71. System.out.println(i+": "+road_s.next()); // ROAD AND VERTEXES NAMES
  72. }
  73. else {
  74. System.out.println(i+": "+road_s.nextInt()); // ROUTE COST AS INTEGER
  75. }*/
  76. Road[i]=road_s.next();
  77. NodeA[i]=road_s.next();
  78. NodeB[i]=road_s.next();
  79. cost[i]=road_s.nextInt();
  80. //System.out.println("Street:"+Road[i]+",NodeA:"+NodeA[i]+",NodeB:"+NodeB[i]+",cost:"+cost[i]);
  81. i++;
  82. }
  83.  
  84. road_s.close();
  85.  
  86.  
  87.  
  88. // get predictions...
  89.  
  90. String regexString3 = Pattern.quote("<Predictions>") + "(.*?)" + Pattern.quote("</Predictions>");
  91. Pattern pattern3 = Pattern.compile(regexString3);
  92. Matcher matcher3 = pattern3.matcher(input_string);
  93. while (matcher3.find()) {
  94. Prediction_Text = matcher3.group(1);
  95. }
  96.  
  97. //System.out.println(Prediction_Text);
  98.  
  99.  
  100. // ...for all days
  101.  
  102. String regexString4 = Pattern.quote("<Day>") + "(.*?)" + Pattern.quote("</Day>");
  103. Pattern pattern4 = Pattern.compile(regexString4);
  104. Matcher matcher4 = pattern4.matcher(Prediction_Text);
  105. System.out.println(j);
  106. while (matcher4.find()) {
  107. prediction_per_day[j] = matcher4.group(1);
  108. //System.out.println(j+" day: "+prediction_per_day[j]);
  109. j++;
  110. }
  111. j=0;
  112. i=0;
  113. int k=0;
  114. String pred;
  115. String pred_cost;
  116. String prediction_cost[][]=new String[num_of_days][count];
  117. while(j<num_of_days) {
  118. Scanner prediction_s = new Scanner(prediction_per_day[j]);
  119. prediction_s.useDelimiter("\\s*;\\s*");
  120. //System.out.println("Start");
  121. while(prediction_s.hasNext()) {
  122. //System.out.println(i+": "+prediction_s.next()); // PREDICTIONS OF DAY J, 0<J<NUM_OF_DAYS
  123.  
  124. if(i%2==0){
  125. pred=prediction_s.next();
  126. //System.out.println(pred);
  127. for(k=0;k<count;k++){
  128. if(Road[k].equals(pred)){
  129. break;
  130. }
  131. }
  132. if(k==count){
  133. System.out.println("Could not find Street in Predictions!");
  134. prediction_s.close();
  135. return null;
  136. }
  137. }
  138. else{
  139. pred_cost=prediction_s.next();
  140.  
  141. //System.out.println(pred_cost);
  142. prediction_cost[j][k]=pred_cost;
  143. }
  144.  
  145. i++;
  146. }
  147. prediction_s.close();
  148. j++;
  149. }
  150. j=0;
  151. i=0;
  152.  
  153.  
  154. String actual_cost[][]=new String[num_of_days][count];
  155. // get actual traffic...
  156.  
  157. String regexString5 = Pattern.quote("<ActualTrafficPerDay>") + "(.*?)" + Pattern.quote("</ActualTrafficPerDay>");
  158. Pattern pattern5 = Pattern.compile(regexString5);
  159. Matcher matcher5 = pattern5.matcher(input_string);
  160. while (matcher5.find()) {
  161. Actual_Traffic_Text = matcher5.group(1);
  162. }
  163.  
  164. //System.out.println(Actual_Traffic_Text);
  165.  
  166.  
  167. // ...for all days
  168.  
  169. String regexString6 = Pattern.quote("<Day>") + "(.*?)" + Pattern.quote("</Day>");
  170. Pattern pattern6 = Pattern.compile(regexString6);
  171. Matcher matcher6 = pattern6.matcher(Actual_Traffic_Text);
  172. while (matcher6.find()) {
  173. actual_traffic_per_day[j] = matcher6.group(1);
  174. //System.out.println(j+" day: "+actual_traffic_per_day[j]);
  175. j++;
  176. }
  177. j=0;
  178. i=0;
  179.  
  180. while(j<num_of_days) { // edw tha valoume j<num_of_days, alla epeidh to arxeio tou xalkiadakh exei 1 anti gia 2 meres sto actual traffic, vazw gia twra num_of_days-1
  181. Scanner actual_traffic_s = new Scanner(actual_traffic_per_day[j]);
  182. actual_traffic_s.useDelimiter("\\s*;\\s*");
  183.  
  184. while(actual_traffic_s.hasNext()) {
  185. //System.out.println(i+": "+actual_traffic_s.next()); // ACTUAL TRAFFIC OF DAY J, 0<J<NUM_OF_DAYS
  186. if(i%2==0){
  187. pred=actual_traffic_s.next();
  188. for(k=0;k<count;k++){
  189. if(Road[k].equals(pred)){
  190. break;
  191. }
  192. }
  193. if(k==count){
  194. System.out.println("Could not find Street in Actual traffic!");
  195. actual_traffic_s.close();
  196. return null;
  197. }
  198. }
  199. else{
  200. pred_cost=actual_traffic_s.next();
  201. actual_cost[j][k]=pred_cost;
  202. }
  203. i++;
  204. }
  205. actual_traffic_s.close();
  206. j++;
  207. }
  208. j=0;
  209. i=0;
  210.  
  211. return new Object[]{Road,NodeA,NodeB,cost,prediction_cost,actual_cost};
  212.  
  213. } catch (IOException e) {
  214. System.out.println("File error!");
  215. e.printStackTrace();
  216. }
  217. return null;
  218.  
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement