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

Support nested variable groups #537

Merged
merged 5 commits into from
Mar 27, 2020
Merged
Changes from 1 commit
Commits
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
39 changes: 39 additions & 0 deletions src/cpp/cse_config_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -732,13 +732,35 @@ std::vector<std::string> get_variable_group_variables(
{
auto groupIt = descriptions.find(connector);
if (groupIt == descriptions.end()) {
for (const auto& description : descriptions) {
if (description.second.variable_group_descriptions.size() != 0) {
std::unordered_map<std::string, variable_group_description> nestedDescriptions;
for (const auto& variableGroupDescr : description.second.variable_group_descriptions) {
nestedDescriptions.insert({variableGroupDescr.name, variableGroupDescr});
}
return get_variable_group_variables(nestedDescriptions, element, connector);
}
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't like to build up the nestedDescription map here. If the variable_goup_decsriptions in the variable_group_description struct (line 560) was a map instead of a vector, we could have this recursion without building up temporary maps.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Is there any reason to not make it a map?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't think so. Just didn't want to make changes in the parser now. Let's do this as a separate refactoring task for the ´cse_config_parser´

std::ostringstream oss;
oss << "Cannot find variable group description: " << element << ":" << connector;
throw std::out_of_range(oss.str());
}
return groupIt->second.variables;
}

std::vector<cse::variable_group_description> get_variable_groups(
const std::unordered_map<std::string, variable_group_description>& descriptions,
const std::string& connector)
{
auto groupIt = descriptions.find(connector);
if (groupIt == descriptions.end()) {
std::ostringstream oss;
oss << "Cannot find variable group for " << connector;
throw std::out_of_range(oss.str());
}
return groupIt->second.variable_group_descriptions;
}

void connect_variable_groups(
const std::vector<cse_config_parser::VariableConnection>& variableGroupConnections,
const std::unordered_map<std::string, slave_info>& slaves,
Expand All @@ -754,6 +776,23 @@ void connect_variable_groups(
const auto& variablesB =
get_variable_group_variables(emdB.variableGroups, connection.variableB.simulator, connection.variableB.name);

if (variablesA.size() == 0 || variablesB.size() == 0) {
// Variables for the connection cannot be found directly under any of the variable groups.
// The connection refers to a variableGroup with variableGroups.
const auto& variableGroupsA = get_variable_groups(emdA.variableGroups, connection.variableA.name);
const auto& variableGroupsB = get_variable_groups(emdB.variableGroups, connection.variableB.name);

std::vector<cse_config_parser::VariableConnection> nestedVariableGroupConnections;
// clang-format off
for (std::size_t i = 0; i < variableGroupsA.size(); ++i) {
nestedVariableGroupConnections.push_back({
{connection.variableA.simulator, variableGroupsA.at(i).name},
{connection.variableB.simulator, variableGroupsB.at(i).name}});
}
// clang-format on
connect_variable_groups(nestedVariableGroupConnections, slaves, execution, emds);
}

if (variablesA.size() != variablesB.size()) {
std::ostringstream oss;
oss << "Cannot create connection between variable groups. Variable group "
Expand Down