+
+ public static Resource createNetworkDiagram(WriteGraph graph, Resource target, Resource compositeType, String defaultName, Resource defaultEdgeMapping, Resource defaultVertexMapping, Resource rightClickVertexMapping, Resource leftClickVertexMapping, Resource crs) throws DatabaseException {
+ Resource composite = NewCompositeActionFactory.createComposite(graph, target, defaultName, compositeType);
+
+ DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
+ Resource diagram = graph.getSingleObject(composite, ModelingResources.getInstance(graph).CompositeToDiagram);
+ graph.claim(diagram, DN.EdgeDefaultMapping, defaultEdgeMapping);
+ graph.claim(diagram, DN.VertexDefaultMapping, defaultVertexMapping);
+ graph.claim(diagram, DN.RightClickDefaultMapping, rightClickVertexMapping);
+ graph.claim(diagram, DN.LeftClickDefaultMapping, leftClickVertexMapping);
+ graph.claim(diagram, DN.HasSpatialRefSystem, crs);
+
+ // Generated name prefix from composite name
+ String compositeName = graph.getRelatedValue2(composite, Layer0.getInstance(graph).HasName, Bindings.STRING);
+ graph.claimLiteral(diagram, Layer0X.getInstance(graph).HasGeneratedNamePrefix, "N" + compositeName.substring(compositeName.length() - 1, compositeName.length()));
+
+ return composite;
+ }
+
+ public static final class MappedComponentRequest extends ResourceRead<Resource> {
+ public MappedComponentRequest(Resource element) {
+ super(element);
+ }
+
+ @Override
+ public Resource perform(ReadGraph graph) throws DatabaseException {
+ return getMappedComponent(graph, resource);
+ }
+ }
+
+ public static class ResourceVertex {
+
+ public final boolean isConsumer;
+ public final Resource vertex;
+ public final double[] coords;
+
+ public ResourceVertex(Resource vertex, double[] coords, boolean isConsumer) {
+ this.vertex = vertex;
+ this.coords = coords;
+ this.isConsumer = isConsumer;
+ }
+ }
+
+ public static void changeMappingType(WriteGraph graph, Resource newMapping, List<Resource> elements) throws DatabaseException {
+ DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
+ for (Resource element : elements) {
+ graph.deny(element, DN.HasMapping);
+ graph.claim(element, DN.HasMapping, newMapping);
+ }
+ }
+
+ public static Stream<Resource> findDNElementsById(ReadGraph graph, Resource context, String idToFind) throws DatabaseException {
+ DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
+ return IndexUtils.findByType(graph,
+ graph.syncRequest(new IndexRoot(context)),
+ DN.Element
+ ).stream().filter(element -> {
+ try {
+ String id = graph.getPossibleRelatedValue(element, DN.HasId, Bindings.STRING);
+ return id != null && id.contains(idToFind);
+ } catch (DatabaseException e) {
+ LOGGER.error("Could not read id for element {]", element, e);
+ return false;
+ }
+ });
+ }
+
+ public static Resource findDNElementById(ReadGraph graph, Resource context, String idToFind) throws DatabaseException {
+ List<Resource> elements = findDNElementsById(graph, context, idToFind).collect(Collectors.toList());
+ if (elements.size() == 1) {
+ return elements.iterator().next();
+ }
+ return null;
+ }
+
+ public static List<Resource> findDNElementByXYCoordinates(ReadGraph graph, Resource context, double lat, double lon, double padding) throws DatabaseException {
+ DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
+ DiagramResource DIA = DiagramResource.getInstance(graph);
+ List<Resource> results = new ArrayList<>();
+ Collection<Resource> vertices = IndexUtils.findByType(graph, graph.syncRequest(new IndexRoot(context)), DN.Vertex);
+ Rectangle2D rect = new Rectangle2D.Double(lat, lon, padding, padding);
+ for (Resource vertex : vertices) {
+ double[] location = graph.getRelatedValue(vertex, DIA.HasLocation, Bindings.DOUBLE_ARRAY);
+ if (rect.contains(location[0], location[1])) {
+ results.add(vertex);
+ }
+ }
+ return results;
+ }
+
+