Skip to content

Commit

Permalink
Merge pull request #10 from ie3-institute/ck/#8-lineLengthDiscrepancy…
Browse files Browse the repository at this point in the history
…-toGeographical

Set electrical line length to geographic distance
  • Loading branch information
ckittl authored May 28, 2021
2 parents 66a388c + 994e139 commit 7cb5573
Show file tree
Hide file tree
Showing 15 changed files with 809 additions and 127 deletions.
33 changes: 30 additions & 3 deletions src/main/java/edu/ie3/netpad/grid/controller/GridController.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
import edu.ie3.datamodel.models.input.NodeInput;
import edu.ie3.datamodel.models.input.connector.LineInput;
import edu.ie3.datamodel.models.input.connector.Transformer2WInput;
import edu.ie3.datamodel.models.input.container.*;
import edu.ie3.datamodel.models.input.container.GridContainer;
import edu.ie3.datamodel.models.input.container.JointGridContainer;
import edu.ie3.datamodel.models.input.container.SubGridContainer;
import edu.ie3.datamodel.models.input.system.LoadInput;
import edu.ie3.datamodel.models.input.system.PvInput;
import edu.ie3.datamodel.models.input.system.StorageInput;
Expand All @@ -27,10 +29,13 @@
import edu.ie3.netpad.io.event.ReadGridEvent;
import edu.ie3.netpad.io.event.SaveGridEvent;
import edu.ie3.netpad.map.event.MapEvent;
import edu.ie3.netpad.tool.ToolController;
import edu.ie3.netpad.tool.controller.ToolController;
import edu.ie3.netpad.tool.event.FixLineLengthRequestEvent;
import edu.ie3.netpad.tool.event.LayoutGridRequestEvent;
import edu.ie3.netpad.tool.event.LayoutGridResponse;
import edu.ie3.netpad.tool.event.ToolEvent;
import edu.ie3.netpad.tool.grid.LineLengthFixer;
import edu.ie3.netpad.tool.grid.LineLengthResolutionMode;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand All @@ -50,11 +55,19 @@ public class GridController {

private static final Logger log = LoggerFactory.getLogger(GridController.class);

private static final class InstanceHolder {
static final GridController INSTANCE = new GridController();
}

public static GridController getInstance() {
return GridController.InstanceHolder.INSTANCE;
}

private final Map<UUID, GridModel> subGrids = new LinkedHashMap<>();

private final ObjectProperty<GridEvent> gridUpdateEventProperty = new SimpleObjectProperty<>();

public GridController() {
private GridController() {

// register for updates from iOController
IoController.getInstance().registerGridControllerListener(this.ioEventListener());
Expand All @@ -68,6 +81,14 @@ public GridController() {
(observable, oldValue, gridContextEvent) -> handleGridModifications(gridContextEvent));
}

public boolean isGridLoaded() {
return subGrids.isEmpty();
}

public Map<UUID, GridModel> getSubGrids() {
return subGrids;
}

private void handleReadGridEvent(ReadGridEvent newValue) {

// clear subGrids
Expand Down Expand Up @@ -189,6 +210,12 @@ private ChangeListener<ToolEvent> toolEventListener() {
} else if (newValue instanceof LayoutGridResponse) {
handleReadGridEvent(new ReadGridEvent(((LayoutGridResponse) newValue).getGrid()));
log.debug("Received Tool response event");
} else if (newValue instanceof FixLineLengthRequestEvent) {
FixLineLengthRequestEvent event = (FixLineLengthRequestEvent) newValue;
LineLengthResolutionMode resolutionMode = event.getResolutionMode();
Set<Integer> selectedSubnets = event.getSelectedSubnets();
LineLengthFixer.execute(resolutionMode, selectedSubnets, subGrids)
.ifPresent(this::handleReadGridEvent);
} else {
throw new RuntimeException("Invalid GridContainer provided!");
}
Expand Down
86 changes: 53 additions & 33 deletions src/main/java/edu/ie3/netpad/grid/info/GridInfoController.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import static javafx.scene.control.CheckBoxTreeItem.checkBoxSelectionChangedEvent;

import edu.ie3.datamodel.models.input.container.SubGridContainer;
import edu.ie3.datamodel.models.voltagelevels.VoltageLevel;
import edu.ie3.netpad.grid.event.GridEvent;
import edu.ie3.netpad.grid.event.GridEventListener;
Expand Down Expand Up @@ -40,7 +41,6 @@ public class GridInfoController implements GridEventListener {

private final ChangeListener<GridEvent> gridEventListener =
ListenerUtil.createGridEventListener(this);
private final HashMap<CheckBoxTreeItem<String>, UUID> subGridCheckBoxes = new HashMap<>();

private final ObjectProperty<GridInfoEvent> gridInfoEventProperty = new SimpleObjectProperty<>();

Expand Down Expand Up @@ -69,40 +69,60 @@ private void handleReplaceGridEvent(ReplaceGridEvent gridEvent) {

selectedGridCheckTreeView.setRoot(root);

Map<VoltageLevel, CheckBoxTreeItem<String>> voltageLvls = new HashMap<>();

gridEvent
.getSubGrids()
.forEach(
(uuid, subGrid) -> {
CheckBoxTreeItem<String> voltageLvlChkBox =
Optional.ofNullable(voltageLvls.get(subGrid.getPredominantVoltageLevel()))
.orElseGet(
() ->
new CheckBoxTreeItem<>(subGrid.getPredominantVoltageLevel().getId()));

voltageLvlChkBox.setSelected(true);

CheckBoxTreeItem<String> checkBoxTreeItem =
new CheckBoxTreeItem<>(Integer.toString(subGrid.getSubnet()));

checkBoxTreeItem.setSelected(true);
checkBoxTreeItem.addEventHandler(
checkBoxSelectionChangedEvent(),
(EventHandler<CheckBoxTreeItem.TreeModificationEvent<String>>)
event -> {
CheckBoxTreeItem<String> chk = event.getTreeItem();
UUID subGridUUID = subGridCheckBoxes.get(chk);
notifyListener(new GridInfoEvent(subGridUUID, chk.isSelected()));
});

voltageLvlChkBox.getChildren().add(checkBoxTreeItem);
addToRootTreeItem(root, gridEvent.getSubGrids());
}

voltageLvls.put(subGrid.getPredominantVoltageLevel(), voltageLvlChkBox);
subGridCheckBoxes.put(checkBoxTreeItem, uuid);
});
/**
* Adds {@link CheckBoxTreeItem}s for each voltage level
*
* @param root Root entry of the check box tree
* @param subGridContainerMap Mapping from uuid to sub grid container
*/
public void addToRootTreeItem(
CheckBoxTreeItem<String> root, Map<UUID, SubGridContainer> subGridContainerMap) {
Collection<CheckBoxTreeItem<String>> treeItems = buildTreeItems(subGridContainerMap);
treeItems.forEach(voltageLvlChkBox -> root.getChildren().add(voltageLvlChkBox));
}

voltageLvls.values().forEach(voltageLvlChkBox -> root.getChildren().add(voltageLvlChkBox));
/**
* Builds all the {@link CheckBoxTreeItem}s for each voltage level
*
* @param subGridContainerMap Mapping from uuid to sub grid container
* @return A collection of nested tree items
*/
private Collection<CheckBoxTreeItem<String>> buildTreeItems(
Map<UUID, SubGridContainer> subGridContainerMap) {
Map<VoltageLevel, CheckBoxTreeItem<String>> voltLvlToTreeItem = new HashMap<>();
HashMap<CheckBoxTreeItem<String>, UUID> subGridCheckBoxes = new HashMap<>();

subGridContainerMap.forEach(
(uuid, subGrid) -> {
CheckBoxTreeItem<String> voltageLvlChkBox =
Optional.ofNullable(voltLvlToTreeItem.get(subGrid.getPredominantVoltageLevel()))
.orElseGet(
() -> new CheckBoxTreeItem<>(subGrid.getPredominantVoltageLevel().getId()));

voltageLvlChkBox.setSelected(true);

CheckBoxTreeItem<String> checkBoxTreeItem =
new CheckBoxTreeItem<>(Integer.toString(subGrid.getSubnet()));

checkBoxTreeItem.setSelected(true);
checkBoxTreeItem.addEventHandler(
checkBoxSelectionChangedEvent(),
(EventHandler<CheckBoxTreeItem.TreeModificationEvent<String>>)
event -> {
CheckBoxTreeItem<String> chk = event.getTreeItem();
UUID subGridUUID = subGridCheckBoxes.get(chk);
notifyListener(new GridInfoEvent(subGridUUID, chk.isSelected()));
});

voltageLvlChkBox.getChildren().add(checkBoxTreeItem);

voltLvlToTreeItem.put(subGrid.getPredominantVoltageLevel(), voltageLvlChkBox);
subGridCheckBoxes.put(checkBoxTreeItem, uuid);
});
return voltLvlToTreeItem.values();
}

public ObjectProperty<GridInfoEvent> gridInfoEvents() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ public class MainController implements GridEventListener {

@FXML private MainMenuBarController mainMenuBarController;

private final GridController gridController;

private final ChangeListener<GridEvent> gridEventListener;

private boolean gridInfoActive = false;
Expand All @@ -48,7 +46,6 @@ public class MainController implements GridEventListener {
private final DoubleProperty gridInfoDividerPosition = new SimpleDoubleProperty(0.25);

public MainController() {
this.gridController = new GridController();
this.gridEventListener = ListenerUtil.createGridEventListener(this);
}

Expand Down Expand Up @@ -109,10 +106,12 @@ public void postInitialization() {
gridInfoController.gridEventListener(),
mainMenuBarController.getToolMenuController().gridEventListener(),
this.gridEventListener())
.forEach(gridController.gridUpdateEvents()::addListener);
.forEach(GridController.getInstance().gridUpdateEvents()::addListener);

/* register listener that receive updates from map controller (e.g. dragged nodes) */
mapController.mapUpdateEvents().addListener(gridController.gridMapEventListener());
mapController
.mapUpdateEvents()
.addListener(GridController.getInstance().gridMapEventListener());

/* register listener that receive updates from gridInfoController*/
gridInfoController.gridInfoEvents().addListener(mapController.gridInfoEventListener());
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/edu/ie3/netpad/menu/ToolMenuController.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
import edu.ie3.netpad.grid.event.GridEvent;
import edu.ie3.netpad.grid.event.GridEventListener;
import edu.ie3.netpad.grid.event.ReplaceGridEvent;
import edu.ie3.netpad.tool.ToolController;
import edu.ie3.netpad.tool.controller.ToolController;
import edu.ie3.netpad.tool.controller.ToolDialogs;
import edu.ie3.netpad.util.ListenerUtil;
import javafx.beans.value.ChangeListener;
import javafx.fxml.FXML;
Expand All @@ -30,10 +31,16 @@ public class ToolMenuController implements GridEventListener {
ListenerUtil.createGridEventListener(this);

@FXML public MenuItem layoutGridItem;
@FXML private MenuItem fixLineLengthItem;

@FXML
public void initialize() {
layoutGridItem.setOnAction(event -> ToolController.getInstance().layoutGrid());
fixLineLengthItem.setOnAction(
event ->
ToolDialogs.fixLineLengthDialog()
.showAndWait()
.ifPresent(data -> ToolController.getInstance().fixLineLength(data)));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
* Institute of Energy Systems, Energy Efficiency and Energy Economics,
* Research group Distribution grid planning and operation
*/
package edu.ie3.netpad.tool;
package edu.ie3.netpad.tool.controller;

import edu.ie3.datamodel.models.input.container.JointGridContainer;
import edu.ie3.netpad.exception.GridControllerListenerException;
import edu.ie3.netpad.grid.controller.GridController;
import edu.ie3.netpad.tool.event.FixLineLengthRequestEvent;
import edu.ie3.netpad.tool.event.LayoutGridRequestEvent;
import edu.ie3.netpad.tool.event.LayoutGridResponse;
import edu.ie3.netpad.tool.event.ToolEvent;
Expand All @@ -22,12 +24,21 @@
* @since 04.06.20
*/
public class ToolController {

private static final ObjectProperty<ToolEvent> toolEventProperty = new SimpleObjectProperty<>();

private static final class InstanceHolder {
static final ToolController INSTANCE = new ToolController();
}

public static ToolController getInstance() {
return ToolController.InstanceHolder.INSTANCE;
}

private boolean initialized;

public void layoutGrid() {
private ToolController() {}

public void layoutGrid() {
// issue an event that we want to layout the grid
// the listener provided is a one-shot instance which fires when the
// gridController returns
Expand All @@ -45,14 +56,15 @@ public void layoutGrid() {
}));
}

private static final class InstanceHolder {
static final ToolController INSTANCE = new ToolController();
}

private ToolController() {}

public static ToolController getInstance() {
return ToolController.InstanceHolder.INSTANCE;
/**
* Ask the {@link GridController} to fix discrepancy between electrical and geographical line
* length
*
* @param data user preferences for the given operation
*/
public void fixLineLength(ToolDialogs.FixLineLengthData data) {
notifyListener(
new FixLineLengthRequestEvent(data.getResolutionMode(), data.getAffectedSubnets()));
}

public void registerGridControllerListener(ChangeListener<ToolEvent> listener) {
Expand Down
Loading

0 comments on commit 7cb5573

Please sign in to comment.