1 package org.simantics.district.route;
4 import java.util.Objects;
5 import java.util.concurrent.CompletableFuture;
7 import org.eclipse.core.runtime.IProgressMonitor;
8 import org.eclipse.core.runtime.IStatus;
9 import org.eclipse.core.runtime.Status;
10 import org.eclipse.core.runtime.jobs.Job;
11 import org.eclipse.osgi.util.NLS;
12 import org.simantics.ObjectIdentitySchedulingRule;
13 import org.simantics.db.Resource;
14 import org.simantics.district.route.internal.Activator;
15 import org.simantics.district.route.internal.RoutePersistence;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
20 * @author Tuukka Lehtonen
23 public class RouteJob extends Job {
25 private static final Logger LOGGER = LoggerFactory.getLogger(RouteJob.class);
27 private RouterConfiguration config;
28 private List<Resource> waypoints;
29 private CompletableFuture<List<Resource>> callback;
31 public RouteJob(RouterConfiguration config, Route route, CompletableFuture<List<Resource>> callback) {
32 super("Compute route");
33 Objects.requireNonNull(callback, "Callback must be non-null");
35 setRule(new ObjectIdentitySchedulingRule(route));
37 this.waypoints = RoutePersistence.toResources(route.waypoints());
38 this.callback = callback;
42 protected IStatus run(IProgressMonitor monitor) {
43 List<Router> routers = Activator.getInstance().getRouteService().routers();
44 for (Router router : routers) {
46 List<Resource> path = router.findShortestPath(config, waypoints);
47 if (!path.isEmpty()) {
48 callback.complete(path);
49 return Status.OK_STATUS;
51 } catch (RoutingException e) {
52 LOGGER.error("Routing failed for waypoints {} and router '{}'", waypoints, router, e);
53 callback.completeExceptionally(e);
54 // This results in UI feedback of the error.
55 // Perhaps later we get rid of this and let the UI code handle the notifications.
56 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage());
59 LOGGER.info("No router could calculate route between waypoints {}. Available routers: {}", waypoints, routers);
60 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, NLS.bind("No router could calculate route between waypoints {0}. Available routers: {1}", waypoints, routers));