Search in sources :

Example 1 with TrivialPathException

use of org.opentripplanner.routing.error.TrivialPathException in project OpenTripPlanner by opentripplanner.

the class GraphPathToTripPlanConverter method sliceStates.

/**
 * Slice a {@link State} array at the leg boundaries. Leg switches occur when:
 * 1. A LEG_SWITCH mode (which itself isn't part of any leg) is seen
 * 2. The mode changes otherwise, for instance from BICYCLE to WALK
 * 3. A PatternInterlineDwell edge (i.e. interlining) is seen
 *
 * @param states The one-dimensional array of input states
 * @return An array of arrays of states belonging to a single leg (i.e. a two-dimensional array)
 */
private static State[][] sliceStates(State[] states) {
    boolean trivial = true;
    for (State state : states) {
        TraverseMode traverseMode = state.getBackMode();
        if (traverseMode != null && traverseMode != TraverseMode.LEG_SWITCH) {
            trivial = false;
            break;
        }
    }
    if (trivial) {
        throw new TrivialPathException();
    }
    int[] legIndexPairs = { 0, states.length - 1 };
    List<int[]> legsIndexes = new ArrayList<int[]>();
    for (int i = 1; i < states.length - 1; i++) {
        TraverseMode backMode = states[i].getBackMode();
        TraverseMode forwardMode = states[i + 1].getBackMode();
        if (backMode == null || forwardMode == null)
            continue;
        Edge edge = states[i + 1].getBackEdge();
        if (backMode == TraverseMode.LEG_SWITCH || forwardMode == TraverseMode.LEG_SWITCH) {
            if (backMode != TraverseMode.LEG_SWITCH) {
                // Start of leg switch
                legIndexPairs[1] = i;
            } else if (forwardMode != TraverseMode.LEG_SWITCH) {
                // End of leg switch
                if (legIndexPairs[1] != states.length - 1) {
                    legsIndexes.add(legIndexPairs);
                }
                legIndexPairs = new int[] { i, states.length - 1 };
            }
        } else if (backMode != forwardMode) {
            // Mode change => leg switch
            legIndexPairs[1] = i;
            legsIndexes.add(legIndexPairs);
            legIndexPairs = new int[] { i, states.length - 1 };
        } else if (edge instanceof PatternInterlineDwell) {
            // Interlining => leg switch
            legIndexPairs[1] = i;
            legsIndexes.add(legIndexPairs);
            legIndexPairs = new int[] { i + 1, states.length - 1 };
        }
    }
    // Final leg
    legsIndexes.add(legIndexPairs);
    State[][] legsStates = new State[legsIndexes.size()][];
    // Fill the two-dimensional array with states
    for (int i = 0; i < legsStates.length; i++) {
        legIndexPairs = legsIndexes.get(i);
        legsStates[i] = new State[legIndexPairs[1] - legIndexPairs[0] + 1];
        for (int j = 0; j <= legIndexPairs[1] - legIndexPairs[0]; j++) {
            legsStates[i][j] = states[legIndexPairs[0] + j];
        }
    }
    return legsStates;
}
Also used : Edge(org.opentripplanner.routing.graph.Edge) TrivialPathException(org.opentripplanner.routing.error.TrivialPathException)

Aggregations

TrivialPathException (org.opentripplanner.routing.error.TrivialPathException)1 Edge (org.opentripplanner.routing.graph.Edge)1