1 package org.simantics.district.route.internal;
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.HashSet;
9 import java.util.stream.Collectors;
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;
35 * @author Tuukka Lehtonen
37 public class RoutePersistence {
39 private static final Logger LOGGER = LoggerFactory.getLogger(RoutePersistence.class);
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);
48 public static Resource getOrCreateRouteFolder(WriteGraph graph, Resource model) throws DatabaseException {
49 Resource rf = getRouteFolder(graph, model);
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);
61 public static Resource createRoute(WriteGraph graph, Resource model, String label, List<Resource> waypoints) throws DatabaseException {
62 Resource rf = getOrCreateRouteFolder(graph, model);
64 RouteResource RR = RouteResource.getInstance(graph);
65 Layer0 L0 = Layer0.getInstance(graph);
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);
72 LOGGER.info("Persisted route {} with label {} and waypoints {}", route, label, waypoints);
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);
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);
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);
93 LOGGER.info("Updated route {} with label {} and waypoints {}", route, label, waypoints);
96 public static void removeRoute(WriteGraph graph, Resource route) throws DatabaseException {
97 RemoverUtil.remove(graph, route);
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));
113 LOGGER.warn("Tried to convert unrecognized resource {} to a route waypoint", waypoint);
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);
127 public static List<RouteImpl> findRoutes(ReadGraph graph, Resource model) throws DatabaseException {
128 Resource rf = model != null ? getRouteFolder(graph, model) : null;
130 return Collections.emptyList();
132 Layer0 L0 = Layer0.getInstance(graph);
133 RouteResource RR = RouteResource.getInstance(graph);
135 List<RouteImpl> routes = new ArrayList<>();
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))
141 .waypoints(toWaypoints(graph, ListUtils.toList(graph, route)));
148 public static class ModelRoutesRequest extends ResourceRead<List<RouteImpl>> {
149 public ModelRoutesRequest(Resource model) {
153 public List<RouteImpl> perform(ReadGraph graph) throws DatabaseException {
154 return findRoutes(graph, resource);
158 public static class ActiveModelRoutesRequest extends UniqueRead<List<RouteImpl>> {
160 public List<RouteImpl> perform(ReadGraph graph) throws DatabaseException {
161 return findRoutes(graph, graph.syncRequest(new PossibleActiveModel(Simantics.getProjectResource())));
165 public static List<Resource> toResources(List<Waypoint> waypoints) {
166 return waypoints.stream()
167 .<Resource>map(Waypoint::getObject)
168 .collect(Collectors.toList());