|
3 | 3 | // SPDX-License-Identifier: Apache-2.0
|
4 | 4 | package org.lfenergy.compas.scl.auto.alignment.service;
|
5 | 5 |
|
| 6 | +import com.google.gson.JsonArray; |
| 7 | +import com.google.gson.JsonElement; |
6 | 8 | import com.google.gson.JsonObject;
|
7 | 9 | import com.google.gson.JsonParser;
|
| 10 | +import org.lfenergy.compas.scl.auto.alignment.model.GenericBay; |
| 11 | +import org.lfenergy.compas.scl.auto.alignment.model.GenericConductingEquipment; |
8 | 12 | import org.lfenergy.compas.scl.auto.alignment.model.GenericSCL;
|
9 | 13 | import org.lfenergy.compas.scl.auto.alignment.model.GenericSubstation;
|
10 |
| -import org.lfenergy.compas.scl.auto.alignment.model.GenericVoltageLevel; |
11 | 14 |
|
| 15 | +import java.util.Optional; |
12 | 16 | import java.util.concurrent.atomic.AtomicLong;
|
| 17 | +import java.util.stream.StreamSupport; |
13 | 18 |
|
14 | 19 | import static org.lfenergy.compas.scl.auto.alignment.common.CommonUtil.cleanSXYDeclarationAndAttributes;
|
15 | 20 |
|
@@ -49,39 +54,63 @@ private void enrichVoltageLevel(GenericSubstation substation, JsonObject jsonVol
|
49 | 54 | voltageLevel.setXYCoordinates(getCoordinate(jsonVoltageLevel, "x"),
|
50 | 55 | getCoordinate(jsonVoltageLevel, "y"));
|
51 | 56 |
|
52 |
| - AtomicLong bayXCoordinate = new AtomicLong(1); |
53 |
| - voltageLevel.getBays() |
54 |
| - .stream() |
55 |
| - .filter(bay -> !bay.isBusbar()) |
56 |
| - .forEach(bay -> bay.setXYCoordinates(bayXCoordinate.getAndIncrement(), 1)); |
57 |
| - |
58 | 57 | if (jsonVoltageLevel.has("nodes")) {
|
59 |
| - jsonVoltageLevel.getAsJsonArray("nodes") |
60 |
| - .forEach(jsonNode -> { |
61 |
| - var type = jsonNode.getAsJsonObject().get("type").getAsString(); |
62 |
| - if ("BUS".equals(type)) { |
63 |
| - enrichBusbar(voltageLevel, jsonNode.getAsJsonObject()); |
| 58 | + JsonArray jsonNodes = jsonVoltageLevel.getAsJsonArray("nodes"); |
| 59 | + voltageLevel.getBays() |
| 60 | + .forEach(bay -> { |
| 61 | + if (bay.isBusbar()) { |
| 62 | + enrichBusbar(jsonNodes, bay); |
64 | 63 | } else {
|
65 |
| - enrichConductingEquipment(voltageLevel, jsonNode.getAsJsonObject()); |
| 64 | + enrichBay(jsonNodes, bay); |
66 | 65 | }
|
67 | 66 | });
|
68 | 67 | }
|
69 | 68 | });
|
70 | 69 | }
|
71 | 70 |
|
72 |
| - private void enrichBusbar(GenericVoltageLevel voltageLevel, JsonObject jsonBusbar) { |
73 |
| - var fullName = jsonBusbar.get("id").getAsString(); |
74 |
| - var sclBusbar = voltageLevel.getBusbarByFullName(fullName); |
75 |
| - sclBusbar.ifPresent(busbar -> |
| 71 | + private void enrichBusbar(JsonArray jsonNodes, GenericBay busbar) { |
| 72 | + var jsonObject = findNode(jsonNodes, busbar.getFullName()); |
| 73 | + jsonObject.ifPresent(jsonBusbar -> |
76 | 74 | busbar.setXYCoordinates(getCoordinate(jsonBusbar, "x"), getCoordinate(jsonBusbar, "y")));
|
77 | 75 | }
|
78 | 76 |
|
79 |
| - private void enrichConductingEquipment(GenericVoltageLevel voltageLevel, JsonObject jsonCoductingEquipment) { |
80 |
| - var fullName = jsonCoductingEquipment.get("id").getAsString(); |
81 |
| - var sclConductingEquipment = voltageLevel.getConductingEquipmentByFullName(fullName); |
82 |
| - sclConductingEquipment.ifPresent(conductingEquipment -> |
83 |
| - conductingEquipment.setXYCoordinates(getCoordinate(jsonCoductingEquipment, "x"), |
84 |
| - getCoordinate(jsonCoductingEquipment, "y"))); |
| 77 | + private void enrichBay(JsonArray jsonNodes, GenericBay bay) { |
| 78 | + var xCoordinate = getMinimumCoordinate(jsonNodes, bay, "x"); |
| 79 | + var yCoordinate = getMinimumCoordinate(jsonNodes, bay, "y"); |
| 80 | + bay.setXYCoordinates(xCoordinate, yCoordinate); |
| 81 | + |
| 82 | + bay.getConductingEquipments() |
| 83 | + .forEach(conductingEquipment -> enrichConductingEquipment(jsonNodes, bay, conductingEquipment)); |
| 84 | + } |
| 85 | + |
| 86 | + private void enrichConductingEquipment(JsonArray jsonNodes, GenericBay bay, GenericConductingEquipment conductingEquipment) { |
| 87 | + var jsonObject = findNode(jsonNodes, conductingEquipment.getFullName()); |
| 88 | + jsonObject.ifPresent(jsonConductingEquipment -> |
| 89 | + conductingEquipment.setXYCoordinates( |
| 90 | + getCoordinateForConductingEquipment(jsonConductingEquipment, bay, "x"), |
| 91 | + getCoordinateForConductingEquipment(jsonConductingEquipment, bay, "y"))); |
| 92 | + } |
| 93 | + |
| 94 | + private long getMinimumCoordinate(JsonArray jsonNodes, GenericBay bay, String fieldName) { |
| 95 | + return bay.getConductingEquipments() |
| 96 | + .stream() |
| 97 | + .map(conductingEquipment -> findNode(jsonNodes, conductingEquipment.getFullName())) |
| 98 | + .filter(Optional::isPresent) |
| 99 | + .flatMap(Optional::stream) |
| 100 | + .mapToLong(jsonObject -> getCoordinate(jsonObject, fieldName)) |
| 101 | + .min() |
| 102 | + .orElse(1); |
| 103 | + } |
| 104 | + |
| 105 | + private Optional<JsonObject> findNode(JsonArray jsonNodes, String fullname) { |
| 106 | + return StreamSupport.stream(jsonNodes.spliterator(), false) |
| 107 | + .map(JsonElement::getAsJsonObject) |
| 108 | + .filter(jsonObject -> fullname.equals(jsonObject.get("id").getAsString())) |
| 109 | + .findFirst(); |
| 110 | + } |
| 111 | + |
| 112 | + private long getCoordinateForConductingEquipment(JsonObject jsonObject, GenericBay bay, String fieldName) { |
| 113 | + return Math.max(getCoordinate(jsonObject, fieldName) - bay.getCoordinate(fieldName) + 1, 1); |
85 | 114 | }
|
86 | 115 |
|
87 | 116 | private long getCoordinate(JsonObject jsonObject, String fieldName) {
|
|
0 commit comments