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

GetConnectables performance optimization #347

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,51 @@ public <C extends Connectable> Iterable<C> getConnectables(Class<C> clazz) {

@Override
public <C extends Connectable> Stream<C> getConnectableStream(Class<C> clazz) {
return index.getIdentifiables().stream().filter(clazz::isInstance).map(clazz::cast);
Objects.requireNonNull(clazz);
if (clazz == BusbarSection.class) {
return getBusbarSectionStream().map(c -> (C) c);
} else if (clazz == Generator.class) {
return getGeneratorStream().map(c -> (C) c);
} else if (clazz == Line.class) {
return getLineStream().map(c -> (C) c);
} else if (clazz == TwoWindingsTransformer.class) {
return getTwoWindingsTransformerStream().map(c -> (C) c);
} else if (clazz == ThreeWindingsTransformer.class) {
return getThreeWindingsTransformerStream().map(c -> (C) c);
} else if (clazz == Battery.class) {
return getBatteryStream().map(c -> (C) c);
} else if (clazz == DanglingLine.class) {
return getDanglingLineStream().map(c -> (C) c);
} else if (clazz == LccConverterStation.class) {
return getLccConverterStationStream().map(c -> (C) c);
} else if (clazz == VscConverterStation.class) {
return getVscConverterStationStream().map(c -> (C) c);
} else if (clazz == Load.class) {
return getLoadStream().map(c -> (C) c);
} else if (clazz == ShuntCompensator.class) {
return getShuntCompensatorStream().map(c -> (C) c);
} else if (clazz == StaticVarCompensator.class) {
return getStaticVarCompensatorStream().map(c -> (C) c);
} else {
Stream<C> s = Stream.empty();
for (Class<?> connectableClass : List.of(BusbarSection.class,
Generator.class,
Line.class,
TwoWindingsTransformer.class,
ThreeWindingsTransformer.class,
Battery.class,
DanglingLine.class,
LccConverterStation.class,
VscConverterStation.class,
Load.class,
ShuntCompensator.class,
StaticVarCompensator.class)) {
if (clazz.isAssignableFrom(connectableClass)) {
s = Stream.concat(s, getConnectableStream((Class<C>) connectableClass));
}
}
return s;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* 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.network.store.iidm.impl;

import com.powsybl.iidm.network.*;
import com.powsybl.iidm.network.test.EurostagTutorialExample1Factory;
import org.junit.Test;

import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
* @author Geoffroy Jamgotchian <geoffroy.jamgotchian at rte-france.com>
*/
public class GetConnectablesTest {

@Test
public void test() {
Network network = EurostagTutorialExample1Factory.create();
List<Generator> generators = network.getConnectableStream(Generator.class).toList();
assertEquals(1, generators.size());
List<Line> lines = network.getConnectableStream(Line.class).toList();
assertEquals(2, lines.size());
List<TwoWindingsTransformer> transformers = network.getConnectableStream(TwoWindingsTransformer.class).toList();
assertEquals(2, transformers.size());
List<Load> loads = network.getConnectableStream(Load.class).toList();
assertEquals(1, loads.size());
List<Injection> injections = network.getConnectableStream(Injection.class).toList();
assertEquals(2, injections.size());
List<HvdcConverterStation> converterStations = network.getConnectableStream(HvdcConverterStation.class).toList();
assertTrue(converterStations.isEmpty());
}
}
Loading