Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify nad and sld api for better consistency #475

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,15 @@
import com.powsybl.iidm.network.Network;
import com.powsybl.iidm.network.VoltageLevel;
import com.powsybl.nad.build.iidm.IdProvider;
import com.powsybl.nad.build.iidm.IntIdProvider;
import com.powsybl.nad.build.iidm.NetworkGraphBuilder;
import com.powsybl.nad.build.iidm.VoltageLevelFilter;
import com.powsybl.nad.layout.BasicForceLayoutFactory;
import com.powsybl.nad.layout.LayoutFactory;
import com.powsybl.nad.layout.LayoutParameters;
import com.powsybl.nad.model.Graph;
import com.powsybl.nad.svg.LabelProvider;
import com.powsybl.nad.svg.StyleProvider;
import com.powsybl.nad.svg.SvgParameters;
import com.powsybl.nad.svg.SvgWriter;
import com.powsybl.nad.svg.iidm.DefaultLabelProvider;
import com.powsybl.nad.svg.iidm.TopologicalStyleProvider;

import java.io.IOException;
import java.io.StringWriter;
Expand All @@ -35,116 +31,91 @@
/**
* @author Florian Dupuy <florian.dupuy at rte-france.com>
*/
public class NetworkAreaDiagram {
public final class NetworkAreaDiagram {

private final Network network;
private final Predicate<VoltageLevel> voltageLevelFilter;

public NetworkAreaDiagram(Network network) {
this(network, VoltageLevelFilter.NO_FILTER);
}

public NetworkAreaDiagram(Network network, String voltageLevelId, int depth) {
this(network, VoltageLevelFilter.createVoltageLevelDepthFilter(network, voltageLevelId, depth));
}

public NetworkAreaDiagram(Network network, List<String> voltageLevelIds) {
this(network, VoltageLevelFilter.createVoltageLevelsFilter(network, voltageLevelIds));
}

public NetworkAreaDiagram(Network network, List<String> voltageLevelIds, int depth) {
this(network, VoltageLevelFilter.createVoltageLevelsDepthFilter(network, voltageLevelIds, depth));
}

public NetworkAreaDiagram(Network network, Predicate<VoltageLevel> voltageLevelFilter) {
this.network = Objects.requireNonNull(network);
this.voltageLevelFilter = Objects.requireNonNull(voltageLevelFilter);
private NetworkAreaDiagram() {
}

public Network getNetwork() {
return network;
}
/* ------------------------------------------------------------------------------------------------ */
// Network, Path and different options of filtering
/* ------------------------------------------------------------------------------------------------ */

public void draw(Path svgFile) {
draw(svgFile, new SvgParameters());
public static void draw(Network network, Path svgFile) {
draw(network, svgFile, new NetworkAreaDiagramConfigurationBuilder(network).build(), VoltageLevelFilter.NO_FILTER);
}

public void draw(Path svgFile, SvgParameters svgParameters) {
draw(svgFile, svgParameters, new LayoutParameters());
public static void draw(Network network, Path svgFile, String voltageLevelId, int depth) {
draw(network, svgFile, new NetworkAreaDiagramConfigurationBuilder(network).build(), VoltageLevelFilter.createVoltageLevelDepthFilter(network, voltageLevelId, depth));
}

public void draw(Path svgFile, SvgParameters svgParameters, LayoutParameters layoutParameters) {
draw(svgFile, svgParameters, layoutParameters, new TopologicalStyleProvider(network));
public static void draw(Network network, Path svgFile, List<String> voltageLevelIds) {
draw(network, svgFile, new NetworkAreaDiagramConfigurationBuilder(network).build(), VoltageLevelFilter.createVoltageLevelsFilter(network, voltageLevelIds));
}

public void draw(Path svgFile, SvgParameters svgParameters, LayoutParameters layoutParameters,
StyleProvider styleProvider) {
draw(svgFile, svgParameters, layoutParameters, styleProvider, new DefaultLabelProvider(network, svgParameters));
public static void draw(Network network, Path svgFile, List<String> voltageLevelIds, int depth) {
draw(network, svgFile, new NetworkAreaDiagramConfigurationBuilder(network).build(), VoltageLevelFilter.createVoltageLevelsDepthFilter(network, voltageLevelIds, depth));
}

public void draw(Path svgFile, SvgParameters svgParameters, LayoutParameters layoutParameters,
StyleProvider styleProvider, LabelProvider labelProvider) {
draw(svgFile, svgParameters, layoutParameters, styleProvider, labelProvider, new BasicForceLayoutFactory());
}
/* ---------------------------------------------------------------- */
// Network, Path, Configuration, Predicate
/* ---------------------------------------------------------------- */

public void draw(Path svgFile, SvgParameters svgParameters, LayoutParameters layoutParameters,
StyleProvider styleProvider, LabelProvider labelProvider, LayoutFactory layoutFactory) {
draw(svgFile, svgParameters, layoutParameters, styleProvider, labelProvider, layoutFactory, new IntIdProvider());
}
public static void draw(Network network, Path svgFile, NetworkAreaDiagramConfiguration networkAreaDiagramConfiguration, Predicate<VoltageLevel> voltageLevelFilter) {

public void draw(Path svgFile, SvgParameters svgParameters, LayoutParameters layoutParameters,
StyleProvider styleProvider, LabelProvider labelProvider, LayoutFactory layoutFactory,
IdProvider idProvider) {
Objects.requireNonNull(network);
Objects.requireNonNull(svgFile);
LayoutParameters layoutParameters = networkAreaDiagramConfiguration.getLayoutParameters();
SvgParameters svgParameters = networkAreaDiagramConfiguration.getSvgParameters();
StyleProvider styleProvider = networkAreaDiagramConfiguration.getStyleProvider();
LayoutFactory layoutFactory = networkAreaDiagramConfiguration.getLayoutFactory();
IdProvider idProvider = networkAreaDiagramConfiguration.getIdProvider();
LabelProvider labelProvider = networkAreaDiagramConfiguration.getLabelProvider();
Objects.requireNonNull(layoutParameters);
Objects.requireNonNull(svgParameters);
Objects.requireNonNull(styleProvider);
Objects.requireNonNull(layoutFactory);
Objects.requireNonNull(idProvider);
Objects.requireNonNull(voltageLevelFilter);

Graph graph = new NetworkGraphBuilder(network, voltageLevelFilter, idProvider).buildGraph();
layoutFactory.create().run(graph, layoutParameters);
new SvgWriter(svgParameters, styleProvider, labelProvider).writeSvg(graph, svgFile);
}

public void draw(Writer writer) {
draw(writer, new SvgParameters());
}
/* ------------------------------------------------------------------------------------------------ */
// Network, Writer and different options of filtering
/* ------------------------------------------------------------------------------------------------ */

public void draw(Writer writer, SvgParameters svgParameters) {
draw(writer, svgParameters, new LayoutParameters());
public static void draw(Network network, Writer writer) {
draw(network, writer, new NetworkAreaDiagramConfigurationBuilder(network).build(), VoltageLevelFilter.NO_FILTER);
}

public void draw(Writer writer, SvgParameters svgParameters, LayoutParameters layoutParameters) {
draw(writer, svgParameters, layoutParameters, new TopologicalStyleProvider(network));
public static void draw(Network network, Writer writer, String voltageLevelId, int depth) {
draw(network, writer, new NetworkAreaDiagramConfigurationBuilder(network).build(), VoltageLevelFilter.createVoltageLevelDepthFilter(network, voltageLevelId, depth));
}

public void draw(Writer writer, SvgParameters svgParameters, LayoutParameters layoutParameters,
StyleProvider styleProvider) {
draw(writer, svgParameters, layoutParameters, styleProvider, new DefaultLabelProvider(network, svgParameters));
public static void draw(Network network, Writer writer, List<String> voltageLevelIds) {
draw(network, writer, new NetworkAreaDiagramConfigurationBuilder(network).build(), VoltageLevelFilter.createVoltageLevelsFilter(network, voltageLevelIds));
}

public void draw(Writer writer, SvgParameters svgParameters, LayoutParameters layoutParameters,
StyleProvider styleProvider, LabelProvider labelProvider) {
draw(writer, svgParameters, layoutParameters, styleProvider, labelProvider, new BasicForceLayoutFactory());
public void draw(Network network, Writer writer, Predicate<VoltageLevel> voltageLevelFilter) {
draw(network, writer, new NetworkAreaDiagramConfigurationBuilder(network).build(), voltageLevelFilter);
}

public void draw(Writer writer, SvgParameters svgParameters, LayoutParameters layoutParameters,
StyleProvider styleProvider, LabelProvider labelProvider, LayoutFactory layoutFactory) {
draw(writer, svgParameters, layoutParameters, styleProvider, labelProvider, layoutFactory, new IntIdProvider());
}
/* ----------------------------------------------------------------------------------------------- */
// Network, Writer, Configuration, Predicate
/* ----------------------------------------------------------------------------------------------- */

public void draw(Writer writer, SvgParameters svgParameters, LayoutParameters layoutParameters,
StyleProvider styleProvider, LabelProvider labelProvider, LayoutFactory layoutFactory,
IdProvider idProvider) {
Graph graph = new NetworkGraphBuilder(network, voltageLevelFilter, idProvider).buildGraph();
layoutFactory.create().run(graph, layoutParameters);
new SvgWriter(svgParameters, styleProvider, labelProvider).writeSvg(graph, writer);
public static void draw(Network network, Writer writer, NetworkAreaDiagramConfiguration networkAreaDiagramConfiguration, Predicate<VoltageLevel> voltageLevelFilter) {
Graph graph = new NetworkGraphBuilder(network, voltageLevelFilter, networkAreaDiagramConfiguration.getIdProvider()).buildGraph();
networkAreaDiagramConfiguration.getLayoutFactory().create().run(graph, networkAreaDiagramConfiguration.getLayoutParameters());
new SvgWriter(networkAreaDiagramConfiguration.getSvgParameters(), networkAreaDiagramConfiguration.getStyleProvider(), networkAreaDiagramConfiguration.getLabelProvider()).writeSvg(graph, writer);
}

public String drawToString(SvgParameters svgParameters) {
public String drawToString(Network network, SvgParameters svgParameters) {
try (StringWriter writer = new StringWriter()) {
draw(writer, svgParameters);
NetworkAreaDiagramConfiguration networkAreaDiagramConfiguration = new NetworkAreaDiagramConfigurationBuilder(network).withSvgParameters(svgParameters).build();
draw(network, writer, networkAreaDiagramConfiguration, VoltageLevelFilter.NO_FILTER);
return writer.toString();
} catch (IOException e) {
throw new UncheckedIOException(e);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.powsybl.nad;
Copy link
Contributor

Choose a reason for hiding this comment

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

Copyright and authorship are missing for the new files of this PR


import com.powsybl.nad.build.iidm.IdProvider;
import com.powsybl.nad.layout.LayoutFactory;
import com.powsybl.nad.layout.LayoutParameters;
import com.powsybl.nad.svg.LabelProvider;
import com.powsybl.nad.svg.StyleProvider;
import com.powsybl.nad.svg.SvgParameters;

public class NetworkAreaDiagramConfiguration {
Copy link
Contributor

Choose a reason for hiding this comment

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

We could simplify this with DiagramConfig, or just Config?


SvgParameters svgParameters;
LayoutParameters layoutParameters;
StyleProvider styleProvider;
LabelProvider labelProvider;
LayoutFactory layoutFactory;
IdProvider idProvider;

public NetworkAreaDiagramConfiguration(SvgParameters svgParameters, LayoutParameters layoutParameters, StyleProvider styleProvider, LabelProvider labelProvider, LayoutFactory layoutFactory, IdProvider idProvider) {
this.svgParameters = svgParameters;
this.layoutParameters = layoutParameters;
this.styleProvider = styleProvider;
this.labelProvider = labelProvider;
this.layoutFactory = layoutFactory;
this.idProvider = idProvider;
}

public SvgParameters getSvgParameters() {
return svgParameters;
}

public LayoutParameters getLayoutParameters() {
return layoutParameters;
}

public StyleProvider getStyleProvider() {
return styleProvider;
}

public LabelProvider getLabelProvider() {
return labelProvider;
}

public LayoutFactory getLayoutFactory() {
return layoutFactory;
}

public IdProvider getIdProvider() {
return idProvider;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.powsybl.nad;

import com.powsybl.iidm.network.Network;
import com.powsybl.nad.build.iidm.IdProvider;
import com.powsybl.nad.build.iidm.IntIdProvider;
import com.powsybl.nad.layout.BasicForceLayoutFactory;
import com.powsybl.nad.layout.LayoutFactory;
import com.powsybl.nad.layout.LayoutParameters;
import com.powsybl.nad.svg.LabelProvider;
import com.powsybl.nad.svg.StyleProvider;
import com.powsybl.nad.svg.SvgParameters;
import com.powsybl.nad.svg.iidm.DefaultLabelProvider;
import com.powsybl.nad.svg.iidm.TopologicalStyleProvider;

import java.util.function.BiFunction;

public class NetworkAreaDiagramConfigurationBuilder {
SvgParameters svgParameters = new SvgParameters();
LayoutParameters layoutParameters = new LayoutParameters();
StyleProvider styleProvider;
LabelProvider labelProvider;
Copy link
Contributor

Choose a reason for hiding this comment

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

you can remove this parameter, we shouldn't create it before build call.

Or in fact it should maybe be build only when calling config.getLabelProvider(). Indeed, one would expect one config to be network-agnostic, because you expect when speaking of config to reuse the same config for different networks. That implies that both StyleProvider and LabelProvider are created only when calling config.getLabelProvider(network). And then similarly for the idProvider.

BiFunction<Network, SvgParameters, LabelProvider> labelProviderCreator = DefaultLabelProvider::new;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
BiFunction<Network, SvgParameters, LabelProvider> labelProviderCreator = DefaultLabelProvider::new;
LabelProviderFactory labelProviderFactory = DefaultLabelProvider::new;

LayoutFactory layoutFactory = new BasicForceLayoutFactory();
IdProvider idProvider = new IntIdProvider();
Network network;

private static <R extends LabelProvider> R factory(Network network, SvgParameters svgParameters, BiFunction<Network, SvgParameters, R> function) {
return function.apply(network, svgParameters);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
private static <R extends LabelProvider> R factory(Network network, SvgParameters svgParameters, BiFunction<Network, SvgParameters, R> function) {
return function.apply(network, svgParameters);
}
@FunctionalInterface
public interface LabelProviderFactory {
LabelProvider create(Network network, SvgParameters svgParameters);
}


public NetworkAreaDiagramConfigurationBuilder(Network network) {
this.network = network;
this.styleProvider = new TopologicalStyleProvider(network);
this.labelProvider = factory(network, svgParameters, labelProviderCreator);
}

public NetworkAreaDiagramConfigurationBuilder withSvgParameters(SvgParameters svgParameters) {
this.svgParameters = svgParameters;
this.labelProvider = factory(network, svgParameters, labelProviderCreator);
return this;
}

public NetworkAreaDiagramConfigurationBuilder withLayoutParameters(LayoutParameters layoutParameters) {
this.layoutParameters = layoutParameters;
return this;
}

public NetworkAreaDiagramConfigurationBuilder withStyleProvider(StyleProvider styleProvider) {
this.styleProvider = styleProvider;
return this;
}

public NetworkAreaDiagramConfigurationBuilder withLabelProviderCreator(BiFunction<Network, SvgParameters, LabelProvider> labelProviderCreator) {
this.labelProviderCreator = labelProviderCreator;
this.labelProvider = factory(network, svgParameters, labelProviderCreator);
return this;
}

public NetworkAreaDiagramConfigurationBuilder withLayoutFactory(LayoutFactory layoutFactory) {
this.layoutFactory = layoutFactory;
return this;
}

public NetworkAreaDiagramConfigurationBuilder withIdProvider(IdProvider idProvider) {
this.idProvider = idProvider;
return this;
}

public NetworkAreaDiagramConfiguration build() {
return new NetworkAreaDiagramConfiguration(svgParameters, layoutParameters, styleProvider, labelProvider, layoutFactory, idProvider);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@
import com.google.common.jimfs.Configuration;
import com.google.common.jimfs.Jimfs;
import com.powsybl.iidm.network.Network;
import com.powsybl.nad.build.iidm.IdProvider;
import com.powsybl.nad.build.iidm.IntIdProvider;
import com.powsybl.nad.build.iidm.VoltageLevelFilter;
import com.powsybl.nad.layout.BasicForceLayoutFactory;
import com.powsybl.nad.layout.LayoutFactory;
import com.powsybl.nad.layout.LayoutParameters;
import com.powsybl.nad.svg.LabelProvider;
import com.powsybl.nad.svg.NetworkTestFactory;
Expand Down Expand Up @@ -60,14 +56,6 @@ protected LabelProvider getLabelProvider(Network network) {
};
}

private LayoutFactory getLayoutFactory() {
return new BasicForceLayoutFactory();
}

private IdProvider getIdProvider() {
return new IntIdProvider();
}

private String getContentFile(Path svgFile) {
try (Stream<String> lines = Files.lines(svgFile)) {
return lines.collect(Collectors.joining("\n")) + "\n";
Expand All @@ -79,14 +67,12 @@ private String getContentFile(Path svgFile) {
@Test
public void testDrawSvg() {
Network network = NetworkTestFactory.createThreeVoltageLevelsFiveBuses();
NetworkAreaDiagram nad = new NetworkAreaDiagram(network, VoltageLevelFilter.NO_FILTER);

Path svgFile = fileSystem.getPath("nad-test.svg");
nad.draw(svgFile,
getSvgParameters(),
getLayoutParameters(),
getStyleProvider(network));

NetworkAreaDiagramConfiguration networkAreaDiagramConfiguration = new NetworkAreaDiagramConfigurationBuilder(network)
.withSvgParameters(getSvgParameters())
.withStyleProvider(getStyleProvider(network))
.build();
Copy link
Contributor

Choose a reason for hiding this comment

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

indenting isn't right

NetworkAreaDiagram.draw(network, svgFile, networkAreaDiagramConfiguration, VoltageLevelFilter.NO_FILTER);
assertEquals(toString("/dangling_line_connected.svg"), getContentFile(svgFile));
}
}
Loading