Skip to content

Commit 6509655

Browse files
author
Rob Tjalma
authored
Merge pull request #44 from com-pas/fix-bay-coordinates
Calculate coordinates for Bay using Conducting Equipment.
2 parents 643cd78 + 11bf859 commit 6509655

File tree

4 files changed

+64
-107
lines changed

4 files changed

+64
-107
lines changed

service/src/main/java/org/lfenergy/compas/scl/auto/alignment/model/AbstractGenericEntity.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ public P getParent() {
4141
return parent;
4242
}
4343

44+
public long getCoordinate(String fieldName) {
45+
var value = element.getAttributeNS(SCLXY_NS_URI, fieldName);
46+
if (!value.isBlank()) {
47+
return Long.parseLong(value);
48+
}
49+
return 0;
50+
}
51+
4452
public void setXYCoordinates(long x, long y) {
4553
element.setAttributeNS(SCLXY_NS_URI, SCLXY_PREFIX + ":x", String.valueOf(x));
4654
element.setAttributeNS(SCLXY_NS_URI, SCLXY_PREFIX + ":y", String.valueOf(y));

service/src/main/java/org/lfenergy/compas/scl/auto/alignment/service/SclAutoAlignmentEnricher.java

Lines changed: 52 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@
33
// SPDX-License-Identifier: Apache-2.0
44
package org.lfenergy.compas.scl.auto.alignment.service;
55

6+
import com.google.gson.JsonArray;
7+
import com.google.gson.JsonElement;
68
import com.google.gson.JsonObject;
79
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;
812
import org.lfenergy.compas.scl.auto.alignment.model.GenericSCL;
913
import org.lfenergy.compas.scl.auto.alignment.model.GenericSubstation;
10-
import org.lfenergy.compas.scl.auto.alignment.model.GenericVoltageLevel;
1114

15+
import java.util.Optional;
1216
import java.util.concurrent.atomic.AtomicLong;
17+
import java.util.stream.StreamSupport;
1318

1419
import static org.lfenergy.compas.scl.auto.alignment.common.CommonUtil.cleanSXYDeclarationAndAttributes;
1520

@@ -49,39 +54,63 @@ private void enrichVoltageLevel(GenericSubstation substation, JsonObject jsonVol
4954
voltageLevel.setXYCoordinates(getCoordinate(jsonVoltageLevel, "x"),
5055
getCoordinate(jsonVoltageLevel, "y"));
5156

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-
5857
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);
6463
} else {
65-
enrichConductingEquipment(voltageLevel, jsonNode.getAsJsonObject());
64+
enrichBay(jsonNodes, bay);
6665
}
6766
});
6867
}
6968
});
7069
}
7170

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 ->
7674
busbar.setXYCoordinates(getCoordinate(jsonBusbar, "x"), getCoordinate(jsonBusbar, "y")));
7775
}
7876

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);
85114
}
86115

87116
private long getCoordinate(JsonObject jsonObject, String fieldName) {

0 commit comments

Comments
 (0)