Skip to content

Commit

Permalink
New queries (#1121)
Browse files Browse the repository at this point in the history
* add some broker queries

* add some new queries and capabilities

* update the queries and add a test
[codecov]

* update the current_state query

* update formatting and docs

* fix test issue with certain error generation sequences

* fix codacy issue about unused variable

* Automated formatting of source files (#1122)

Co-authored-by: HELICS-bot <[email protected]>

* Apply suggestions from code review

Co-Authored-By: Ryan Mast <[email protected]>

* add enumeration of cascading queries for processing.

* Automated formatting of source files (#1128)

Co-authored-by: HELICS-bot <[email protected]>

* remove static qualifier from enumeration definition

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: HELICS-bot <[email protected]>
Co-authored-by: Ryan Mast <[email protected]>
  • Loading branch information
4 people authored Mar 7, 2020
1 parent 82839b1 commit 2374227
Show file tree
Hide file tree
Showing 12 changed files with 444 additions and 25 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Increased code coverage and additional bug fixes. The error propagation in HELI
- `helics_terminate_on_error` flag to escalate what would be a local error into a global one that will halt the co-simulation. This flag can be specified through the flag to federates or to brokers and cores through a command line option `--terminate_on_error`
- `addDependency` function was added to the C++ Federate API and shared library API, it can add a direct dependency between federates manually.
- A 32-bit Windows zip install archive for releases
- "global_time", "current_time", and "state" queries for brokers and cores, and "current_time" query for federates.
- Support for a 'helics-release-build' event trigger to the release build GitHub Actions workflow

### Deprecated
Expand Down
16 changes: 16 additions & 0 deletions docs/user-guide/queries.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ The following queries are defined for federates. Federates may specify a callba
+--------------------+------------------------------------------------------------+
| ``dependents`` | list of dependent objects [sv] |
+--------------------+------------------------------------------------------------+
| ``current_time`` | the current time of the federate [JSON] |
+--------------------+------------------------------------------------------------+
|``endpoint_filters``| data structure containing the filters on endpoints[JSON] |
+--------------------+------------------------------------------------------------+
| ``queries`` | list of available queries [sv] |
Expand All @@ -89,6 +91,8 @@ The following queries are defined for federates but can only be queried on the l
+---------------------------+------------------------------------------------------------+
| ``values`` | current values of all inputs [JSON] |
+---------------------------+------------------------------------------------------------+
| ``time`` | the current granted time [string] |
+---------------------------+------------------------------------------------------------+
```

Other strings may be defined for specific federates.
Expand Down Expand Up @@ -126,6 +130,12 @@ The following queries will be answered by a core.
+----------------------+-------------------------------------------------------------------------------------+
| ``federate_map`` | a Hierarchical map of the federates contained in a core [JSON] |
+----------------------+-------------------------------------------------------------------------------------+
| ``federation_state`` | a structure with the current known status of the brokers and federates [JSON] |
+----------------------+-------------------------------------------------------------------------------------+
| ``current_time`` | if a time is computed locally that time sequence is returned, otherwise #na [JSON] |
+----------------------+-------------------------------------------------------------------------------------+
| ``global_time`` | get a structure with the current time status of all the federates/cores [JSON] |
+----------------------+-------------------------------------------------------------------------------------+
| ``dependency_graph`` | a representation of the dependencies in the core and its contained federates [JSON] |
+----------------------+-------------------------------------------------------------------------------------+
|``endpoint_filters`` | data structure containing the filters on endpoints for the core[JSON] |
Expand Down Expand Up @@ -167,6 +177,12 @@ The Following queries will be answered by a broker.
+----------------------+-------------------------------------------------------------------------------------+
| ``counts`` | a simple count of the number of brokers, federates, and handles [JSON] |
+----------------------+-------------------------------------------------------------------------------------+
| ``federation_state`` | a structure with the current known status of the brokers and federates [JSON] |
+----------------------+-------------------------------------------------------------------------------------+
| ``current_time`` | if a time is computed locally that time sequence is returned, otherwise #na [string] |
+----------------------+-------------------------------------------------------------------------------------+
| ``global_time`` | get a structure with the current time status of all the federates/cores [JSON] |
+----------------------+-------------------------------------------------------------------------------------+
| ``federate_map`` | a Hierarchical map of the federates contained in a broker [JSON] |
+----------------------+-------------------------------------------------------------------------------------+
| ``dependency_graph`` | a representation of the dependencies in the broker and all contained members [JSON] |
Expand Down
2 changes: 2 additions & 0 deletions src/helics/application_api/Federate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1018,6 +1018,8 @@ std::string Federate::query(const std::string& queryStr)
} else {
res = "#unknown";
}
} else if (queryStr == "time") {
res = std::to_string(currentTime);
} else {
res = localQuery(queryStr);
}
Expand Down
68 changes: 65 additions & 3 deletions src/helics/core/CommonCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,22 @@ SPDX-License-Identifier: BSD-3-Clause
#include <functional>

namespace helics {

const std::string& state_string(operation_state state)
{
static const std::string c1{"connected"};
static const std::string estate{"error"};
static const std::string dis{"disconnected"};
switch (state) {
case operation_state::operating:
return c1;
case operation_state::disconnected:
return dis;
case operation_state::error:
default:
return estate;
}
}
// timeoutMon is a unique_ptr
CommonCore::CommonCore() noexcept: timeoutMon(new TimeoutMonitor) {}

Expand Down Expand Up @@ -2009,7 +2025,7 @@ std::string CommonCore::federateQuery(const FederateState* fed, const std::strin
return filteredEndpointQuery(fed);
}
if ((queryStr == "queries") || (queryStr == "available_queries")) {
return std::string("[exists;isinit;state;queries;filtered_endpoints;") +
return std::string("[exists;isinit;state;queries;filtered_endpoints;current_time;") +
fed->processQuery(queryStr) + "]";
}
return fed->processQuery(queryStr);
Expand All @@ -2019,7 +2035,7 @@ std::string CommonCore::quickCoreQueries(const std::string& queryStr) const
{
if ((queryStr == "queries") || (queryStr == "available_queries")) {
return "[isinit;isconnected;name;address;queries;address;federates;inputs;endpoints;filtered_endpoints;"
"publications;filters;federate_map;dependency_graph;dependencies;dependson;dependents]";
"publications;filters;federate_map;dependency_graph;dependencies;dependson;dependents;current_time;global_time;current_state]";
}
if (queryStr == "isconnected") {
return (isConnected()) ? "true" : "false";
Expand Down Expand Up @@ -2090,6 +2106,48 @@ std::string CommonCore::coreQuery(const std::string& queryStr) const
if (queryStr == "filtered_endpoints") {
return filteredEndpointQuery(nullptr);
}
if (queryStr == "current_time") {
if (hasTimeDependency) {
return timeCoord->printTimeStatus();
} else {
return "{}";
}
}
if (queryStr == "current_state") {
Json::Value base;
base["name"] = getIdentifier();
base["id"] = global_broker_id_local.baseValue();
base["state"] = brokerStateName(brokerState.load());
base["federates"] = Json::arrayValue;
for (auto& fed : loopFederates) {
Json::Value fedstate;
fedstate["state"] = state_string(fed.state);
fedstate["id"] = fed.fed->global_id.load().baseValue();
fedstate["name"] = fed.fed->getIdentifier();
base["federates"].append(std::move(fedstate));
}
return generateJsonString(base);
}

if (queryStr == "global_time") {
Json::Value block;
block["name"] = getIdentifier();
block["id"] = global_broker_id_local.baseValue();
block["parent"] = higher_broker_id.baseValue();
block["federates"] = Json::arrayValue;
if (hasTimeDependency) {
block["next_time"] = static_cast<double>(timeCoord->getNextTime());
}
for (auto fed : loopFederates) {
Json::Value fedBlock;
fedBlock["name"] = fed->getIdentifier();
fedBlock["id"] = fed->global_id.load().baseValue();
fedBlock["granted_time"] = static_cast<double>(fed->grantedTime());
fedBlock["send_time"] = static_cast<double>(fed->nextAllowedSendTime());
block["federates"].append(fedBlock);
}
return generateJsonString(block);
}
if (queryStr == "dependencies") {
Json::Value base;
base["name"] = getIdentifier();
Expand Down Expand Up @@ -2807,7 +2865,11 @@ void CommonCore::processCommand(ActionMessage&& command)
command.dest_id = root_broker_id;
transmit(parent_route_id, std::move(command));
break;
} else {
} else if (command.source_id.isValid()) {
auto fed = loopFederates.find(command.source_id);
if (fed != loopFederates.end()) {
fed->state = operation_state::error;
}
}
}
routeMessage(command);
Expand Down
3 changes: 3 additions & 0 deletions src/helics/core/CommonCore.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ enum class handle_type : char;
/** enumeration of possible operating conditions for a federate*/
enum class operation_state : std::uint8_t { operating = 0, error = 5, disconnected = 10 };

/** function to print string for the state*/
const std::string& state_string(operation_state state);

/** helper class for containing some wrapper around a federate for the core*/
class FedInfo {
public:
Expand Down
Loading

0 comments on commit 2374227

Please sign in to comment.