powsybl / powsybl-core

A framework to build power system oriented software
https://www.powsybl.org
Mozilla Public License 2.0
126 stars 41 forks source link

Clarify graph API regarding vertex indices and its use in node breaker topology #781

Closed sylvlecl closed 4 years ago

sylvlecl commented 5 years ago

API improvement.

See PR #775: the node breaker topology uses the UndirectGraph interface to represent the topology. When creating it, it starts by setting a "node count" before actually adding those nodes. A problem arises when nodes indices are not contiguous, for example after removing some equipments. In that case, the index of some vertices can overflow the allocated array. The PR fixes this by ensuring that the array is extended in that case, but the node breaker API remains ambiguous.

The API must be clarified:

Clarify the API to avoid bugs when using it, for example by underallocating vertices, or by looping on vertices indices from 0 to N whereas they are not contiguous.

Follows bug fix in PR #775 .

mathbagu commented 5 years ago

The graph API has not setNodeCount method. This method is in NodeBreakerView interface.

From my point of view the behavior of the graph is fine, but I agree to add javadoc to clarify things:

About for-loop, we have to clarify that:

I think we should add a way to create a vertex with a specific index. This is what I propose in the PR #775. But we should not change the behavior of addEdge(): this method should throw an exception if one of its ends doesn’t exist.

From my point of view, we can change the meaning of the setNodeCount method. If we care about memory consumption and user friendly API, we should remove this method and create vertices when they are needed. If we care about memory consumption, the setNodeCount method should not allocate vertex but reserve memory (creating unused vertices). If we want to change nothing, we should remove the call to removeIsolatedVertex when we remove a connectable.

sylvlecl commented 5 years ago

Indeed, I fixed the confusion between the graph API and its use in node breaker topology creation, in the issue text.

For the "compact" method, I did not mean to re-index the vertices, but only resize the array to not waste space with overallocation. But thinking again it does not seem very useful, in any case today the real capacity of the underlying array is managed by ArrayList.

--> I vote for removal of the setNodeCount to avoid confusions.