Skip to content

Commit 994e139

Browse files
committed
restructuring parts of the code
1 parent 2bdfd04 commit 994e139

File tree

8 files changed

+366
-310
lines changed

8 files changed

+366
-310
lines changed

src/main/java/edu/ie3/netpad/grid/controller/GridController.java

Lines changed: 4 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,12 @@
1414
import edu.ie3.datamodel.models.input.connector.Transformer2WInput;
1515
import edu.ie3.datamodel.models.input.container.GridContainer;
1616
import edu.ie3.datamodel.models.input.container.JointGridContainer;
17-
import edu.ie3.datamodel.models.input.container.RawGridElements;
1817
import edu.ie3.datamodel.models.input.container.SubGridContainer;
1918
import edu.ie3.datamodel.models.input.system.LoadInput;
2019
import edu.ie3.datamodel.models.input.system.PvInput;
2120
import edu.ie3.datamodel.models.input.system.StorageInput;
2221
import edu.ie3.datamodel.models.input.system.SystemParticipantInput;
2322
import edu.ie3.datamodel.utils.ContainerUtils;
24-
import edu.ie3.datamodel.utils.GridAndGeoUtils;
2523
import edu.ie3.netpad.grid.GridModel;
2624
import edu.ie3.netpad.grid.GridModification;
2725
import edu.ie3.netpad.grid.ModifiedSubGridData;
@@ -31,26 +29,21 @@
3129
import edu.ie3.netpad.io.event.ReadGridEvent;
3230
import edu.ie3.netpad.io.event.SaveGridEvent;
3331
import edu.ie3.netpad.map.event.MapEvent;
34-
import edu.ie3.netpad.tool.LineLengthResolutionMode;
3532
import edu.ie3.netpad.tool.controller.ToolController;
36-
import edu.ie3.netpad.tool.controller.ToolDialogs;
3733
import edu.ie3.netpad.tool.event.FixLineLengthRequestEvent;
3834
import edu.ie3.netpad.tool.event.LayoutGridRequestEvent;
3935
import edu.ie3.netpad.tool.event.LayoutGridResponse;
4036
import edu.ie3.netpad.tool.event.ToolEvent;
41-
import edu.ie3.util.geo.GeoUtils;
37+
import edu.ie3.netpad.tool.grid.LineLengthFixer;
38+
import edu.ie3.netpad.tool.grid.LineLengthResolutionMode;
4239
import java.util.*;
4340
import java.util.stream.Collectors;
4441
import java.util.stream.Stream;
4542
import javafx.beans.property.ObjectProperty;
4643
import javafx.beans.property.SimpleObjectProperty;
4744
import javafx.beans.value.ChangeListener;
48-
import javax.measure.quantity.Length;
49-
import org.locationtech.jts.geom.Coordinate;
50-
import org.locationtech.jts.geom.LineString;
5145
import org.slf4j.Logger;
5246
import org.slf4j.LoggerFactory;
53-
import tech.units.indriya.ComparableQuantity;
5447

