Skip to content

Commit

Permalink
Increase code coverage + cleaning code
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas ADAM <[email protected]>
  • Loading branch information
tadam50 committed Dec 13, 2023
1 parent 5d7fa36 commit 36a6d98
Show file tree
Hide file tree
Showing 13 changed files with 95 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import java.util.*;

/**
* @author Thomas Adam <tadam at silicom.fr>
* @author Thomas Adam {@literal <tadam at neverhack.com>}
*/
public class MatrixZoneLayout extends AbstractZoneLayout {
private final MatrixZoneLayoutModel model;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import java.util.*;

/**
* @author Thomas Adam <tadam at silicom.fr>
* @author Thomas Adam {@literal <tadam at neverhack.com>}
*/
public class MatrixZoneLayoutFactory implements ZoneLayoutFactory {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import java.util.*;

/**
* @author Thomas Adam <tadam at neverhack.com>
* @author Thomas Adam {@literal <tadam at neverhack.com>}
*/
public abstract class AbstractPathFinder implements PathFinder {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import java.util.*;

/**
* @author Thomas Adam <tadam at neverhack.com>
* @author Thomas Adam {@literal <tadam at neverhack.com>}
*/
public final class DijkstraPathFinder extends AbstractPathFinder {

Expand All @@ -19,7 +19,7 @@ public DijkstraPathFinder() {
}

@Override
public List<Point> findShortestPath(Grid grid, int startX, int startY, int endX, int endY, boolean setSnakeLineAsObstacle) {
public List<Point> findShortestPath(Grid grid, int startX, int startY, int endX, int endY) {
Point start = new Point(startX, startY);
Point goal = new Point(endX, endY);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import java.util.*;

/**
* @author Thomas Adam <tadam at neverhack.com>
* @author Thomas Adam {@literal <tadam at neverhack.com>}
*/
public class Grid {

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -111,7 +92,7 @@ public void setAvailability(Point point, boolean available) {
setAvailability(point.x(), point.y(), available);
}

public void setAvailability(List<com.powsybl.sld.layout.pathfinding.Point> path, boolean available) {
public void setAvailability(List<Point> path, boolean available) {
path.forEach(p -> setAvailability(p, available));
}

Expand All @@ -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<Node> getNeighbors(Point point) {
protected List<Node> getNeighbors(Point point) {
// Considering only adjacent points
List<Node> neighbors = new ArrayList<>();
Node right = getNode(point.x() + 1, point.y());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
import java.util.*;

/**
* @author Thomas Adam <tadam at neverhack.com>
* @author Thomas Adam {@literal <tadam at neverhack.com>}
*/
public interface PathFinder {

List<Point> findShortestPath(Grid grid, int startX, int startY, int endX, int endY, boolean setSnakeLineAsObstacle);
List<Point> findShortestPath(Grid grid, int startX, int startY, int endX, int endY);

List<com.powsybl.sld.model.coordinate.Point> toSnakeLine(List<Point> path);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
package com.powsybl.sld.layout.pathfinding;

/**
* @author Thomas Adam <tadam at silicom.fr>
* @author Thomas Adam {@literal <tadam at neverhack.com>}
*/
public class PathFinderFactory {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
import java.util.*;

/**
* @author Thomas Adam <tadam at neverhack.com>
* @author Thomas Adam {@literal <tadam at neverhack.com>}
*/
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import com.powsybl.sld.model.graphs.*;

/**
* @author Thomas Adam <tadam at neverhack.com>
* @author Thomas Adam {@literal <tadam at neverhack.com>}
*/
public record MatrixCell(BaseGraph graph, int col, int row) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import java.util.*;

/**
* @author Thomas Adam <tadam at neverhack.com>
* @author Thomas Adam {@literal <tadam at neverhack.com>}
*/
public class MatrixZoneLayoutModel {

Expand Down Expand Up @@ -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);
}
Expand All @@ -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<Point> buildSnakeline(PathFinder pathfinder,
Point p1, Direction d1,
Point p2, Direction d2) {
Expand All @@ -98,8 +83,7 @@ public List<Point> 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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <tadam at neverhack.com>}
*/
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<Point> 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<Point> 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<Point> 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<Point> 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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import static com.powsybl.sld.model.coordinate.Direction.*;

/*
/**
* @author Thomas Adam {@literal <tadam at neverhack.com>}
*/
public final class RawGraphBuilderUtils {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;

/*
/**
* @author Thomas Adam {@literal <tadam at silicom.fr>}
*/
class TestCase12ZoneGraph extends AbstractTestCaseRaw {
Expand Down

0 comments on commit 36a6d98

Please sign in to comment.