|
| 1 | +package org.eqasim.server; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.IOException; |
| 5 | +import java.util.LinkedList; |
| 6 | +import java.util.List; |
| 7 | +import java.util.concurrent.Callable; |
| 8 | +import java.util.concurrent.ExecutionException; |
| 9 | +import java.util.concurrent.ExecutorService; |
| 10 | +import java.util.concurrent.Executors; |
| 11 | +import java.util.function.Function; |
| 12 | + |
| 13 | +import org.eqasim.core.misc.ParallelProgress; |
| 14 | +import org.eqasim.server.ServiceBuilder.Services; |
| 15 | +import org.eqasim.server.services.isochrone.road.RoadIsochroneRequest; |
| 16 | +import org.eqasim.server.services.isochrone.road.RoadIsochroneResponse; |
| 17 | +import org.eqasim.server.services.isochrone.transit.TransitIsochroneRequest; |
| 18 | +import org.eqasim.server.services.isochrone.transit.TransitIsochroneResponse; |
| 19 | +import org.eqasim.server.services.router.road.FreespeedSettings; |
| 20 | +import org.eqasim.server.services.router.road.RoadRouterRequest; |
| 21 | +import org.eqasim.server.services.router.road.RoadRouterResponse; |
| 22 | +import org.eqasim.server.services.router.transit.TransitRouterRequest; |
| 23 | +import org.eqasim.server.services.router.transit.TransitRouterResponse; |
| 24 | +import org.eqasim.server.services.router.transit.TransitUtilities; |
| 25 | +import org.matsim.core.config.CommandLine; |
| 26 | +import org.matsim.core.config.CommandLine.ConfigurationException; |
| 27 | + |
| 28 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 29 | +import com.fasterxml.jackson.core.JsonParseException; |
| 30 | +import com.fasterxml.jackson.databind.JsonMappingException; |
| 31 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 32 | + |
| 33 | +public class RunProcessor { |
| 34 | + public static void main(String[] args) |
| 35 | + throws ConfigurationException, JsonParseException, JsonMappingException, IOException, InterruptedException, |
| 36 | + ExecutionException { |
| 37 | + CommandLine cmd = new CommandLine.Builder(args) // |
| 38 | + .requireOptions("config-path", "input-path", "output-path") // |
| 39 | + .allowOptions("threads", "configuration-path", "use-transit", "indent-response") // |
| 40 | + .build(); |
| 41 | + |
| 42 | + Services services = new ServiceBuilder().build(cmd); |
| 43 | + |
| 44 | + int threads = cmd.getOption("threads").map(Integer::parseInt) |
| 45 | + .orElse(Runtime.getRuntime().availableProcessors()); |
| 46 | + |
| 47 | + ObjectMapper objectMapper = new ObjectMapper(); |
| 48 | + if (cmd.getOption("indent-response").map(Boolean::parseBoolean).orElse(false)) { |
| 49 | + |
| 50 | + } |
| 51 | + |
| 52 | + ProcessorInput input = objectMapper.readValue(new File(cmd.getOptionStrict("input-path")), |
| 53 | + ProcessorInput.class); |
| 54 | + ProcessorOutput output = new ProcessorOutput(); |
| 55 | + |
| 56 | + ExecutorService executor = Executors.newFixedThreadPool(threads); |
| 57 | + |
| 58 | + process(input.roadRouter, output.roadRouter, |
| 59 | + request -> services.roadRouterService().processRequest(request, input.freespeed), "road_router", |
| 60 | + executor); |
| 61 | + |
| 62 | + process(input.roadIsochrone, output.roadIsochrone, services.roadIsochroneService()::processRequest, |
| 63 | + "road_isochrone", executor); |
| 64 | + |
| 65 | + process(input.transitRouter, output.transitRouter, |
| 66 | + request -> services.transitRouterService().processRequest(request, input.transitUtilities), |
| 67 | + "transit_router", executor); |
| 68 | + |
| 69 | + process(input.transitIsochrone, output.transitIsochrone, services.transitIsochroneService()::processRequest, |
| 70 | + "transit_isochrone", executor); |
| 71 | + |
| 72 | + objectMapper.writerWithDefaultPrettyPrinter().writeValue(new File(cmd.getOptionStrict("output-path")), output); |
| 73 | + } |
| 74 | + |
| 75 | + private static class ProcessorInput { |
| 76 | + @JsonProperty("freespeed") |
| 77 | + FreespeedSettings freespeed = null; |
| 78 | + |
| 79 | + @JsonProperty("road_router") |
| 80 | + List<RoadRouterRequest> roadRouter = new LinkedList<>(); |
| 81 | + |
| 82 | + @JsonProperty("road_isochrone") |
| 83 | + List<RoadIsochroneRequest> roadIsochrone = new LinkedList<>(); |
| 84 | + |
| 85 | + @JsonProperty("transit_utilities") |
| 86 | + TransitUtilities transitUtilities = null; |
| 87 | + |
| 88 | + @JsonProperty("transit_router") |
| 89 | + List<TransitRouterRequest> transitRouter = new LinkedList<>(); |
| 90 | + |
| 91 | + @JsonProperty("transit_isochrone") |
| 92 | + List<TransitIsochroneRequest> transitIsochrone = new LinkedList<>(); |
| 93 | + } |
| 94 | + |
| 95 | + private static class ProcessorOutput { |
| 96 | + @JsonProperty("road_router") |
| 97 | + List<RoadRouterResponse> roadRouter = new LinkedList<>(); |
| 98 | + |
| 99 | + @JsonProperty("road_isochrone") |
| 100 | + List<RoadIsochroneResponse> roadIsochrone = new LinkedList<>(); |
| 101 | + |
| 102 | + @JsonProperty("transit_router") |
| 103 | + List<TransitRouterResponse> transitRouter = new LinkedList<>(); |
| 104 | + |
| 105 | + @JsonProperty("transit_isochrone") |
| 106 | + List<TransitIsochroneResponse> transitIsochrone = new LinkedList<>(); |
| 107 | + } |
| 108 | + |
| 109 | + private static <Response, Request> void process(List<Request> requests, List<Response> responses, |
| 110 | + Function<Request, Response> service, String serivceName, ExecutorService executor) |
| 111 | + throws InterruptedException, ExecutionException { |
| 112 | + if (requests.size() > 0) { |
| 113 | + ParallelProgress progress = new ParallelProgress("Processing " + serivceName, requests.size()); |
| 114 | + |
| 115 | + List<Callable<Response>> tasks = new LinkedList<>(); |
| 116 | + for (Request request : requests) { |
| 117 | + tasks.add(() -> { |
| 118 | + Response response = service.apply(request); |
| 119 | + progress.update(); |
| 120 | + return response; |
| 121 | + }); |
| 122 | + } |
| 123 | + |
| 124 | + for (var task : executor.invokeAll(tasks)) { |
| 125 | + responses.add(task.get()); |
| 126 | + } |
| 127 | + |
| 128 | + progress.close(); |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments