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 all 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
63 changes: 63 additions & 0 deletions network-area-diagram/src/main/java/com/powsybl/nad/Config.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* 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/.
*/
package com.powsybl.nad;

import com.powsybl.iidm.network.Network;
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;

/**
*
* @author Sophie Frasnedo <sophie.frasnedo at rte-france.com>
*/
public class Config {

SvgParameters svgParameters;
LayoutParameters layoutParameters;
ConfigBuilder.StyleProviderFactory styleProviderFactory;
ConfigBuilder.LabelProviderFactory labelProviderFactory;
LayoutFactory layoutFactory;
ConfigBuilder.IdProviderFactory idProviderFactory;

public Config(SvgParameters svgParameters, LayoutParameters layoutParameters, ConfigBuilder.StyleProviderFactory styleProviderFactory, ConfigBuilder.LabelProviderFactory labelProviderFactory, LayoutFactory layoutFactory, ConfigBuilder.IdProviderFactory idProviderFactory) {
this.svgParameters = svgParameters;
this.layoutParameters = layoutParameters;
this.styleProviderFactory = styleProviderFactory;
this.labelProviderFactory = labelProviderFactory;
this.layoutFactory = layoutFactory;
this.idProviderFactory = idProviderFactory;
}

public SvgParameters getSvgParameters() {
return svgParameters;
}

public LayoutParameters getLayoutParameters() {
return layoutParameters;
}

public StyleProvider createStyleProvider(Network network) {
return styleProviderFactory.create(network);
}

public LabelProvider createLabelProvider(Network network) {
return labelProviderFactory.create(network, svgParameters);
}

public LayoutFactory getLayoutFactory() {
return layoutFactory;
}

public IdProvider createIdProvider() {
return idProviderFactory.create();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/**
* 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/.
*/
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;

/**
*
* @author Sophie Frasnedo <sophie.frasnedo at rte-france.com>
*/
public class ConfigBuilder {
SvgParameters svgParameters = new SvgParameters();
LayoutParameters layoutParameters = new LayoutParameters();
StyleProviderFactory styleProviderFactory = TopologicalStyleProvider::new;
LabelProviderFactory labelProviderFactory = DefaultLabelProvider::new;
LayoutFactory layoutFactory = new BasicForceLayoutFactory();
IdProviderFactory idProviderFactory = IntIdProvider::new;

@FunctionalInterface
public interface LabelProviderFactory {
LabelProvider create(Network network, SvgParameters svgParameters);
}

@FunctionalInterface
public interface IdProviderFactory {
IdProvider create();
}

@FunctionalInterface
public interface StyleProviderFactory {
StyleProvider create(Network network);
}

public ConfigBuilder() {
}

public ConfigBuilder withSvgParameters(SvgParameters svgParameters) {
this.svgParameters = svgParameters;
return this;
}

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

public ConfigBuilder withStyleProviderFactory(StyleProviderFactory styleProviderFactory) {
this.styleProviderFactory = styleProviderFactory;
return this;
}

public ConfigBuilder withLabelProviderFactory(LabelProviderFactory labelProviderFactory) {
this.labelProviderFactory = labelProviderFactory;
return this;
}

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

public ConfigBuilder withIdProviderFactory(IdProviderFactory idProviderFactory) {
this.idProviderFactory = idProviderFactory;
return this;
}

public Config build() {
return new Config(svgParameters, layoutParameters, styleProviderFactory, labelProviderFactory, layoutFactory, idProviderFactory);
}

}
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 ConfigBuilder().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 ConfigBuilder().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 ConfigBuilder().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 ConfigBuilder().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, Config config, 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 = config.getLayoutParameters();
SvgParameters svgParameters = config.getSvgParameters();
StyleProvider styleProvider = config.createStyleProvider(network);
LayoutFactory layoutFactory = config.getLayoutFactory();
IdProvider idProvider = config.createIdProvider();
LabelProvider labelProvider = config.createLabelProvider(network);
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 ConfigBuilder().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 ConfigBuilder().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 ConfigBuilder().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 ConfigBuilder().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, Config config, Predicate<VoltageLevel> voltageLevelFilter) {
Graph graph = new NetworkGraphBuilder(network, voltageLevelFilter, config.createIdProvider()).buildGraph();
config.getLayoutFactory().create().run(graph, config.getLayoutParameters());
new SvgWriter(config.getSvgParameters(), config.createStyleProvider(network), config.createLabelProvider(network)).writeSvg(graph, writer);
}

public String drawToString(SvgParameters svgParameters) {
public String drawToString(Network network, SvgParameters svgParameters) {
try (StringWriter writer = new StringWriter()) {
draw(writer, svgParameters);
Config config = new ConfigBuilder().withSvgParameters(svgParameters).build();
draw(network, writer, config, 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
@@ -1,3 +1,9 @@
/**
* Copyright (c) 2022, 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/.
*/
package com.powsybl.nad.layout;

import com.powsybl.nad.model.*;
Expand All @@ -6,6 +12,10 @@
import java.util.*;
import java.util.stream.Stream;

/**
*
* @author Florian Dupuy <florian.dupuy at rte-france.com>
*/
public abstract class AbstractLayout implements Layout {

private Map<String, Point> initialNodePositions = Collections.emptyMap();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
/**
* Copyright (c) 2022, 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/.
*/
package com.powsybl.nad.svg.metadata;

import javax.xml.stream.XMLStreamReader;

/**
*
* @author Florian Dupuy <florian.dupuy at rte-france.com>
*/
public class BusNodeMetadata extends AbstractMetadataItem {
private static final String ELEMENT_NAME = "busNode";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* Copyright (c) 2022, 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/.
*/
package com.powsybl.nad.utils.iidm;

import com.powsybl.iidm.network.*;
Expand All @@ -7,6 +13,11 @@

import java.util.Objects;

/**
*
* @author Florian Dupuy <florian.dupuy at rte-france.com>
* @author Luma Zamarreño <zamarrenolm at aia.es>
*/
public final class IidmUtils {

private IidmUtils() {
Expand Down
Loading