/************************************************************************************ * Copyright (C) 2008 by Politehnica University of Bucharest and Rutgers University * All rights reserved. * Refer to LICENSE for terms and conditions of use. ***********************************************************************************/ package vnsim.vehicular.routePlan.selfRouted; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Vector; import vnsim.map.object.Cross; import vnsim.map.object.Globals; import vnsim.map.object.Point; import vnsim.map.object.Road; import vnsim.vehicular.simulator.CarInstance; import vnsim.vehicular.simulator.Location; import vnsim.vehicular.simulator.RouteSegment; import vnsim.vehicular.simulator.RoutingUtils; import vnsim.vehicular.simulator.intersections.Intersection; import vnsim.vehicular.simulator.intersections.IntersectionWithTrafficLights; public class QueryReply { String message; CarInstance carReceivingMsg; int sourceRoadIdx, sourcePointIdx; public static PrintWriter fileWriter; myPrinter combinedWriter; class myPrinter { public myPrinter() { } public void println(String str) { // fileWriter.println(str); // System.out.println("Car " + carReceivingMsg.ID + ": " + str); // System.out.flush(); } } public QueryReply(String msg, CarInstance car, int rd, int pt) { this.message = msg; this.carReceivingMsg = car; this.sourcePointIdx = pt; this.sourceRoadIdx = rd; this.combinedWriter = new myPrinter(); } public boolean analizeMessage() { String reply = new String(this.message); int rdIdx = -1, ptIdx = -1, rdCond; int totalSpeed = 0; Vector roadProblems = new Vector(); Vector problemPoints = new Vector(); double speedCount = 0; String tip, tpVal; int i, j, k; this.carReceivingMsg.receivedMsgCount++; i = -1; i = reply.indexOf(" "); if (i > 0) { tip = reply.substring(0, i); if (tip.equals("RI")) { i++; while (i < reply.length()) { if (reply.substring(i, i + 6).equals("speed=")) { i = i + 6; tpVal = new String(); while (i < reply.length() && reply.charAt(i) != 's' && reply.charAt(i) != 'R') { tpVal = tpVal + reply.charAt(i); i++; } try { totalSpeed = totalSpeed + (new Double(tpVal)).intValue(); speedCount++; } catch (Exception e) { combinedWriter .println("Error in reply format! Incorect speed value!"); return false; } } else { if (reply.charAt(i) == 'R') { tpVal = new String(); i++; while (i < reply.length() && reply.charAt(i) != 'P') { tpVal = tpVal + reply.charAt(i); i++; } if (i == reply.length()) { combinedWriter.println("incompleteRoadCondition"); break; } try { rdIdx = (new Integer(tpVal)).intValue(); } catch (Exception e) { combinedWriter .println("Error in reply format! Incorect road value!" + tpVal); return false; } tpVal = new String(); i++; while (i < reply.length() && reply.charAt(i) != 'C') { tpVal = tpVal + reply.charAt(i); i++; } if (i == reply.length()) { combinedWriter.println("incompleteRoadCondition"); break; } try { ptIdx = (new Integer(tpVal)).intValue(); problemPoints.add(new Integer(ptIdx)); } catch (Exception e) { combinedWriter .println("Error in reply format! Incorect point value!"); return false; } tpVal = new String(); i++; while (i < reply.length() && reply.charAt(i) != 's' && reply.charAt(i) != 'R') { tpVal = tpVal + reply.charAt(i); i++; } try { rdCond = (new Integer(tpVal)).intValue(); roadProblems.add(new Integer(rdCond)); } catch (Exception e) { combinedWriter .println("Error in reply format! Incorect road condition value!"); return false; } } else { combinedWriter.println("Not a good format for the reply!"); return false; } } } // combinedWriter.println("CAR " + this.carReceivingMsg.ID+" primesc raspuns cu spCount="+speedCount); if (speedCount != 0) { // combinedWriter.println("average travel time is: " + totalSpeed // / speedCount); int h = getIndexInRouteForSegment(this.sourceRoadIdx, this.sourcePointIdx); // combinedWriter.println("CAR " + this.car.ID + "Aflu ceva rau de " // + this.rd + " la pct " + this.pt + " eu sunt la " // + this.car.getRoadIdx() + " sourcePointIdx=" // + this.car.getPointIdx()); if (h >= 0 && h < carReceivingMsg.route.length) { // combinedWriter.println("e in ruta mea " ); if ((((double) (totalSpeed / speedCount)) > timeAproximationForSegment(h))) { changeRoute(h); } else { this.carReceivingMsg.noChangeNeededCount++; // combinedWriter.println(" traff ok primesc: " // + (totalSpeed / speedCount) + " aprox " // + timeAproximationForSegment(h)); } } } else { this.carReceivingMsg.emptyMsgCount++; // combinedWriter.println("for road : " + sourceRoadIdx + " point" + sourcePointIdx // + " no speed was set: the reply was:" + this.msg); } // for (i = 0; i < roadProblems.size(); i++) { // combinedWriter.println("for road " + sourceRoadIdx + " condition:" // + roadProblems.elementAt(i).intValue()); // } return true; } } combinedWriter.println("a aparut o eroare la parsarea mesajui"); return false; } public double timeAproximationForSegment(int h) { double rez = Double.MAX_VALUE, speed, dist; Road r; Point p1, p2; r = Globals.map.roads.get(carReceivingMsg.route[h].roadIndex); p1 = r.points.get(carReceivingMsg.route[h].pt1); p2 = r.points.get(carReceivingMsg.route[h].pt2); dist = Math.abs((double) (p1.getDistance() - p2.getDistance())); // combinedWriter.println("Dist sourcePointIdx care calculez e:" + dist); // if (r.getRoadinfo()[1] > 50) { speed = 50.0; // } else { // speed = 130.0f; // } dist = (double) ((dist * 1800.0) / speed) * 3000.0; // combinedWriter.println("First aprox= sourcePointIdx care calculez e:" + dist); int i; Cross c; // if (r.getRoadinfo()[1] > 49) { Intersection it = null; for (i = 0; i < r.crosses.size(); i++) { c = r.crosses.get(i); if (c.getPointIndex() == carReceivingMsg.route[h].pt2) { it = Globals.map.allIntersections.get(c.intersectionIndex); break; } } double total = 0.0; if (it != null) { if(it instanceof IntersectionWithTrafficLights){ total=((IntersectionWithTrafficLights)it).cycleLength; int green=0; for(i=0;i= pt) && (p2 <= pt)) { h = i; break; } } else { if ((p1 <= pt) && (p2 >= pt)) { h = i; break; } } } } return h; } public void changeRoute(int h) { ArrayList tpRoute = null; int i, j; // combinedWriter // .println("Car " + this.car.ID // + " imi schimb cursul! recalculez....."); Location src, dest; // combinedWriter.println(" gasesc segmentul in ruta mea la poz : " + h); if (h <= carReceivingMsg.routeIndex) { carReceivingMsg.toLateFailCount++; // combinedWriter // .println("Mesaj primit prea tarziu! Sunt deja pe segmentul // respectiv"); } else { src = new Location(0, 0); dest = new Location(0, 0); int destIdx; if (h + Globals.routePlanConstants.HOW_MORE_TO_REMOVE >= carReceivingMsg.route.length) { // combinedWriter // .println("E chiar printre ultimele segmente catre // destinatie"); src.roadIdx = carReceivingMsg.route[carReceivingMsg.routeIndex].roadIndex; src.ptIdx = carReceivingMsg.route[carReceivingMsg.routeIndex].pt2; dest.roadIdx = carReceivingMsg.route[carReceivingMsg.route.length - 1].roadIndex; dest.ptIdx = carReceivingMsg.route[carReceivingMsg.route.length - 1].pt2; } else { src.roadIdx = carReceivingMsg.route[carReceivingMsg.routeIndex].roadIndex; src.ptIdx = carReceivingMsg.route[carReceivingMsg.routeIndex].pt2; dest.roadIdx = carReceivingMsg.route[h + Globals.routePlanConstants.HOW_MORE_TO_REMOVE].roadIndex; dest.ptIdx = carReceivingMsg.route[h + Globals.routePlanConstants.HOW_MORE_TO_REMOVE].pt2; } // combinedWriter.println("Incep calcul ruta de evitare cu // avoid="+source+" pana la "+destination); Location parentAvoid = null, nodeAvoid = null; parentAvoid = new Location(carReceivingMsg.route[h].roadIndex, carReceivingMsg.route[h].pt1); nodeAvoid = new Location(carReceivingMsg.route[h].roadIndex, carReceivingMsg.route[h].pt2); // combinedWriter.println("Caut ruta intre " + source + " si " + destination // + " cu parentAvoid:" + parentAvoid + " si nodeAvoid:" // + nodeAvoid); ArrayList points = carReceivingMsg.routeComputer.shortestDetour(src, dest, parentAvoid, nodeAvoid); if (points == null) { carReceivingMsg.noRouteFailCount++; // combinedWriter.println("Nu am mai gasit drum :("); return; } this.carReceivingMsg.routeChangesCount++; points.add(new Location(dest.roadIdx, dest.ptIdx)); ArrayList ret = RoutingUtils.mergeRoute(points); if (ret != null) { ret = RoutingUtils.splitRoute(ret); // combinedWriter.println("Found avoid route..inlocuiesc vechia ruta: " // + carReceivingMsg.route); int ii = 0; while (ii < ret.size()) { if (ret.get(ii).pt1 == ret.get(ii).pt2) { ret.remove(ii); } else { ii++; } } tpRoute = new ArrayList(); for (i = 0; i <= carReceivingMsg.routeIndex; i++) { tpRoute.add(carReceivingMsg.route[i]); } for (j = 0; j < ret.size(); j++) { tpRoute.add(ret.get(j)); } destIdx = i + j; for (i = h + Globals.routePlanConstants.HOW_MORE_TO_REMOVE; i < carReceivingMsg.route.length; i++) { tpRoute.add(carReceivingMsg.route[i]); } // combinedWriter.println("pctLipitura devine " // + destIdx); while (true) { if (destIdx >= (tpRoute.size() - 1)) { break; } if (tpRoute.get(destIdx).roadIndex == tpRoute .get(destIdx - 1).roadIndex) { if (tpRoute.get(destIdx).pt1 == tpRoute .get(destIdx - 1).pt2) { if (tpRoute.get(destIdx).pt2 == tpRoute .get(destIdx - 1).pt1) { tpRoute.remove(destIdx - 1); tpRoute.remove(destIdx - 1); destIdx--; } else { break; } } else { if ((tpRoute.get(destIdx).pt1 == tpRoute .get(destIdx - 1).pt1) && ((tpRoute.get(destIdx).pt2 == tpRoute .get(destIdx - 1).pt2))) { tpRoute.remove(destIdx - 1); destIdx--; } else { break; } } } else { break; } } carReceivingMsg.route = new RouteSegment[tpRoute.size()]; for (i = 0; i < tpRoute.size(); i++) { carReceivingMsg.route[i] = tpRoute.get(i); } // carReceivingMsg.setRouteFromNowOn(myRoute.route); combinedWriter.println("ROUTE succesfully changed to:" + tpRoute+" nRchanges are "+this.carReceivingMsg.routeChangesCount); this.carReceivingMsg.startOnRoad(); } else { this.carReceivingMsg.routeComputingFailCount++; combinedWriter.println("Could not find a detour"); } } } }