/opentripplanner-routing/src/main/java/org/opentripplanner/routing/impl/raptor/RaptorState.java

https://github.com/barbeau/OpenTripPlanner · Java · 172 lines · 128 code · 24 blank · 20 comment · 28 complexity · a9a446be6c32a3da21f9ffc892844fde MD5 · raw file

  1. /* This program is free software: you can redistribute it and/or
  2. modify it under the terms of the GNU Lesser General Public License
  3. as published by the Free Software Foundation, either version 3 of
  4. the License, or (at your option) any later version.
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License
  10. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  11. package org.opentripplanner.routing.impl.raptor;
  12. import java.util.ArrayList;
  13. import java.util.Date;
  14. import java.util.List;
  15. import org.onebusaway.gtfs.model.AgencyAndId;
  16. import org.onebusaway.gtfs.model.Route;
  17. import org.onebusaway.gtfs.model.Trip;
  18. import org.opentripplanner.routing.core.RoutingRequest;
  19. import org.opentripplanner.routing.core.ServiceDay;
  20. import org.opentripplanner.routing.core.State;
  21. import org.opentripplanner.routing.trippattern.TripTimes;
  22. /* RaptorStates are always at some transit stop;
  23. * they either got there via a walk, in which case walkPath != null, or
  24. * via a transit hop, in which case boardStop etc have been set.
  25. */
  26. public class RaptorState implements Comparable<RaptorState>, Cloneable {
  27. /* dominance characteristics */
  28. double walkDistance;
  29. int nBoardings;
  30. int arrivalTime;
  31. /* if this state has just boarded transit, this is the boarding location */
  32. RaptorStop boardStop;
  33. int boardStopSequence = -1; //this is the index in this route
  34. RaptorRoute route;
  35. public int patternIndex = -1;
  36. public TripTimes tripTimes = null;
  37. boolean rentingBike;
  38. public AgencyAndId tripId;
  39. /* if has walked to transit, */
  40. State walkPath;
  41. /* path info */
  42. private RaptorState parent;
  43. public ServiceDay serviceDay;
  44. public RaptorStop stop;
  45. private boolean arriveBy;
  46. public double weight;
  47. public int initialWaitTime;
  48. public boolean interlining = false;
  49. private RoutingRequest request;
  50. public RaptorState(RoutingRequest request) {
  51. this.request = request;
  52. arriveBy = request.arriveBy;
  53. }
  54. public RaptorState(RaptorState parent) {
  55. this.parent = parent;
  56. this.arriveBy = parent.arriveBy;
  57. this.weight = parent.weight;
  58. this.initialWaitTime = parent.initialWaitTime;
  59. this.rentingBike = parent.rentingBike;
  60. }
  61. public String toString() {
  62. if (stop == null) {
  63. String routes = "";
  64. RaptorState cur = this;
  65. while (cur != null) {
  66. if (cur.route != null) {
  67. routes += cur.route + ", ";
  68. }
  69. cur = cur.parent;
  70. }
  71. return "(" + routes + ") at " + new Date(((long) arrivalTime) * 1000);
  72. } else {
  73. return "at " + stop + " boarded at " + boardStop + " on " + route + " time "
  74. + new Date(((long) arrivalTime) * 1000) + " walkDistance " + walkDistance;
  75. }
  76. }
  77. public void dump () {
  78. RaptorState state = this;
  79. while (state != null) {
  80. String routeStr = "()";
  81. if (state.route != null) {
  82. Trip gtfsTrip = state.route.boards[0][0].getPattern().getExemplar();
  83. Route gtfsRoute = gtfsTrip.getRoute();
  84. routeStr = gtfsTrip.getTripHeadsign();
  85. if (routeStr == null) {
  86. routeStr = gtfsRoute.getId().getId();
  87. } else {
  88. routeStr = gtfsRoute.getId().getId() + " " + routeStr;
  89. }
  90. }
  91. System.out.println("At stop " + state.stop.stopVertex.getName() + "(" + state.stop
  92. + ") on " + routeStr + " at time "
  93. + new Date(((long) state.arrivalTime) * 1000));
  94. state = state.parent;
  95. }
  96. }
  97. public boolean eDominates(RaptorState other) {
  98. if (rentingBike != other.rentingBike)
  99. return false;
  100. if (arriveBy) {
  101. return nBoardings <= other.nBoardings
  102. && walkDistance <= other.walkDistance * Raptor.WALK_EPSILON
  103. && arrivalTime >= other.arrivalTime;
  104. } else {
  105. return nBoardings <= other.nBoardings
  106. && walkDistance <= other.walkDistance * Raptor.WALK_EPSILON
  107. && arrivalTime <= other.arrivalTime;
  108. }
  109. }
  110. @Override
  111. public int compareTo(RaptorState other) {
  112. return (int) Math.signum(weight - other.weight);
  113. }
  114. public RaptorState getParent() {
  115. return parent;
  116. }
  117. public RaptorRoute getRoute() {
  118. return route;
  119. }
  120. public void setRoute(RaptorRoute route) {
  121. this.route = route;
  122. }
  123. public RaptorState clone() {
  124. try {
  125. return (RaptorState) super.clone();
  126. } catch (CloneNotSupportedException e) {
  127. throw new RuntimeException(e);
  128. }
  129. }
  130. public List<AgencyAndId> getTrips() {
  131. ArrayList<AgencyAndId> out = new ArrayList<AgencyAndId>();
  132. RaptorState cur = this;
  133. while (cur != null) {
  134. if (cur.tripId != null)
  135. out.add(cur.tripId);
  136. cur = cur.parent;
  137. }
  138. return out;
  139. }
  140. public RoutingRequest getRequest() {
  141. if (request == null) {
  142. return parent.getRequest();
  143. }
  144. return request;
  145. }
  146. }