All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.opentripplanner.graph_builder.module.GraphCoherencyCheckerModule Maven / Gradle / Ivy

There is a newer version: 2.5.0
Show newest version
package org.opentripplanner.graph_builder.module;

import org.opentripplanner.graph_builder.DataImportIssueStore;
import org.opentripplanner.graph_builder.services.GraphBuilderModule;
import org.opentripplanner.routing.graph.Edge;
import org.opentripplanner.routing.graph.Graph;
import org.opentripplanner.routing.graph.Vertex;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

/**
 * Check the every vertex and edge in the graph to make sure the edge lists and from/to
 * members are coherent, and that there are no edgeless vertices. Primarily intended for debugging.
 */
public class GraphCoherencyCheckerModule implements GraphBuilderModule {


    /** An set of ids which identifies what stages this graph builder provides (i.e. streets, elevation, transit) */
    public List provides() {
        return Collections.emptyList();
    }

    /** A list of ids of stages which must be provided before this stage */
    public List getPrerequisites() {
        return Arrays.asList("streets");
    }
    
    private static final Logger LOG = LoggerFactory.getLogger(GraphCoherencyCheckerModule.class);

    @Override
    public void buildGraph(
            Graph graph,
            HashMap, Object> extra,
            DataImportIssueStore issueStore
    ) {
        boolean coherent = true;
        LOG.info("checking graph coherency...");
        for (Vertex v : graph.getVertices()) {
            if (v.getOutgoing().isEmpty() && v.getIncoming().isEmpty()) {
                LOG.error("vertex {} has no edges", v);
                coherent = false;
            }
            for (Edge e : v.getOutgoing()) {
                if (e.getFromVertex() != v) {
                    LOG.error("outgoing edge of {}: from vertex {} does not match", v, e);
                    coherent = false;
                }
                if (e.getToVertex() == null) {
                    LOG.error("outgoing edge has no to vertex {}", e);
                    coherent = false;
                }
            }
            for (Edge e : v.getIncoming()) {
                if (e.getFromVertex() == null) {
                    LOG.error("incoming edge has no from vertex {}", e);
                    coherent = false;
                }
                if (e.getToVertex() != v) {
                    LOG.error("incoming edge of {}: to vertex {} does not match", v, e);
                    coherent = false;
                }
            }
        }
        LOG.info("edge lists and from/to members are {}coherent.", coherent ? "": "not ");
    }

    @Override
    public void checkInputs() {
        //No inputs other than the graph itself
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy