]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.route/src/org/simantics/district/route/RouteJob.java
Fix dynamic visualisations UI initialization
[simantics/district.git] / org.simantics.district.route / src / org / simantics / district / route / RouteJob.java
1 package org.simantics.district.route;
2
3 import java.util.List;
4 import java.util.Objects;
5 import java.util.concurrent.CompletableFuture;
6
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;
18
19 /**
20  * @author Tuukka Lehtonen
21  * @since 6.09
22  */
23 public class RouteJob extends Job {
24
25     private static final Logger LOGGER = LoggerFactory.getLogger(RouteJob.class);
26
27     private RouterConfiguration config;
28     private List<Resource> waypoints;
29     private CompletableFuture<List<Resource>> callback;
30
31     public RouteJob(RouterConfiguration config, Route route, CompletableFuture<List<Resource>> callback) {
32         super("Compute route");
33         Objects.requireNonNull(callback, "Callback must be non-null");
34         setUser(true);
35         setRule(new ObjectIdentitySchedulingRule(route));
36         this.config = config;
37         this.waypoints = RoutePersistence.toResources(route.waypoints());
38         this.callback = callback;
39     }
40
41     @Override
42     protected IStatus run(IProgressMonitor monitor) {
43         List<Router> routers = Activator.getInstance().getRouteService().routers();
44         for (Router router : routers) {
45             try {
46                 List<Resource> path = router.findShortestPath(config, waypoints);
47                 if (!path.isEmpty()) {
48                     callback.complete(path);
49                     return Status.OK_STATUS;
50                 }
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());
57             }
58         }
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));
61     }
62
63 }