Skip to content

Commit 309a289

Browse files
committed
update xsd, added check to ensure uniqueness of powerbond names
1 parent e70d576 commit 309a289

File tree

4 files changed

+24
-10
lines changed

4 files changed

+24
-10
lines changed

data/xsd/OspSystemStructure.xsd

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
<xs:element name="OspSystemStructure">
1010
<xs:complexType>
1111
<xs:sequence>
12-
<xs:element name="StartTime" minOccurs="0" default="0.0" type="xs:double"/>
13-
<xs:element name="EndTime" minOccurs="0" type="xs:double"/>
14-
<xs:element name="BaseStepSize" minOccurs="0" type="xs:double"/>
15-
<xs:element name="Algorithm" minOccurs="0" default="fixedStep" type="xs:string"/>
12+
<xs:element name="StartTime" minOccurs="0" maxOccurs="1" default="0.0" type="xs:double"/>
13+
<xs:element name="EndTime" minOccurs="0" maxOccurs="1" type="xs:double"/>
14+
<xs:element name="BaseStepSize" minOccurs="0" maxOccurs="1" type="xs:double"/>
15+
<xs:element name="Algorithm" minOccurs="0" maxOccurs="1" default="fixedStep" type="xs:string"/>
1616
<xs:element name="Simulators" type="osp:simulators"/>
1717
<xs:element name="Functions" type="osp:functions" minOccurs="0"/>
1818
<xs:element name="Connections" type="osp:connections" minOccurs="0"/>
19-
<xs:element name="EccoConfiguration" type="osp:eccoconfiguration" minOccurs="0"/>
19+
<xs:element name="EccoConfiguration" type="osp:eccoconfiguration" minOccurs="0" maxOccurs="0"/>
2020
</xs:sequence>
2121
<xs:attribute name="version" type="xs:normalizedString" use="required" fixed="0.1">
2222
<xs:annotation>

include/cosim/system_structure.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,8 @@ class system_structure
269269
}
270270

271271
power_bond_map get_power_bonds() const noexcept;
272-
void add_power_bond(std::string name, power_bond pb);
272+
void add_power_bond(std::string name, power_bond pb);
273+
273274

274275
/**
275276
* Returns a list of the entities in the system.

src/cosim/osp_config_parser.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <xercesc/util/XMLString.hpp>
2424
#include <xercesc/util/XMLUni.hpp>
2525

26+
#include <algorithm>
2627
#include <ios>
2728
#include <iostream>
2829
#include <memory>
@@ -804,12 +805,14 @@ constexpr uint64_t operator"" _hash(const char* str, size_t len)
804805

805806
void add_power_bonds(const std::vector<osp_config_parser::PowerBondConnection>& pbConnections, system_structure& systemStructure)
806807
{
807-
std::set<std::string> powerbondNames;
808+
std::vector<std::string> powerBondNames;
809+
std::set<std::string> uniquePowerBondNames;
808810
for (const auto& pbConnection : pbConnections) {
809-
powerbondNames.insert(pbConnection.name);
811+
powerBondNames.emplace_back(pbConnection.name);
812+
uniquePowerBondNames.insert(pbConnection.name);
810813
}
811814

812-
for (const auto& pbName : powerbondNames) {
815+
for (const auto& pbName : uniquePowerBondNames) {
813816
std::vector<osp_config_parser::PowerBondConnection> aPowerBond;
814817
std::copy_if(pbConnections.begin(), pbConnections.end(), std::back_inserter(aPowerBond), [pbName](osp_config_parser::PowerBondConnection conn) {
815818
return pbName == conn.name;
@@ -888,6 +891,17 @@ void add_power_bonds(const std::vector<osp_config_parser::PowerBondConnection>&
888891
}
889892

890893
systemStructure.add_power_bond(pbName, powerbond);
894+
895+
// Check that the number of unique power bond names is equal to the number of power bonds. Otherwise, it is not possible to correctly connect the bonds.
896+
std::sort(powerBondNames.begin(), powerBondNames.end());
897+
auto uniqueCount = static_cast<int>(std::unique(powerBondNames.begin(), powerBondNames.end()) - powerBondNames.begin());
898+
auto numPowerBonds = static_cast<int>(systemStructure.get_power_bonds().size());
899+
900+
if (uniqueCount != systemStructure.get_power_bonds().size()) {
901+
std::ostringstream oss;
902+
oss << "The number of powerbonds (" << numPowerBonds << ") is not equal to the number of unique power bond names (" << uniqueCount << ") found in the configured system. Power bond names must be unique pr. bond, that is found on only and exactly the two VariableConnections that form the bond.";
903+
throw std::runtime_error(oss.str());
904+
}
891905
}
892906
}
893907

src/cosim/system_structure.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@ void system_structure::add_power_bond(std::string name, system_structure::power_
140140
powerBonds_.emplace(name, pb);
141141
}
142142

143-
144143
system_structure::entity_range system_structure::entities() const noexcept
145144
{
146145
return boost::adaptors::values(entities_);

0 commit comments

Comments
 (0)