Search in sources :

Example 1 with OTPScript

use of org.opentripplanner.scripting.impl.OTPScript in project OpenTripPlanner by opentripplanner.

the class OTPMain method run.

/**
 * Making OTPMain a concrete class and placing this logic an instance method instead of embedding it in the static
 * main method makes it possible to build graphs from web services or scripts, not just from the command line.
 */
public void run() {
    // TODO do params.infer() here to ensure coherency?
    /* Create the top-level objects that represent the OTP server. */
    makeGraphService();
    otpServer = new OTPServer(params, graphService);
    /* Start graph builder if requested */
    if (params.build != null) {
        // TODO multiple directories
        GraphBuilder graphBuilder = GraphBuilder.forDirectory(params, params.build);
        if (graphBuilder != null) {
            graphBuilder.run();
            /* If requested, hand off the graph to the server as the default graph using an in-memory GraphSource. */
            if (params.inMemory || params.preFlight) {
                Graph graph = graphBuilder.getGraph();
                graph.index(new DefaultStreetVertexIndexFactory());
                // FIXME set true router IDs
                graphService.registerGraph("", new MemoryGraphSource("", graph));
            }
        } else {
            LOG.error("An error occurred while building the graph. Exiting.");
            System.exit(-1);
        }
    }
    // FIXME eventually router IDs will be present even when just building a graph.
    if ((params.routerIds != null && params.routerIds.size() > 0) || params.autoScan) {
        /* Auto-register pre-existing graph on disk, with optional auto-scan. */
        GraphScanner graphScanner = new GraphScanner(graphService, params.graphDirectory, params.autoScan);
        graphScanner.basePath = params.graphDirectory;
        if (params.routerIds != null && params.routerIds.size() > 0) {
            graphScanner.defaultRouterId = params.routerIds.get(0);
        }
        graphScanner.autoRegister = params.routerIds;
        graphScanner.startup();
    }
    /* Start visualizer if requested */
    if (params.visualize) {
        Router defaultRouter = graphService.getRouter();
        defaultRouter.graphVisualizer = new GraphVisualizer(defaultRouter);
        defaultRouter.graphVisualizer.run();
        // avoid timeouts due to search animation
        defaultRouter.timeouts = new double[] { 60 };
    }
    /* Start script if requested */
    if (params.scriptFile != null) {
        try {
            OTPScript otpScript = new BSFOTPScript(otpServer, params.scriptFile);
            if (otpScript != null) {
                Object retval = otpScript.run();
                if (retval != null) {
                    LOG.warn("Your script returned something, no idea what to do with it: {}", retval);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    /* Start web server if requested */
    if (params.server) {
        GrizzlyServer grizzlyServer = new GrizzlyServer(params, otpServer);
        while (true) {
            // Loop to restart server on uncaught fatal exceptions.
            try {
                grizzlyServer.run();
                return;
            } catch (Throwable throwable) {
                LOG.error("An uncaught {} occurred inside OTP. Restarting server.", throwable.getClass().getSimpleName(), throwable);
            }
        }
    }
}
Also used : GraphVisualizer(org.opentripplanner.visualizer.GraphVisualizer) BSFOTPScript(org.opentripplanner.scripting.impl.BSFOTPScript) BSFOTPScript(org.opentripplanner.scripting.impl.BSFOTPScript) OTPScript(org.opentripplanner.scripting.impl.OTPScript) GraphScanner(org.opentripplanner.routing.impl.GraphScanner) DefaultStreetVertexIndexFactory(org.opentripplanner.routing.impl.DefaultStreetVertexIndexFactory) ParameterException(com.beust.jcommander.ParameterException) FileNotFoundException(java.io.FileNotFoundException) Graph(org.opentripplanner.routing.graph.Graph) MemoryGraphSource(org.opentripplanner.routing.impl.MemoryGraphSource) GraphBuilder(org.opentripplanner.graph_builder.GraphBuilder)

Aggregations

ParameterException (com.beust.jcommander.ParameterException)1 FileNotFoundException (java.io.FileNotFoundException)1 GraphBuilder (org.opentripplanner.graph_builder.GraphBuilder)1 Graph (org.opentripplanner.routing.graph.Graph)1 DefaultStreetVertexIndexFactory (org.opentripplanner.routing.impl.DefaultStreetVertexIndexFactory)1 GraphScanner (org.opentripplanner.routing.impl.GraphScanner)1 MemoryGraphSource (org.opentripplanner.routing.impl.MemoryGraphSource)1 BSFOTPScript (org.opentripplanner.scripting.impl.BSFOTPScript)1 OTPScript (org.opentripplanner.scripting.impl.OTPScript)1 GraphVisualizer (org.opentripplanner.visualizer.GraphVisualizer)1