Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions launch/tier4_system_launch/launch/system.launch.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<arg name="mrm_handler_param_path"/>
<arg name="diagnostic_graph_aggregator_param_path"/>
<arg name="diagnostic_graph_aggregator_graph_path"/>
<arg name="diagnostic_graph_aggregator_graph_vars" default=""/>
<arg name="diag_struct_topic" default="/diagnostics_graph/struct"/>
<arg name="diag_status_topic" default="/diagnostics_graph/status"/>
<arg name="diag_reset_srv" default="/diagnostics_graph/reset"/>
Expand Down Expand Up @@ -129,6 +130,7 @@
<include file="$(find-pkg-share autoware_diagnostic_graph_aggregator)/launch/aggregator.launch.xml">
<arg name="param_file" value="$(var diagnostic_graph_aggregator_param_path)"/>
<arg name="graph_file" value="$(var diagnostic_graph_aggregator_graph_path)"/>
<arg name="graph_vars" value="$(var diagnostic_graph_aggregator_graph_vars)"/>
<arg name="~/struct" value="$(var diag_struct_topic)"/>
<arg name="~/status" value="$(var diag_status_topic)"/>
<arg name="~/reset" value="$(var diag_reset_srv)"/>
Expand Down
27 changes: 23 additions & 4 deletions system/autoware_diagnostic_graph_aggregator/doc/format/path.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,26 @@ The structure of the subgraph file should be [graph object](./graph.md).

File paths can contain substitutions like ROS 2 launch. The supported substitutions are as follows.

| Substitution | Description |
| ----------------------------- | -------------------------------- |
| `$(dirname)` | The path of this file directory. |
| `$(find-pkg-share <package>)` | The path of the package. |
| Substitution | Description |
| ----------------------------- | ----------------------------------------------- |
| `$(dirname)` | The path of this file directory. |
| `$(find-pkg-share <package>)` | The path of the package. |
| `$(var <name>)` | The value of the variable passed from the node. |

### Using `$(var <name>)`

The `$(var <name>)` substitution allows you to use variables in the graph file.
Variables are passed via the `graph_vars` parameter as a YAML map string.

Note: In launch XML, the value must be wrapped in single quotes to prevent YAML parsing.

```xml
<arg name="graph_vars" value="'{vehicle_id: vehicle1, config_dir: /path/to/config}'"/>
```

Then in your graph file:

```yaml
files:
- { path: $(var config_dir)/$(var vehicle_id)/module.yaml }
```
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<arg name="converter_param_file" default="$(find-pkg-share autoware_diagnostic_graph_aggregator)/config/converter.param.yaml"/>
<arg name="param_file" default="$(find-pkg-share autoware_diagnostic_graph_aggregator)/config/default.param.yaml"/>
<arg name="graph_file"/>
<arg name="graph_vars" default=""/>
<arg name="~/struct" default="/diagnostics_graph/struct"/>
<arg name="~/status" default="/diagnostics_graph/status"/>
<arg name="~/unknowns" default="/diagnostics_graph/unknowns"/>
Expand All @@ -11,6 +12,7 @@
<node pkg="autoware_diagnostic_graph_aggregator" exec="aggregator_node">
<param from="$(var param_file)"/>
<param name="graph_file" value="$(var graph_file)"/>
<param name="graph_vars" value="$(var graph_vars)"/>
<remap from="~/struct" to="$(var ~/struct)"/>
<remap from="~/status" to="$(var ~/status)"/>
<remap from="~/unknowns" to="$(var ~/unknowns)"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <memory>
#include <optional>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

Expand Down Expand Up @@ -50,6 +51,7 @@ struct FileContext
{
std::string file;
std::shared_ptr<std::unordered_set<std::string>> visited;
std::shared_ptr<std::unordered_map<std::string, std::string>> variables;
};

class Parser
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ struct UnknownSubstitution : public Exception
using Exception::Exception;
};

struct UnknownVariable : public Exception
{
using Exception::Exception;
};