5548
/**
5649
* //ToDo: Class Description
@@ -221,7 +214,8 @@ private ChangeListener<ToolEvent> toolEventListener() {
221214
FixLineLengthRequestEvent event = (FixLineLengthRequestEvent) newValue;
222215
LineLengthResolutionMode resolutionMode = event.getResolutionMode();
223216
Set<Integer> selectedSubnets = event.getSelectedSubnets();
224-
fixLineLength(resolutionMode, selectedSubnets);
217+
LineLengthFixer.execute(resolutionMode, selectedSubnets, subGrids)
218+
.ifPresent(this::handleReadGridEvent);
225219
} else {
226220
throw new RuntimeException("Invalid GridContainer provided!");
227221
}
@@ -231,131 +225,6 @@ private ChangeListener<ToolEvent> toolEventListener() {
231225
};
232226
}
233227

234-
/**
235-
* Fix the line length discrepancy based on the user given {@link ToolDialogs.FixLineLengthData}
236-
*
237-
* @param resolutionMode Selected resolution mode
238-
* @param selectedSubnets Subnets to apply adjustments to
239-
*/
240-
public void fixLineLength(LineLengthResolutionMode resolutionMode, Set<Integer> selectedSubnets) {
241-
JointGridContainer updatedGrid;
242-
243-
/* Act depending on the chosen resolution mode */
244-
switch (resolutionMode) {
245-
case GEOGRAPHICAL:
246-
updatedGrid = setElectricalToGeographicalLineLength(selectedSubnets);
247-
break;
248-
case ELECTRICAL:
249-
/* TODO CK: Figure out, what to do here */
250-
default:
251-
log.error("Unknown resolution mode '{}'", resolutionMode);
252-
return;
253-
}
254-
255-
/* Build a new event and inform the listeners about the "new" / adapted grid model */
256-
handleReadGridEvent(new ReadGridEvent(updatedGrid));
257-
}
258-
259-
/**
260-
* Sets the electrical length of all lines within the selected sub nets to the length of their
261-
* geographical line string if apparent. If not, it is set to the geographical distance between
262-
* start and end node.
263-
*
264-
* @param selectedSubnets Subnets to apply adjustments to
265-
* @return A {@link JointGridContainer} with updated line models
266-
*/
267-
private JointGridContainer setElectricalToGeographicalLineLength(Set<Integer> selectedSubnets) {
268-
/* Adjust the electrical line length to be the same as the geographical distance */
269-
List<SubGridContainer> subGridContainers =
270-
subGrids.values().parallelStream()
271-
.map(GridModel::getSubGridContainer)
272-
.map(
273-
subGridContainer -> {
274-
if (!selectedSubnets.contains(subGridContainer.getSubnet())) {
275-
/* If this grid isn't selected, hand it back, as it is */
276-
return subGridContainer;
277-
} else {
278-
/* Update all lines */
279-
Set<LineInput> lines =
280-
subGridContainer.getRawGrid().getLines().parallelStream()
281-
.map(GridController::setLineLengthToGeographicDistance)
282-
.collect(Collectors.toSet());
283-
284-
/* Put together, what has been there before */
285-
RawGridElements rawGrid =
286-
new RawGridElements(
287-
subGridContainer.getRawGrid().getNodes(),
288-
lines,
289-
subGridContainer.getRawGrid().getTransformer2Ws(),
290-
subGridContainer.getRawGrid().getTransformer3Ws(),
291-
subGridContainer.getRawGrid().getSwitches(),
292-
subGridContainer.getRawGrid().getMeasurementUnits());
293-
return new SubGridContainer(
294-
subGridContainer.getGridName(),
295-
subGridContainer.getSubnet(),
296-
rawGrid,
297-
subGridContainer.getSystemParticipants(),
298-
subGridContainer.getGraphics());
299-
}
300-
})
301-
.collect(Collectors.toList());
302-
303-
/* Assemble all sub grids to one container */
304-
return ContainerUtils.combineToJointGrid(subGridContainers);
305-
}
306-
307-
/**
308-
* Adjusts the line length to the length of their geographical line string if apparent. If not, it
309-
* is set to the geographical distance between start and end node.
310-
*
311-
* @param line line model to adjust
312-
* @return The adjusted line model
313-
* @deprecated This method should be transferred to PowerSystemDataModel
314-
*/
315-
@Deprecated
316-
private static LineInput setLineLengthToGeographicDistance(LineInput line) {
317-
ComparableQuantity<Length> lineLength;
318-
lineLength =
319-
lengthOfLineString(line.getGeoPosition())
320-
.orElseGet(
321-
() -> {
322-
log.warn(
323-
"Cannot determine the length of the line string of line '{}' as it only contains one coordinate."
324-
+ " Take distance between it's nodes instead.",
325-
line);
326-
return GridAndGeoUtils.distanceBetweenNodes(line.getNodeA(), line.getNodeB());
327-
});
328-
return line.copy().length(lineLength).build();
329-
}
330-
331-
/**
332-
* Calculate the length of a line string
333-
*
334-
* @param lineString The line string to calculate the length of
335-
* @return An option to the length, if it can be determined
336-
* @deprecated This method should be transferred to PowerSystemUtils
337-
*/
338-
@Deprecated
339-
private static Optional<ComparableQuantity<Length>> lengthOfLineString(LineString lineString) {
340-
Coordinate[] coordinates = lineString.getCoordinates();
341-
342-
if (coordinates.length == 1) {
343-
return Optional.empty();
344-
}
345-
346-
/* Go over the line piecewise and sum up the distance */
347-
Coordinate a = coordinates[0];
348-
Coordinate b = coordinates[1];
349-
ComparableQuantity<Length> length = GeoUtils.calcHaversine(a.x, a.y, b.x, b.y);
350-
for (int coordIndex = 2; coordIndex < coordinates.length; coordIndex++) {
351-
a = b;
352-
b = coordinates[coordIndex];
353-
length = length.add(GeoUtils.calcHaversine(a.x, a.y, b.x, b.y));
354-
}
355-
356-
return Optional.of(length);
357-
}
358-
359228
private ChangeListener<IOEvent> ioEventListener() {
360229
return (observable, oldValue, newValue) -> {
361230
if (newValue instanceof ReadGridEvent) {

src/main/java/edu/ie3/netpad/tool/controller/ToolDialogs.java

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,18 @@
55
*/
66
package edu.ie3.netpad.tool.controller;
77

8-
import static edu.ie3.netpad.tool.LineLengthResolutionMode.ELECTRICAL;
9-
import static edu.ie3.netpad.tool.LineLengthResolutionMode.GEOGRAPHICAL;
8+
import static edu.ie3.netpad.tool.grid.LineLengthResolutionMode.ELECTRICAL;
9+
import static edu.ie3.netpad.tool.grid.LineLengthResolutionMode.GEOGRAPHICAL;
1010
import static javafx.scene.control.CheckBoxTreeItem.checkBoxSelectionChangedEvent;
1111

1212
import edu.ie3.datamodel.models.input.container.SubGridContainer;
1313
import edu.ie3.datamodel.models.voltagelevels.VoltageLevel;
1414
import edu.ie3.netpad.exception.NetPadPlusPlusException;
1515
import edu.ie3.netpad.grid.controller.GridController;
16-
import edu.ie3.netpad.tool.LineLengthResolutionMode;
16+
import edu.ie3.netpad.tool.grid.LineLengthResolutionMode;
1717
import java.util.*;
1818
import java.util.stream.Collectors;
1919
import javafx.beans.binding.Bindings;
20-
import javafx.event.EventHandler;
2120
import javafx.geometry.Insets;
2221
import javafx.scene.control.*;
2322
import javafx.scene.layout.GridPane;
@@ -153,20 +152,19 @@ private static Collection<CheckBoxTreeItem<String>> buildTreeItems(
153152
new CheckBoxTreeItem<>(Integer.toString(subGrid.getSubnet()));
154153
checkBoxTreeItem.addEventHandler(
155154
checkBoxSelectionChangedEvent(),
156-
(EventHandler<CheckBoxTreeItem.TreeModificationEvent<String>>)
157-
event -> {
158-
CheckBoxTreeItem<String> chk = event.getTreeItem();
159-
try {
160-
int subnetNumber = Integer.parseInt(chk.getValue());
161-
if (chk.isSelected()) {
162-
selectedSubnets.add(subnetNumber);
163-
} else {
164-
selectedSubnets.remove(subnetNumber);
165-
}
166-
} catch (NumberFormatException nfe) {
167-
logger.error("Unable to parse '{}' to integer.", chk.getValue());
168-
}
169-
});
155+
event -> {
156+
CheckBoxTreeItem<Object> chk = event.getTreeItem();
157+
try {
158+
int subnetNumber = Integer.parseInt((String) chk.getValue());
159+
if (chk.isSelected()) {
160+
selectedSubnets.add(subnetNumber);
161+
} else {
162+
selectedSubnets.remove(subnetNumber);
163+
}
164+
} catch (NumberFormatException nfe) {
165+
logger.error("Unable to parse '{}' to integer.", chk.getValue());
166+
}
167+
});
170168
checkBoxTreeItem.setSelected(true);
171169

172170
voltageLvlChkBox.getChildren().add(checkBoxTreeItem);

src/main/java/edu/ie3/netpad/tool/event/FixLineLengthRequestEvent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
package edu.ie3.netpad.tool.event;
77

8-
import edu.ie3.netpad.tool.LineLengthResolutionMode;
8+
import edu.ie3.netpad.tool.grid.LineLengthResolutionMode;
99
import java.util.Objects;
1010
import java.util.Set;
1111

0 commit comments

Comments
 (0)