diff --git a/include/powsybl/iidm/Identifiable.hxx b/include/powsybl/iidm/Identifiable.hxx new file mode 100644 index 000000000..d391f5804 --- /dev/null +++ b/include/powsybl/iidm/Identifiable.hxx @@ -0,0 +1,26 @@ +/** + * Copyright (c) 2020, 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/. + */ + +#ifndef POWSYBL_IIDM_IDENTIFIABLE_HXX +#define POWSYBL_IIDM_IDENTIFIABLE_HXX + +#include + +namespace powsybl { + +namespace iidm { + +template +bool Identifiable::isInstanceOf(const Identifiable& identifiable) { + return stdcxx::isInstanceOf(identifiable); +} + +} // namespace iidm + +} // namespace powsybl + +#endif // POWSYBL_IIDM_IDENTIFIABLE_HXX diff --git a/include/powsybl/iidm/Network.hpp b/include/powsybl/iidm/Network.hpp index 9a39fb5d5..091b7f6aa 100644 --- a/include/powsybl/iidm/Network.hpp +++ b/include/powsybl/iidm/Network.hpp @@ -150,6 +150,15 @@ class Network : public Container, public VariantManagerHolder { const stdcxx::DateTime& getCaseDate() const; + template ::value>::type> + unsigned long getConnectableCount() const; + + template ::value>::type> + stdcxx::const_range getConnectables() const; + + template ::value>::type> + stdcxx::range getConnectables(); + unsigned long getCountryCount() const; const DanglingLine& getDanglingLine(const std::string& id) const; diff --git a/include/powsybl/iidm/Network.hxx b/include/powsybl/iidm/Network.hxx index 0e670ed71..babe0417b 100644 --- a/include/powsybl/iidm/Network.hxx +++ b/include/powsybl/iidm/Network.hxx @@ -10,6 +10,11 @@ #include +#include +#include + +#include + namespace powsybl { namespace iidm { @@ -29,6 +34,29 @@ T& Network::get(const std::string& id) { return m_networkIndex.get(id); } +template +unsigned long Network::getConnectableCount() const { + return boost::size(getConnectables()); +} + +template +stdcxx::const_range Network::getConnectables() const { + const auto& filter = [](const Identifiable& identifiable) { + return stdcxx::isInstanceOf(identifiable); + }; + + return m_networkIndex.getAll() | boost::adaptors::filtered(filter) | boost::adaptors::transformed(map); +} + +template +stdcxx::range Network::getConnectables() { + const auto& filter = [](const Identifiable& identifiable) { + return stdcxx::isInstanceOf(identifiable); + }; + + return m_networkIndex.getAll() | boost::adaptors::filtered(filter) | boost::adaptors::transformed(map); +} + template unsigned long Network::getObjectCount() const { return m_networkIndex.getObjectCount(); diff --git a/test/iidm/NetworkTest.cpp b/test/iidm/NetworkTest.cpp index 9357d2197..334e4259f 100644 --- a/test/iidm/NetworkTest.cpp +++ b/test/iidm/NetworkTest.cpp @@ -8,8 +8,11 @@ #include #include +#include #include +#include #include +#include #include #include #include @@ -365,6 +368,47 @@ BOOST_AUTO_TEST_CASE(country) { BOOST_CHECK_EQUAL(1, network.getCountryCount()); } +BOOST_AUTO_TEST_CASE(getConnectablesTest) { + Network network = createTestNetwork(); + const Network& cNetwork = network; + + BOOST_CHECK_EQUAL(2, boost::size(network.getConnectables())); + BOOST_CHECK_EQUAL(2, boost::size(cNetwork.getConnectables())); + BOOST_CHECK_EQUAL(2UL, network.getConnectableCount()); + + BOOST_CHECK_EQUAL(2, boost::size(network.getConnectables())); + BOOST_CHECK_EQUAL(2, boost::size(cNetwork.getConnectables())); + BOOST_CHECK_EQUAL(2UL, network.getConnectableCount()); + + BOOST_CHECK_EQUAL(1, boost::size(network.getConnectables())); + BOOST_CHECK_EQUAL(1, boost::size(cNetwork.getConnectables())); + BOOST_CHECK_EQUAL(1UL, network.getConnectableCount()); + + BOOST_CHECK_EQUAL(1, boost::size(network.getConnectables())); + BOOST_CHECK_EQUAL(1, boost::size(cNetwork.getConnectables())); + BOOST_CHECK_EQUAL(1UL, network.getConnectableCount()); + + BOOST_CHECK_EQUAL(1, boost::size(network.getConnectables())); + BOOST_CHECK_EQUAL(1, boost::size(cNetwork.getConnectables())); + BOOST_CHECK_EQUAL(1UL, network.getConnectableCount()); + + BOOST_CHECK_EQUAL(3, boost::size(network.getConnectables())); + BOOST_CHECK_EQUAL(3, boost::size(cNetwork.getConnectables())); + BOOST_CHECK_EQUAL(3UL, network.getConnectableCount()); + + BOOST_CHECK_EQUAL(6UL, boost::size(network.getConnectables())); + BOOST_CHECK_EQUAL(6UL, boost::size(cNetwork.getConnectables())); + BOOST_CHECK_EQUAL(6UL, network.getConnectableCount()); + + BOOST_CHECK_EQUAL(3UL, boost::size(network.getConnectables())); + BOOST_CHECK_EQUAL(3UL, boost::size(cNetwork.getConnectables())); + BOOST_CHECK_EQUAL(3UL, network.getConnectableCount()); + + BOOST_CHECK_EQUAL(0, boost::size(network.getConnectables())); + BOOST_CHECK_EQUAL(0, boost::size(cNetwork.getConnectables())); + BOOST_CHECK_EQUAL(0UL, network.getConnectableCount()); +} + BOOST_AUTO_TEST_CASE(branch) { Network network = createTestNetwork(); const Network& cNetwork = network;