struct UnknownLogic : public Exception
{
using Exception::Exception;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,14 @@
namespace autoware::diagnostic_graph_aggregator
{

ConfigLoader::ConfigLoader(std::shared_ptr<Logger> logger)
ConfigLoader::ConfigLoader(std::shared_ptr<Logger> logger) : ConfigLoader(logger, nullptr)
{
}

ConfigLoader::ConfigLoader(std::shared_ptr<Logger> logger, std::shared_ptr<VariablesMap> variables)
{
logger_ = logger ? logger : std::make_shared<DummyLogger>();
variables_ = variables;
}

ConfigLoader::~ConfigLoader()
Expand All @@ -50,7 +55,13 @@ ConfigLoader::~ConfigLoader()

GraphData ConfigLoader::Load(const std::string & path, std::shared_ptr<Logger> logger)
{
ConfigLoader loader(logger);
return Load(path, logger, nullptr);
}

GraphData ConfigLoader::Load(
const std::string & path, std::shared_ptr<Logger> logger, std::shared_ptr<VariablesMap> variables)
{
ConfigLoader loader(logger, variables);
loader.load(path);
return loader.take();
}
Expand Down Expand Up @@ -109,7 +120,8 @@ FileData * ConfigLoader::load_file(const FileContext & context, const std::strin
result->original_path = path;
result->resolved_path = resolved_path;
result->yaml = ConfigYaml::LoadFile(result->resolved_path);
result->files = load_files(FileContext{resolved_path, context.visited}, result->yaml);
result->files =
load_files(FileContext{resolved_path, context.visited, context.variables}, result->yaml);
return result;
}

Expand All @@ -134,7 +146,7 @@ BaseUnit * ConfigLoader::load_diag(ConfigYaml yaml, const std::string & name)
void ConfigLoader::load_file_tree(const std::string & path)
{
const auto visited = std::make_shared<std::unordered_set<std::string>>();
root_file_ = load_file(FileContext{"ROOT_FILE", visited}, path);
root_file_ = load_file(FileContext{"ROOT_FILE", visited, variables_}, path);
}

void ConfigLoader::make_node_units()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,28 @@

#include <memory>
#include <string>
#include <unordered_map>
#include <vector>

namespace autoware::diagnostic_graph_aggregator
{

using VariablesMap = std::unordered_map<std::string, std::string>;

class ConfigLoader
{
public:
explicit ConfigLoader(std::shared_ptr<Logger> logger);
ConfigLoader(std::shared_ptr<Logger> logger, std::shared_ptr<VariablesMap> variables);
~ConfigLoader();

// Overall execution for normal use
void load(const std::string & path);
GraphData take();
static GraphData Load(const std::string & path, std::shared_ptr<Logger> logger);
static GraphData Load(
const std::string & path, std::shared_ptr<Logger> logger,
std::shared_ptr<VariablesMap> variables);

// Step execution for debug tools.
void load_file_tree(const std::string & path);
Expand All @@ -61,6 +68,7 @@ class ConfigLoader
BaseUnit * load_diag(ConfigYaml yaml, const std::string & name);

std::shared_ptr<Logger> logger_;
std::shared_ptr<VariablesMap> variables_;
std::vector<std::unique_ptr<FileData>> files_;
std::vector<std::unique_ptr<TempUnit>> temps_;
std::vector<std::unique_ptr<LinkUnit>> links_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ std::string resolve(const std::string & substitution, const FileContext & contex
if (words.size() == 1 && words[0] == "dirname") {
return std::filesystem::path(context.file).parent_path();
}
if (words.size() == 2 && words[0] == "var") {
if (context.variables && context.variables->count(words[1])) {
return context.variables->at(words[1]);
}
throw UnknownVariable(words[1]);
}
throw UnknownSubstitution(substitution);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,17 @@ Graph::Graph(const std::string & path) : Graph(path, "", nullptr)
}

Graph::Graph(const std::string & path, const std::string & id, std::shared_ptr<Logger> logger)
: Graph(path, id, logger, nullptr)
{
}

Graph::Graph(
const std::string & path, const std::string & id, std::shared_ptr<Logger> logger,
std::shared_ptr<VariablesMap> variables)
{
id_ = id;

auto graph = ConfigLoader::Load(path, logger);
auto graph = ConfigLoader::Load(path, logger, variables);
alloc_nodes_ = std::move(graph.nodes);
alloc_diags_ = std::move(graph.diags);
alloc_ports_ = std::move(graph.ports);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,16 @@

namespace autoware::diagnostic_graph_aggregator
{
using VariablesMap = std::unordered_map<std::string, std::string>;

class Graph
{
public:
explicit Graph(const std::string & path);
Graph(const std::string & path, const std::string & id, std::shared_ptr<Logger> logger);
Graph(
const std::string & path, const std::string & id, std::shared_ptr<Logger> logger,
std::shared_ptr<VariablesMap> variables);
~Graph();
void update(const rclcpp::Time & stamp);
bool update(const rclcpp::Time & stamp, const DiagnosticArray & array);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@

#include "aggregator.hpp"

#include <yaml-cpp/yaml.h>

#include <memory>
#include <sstream>
#include <string>
#include <unordered_map>

namespace autoware::diagnostic_graph_aggregator
{
Expand All @@ -28,9 +31,20 @@ AggregatorNode::AggregatorNode(const rclcpp::NodeOptions & options) : Node("aggr
// Init diagnostics graph.
{
const auto graph_file = declare_parameter<std::string>("graph_file");

// Parse graph variables from YAML map format (e.g., "{key1: value1, key2: value2}").
auto variables = std::make_shared<std::unordered_map<std::string, std::string>>();
const auto vars_text = declare_parameter<std::string>("graph_vars", "");
if (!vars_text.empty()) {
const auto vars_yaml = YAML::Load(vars_text);
for (const auto & var : vars_yaml) {
variables->emplace(var.first.as<std::string>(), var.second.as<std::string>());
}
}

std::ostringstream id;
id << std::hex << stamp.nanoseconds();
graph_ = std::make_unique<Graph>(graph_file, id.str(), nullptr);
graph_ = std::make_unique<Graph>(graph_file, id.str(), nullptr, variables);
}

// Init plugins.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
files:
- { path: $(var unknown_variable) }
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
files:
- { path: $(var test_dir)/module.yaml }
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
units:
- path: /test/unit
type: ok
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@

#include <gtest/gtest.h>

#include <memory>
#include <string>
#include <unordered_map>

using namespace autoware::diagnostic_graph_aggregator; // NOLINT(build/namespaces)

TEST(GraphLoad, RootNotFound)
Expand Down Expand Up @@ -50,6 +54,18 @@ TEST(GraphLoad, UnknownSubstitution)
EXPECT_THROW(Graph(resource("graph-load/unknown-substitution.yaml")), UnknownSubstitution);
}

TEST(GraphLoad, UnknownVariable)
{
EXPECT_THROW(Graph(resource("graph-load/unknown-variable.yaml")), UnknownVariable);
}

TEST(GraphLoad, VariableSubstitution)
{
auto variables = std::make_shared<std::unordered_map<std::string, std::string>>();
variables->emplace("test_dir", resource("graph-load/variable-test").string());
EXPECT_NO_THROW(Graph(resource("graph-load/variable-substitution.yaml"), "", nullptr, variables));
}

TEST(GraphLoad, UnknownLogic)
{
EXPECT_THROW(Graph(resource("graph-load/unknown-logic-type.yaml")), UnknownLogic);
Expand Down
Loading