]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.route/src/org/simantics/district/route/internal/RoutePersistence.java
Improved Routes view functionality
[simantics/district.git] / org.simantics.district.route / src / org / simantics / district / route / internal / RoutePersistence.java
1 package org.simantics.district.route.internal;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.HashSet;
6 import java.util.List;
7 import java.util.Set;
8 import java.util.UUID;
9 import java.util.stream.Collectors;
10
11 import org.eclipse.osgi.util.NLS;
12 import org.simantics.Simantics;
13 import org.simantics.databoard.Bindings;
14 import org.simantics.databoard.util.ObjectUtils;
15 import org.simantics.db.ReadGraph;
16 import org.simantics.db.Resource;
17 import org.simantics.db.WriteGraph;
18 import org.simantics.db.common.request.ObjectsWithType;
19 import org.simantics.db.common.request.ResourceRead;
20 import org.simantics.db.common.request.UniqueRead;
21 import org.simantics.db.common.utils.ListUtils;
22 import org.simantics.db.common.utils.NameUtils;
23 import org.simantics.db.exception.DatabaseException;
24 import org.simantics.db.layer0.QueryIndexUtils;
25 import org.simantics.db.layer0.request.PossibleActiveModel;
26 import org.simantics.db.layer0.util.RemoverUtil;
27 import org.simantics.district.network.ontology.DistrictNetworkResource;
28 import org.simantics.district.route.Waypoint;
29 import org.simantics.district.route.ontology.RouteResource;
30 import org.simantics.layer0.Layer0;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  * @author Tuukka Lehtonen
36  */
37 public class RoutePersistence {
38
39     private static final Logger LOGGER = LoggerFactory.getLogger(RoutePersistence.class);
40
41     public static Resource getRouteFolder(ReadGraph graph, Resource model) throws DatabaseException {
42         List<Resource> diagrams = QueryIndexUtils.searchByType(graph, model, RouteResource.getInstance(graph).RouteFolder);
43         if (diagrams.size() > 0)
44             return diagrams.get(0);
45         return null;
46     }
47
48     public static Resource getOrCreateRouteFolder(WriteGraph graph, Resource model) throws DatabaseException {
49         Resource rf = getRouteFolder(graph, model);
50         if (rf == null) {
51             Layer0 L0 = Layer0.getInstance(graph);
52             RouteResource RR = RouteResource.getInstance(graph);
53             rf = graph.newResource();
54             graph.claim(rf, L0.InstanceOf, null, RR.RouteFolder);
55             graph.claimLiteral(rf, L0.HasName, L0.NameOf, L0.String, UUID.randomUUID().toString(), Bindings.STRING);
56             graph.claim(model, L0.ConsistsOf, L0.PartOf, rf);
57         }
58         return rf;
59     }
60
61     public static Resource createRoute(WriteGraph graph, Resource model, String label, List<Resource> waypoints) throws DatabaseException {
62         Resource rf = getOrCreateRouteFolder(graph, model);
63
64         RouteResource RR = RouteResource.getInstance(graph);
65         Layer0 L0 = Layer0.getInstance(graph);
66
67         Resource route = ListUtils.create(graph, RR.Route, waypoints);
68         graph.claim(rf, L0.ConsistsOf, L0.PartOf, route);
69         graph.claimLiteral(route, L0.HasName, UUID.randomUUID().toString(), Bindings.STRING);
70         graph.claimLiteral(route, L0.HasLabel, label, Bindings.STRING);
71
72         LOGGER.info("Persisted route {} with label {} and waypoints {}", route, label, waypoints);
73
74         return route;
75     }
76
77     public static void updateRoute(WriteGraph graph, Resource route, String label, List<Resource> waypoints) throws DatabaseException {
78         RouteResource RR = RouteResource.getInstance(graph);
79         Layer0 L0 = Layer0.getInstance(graph);
80
81         String existingLabel = graph.getPossibleRelatedValue(route, L0.HasLabel, Bindings.STRING);
82         if (!ObjectUtils.objectEquals(existingLabel, label)) {
83             graph.claimLiteral(route, L0.HasLabel, label, Bindings.STRING);
84         }
85
86         List<Resource> existingWaypoints = ListUtils.toList(graph, route);
87         if (!existingWaypoints.equals(waypoints)) {
88             // TODO: optimize this
89             ListUtils.removeElements(graph, route, new HashSet<>(existingWaypoints));
90             ListUtils.createExisting(graph, RR.Route, waypoints);
91         }
92
93         LOGGER.info("Updated route {} with label {} and waypoints {}", route, label, waypoints);
94     }
95
96     public static void removeRoute(WriteGraph graph, Resource route) throws DatabaseException {
97         RemoverUtil.remove(graph, route);
98     }
99
100     public static Waypoint toWaypoint(ReadGraph graph, Resource waypoint) throws DatabaseException {
101         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
102         Set<Resource> types = graph.getTypes(waypoint);
103         String name = NameUtils.getSafeName(graph, waypoint);
104         if (types.contains(DN.Vertex)) {
105             String address = graph.getPossibleRelatedValue(waypoint, DN.Vertex_HasAddress, Bindings.STRING);
106             String label = address != null
107                     ? NLS.bind("Node {0} - {1}", name, address).trim()
108                     : NLS.bind("Node {0}", name, address).trim();
109             return new WaypointImpl(waypoint, label);
110         } else if (types.contains(DN.Edge)) {
111             return new WaypointImpl(waypoint, NLS.bind("Edge {0}", name));
112         }
113         LOGGER.warn("Tried to convert unrecognized resource {} to a route waypoint", waypoint);
114         return null;
115     }
116
117     public static List<Waypoint> toWaypoints(ReadGraph graph, List<Resource> waypoints) throws DatabaseException {
118         List<Waypoint> result = new ArrayList<>();
119         for (Resource wpr : waypoints) {
120             Waypoint wp = toWaypoint(graph, wpr);
121             if (wp != null)
122                 result.add(wp);
123         }
124         return result;
125     }
126
127     public static List<RouteImpl> findRoutes(ReadGraph graph, Resource model) throws DatabaseException {
128         Resource rf = model != null ? getRouteFolder(graph, model) : null;
129         if (rf == null)
130             return Collections.emptyList();
131
132         Layer0 L0 = Layer0.getInstance(graph);
133         RouteResource RR = RouteResource.getInstance(graph);
134
135         List<RouteImpl> routes = new ArrayList<>();
136
137         for (Resource route : graph.syncRequest(new ObjectsWithType(rf, L0.ConsistsOf, RR.Route))) {
138             RouteImpl ri = new RouteImpl(graph.getRelatedValue(route, L0.HasLabel, Bindings.STRING))
139                     .backend(route)
140                     .modelEntity(model)
141                     .waypoints(toWaypoints(graph, ListUtils.toList(graph, route)));
142             routes.add(ri);
143         }
144
145         return routes;
146     }
147
148     public static class ModelRoutesRequest extends ResourceRead<List<RouteImpl>> {
149         public ModelRoutesRequest(Resource model) {
150             super(model);
151         }
152         @Override
153         public List<RouteImpl> perform(ReadGraph graph) throws DatabaseException {
154             return findRoutes(graph, resource);
155         }
156     }
157
158     public static class ActiveModelRoutesRequest extends UniqueRead<List<RouteImpl>> {
159         @Override
160         public List<RouteImpl> perform(ReadGraph graph) throws DatabaseException {
161             return findRoutes(graph, graph.syncRequest(new PossibleActiveModel(Simantics.getProjectResource())));
162         }
163     }
164
165     public static List<Resource> toResources(List<Waypoint> waypoints) {
166         return waypoints.stream()
167                 .<Resource>map(Waypoint::getObject)
168                 .collect(Collectors.toList());
169     }
170
171 }