diff --git a/single-line-diagram/single-line-diagram-core/src/main/java/com/powsybl/sld/layout/MatrixZoneLayout.java b/single-line-diagram/single-line-diagram-core/src/main/java/com/powsybl/sld/layout/MatrixZoneLayout.java index 40f91ee6c..c705af369 100644 --- a/single-line-diagram/single-line-diagram-core/src/main/java/com/powsybl/sld/layout/MatrixZoneLayout.java +++ b/single-line-diagram/single-line-diagram-core/src/main/java/com/powsybl/sld/layout/MatrixZoneLayout.java @@ -18,7 +18,7 @@ import java.util.*; /** - * @author Thomas Adam + * @author Thomas Adam {@literal } */ public class MatrixZoneLayout extends AbstractZoneLayout { private final MatrixZoneLayoutModel model; diff --git a/single-line-diagram/single-line-diagram-core/src/main/java/com/powsybl/sld/layout/MatrixZoneLayoutFactory.java b/single-line-diagram/single-line-diagram-core/src/main/java/com/powsybl/sld/layout/MatrixZoneLayoutFactory.java index 34165f0da..ffc82a616 100644 --- a/single-line-diagram/single-line-diagram-core/src/main/java/com/powsybl/sld/layout/MatrixZoneLayoutFactory.java +++ b/single-line-diagram/single-line-diagram-core/src/main/java/com/powsybl/sld/layout/MatrixZoneLayoutFactory.java @@ -12,7 +12,7 @@ import java.util.*; /** - * @author Thomas Adam + * @author Thomas Adam {@literal } */ public class MatrixZoneLayoutFactory implements ZoneLayoutFactory { diff --git a/single-line-diagram/single-line-diagram-core/src/main/java/com/powsybl/sld/layout/pathfinding/AbstractPathFinder.java b/single-line-diagram/single-line-diagram-core/src/main/java/com/powsybl/sld/layout/pathfinding/AbstractPathFinder.java index 3716b73d1..80f67c29d 100644 --- a/single-line-diagram/single-line-diagram-core/src/main/java/com/powsybl/sld/layout/pathfinding/AbstractPathFinder.java +++ b/single-line-diagram/single-line-diagram-core/src/main/java/com/powsybl/sld/layout/pathfinding/AbstractPathFinder.java @@ -10,7 +10,7 @@ import java.util.*; /** - * @author Thomas Adam + * @author Thomas Adam {@literal } */ public abstract class AbstractPathFinder implements PathFinder { @Override diff --git a/single-line-diagram/single-line-diagram-core/src/main/java/com/powsybl/sld/layout/pathfinding/DijkstraPathFinder.java b/single-line-diagram/single-line-diagram-core/src/main/java/com/powsybl/sld/layout/pathfinding/DijkstraPathFinder.java index db448d4f0..5f4b02bfd 100644 --- a/single-line-diagram/single-line-diagram-core/src/main/java/com/powsybl/sld/layout/pathfinding/DijkstraPathFinder.java +++ b/single-line-diagram/single-line-diagram-core/src/main/java/com/powsybl/sld/layout/pathfinding/DijkstraPathFinder.java @@ -10,7 +10,7 @@ import java.util.*; /** - * @author Thomas Adam + * @author Thomas Adam {@literal } */ public final class DijkstraPathFinder extends AbstractPathFinder { @@ -19,7 +19,7 @@ public DijkstraPathFinder() { } @Override - public List findShortestPath(Grid grid, int startX, int startY, int endX, int endY, boolean setSnakeLineAsObstacle) { + public List findShortestPath(Grid grid, int startX, int startY, int endX, int endY) { Point start = new Point(startX, startY); Point goal = new Point(endX, endY); diff --git a/single-line-diagram/single-line-diagram-core/src/main/java/com/powsybl/sld/layout/pathfinding/Grid.java b/single-line-diagram/single-line-diagram-core/src/main/java/com/powsybl/sld/layout/pathfinding/Grid.java index c85055d65..1bc8ac821 100644 --- a/single-line-diagram/single-line-diagram-core/src/main/java/com/powsybl/sld/layout/pathfinding/Grid.java +++ b/single-line-diagram/single-line-diagram-core/src/main/java/com/powsybl/sld/layout/pathfinding/Grid.java @@ -10,7 +10,7 @@ import java.util.*; /** - * @author Thomas Adam + * @author Thomas Adam {@literal } */ public class Grid { @@ -46,25 +46,6 @@ public int getDistance() { public Node getParent() { return parent; } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - Node node = (Node) o; - return point == node.point && - cost == node.cost && - distance == node.distance; - } - - @Override - public int hashCode() { - return Objects.hash(point, cost, distance); - } } private static final int NOT_WALKABLE = -1; @@ -111,7 +92,7 @@ public void setAvailability(Point point, boolean available) { setAvailability(point.x(), point.y(), available); } - public void setAvailability(List path, boolean available) { + public void setAvailability(List path, boolean available) { path.forEach(p -> setAvailability(p, available)); } @@ -123,7 +104,7 @@ public boolean isAvailable(Point point) { return point.x() >= 0 && point.x() < width && point.y() >= 0 && point.y() < height && nodes[point.x()][point.y()].cost != -1; } - public List getNeighbors(Point point) { + protected List getNeighbors(Point point) { // Considering only adjacent points List neighbors = new ArrayList<>(); Node right = getNode(point.x() + 1, point.y()); diff --git a/single-line-diagram/single-line-diagram-core/src/main/java/com/powsybl/sld/layout/pathfinding/PathFinder.java b/single-line-diagram/single-line-diagram-core/src/main/java/com/powsybl/sld/layout/pathfinding/PathFinder.java index db7e71a37..e0f9f30a1 100644 --- a/single-line-diagram/single-line-diagram-core/src/main/java/com/powsybl/sld/layout/pathfinding/PathFinder.java +++ b/single-line-diagram/single-line-diagram-core/src/main/java/com/powsybl/sld/layout/pathfinding/PathFinder.java @@ -10,11 +10,11 @@ import java.util.*; /** - * @author Thomas Adam + * @author Thomas Adam {@literal } */ public interface PathFinder { - List findShortestPath(Grid grid, int startX, int startY, int endX, int endY, boolean setSnakeLineAsObstacle); + List findShortestPath(Grid grid, int startX, int startY, int endX, int endY); List toSnakeLine(List path); } diff --git a/single-line-diagram/single-line-diagram-core/src/main/java/com/powsybl/sld/layout/pathfinding/PathFinderFactory.java b/single-line-diagram/single-line-diagram-core/src/main/java/com/powsybl/sld/layout/pathfinding/PathFinderFactory.java index 2a08fd08d..c6e161188 100644 --- a/single-line-diagram/single-line-diagram-core/src/main/java/com/powsybl/sld/layout/pathfinding/PathFinderFactory.java +++ b/single-line-diagram/single-line-diagram-core/src/main/java/com/powsybl/sld/layout/pathfinding/PathFinderFactory.java @@ -8,7 +8,7 @@ package com.powsybl.sld.layout.pathfinding; /** - * @author Thomas Adam + * @author Thomas Adam {@literal } */ public class PathFinderFactory { diff --git a/single-line-diagram/single-line-diagram-core/src/main/java/com/powsybl/sld/layout/pathfinding/Point.java b/single-line-diagram/single-line-diagram-core/src/main/java/com/powsybl/sld/layout/pathfinding/Point.java index 602e1684a..e5a6c52a1 100644 --- a/single-line-diagram/single-line-diagram-core/src/main/java/com/powsybl/sld/layout/pathfinding/Point.java +++ b/single-line-diagram/single-line-diagram-core/src/main/java/com/powsybl/sld/layout/pathfinding/Point.java @@ -10,14 +10,14 @@ import java.util.*; /** - * @author Thomas Adam + * @author Thomas Adam {@literal } */ public class Point { - private int x; - private int y; + private final int x; + private final int y; - Point(int x, int y) { + protected Point(int x, int y) { this.x = x; this.y = y; } diff --git a/single-line-diagram/single-line-diagram-core/src/main/java/com/powsybl/sld/layout/zonebygrid/MatrixCell.java b/single-line-diagram/single-line-diagram-core/src/main/java/com/powsybl/sld/layout/zonebygrid/MatrixCell.java index 4b5741043..7fc0673ad 100644 --- a/single-line-diagram/single-line-diagram-core/src/main/java/com/powsybl/sld/layout/zonebygrid/MatrixCell.java +++ b/single-line-diagram/single-line-diagram-core/src/main/java/com/powsybl/sld/layout/zonebygrid/MatrixCell.java @@ -10,7 +10,7 @@ import com.powsybl.sld.model.graphs.*; /** - * @author Thomas Adam + * @author Thomas Adam {@literal } */ public record MatrixCell(BaseGraph graph, int col, int row) { } diff --git a/single-line-diagram/single-line-diagram-core/src/main/java/com/powsybl/sld/layout/zonebygrid/MatrixZoneLayoutModel.java b/single-line-diagram/single-line-diagram-core/src/main/java/com/powsybl/sld/layout/zonebygrid/MatrixZoneLayoutModel.java index 1c780105a..8fc68eb68 100644 --- a/single-line-diagram/single-line-diagram-core/src/main/java/com/powsybl/sld/layout/zonebygrid/MatrixZoneLayoutModel.java +++ b/single-line-diagram/single-line-diagram-core/src/main/java/com/powsybl/sld/layout/zonebygrid/MatrixZoneLayoutModel.java @@ -16,7 +16,7 @@ import java.util.*; /** - * @author Thomas Adam + * @author Thomas Adam {@literal } */ public class MatrixZoneLayoutModel { @@ -63,19 +63,10 @@ public int getMatrixCellHeight() { return matrixCellHeight; } - public int getX(String id) { - MatrixCell cell = cellsById.get(id); - return getX(cell.col()); - } - public int getX(int col) { return ((col + 1) * snakelineHallwayWidth) + (col * matrixCellWidth); } - public int getY(String id) { - return getY(id, Direction.TOP); - } - public int getY(int row) { return getY(row, Direction.TOP); } @@ -84,12 +75,6 @@ public int getY(int row, Direction direction) { return (row + 1) * snakelineHallwayWidth + (row + (direction == Direction.TOP ? 0 : 1)) * matrixCellHeight; } - public int getY(String id, Direction direction) { - MatrixCell cell = cellsById.get(id); - int row = cell.row(); - return getY(row, direction); - } - public List buildSnakeline(PathFinder pathfinder, Point p1, Direction d1, Point p2, Direction d2) { @@ -98,8 +83,7 @@ public List buildSnakeline(PathFinder pathfinder, // Use path finding algo return pathfinder.toSnakeLine(pathfinder.findShortestPath(pathFinderGrid, (int) p1.getX(), (int) p1.getY(), - (int) p2.getX(), (int) p2.getY(), - true)); + (int) p2.getX(), (int) p2.getY())); } private void insertFreePathInSubstation(Point p1, Direction d1, diff --git a/single-line-diagram/single-line-diagram-core/src/test/java/com/powsybl/sld/iidm/TestPathFinding.java b/single-line-diagram/single-line-diagram-core/src/test/java/com/powsybl/sld/iidm/TestPathFinding.java new file mode 100644 index 000000000..fc10fdccd --- /dev/null +++ b/single-line-diagram/single-line-diagram-core/src/test/java/com/powsybl/sld/iidm/TestPathFinding.java @@ -0,0 +1,75 @@ +/** + * Copyright (c) 2023, RTE (http://www.rte-france.com) + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * SPDX-License-Identifier: MPL-2.0 + */ +package com.powsybl.sld.iidm; + +import com.powsybl.sld.layout.pathfinding.*; +import com.powsybl.sld.model.coordinate.Point; +import org.junit.jupiter.api.*; + +import java.io.*; +import java.util.*; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * @author Thomas Adam {@literal } + */ +class TestPathFinding extends AbstractTestCaseIidm { + + private Grid pathFinderGrid = null; + private final PathFinder pathfinder = new DijkstraPathFinder(); + + @BeforeEach + public void setUp() throws IOException { + pathFinderGrid = new Grid(12, 12); + } + + @Test + void testNoSmoothPath() { + final List expectedSnakeline = new ArrayList<>(); + pathFinderGrid.setAvailability(0, 0, true); + expectedSnakeline.add(new Point(0, 0)); + for (int i = 0; i < 12 - 1; i++) { + pathFinderGrid.setAvailability(i + 1, i, true); + expectedSnakeline.add(new Point(i + 1, i)); + pathFinderGrid.setAvailability(i + 1, i + 1, true); + expectedSnakeline.add(new Point(i + 1, i + 1)); + } + List snakeline = pathfinder.toSnakeLine(pathfinder.findShortestPath(pathFinderGrid, + 0, 0, + 11, 11)); + assertEquals(expectedSnakeline.size(), snakeline.size()); + for (int i = 0; i < expectedSnakeline.size(); i++) { + assertEquals(expectedSnakeline.get(i).toString(), snakeline.get(i).toString()); + } + } + + @Test + void testSmoothPath() { + final List expectedSnakeline = new ArrayList<>(); + // Make available left & right borders + for (int y = 0; y < 12; y++) { + pathFinderGrid.setAvailability(0, y, true); + expectedSnakeline.add(new Point(0, y)); + pathFinderGrid.setAvailability(11, y, true); + } + // Make available up & down borders + for (int x = 1; x < 12; x++) { + pathFinderGrid.setAvailability(x, 0, true); + pathFinderGrid.setAvailability(x, 11, true); + expectedSnakeline.add(new Point(x, 11)); + } + List snakeline = pathfinder.toSnakeLine(pathfinder.findShortestPath(pathFinderGrid, + 0, 0, + 11, 11)); + assertEquals(expectedSnakeline.size(), snakeline.size()); + for (int i = 0; i < expectedSnakeline.size(); i++) { + assertEquals(expectedSnakeline.get(i).toString(), snakeline.get(i).toString()); + } + } +} diff --git a/single-line-diagram/single-line-diagram-core/src/test/java/com/powsybl/sld/raw/RawGraphBuilderUtils.java b/single-line-diagram/single-line-diagram-core/src/test/java/com/powsybl/sld/raw/RawGraphBuilderUtils.java index 7bb081b4d..c736636ae 100644 --- a/single-line-diagram/single-line-diagram-core/src/test/java/com/powsybl/sld/raw/RawGraphBuilderUtils.java +++ b/single-line-diagram/single-line-diagram-core/src/test/java/com/powsybl/sld/raw/RawGraphBuilderUtils.java @@ -21,7 +21,7 @@ import static com.powsybl.sld.model.coordinate.Direction.*; -/* +/** * @author Thomas Adam {@literal } */ public final class RawGraphBuilderUtils { diff --git a/single-line-diagram/single-line-diagram-core/src/test/java/com/powsybl/sld/raw/TestCase12ZoneGraph.java b/single-line-diagram/single-line-diagram-core/src/test/java/com/powsybl/sld/raw/TestCase12ZoneGraph.java index c901042c1..3eee3d595 100644 --- a/single-line-diagram/single-line-diagram-core/src/test/java/com/powsybl/sld/raw/TestCase12ZoneGraph.java +++ b/single-line-diagram/single-line-diagram-core/src/test/java/com/powsybl/sld/raw/TestCase12ZoneGraph.java @@ -16,7 +16,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; -/* +/** * @author Thomas Adam {@literal } */ class TestCase12ZoneGraph extends AbstractTestCaseRaw {