private OsmTrack compileTrip(ScheduledTrip trip, int offset) {
    OsmTrack track = new OsmTrack();
    track.iternity = new ArrayList<String>();
    ScheduledTrip current = trip;
    ScheduledLine lastLine = new ScheduledLine();
    ScheduledLine dummyLine = new ScheduledLine();
    List<ScheduledTrip> list = new ArrayList<ScheduledTrip>();

    int distance = 0;

    ScheduledTrip itrip = null;

    String profile = extractProfile(rc.localFunction);

    OsmNodeP nextNode = null;
    while (current != null) {
      System.out.println("trip=" + current);
      OsmNodeP node = current.getTargetNode();
      OsmPathElement pe = new OsmPathElement(node.ilon, node.ilat, node.selev, null);
      track.addNode(pe);

      if (nextNode != null) {
        distance += node.calcDistance(nextNode);
      }

      boolean isScheduled = current.link instanceof ScheduledLink;
      boolean isConnection = current.link.descriptionBitmap == null && !isScheduled;
      ScheduledLine line =
          isScheduled ? ((ScheduledLink) current.link).line : isConnection ? dummyLine : null;

      if (line != lastLine && !isConnection) {
        itrip = new ScheduledTrip();
        itrip.departure = current.departure;
        itrip.arrival = current.arrival;
        itrip.originNode = current.originNode;
        itrip.link = current.link;

        if (isScheduled && list.size() > 0) {
          list.get(list.size() - 1).originNode = current.getTargetNode();
        }
        list.add(itrip);
      } else if (itrip != null && !isConnection) {
        itrip.departure = current.departure;
        itrip.originNode = current.originNode;
      }
      lastLine = line;
      current = current.origin;
      nextNode = node;
    }
    track.distance = distance;
    track.cost = trip.cost;

    for (int i = list.size() - 1; i >= 0; i--) {
      current = list.get(i);
      String lineName = profile;

      boolean isScheduled = current.link instanceof ScheduledLink;
      if (isScheduled) {
        lineName = ((ScheduledLink) current.link).line.name;
      }
      String stationName = "*position*";
      if (current.originNode instanceof StationNode) {
        stationName = ((StationNode) current.originNode).name;
      }
      String nextStationName = "*position*";
      if (i > 0 && list.get(i - 1).originNode instanceof StationNode) {
        nextStationName = ((StationNode) list.get(i - 1).originNode).name;
      }
      {
        Date d0 = new Date(time0 + 60000L * offset + current.departure);
        Date d1 = new Date(time0 + 60000L * offset + current.arrival);
        if (track.iternity.size() > 0) track.iternity.add("");
        track.iternity.add("depart: " + d0 + " " + stationName);
        track.iternity.add("                                     --- " + lineName + " ---");
        track.iternity.add("arrive: " + d1 + " " + nextStationName);
      }
    }

    return track;
  }
  public OsmTrack findRoute(OsmPos startPos, OsmPos endPos, String startTime, int alternativeIdx)
      throws Exception {
    OsmTrack track = null;

    start = graph.matchNodeForPosition(startPos, rc.expctxWay);
    if (start == null) throw new IllegalArgumentException("unmatched start: " + startPos);
    end = graph.matchNodeForPosition(endPos, rc.expctxWay);
    if (end == null) throw new IllegalArgumentException("unmatched end: " + endPos);

    //		SimpleDateFormat df = new SimpleDateFormat( "dd.MM.yyyy-HH:mm" );
    //		time0 = df.parse(startTime).getTime();
    time0 = System.currentTimeMillis() + (long) (rc.starttimeoffset * 60000L);
    long minutes0 = (time0 + 59999L) / 60000L;
    time0 = minutes0 * 60000L;

    OffsetSet finishedOffsets = OffsetSet.emptySet();

    OsmLinkP startLink = new OsmLinkP(null, start);

    ScheduledTrip startTrip = new ScheduledTrip(OffsetSet.fullSet(), startLink, null, null);
    openSet.add(0, startTrip);
    for (; ; ) {
      if (re.isTerminated()) {
        throw new RuntimeException("operation terminated");
      }
      if (linksProcessed + linksReProcessed > 5000000) {
        throw new RuntimeException("5 Million links limit reached");
      }

      // get cheapest trip from heap
      ScheduledTrip trip = openSet.popLowestKeyValue();
      if (trip == null) {
        break;
      }
      OsmLinkP currentLink = trip.link;
      OsmNodeP currentNode = trip.getTargetNode();
      if (currentNode == null) {
        System.out.println("ups: " + trip);
        continue;
      }

      if (currentLink.isVirgin()) {
        linksProcessed++;
      } else {
        linksReProcessed++;
      }

      // check global closure
      OffsetSet offsets = finishedOffsets.filter(trip.offsets);
      if (offsets == null) continue;

      // check local closure for links:
      offsets =
          currentLink.filterAndClose(offsets, trip.arrival, currentLink instanceof ScheduledLink);
      if (offsets == null) continue;

      // check for arrival
      if (currentNode == end) {
        for (int offset = 0; offset < trip.offsets.size(); offset++) {
          if (trip.offsets.contains(offset)) {
            track = compileTrip(trip, offset);
            System.out.println("---- begin route ------ (cost " + track.cost + ")");
            for (String s : track.iternity) System.out.println(s);
            System.out.println("---- end route ------");
            break; // + plus more offsets..
          }
        }
        finishedOffsets = finishedOffsets.add(offsets);
        if (solutionCount++ >= alternativeIdx) return track;
      }
      for (OsmLinkP link = currentNode.getFirstLink();
          link != null;
          link = link.getNext(currentNode)) {
        addNextTripsForLink(trip, currentNode, currentLink, link, offsets, 0);
      }
    }
    return track;
  }
 private void addToOpenSet(ScheduledTrip nextTrip) {
   int distance = nextTrip.getTargetNode().calcDistance(end);
   nextTrip.adjustedCost = nextTrip.cost + (int) (distance * rc.pass1coefficient + 0.5);
   openSet.add(nextTrip.adjustedCost, nextTrip);
 }