Skip to content

Comments

Refactor graph traversal methods to use new getConnectedComponents API#3753

Open
clementleclercRTE wants to merge 19 commits intomainfrom
graph-connected-components
Open

Refactor graph traversal methods to use new getConnectedComponents API#3753
clementleclercRTE wants to merge 19 commits intomainfrom
graph-connected-components

Conversation

@clementleclercRTE
Copy link
Contributor

@clementleclercRTE clementleclercRTE commented Feb 3, 2026

Please check if the PR fulfills these requirements

  • The commit message follows our guidelines
  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)
  • A PR or issue has been opened in all impacted repositories (if any)

Does this PR already have an issue describing the problem?

What kind of change does this PR introduce?

Refactoring

What is the current behavior?

Currently, BusBreakerTopologyModel, NodeBreakerTopologyModel, and DcTopologyModel all contain duplicated logic to traverse the graph and identify connected components (bus construction).

What is the new behavior (if this is a feature change)?

Introduced getConnectedComponents method and ConnectedComponentCollector interface in UndirectedGraph

Does this PR introduce a breaking change or deprecate an API?

  • Yes
  • No

If yes, please check if the following requirements are fulfilled

  • The Breaking Change or Deprecated label has been added
  • The migration steps are described in the following section

What changes might users need to make in their application due to this PR? (migration steps)

Other information:

…API.

Signed-off-by: Leclerc Clement <clement.leclerc@rte-france.com>
…API.

Signed-off-by: Leclerc Clement <clement.leclerc@rte-france.com>
… graph-connected-components

# Conflicts:
#	iidm/iidm-impl/src/main/java/com/powsybl/iidm/network/impl/NodeBreakerTopologyModel.java
@rolnico rolnico changed the title Refactor graph traversal methods to use new getConnectedComponents Refactor graph traversal methods to use new getConnectedComponents API Feb 3, 2026
… traversal components.

Signed-off-by: Leclerc Clement <clement.leclerc@rte-france.com>
…Predicate`

Signed-off-by: Leclerc Clement <clement.leclerc@rte-france.com>
…` API, and enhance vertex object handling in graph traversal implementations.

Signed-off-by: Leclerc Clement <clement.leclerc@rte-france.com>
Signed-off-by: Leclerc Clement <clement.leclerc@rte-france.com>
Signed-off-by: Leclerc Clement <clement.leclerc@rte-france.com>
@sonarqubecloud
Copy link

sonarqubecloud bot commented Feb 9, 2026

} else {
busSet.add(graph.getVertexObject(v2));
return TraverseResult.CONTINUE;
graph.getConnectedComponents(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

introduce a variable which is the result to improve readability

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. I used a simple for loop instead of streams

@Override
public Iterable<V> getVerticesObj() {
return FluentIterable.from(vertices)
.filter(Predicates.notNull())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you remove the formatting changes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

List<C> components = new ArrayList<>(vertices.size());

for (int v = 0; v < vertices.size(); v++) {
Vertex<V> vertex = vertices.get(v);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you check that vertex != null?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, I've removed that

@Override
public void addVertex(Set<ConfiguredBus> component, int vertexIndex) {
ConfiguredBus bus = graph.getVertexObject(vertexIndex);
if (bus != null) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getVertexObject() cannot return null in bus/breaker topology

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, I've removed that check

}
return TraverseResult.CONTINUE;
},
new UndirectedGraph.ConnectedComponentCollector<Set<ConfiguredBus>>() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no need to have a set anymore, a list would be enough

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switched to a List and updated relevant private function signatures accordingly.


final CalculatedBusTopology calculatedBusTopology
= new CalculatedBusTopology();
= new CalculatedBusTopology();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove formatting changes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}
return TraverseResult.CONTINUE;
}).forEach(nodes -> {
CopyOnWriteArrayList<NodeTerminal> terminals = new CopyOnWriteArrayList<>();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if we really need the CopyOnWriteArrayList for buses

terminals.add(terminal);
}
}
if (getBusChecker().isValid(graph, nodes, terminals)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the terminals argument is never used. Should it be removed or rather used?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed it and moved the logic directly into the addBus method

…comments

Signed-off-by: Leclerc Clement <clement.leclerc@rte-france.com>
…comments

Signed-off-by: Leclerc Clement <clement.leclerc@rte-france.com>
Signed-off-by: Leclerc Clement <clement.leclerc@rte-france.com>
@sonarqubecloud
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants