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 ;
8
- import org .lfenergy .compas .scl .auto .alignment .model .GenericSCL ;
9
- import org .lfenergy .compas .scl .auto .alignment .model .GenericSubstation ;
10
- import org .lfenergy .compas .scl .auto .alignment .model .GenericVoltageLevel ;
10
+ import org .lfenergy .compas .scl .auto .alignment .model .*;
11
11
12
- import java .util .concurrent .atomic .AtomicLong ;
12
+ import java .util .Optional ;
13
+ import java .util .stream .StreamSupport ;
13
14
14
15
import static org .lfenergy .compas .scl .auto .alignment .common .CommonUtil .cleanSXYDeclarationAndAttributes ;
15
16
@@ -32,56 +33,100 @@ public void enrich() {
32
33
33
34
// Next process the VoltageLevels.
34
35
if (jsonSubstation .has ("voltageLevels" )) {
35
- jsonSubstation .getAsJsonArray ("voltageLevels" )
36
- .forEach (jsonVoltageLevel -> enrichVoltageLevel (substation , jsonVoltageLevel .getAsJsonObject ()));
36
+ JsonArray jsonNodes = jsonSubstation .getAsJsonArray ("voltageLevels" );
37
+ substation .getVoltageLevels ()
38
+ .forEach (voltageLevel -> enrichVoltageLevel (jsonNodes , voltageLevel ));
37
39
}
38
40
39
- AtomicLong pwtCoordinate = new AtomicLong (1 );
40
- substation .getPowerTransformers ().forEach (powerTransformer ->
41
- powerTransformer .setXYCoordinates (pwtCoordinate .get (), pwtCoordinate .getAndIncrement ()));
41
+ if (jsonSubstation .has ("multitermNodes" )) {
42
+ JsonArray jsonNodes = jsonSubstation .getAsJsonArray ("multitermNodes" );
43
+ substation .getPowerTransformers ()
44
+ .forEach (powerTransformer -> enrichPowerTransformer (jsonNodes , powerTransformer ));
45
+ }
42
46
});
43
47
}
44
48
45
- private void enrichVoltageLevel (GenericSubstation substation , JsonObject jsonVoltageLevel ) {
46
- var voltageLevelFullName = jsonVoltageLevel .get ("voltageLevelInfos" ).getAsJsonObject ().get ("id" ).getAsString ();
47
- var sclVoltageLevel = substation .getVoltageLevelByFullName (voltageLevelFullName );
48
- sclVoltageLevel .ifPresent (voltageLevel -> {
49
- voltageLevel .setXYCoordinates (getCoordinate (jsonVoltageLevel , "x" ),
50
- getCoordinate (jsonVoltageLevel , "y" ));
49
+ private void enrichPowerTransformer (JsonArray jsonNodes , GenericPowerTransformer powerTransformer ) {
50
+ var jsonObject = findNode (jsonNodes , powerTransformer .getFullName ());
51
+ jsonObject .ifPresent (jsonPowerTransformer ->
52
+ powerTransformer .setXYCoordinates (
53
+ getCoordinate (jsonPowerTransformer , "x" ),
54
+ getCoordinate (jsonPowerTransformer , "y" )));
55
+ }
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
+ private void enrichVoltageLevel (JsonArray jsonNodes , GenericVoltageLevel voltageLevel ) {
58
+ var jsonObject = findVoltageLevelNode (jsonNodes , voltageLevel .getFullName ());
59
+ jsonObject .ifPresent (jsonVoltageLevel -> {
60
+ voltageLevel .setXYCoordinates (
61
+ getCoordinate (jsonVoltageLevel , "x" ),
62
+ getCoordinate (jsonVoltageLevel , "y" ));
57
63
58
64
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 () );
65
+ JsonArray jsonSubNodes = jsonVoltageLevel .getAsJsonArray ("nodes" );
66
+ voltageLevel . getBays ()
67
+ . forEach ( bay -> {
68
+ if (bay . isBusbar ( )) {
69
+ enrichBusbar (jsonSubNodes , bay );
64
70
} else {
65
- enrichConductingEquipment ( voltageLevel , jsonNode . getAsJsonObject () );
71
+ enrichBay ( jsonSubNodes , bay );
66
72
}
67
73
});
68
74
}
69
75
});
70
76
}
71
77
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 ->
76
- busbar .setXYCoordinates (getCoordinate (jsonBusbar , "x" ), getCoordinate (jsonBusbar , "y" )));
78
+ private Optional <JsonObject > findVoltageLevelNode (JsonArray jsonNodes , String fullName ) {
79
+ return StreamSupport .stream (jsonNodes .spliterator (), false )
80
+ .map (JsonElement ::getAsJsonObject )
81
+ .filter (jsonObject -> fullName .equals (jsonObject .get ("voltageLevelInfos" ).getAsJsonObject ().get ("id" ).getAsString ()))
82
+ .findFirst ();
83
+ }
84
+
85
+ private void enrichBusbar (JsonArray jsonNodes , GenericBay busbar ) {
86
+ var jsonObject = findNode (jsonNodes , busbar .getFullName ());
87
+ jsonObject .ifPresent (jsonBusbar ->
88
+ busbar .setXYCoordinates (
89
+ getCoordinate (jsonBusbar , "x" ),
90
+ getCoordinate (jsonBusbar , "y" )));
91
+ }
92
+
93
+ private void enrichBay (JsonArray jsonNodes , GenericBay bay ) {
94
+ var xCoordinate = getMinimumCoordinate (jsonNodes , bay , "x" );
95
+ var yCoordinate = getMinimumCoordinate (jsonNodes , bay , "y" );
96
+ bay .setXYCoordinates (xCoordinate , yCoordinate );
97
+
98
+ bay .getConductingEquipments ()
99
+ .forEach (conductingEquipment -> enrichConductingEquipment (jsonNodes , bay , conductingEquipment ));
100
+ }
101
+
102
+ private void enrichConductingEquipment (JsonArray jsonNodes , GenericBay bay , GenericConductingEquipment conductingEquipment ) {
103
+ var jsonObject = findNode (jsonNodes , conductingEquipment .getFullName ());
104
+ jsonObject .ifPresent (jsonConductingEquipment ->
105
+ conductingEquipment .setXYCoordinates (
106
+ getCoordinateForConductingEquipment (jsonConductingEquipment , bay , "x" ),
107
+ getCoordinateForConductingEquipment (jsonConductingEquipment , bay , "y" )));
108
+ }
109
+
110
+ private long getMinimumCoordinate (JsonArray jsonNodes , GenericBay bay , String fieldName ) {
111
+ return bay .getConductingEquipments ()
112
+ .stream ()
113
+ .map (conductingEquipment -> findNode (jsonNodes , conductingEquipment .getFullName ()))
114
+ .filter (Optional ::isPresent )
115
+ .flatMap (Optional ::stream )
116
+ .mapToLong (jsonObject -> getCoordinate (jsonObject , fieldName ))
117
+ .min ()
118
+ .orElse (1 );
119
+ }
120
+
121
+ private Optional <JsonObject > findNode (JsonArray jsonNodes , String fullName ) {
122
+ return StreamSupport .stream (jsonNodes .spliterator (), false )
123
+ .map (JsonElement ::getAsJsonObject )
124
+ .filter (jsonObject -> fullName .equals (jsonObject .get ("id" ).getAsString ()))
125
+ .findFirst ();
77
126
}
78
127
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" )));
128
+ private long getCoordinateForConductingEquipment (JsonObject jsonObject , GenericBay bay , String fieldName ) {
129
+ return Math .max (getCoordinate (jsonObject , fieldName ) - bay .getCoordinate (fieldName ) + 1 , 1 );
85
130
}
86
131
87
132
private long getCoordinate (JsonObject jsonObject , String fieldName ) {
0 commit comments