From 55927002b32ff465a1bbbadbb5685e60d9b7e66b Mon Sep 17 00:00:00 2001 From: eidekrist Date: Mon, 16 Sep 2019 15:53:36 +0200 Subject: [PATCH] #336 Added health check for connections from SSP --- src/cpp/algorithm.cpp | 2 +- src/cpp/execution.cpp | 4 ++-- src/cpp/ssp_parser.cpp | 40 ++++++++++++++++++++++++++++++++-------- 3 files changed, 35 insertions(+), 11 deletions(-) diff --git a/src/cpp/algorithm.cpp b/src/cpp/algorithm.cpp index cf7b01330..8d5190374 100644 --- a/src/cpp/algorithm.cpp +++ b/src/cpp/algorithm.cpp @@ -235,7 +235,7 @@ class fixed_step_algorithm::impl auto sim = simulators_.find(i); if (sim == simulators_.end()) { std::ostringstream oss; - oss << "Cannot find simulator with reference " << i; + oss << "Cannot find simulator with index " << i; throw std::out_of_range(oss.str()); } return sim->second; diff --git a/src/cpp/execution.cpp b/src/cpp/execution.cpp index f00d4edc1..e0b08b25e 100644 --- a/src/cpp/execution.cpp +++ b/src/cpp/execution.cpp @@ -281,10 +281,10 @@ class execution::impl [=](const auto& var) { return var.causality == causality && var.type == variable.type && var.reference == variable.reference; }); if (it == variables.end()) { std::ostringstream oss; - oss << "Cannot find variable with reference " << variable.reference + oss << "Problem adding connection: Cannot find variable with reference " << variable.reference << ", causality " << cse::to_text(causality) << " and type " << cse::to_text(variable.type) - << " for simulator with reference " << variable.simulator + << " for simulator with index " << variable.simulator << " and name " << simulators_.at(variable.simulator)->name(); throw std::out_of_range(oss.str()); } diff --git a/src/cpp/ssp_parser.cpp b/src/cpp/ssp_parser.cpp index f479a24a6..847c5f490 100644 --- a/src/cpp/ssp_parser.cpp +++ b/src/cpp/ssp_parser.cpp @@ -1,10 +1,10 @@ #include "cse/ssp_parser.hpp" #include "cse/algorithm.hpp" +#include "cse/error.hpp" #include "cse/exception.hpp" #include "cse/fmi/fmu.hpp" #include "cse/log/logger.hpp" -#include "cse/error.hpp" #include #include @@ -252,6 +252,25 @@ std::ostream& operator<<(std::ostream& os, streamer> sv) return os; } +cse::variable_id find_variable(std::map slaves, const std::string& element, const std::string& connector) +{ + auto slaveIt = slaves.find(element); + if (slaveIt == slaves.end()) { + std::ostringstream oss; + oss << "Problem adding connection: Cannot find slave: " << element; + throw std::out_of_range(oss.str()); + } + auto slave = slaveIt->second; + auto vdIt = slave.variables.find(connector); + if (vdIt == slave.variables.end()) { + std::ostringstream oss; + oss << "Problem adding connection: Cannot find variable: " << element << ":" << connector; + throw std::out_of_range(oss.str()); + } + auto variable = vdIt->second; + return {slave.index, variable.type, variable.reference}; +} + } // namespace @@ -312,16 +331,21 @@ std::pair load_ssp( } for (const auto& connection : parser.get_connections()) { - cse::variable_id output = {slaves[connection.startElement].index, - slaves[connection.startElement].variables[connection.startConnector].type, - slaves[connection.startElement].variables[connection.startConnector].reference}; - cse::variable_id input = {slaves[connection.endElement].index, - slaves[connection.endElement].variables[connection.endConnector].type, - slaves[connection.endElement].variables[connection.endConnector].reference}; + cse::variable_id output = find_variable(slaves, connection.startElement, connection.startConnector); + cse::variable_id input = find_variable(slaves, connection.endElement, connection.endConnector); const auto c = std::make_shared(output, input); - execution.add_connection(c); + try { + execution.add_connection(c); + } catch (const std::exception& e) { + BOOST_LOG_SEV(log::logger(), log::error) + << "Encountered error while adding connection from " + << connection.startElement << ":" << connection.startConnector << " to " + << connection.endElement << ":" << connection.endConnector + << ": " << e.what(); + throw; + } } return std::make_pair(std::move(execution), std::move(simulatorMap));