From e907eba7241c0365fc7efde36de5437ef450c78a Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 13 Nov 2026 07:26:32 +0000 Subject: [PATCH] YAML behavior files, ivpico_bhv, and inline behaviors Behaviors can now come from .bhv.yml files, checked against a JSON Schema, or inline in HelmConfig.behaviors, as well as from .bhv files. - behaviors.proto: ivp.interface.BehaviorSet, what one behavior file holds. The parameters every behavior takes are typed fields; a behavior's own parameters are an ordered list. Values keep their source text. - The helm converts YAML and inline behaviors to .bhv text for the unchanged Populator. With any of them, every behavior file (in the order given) and then the inline sets go into one temporary file, and the Populator's warnings are rewritten to the original file, line and column, or the path in the configuration. Configs with only .bhv files keep pHelmIvP's path. - yaml-cpp is optional (enable_yaml, on when found; HAS_YAML). YAML is read with it so parameter order and repeats are kept, scalars are read as written, and every error names its line and column. Merges (<<) and x- keys allow shared settings. - ivpico_bhv: convert (YAML to .bhv), import (.bhv to YAML), check (builds the behaviors, reporting each problem where it was written) and schema. share/ivpico/bhv.schema.json is generated from the BehaviorSet message, committed and installed. - The alpha launch uses alpha.bhv.yml. Tests: unit tests of the reader, writers and the helm; every fixture .bhv round-trips through .bhv and YAML; replay__yaml replays every fixture from YAML; the committed schema must match the generated one, and imported fixtures and alpha.bhv.yml must validate against it; s1_alpha closed loops from alpha.bhv.yml and from inline behaviors. CI also builds without yaml-cpp on one job. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01VLcYK4e29B5FiPbDDJy5fp --- .github/workflows/ci.yml | 6 + CMakeLists.txt | 8 + README.md | 19 +- init.sh | 2 + launch/goby/alpha/config/alpha.bhv | 91 --- launch/goby/alpha/config/alpha.bhv.yml | 84 +++ launch/goby/alpha/config/helm_ivp.pb.cfg | 2 +- share/ivpico/bhv.schema.json | 89 +++ src/bin/CMakeLists.txt | 2 + src/bin/bhv/CMakeLists.txt | 6 + src/bin/bhv/ivpico_bhv.cpp | 201 +++++ src/lib/CMakeLists.txt | 15 +- src/lib/helm/behavior_file.cpp | 704 ++++++++++++++++++ src/lib/helm/behavior_file.h | 107 +++ src/lib/helm/behavior_yaml.cpp | 470 ++++++++++++ src/lib/helm/helm.cpp | 34 +- src/lib/interface/behaviors.proto | 81 ++ src/lib/interface/config.proto | 6 +- src/test/CMakeLists.txt | 34 +- src/test/bhv/check_schema.cmake | 7 + src/test/bhv/validate_schema.py | 64 ++ src/test/helm/test_behavior_file.cpp | 497 +++++++++++++ .../missions/goby/s1_alpha_inline/helm.pb.cfg | 78 ++ .../missions/goby/s1_alpha_inline/test.yml | 2 + .../missions/goby/s1_alpha_yaml/helm.pb.cfg | 6 + src/test/missions/goby/s1_alpha_yaml/test.yml | 2 + src/test/missions/replay.cpp | 66 +- 27 files changed, 2577 insertions(+), 106 deletions(-) delete mode 100644 launch/goby/alpha/config/alpha.bhv create mode 100644 launch/goby/alpha/config/alpha.bhv.yml create mode 100644 share/ivpico/bhv.schema.json create mode 100644 src/bin/bhv/CMakeLists.txt create mode 100644 src/bin/bhv/ivpico_bhv.cpp create mode 100644 src/lib/helm/behavior_file.cpp create mode 100644 src/lib/helm/behavior_file.h create mode 100644 src/lib/helm/behavior_yaml.cpp create mode 100644 src/lib/interface/behaviors.proto create mode 100644 src/test/bhv/check_schema.cmake create mode 100644 src/test/bhv/validate_schema.py create mode 100644 src/test/helm/test_behavior_file.cpp create mode 100644 src/test/missions/goby/s1_alpha_inline/helm.pb.cfg create mode 100644 src/test/missions/goby/s1_alpha_inline/test.yml create mode 100644 src/test/missions/goby/s1_alpha_yaml/helm.pb.cfg create mode 100644 src/test/missions/goby/s1_alpha_yaml/test.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a94fed3..4398eb5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -36,3 +36,9 @@ jobs: run: ./build.sh - name: Test run: ctest --test-dir build --output-on-failure --no-tests=error + - name: Build and test without yaml-cpp + if: matrix.ubuntu == '24.04' && matrix.arch == 'amd64' + run: | + BUILD_DIR=build-noyaml ./build.sh -Denable_yaml=OFF + ctest --test-dir build-noyaml --output-on-failure --no-tests=error \ + -R '^(helm|behavior_file|bhv_schema|closed_loop_s1_alpha_inline)$' diff --git a/CMakeLists.txt b/CMakeLists.txt index ca7663b..75767ec 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,6 +30,14 @@ elseif(GOBY_moos_FOUND) find_package(MOOS 10 QUIET) endif() +# YAML behavior files (.bhv.yml); without it, behaviors come from .bhv files or the helm config +find_package(yaml-cpp QUIET) +option(enable_yaml "Read YAML behavior files (.bhv.yml)" ${yaml-cpp_FOUND}) +if(enable_yaml AND NOT yaml-cpp_FOUND) + message(FATAL_ERROR "enable_yaml is ON but yaml-cpp wasn't found (apt install libyaml-cpp-dev)") +endif() +message(STATUS "YAML behavior files (enable_yaml): ${enable_yaml}") + include(CTest) add_subdirectory(src) diff --git a/README.md b/README.md index 4a9b6ae..329e925 100644 --- a/README.md +++ b/README.md @@ -12,12 +12,14 @@ One way to think of this project is as a generator for the equivalent of the MOO - src: Source code - lib: Library source - interface: Interface messages (Protobuf) - - helm: The helm, pHelmIvP without MOOS (`ivpico::helm::Helm`) + - helm: The helm, pHelmIvP without MOOS (`ivpico::helm::Helm`), and behavior files - bin: Binary source + - bhv: `ivpico_bhv`, for behavior files (`.bhv`, `.bhv.yml`) - goby: Goby middleware app (`goby_helm_ivp`) - ros: ROS middleware node - test: Tests, including replays of MOOS-IvP missions (`test/missions`) - launch: Example missions +- share/ivpico: The JSON Schema of `.bhv.yml` files (`bhv.schema.json`) ## Building @@ -30,6 +32,8 @@ One way to think of this project is as a generator for the equivalent of the MOO All of these come from [Open Ocean Software](https://github.com/openocean-software/openocean-build)'s continuous builds, with its releases for what they depend on (DCCL, MOOS, ...), at apt.oceansoft.org. `init.sh` adds both if apt can't install the packages already. +yaml-cpp (`libyaml-cpp-dev`) is optional: without it, behaviors come only from `.bhv` files or the helm configuration (`-Denable_yaml=OFF` leaves it out). + `build.sh` then builds IvPico in `build/` and installs it into `build/usr`. ``` @@ -49,6 +53,19 @@ ctest --test-dir build - Field levels (CORE, ADVANCED, DEVELOPER) and what each adds to `Status` and `HelmConfig`. - Differences from pHelmIvP: the MOOSDB echo, rejournal, standby helm. +## Behavior files + +*Outline, to be written:* + +- Three ways to give the helm its behaviors (`HelmConfig`): + - `.bhv` files, MOOS-IvP's own format and the reference; + - `.bhv.yml` files: the same content as YAML, checked against `share/ivpico/bhv.schema.json`; + - `behaviors`: the same structure inline in the helm configuration. +- How each is read: everything is converted to `.bhv` text for MOOS-IvP's Populator; with any YAML or inline behaviors, files are read in the order given, not sorted. +- The `.bhv.yml` format: `initialize`, `initialize_deferred`, `modes`, `behaviors`; common parameters as keys, the behavior's own under `params`; a list where a parameter repeats; `x-` keys and merges (`<<: *name`) for shared settings (merges are shallow, so a behavior's `params` replaces a merged `params`). Example: `launch/goby/alpha/config/alpha.bhv.yml`. +- `ivpico_bhv`: `convert` (YAML to `.bhv`), `import` (`.bhv` to YAML), `check` (builds the behaviors and reports problems at their YAML line), `schema`. +- Editor setup: the `# yaml-language-server: $schema=...` line `import` writes. + ## Goby app *This section was written by Claude.* diff --git a/init.sh b/init.sh index 71575fb..3b403ad 100755 --- a/init.sh +++ b/init.sh @@ -35,6 +35,8 @@ apt_install() { # xz-utils: the replay tests' recorded missions are compressed packages=(cmake ninja-build g++ git ca-certificates protobuf-compiler libprotobuf-dev xz-utils) +# optional: YAML behavior files (.bhv.yml), and the test that validates them against the schema +packages+=(libyaml-cpp-dev python3-jsonschema python3-yaml) $SUDO apt-get update apt_install "${packages[@]}" diff --git a/launch/goby/alpha/config/alpha.bhv b/launch/goby/alpha/config/alpha.bhv deleted file mode 100644 index 7847455..0000000 --- a/launch/goby/alpha/config/alpha.bhv +++ /dev/null @@ -1,91 +0,0 @@ -//-------- FILE: alpha.bhv ------------- - -initialize DEPLOY = false -initialize RETURN = false - -//---------------------------------------------- -Behavior = BHV_Waypoint -{ - name = waypt_survey - pwt = 100 - condition = RETURN = false - condition = DEPLOY = true - endflag = RETURN = true - - endflag = SUMMARY=speed=$[SPEED],time=$[DUR_RUN_TIME] - - configflag = CRUISE_SPD = $[SPEED] - - activeflag = INFO=$[OWNSHIP] - activeflag = INFO=$[BHVNAME] - activeflag = INFO=$[BHVTYPE] - - cycleflag = CINFO=$[OSX],$[OSY] - - wptflag = PREV=$(PX),$(PY) - wptflag = NEXT=$(NX),$(NY) - wptflag = TEST=$(X),$(Y) - wptflag = OSPOS=$(OSX),$(OSY) - wptflag_on_start = true - - updates = WPT_UPDATE - perpetual = true - - speed_alt = 1.2 - use_alt_speed = true - lead = 8 - lead_damper = 1 - lead_to_start = true - speed = 3 // meters per second - capture_line = true - capture_radius = 5.0 - slip_radius = 15.0 - efficiency_measure = all - polygon = 60,-40:60,-160:150,-160:180,-100:150,-40 - - order = normal - repeat = 0 - - visual_hints = nextpt_color=yellow - visual_hints = nextpt_vertex_size=8 - visual_hints = nextpt_lcolor=gray70 - visual_hints = vertex_color=dodger_blue, edge_color=white - visual_hints = vertex_size=5, edge_size=1 -} - -//---------------------------------------------- -Behavior=BHV_Waypoint -{ - name = waypt_return - pwt = 100 - condition = RETURN = true - condition = DEPLOY = true - perpetual = true - updates = RETURN_UPDATE - endflag = RETURN = false - endflag = DEPLOY = false - endflag = MISSION = complete - - speed = 2.0 - capture_radius = 2.0 - slip_radius = 8.0 - points = 0,-20 - } - -//---------------------------------------------- -Behavior=BHV_ConstantSpeed -{ - name = const_speed - pwt = 200 - condition = SPD=true - condition = DEPLOY = true - perpetual = true - updates = SPEED_UPDATE - endflag = SPD = false - - speed = 0.5 - duration = 10 - duration_reset = CONST_SPD_RESET=true - -} - diff --git a/launch/goby/alpha/config/alpha.bhv.yml b/launch/goby/alpha/config/alpha.bhv.yml new file mode 100644 index 0000000..c6fc6c7 --- /dev/null +++ b/launch/goby/alpha/config/alpha.bhv.yml @@ -0,0 +1,84 @@ +# yaml-language-server: $schema=https://raw.githubusercontent.com/GobySoft/IvPico/main/share/ivpico/bhv.schema.json +# MOOS-IvP's alpha mission: a survey, then a return to 0,-20, as alpha.bhv in +# ivp/missions/s1_alpha. ivpico_bhv convert writes it as a .bhv file. + +initialize: + DEPLOY: false + RETURN: false + +behaviors: + - type: BHV_Waypoint + name: waypt_survey + pwt: 100 + condition: + - RETURN = false + - DEPLOY = true + updates: WPT_UPDATE + perpetual: true + endflag: + - RETURN = true + - SUMMARY=speed=$[SPEED],time=$[DUR_RUN_TIME] + activeflag: + - INFO=$[OWNSHIP] + - INFO=$[BHVNAME] + - INFO=$[BHVTYPE] + configflag: CRUISE_SPD = $[SPEED] + params: + cycleflag: CINFO=$[OSX],$[OSY] + wptflag: + - PREV=$(PX),$(PY) + - NEXT=$(NX),$(NY) + - TEST=$(X),$(Y) + - OSPOS=$(OSX),$(OSY) + wptflag_on_start: true + speed_alt: 1.2 + use_alt_speed: true + lead: 8 + lead_damper: 1 + lead_to_start: true + speed: 3 + capture_line: true + capture_radius: 5.0 + slip_radius: 15.0 + efficiency_measure: all + polygon: 60,-40:60,-160:150,-160:180,-100:150,-40 + order: normal + repeat: 0 + visual_hints: + - nextpt_color=yellow + - nextpt_vertex_size=8 + - nextpt_lcolor=gray70 + - vertex_color=dodger_blue, edge_color=white + - vertex_size=5, edge_size=1 + + - type: BHV_Waypoint + name: waypt_return + pwt: 100 + condition: + - RETURN = true + - DEPLOY = true + updates: RETURN_UPDATE + perpetual: true + endflag: + - RETURN = false + - DEPLOY = false + - MISSION = complete + params: + speed: 2.0 + capture_radius: 2.0 + slip_radius: 8.0 + points: 0,-20 + + - type: BHV_ConstantSpeed + name: const_speed + pwt: 200 + condition: + - SPD=true + - DEPLOY = true + updates: SPEED_UPDATE + duration: 10 + duration_reset: CONST_SPD_RESET=true + perpetual: true + endflag: SPD = false + params: + speed: 0.5 diff --git a/launch/goby/alpha/config/helm_ivp.pb.cfg b/launch/goby/alpha/config/helm_ivp.pb.cfg index 8d890a1..aba85e4 100644 --- a/launch/goby/alpha/config/helm_ivp.pb.cfg +++ b/launch/goby/alpha/config/helm_ivp.pb.cfg @@ -16,7 +16,7 @@ interprocess { } helm { - behavior_file: "config/alpha.bhv" + behavior_file: "config/alpha.bhv.yml" domain { name: "course" low: 0 high: 359 points: 360 } domain { name: "speed" low: 0 high: 4 points: 41 } } diff --git a/share/ivpico/bhv.schema.json b/share/ivpico/bhv.schema.json new file mode 100644 index 0000000..9f5844b --- /dev/null +++ b/share/ivpico/bhv.schema.json @@ -0,0 +1,89 @@ +{ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "$id": "https://github.com/GobySoft/IvPico/blob/main/share/ivpico/bhv.schema.json", + "title": "IvPico behavior file (.bhv.yml)", + "description": "MOOS-IvP behaviors, modes and initial variables, as a .bhv file holds them", + "type": "object", + "additionalProperties": false, + "patternProperties": { + "^x-": { "description": "Ignored: a place for anchors to merge elsewhere" } + }, + "properties": { + "initialize": { + "description": "Variables posted when the helm starts (initialize)", + "$ref": "#/$defs/variables" + }, + "initialize_deferred": { + "description": "Variables posted unless another app has set them (initialize_)", + "$ref": "#/$defs/variables" + }, + "modes": { + "description": "Hierarchical modes (set), in order", + "type": "array", + "items": { "$ref": "#/$defs/mode" } + }, + "behaviors": { + "type": "array", + "items": { "$ref": "#/$defs/behavior" } + } + }, + "$defs": { + "value": { "type": ["string", "number", "boolean"] }, + "values": { + "oneOf": [ + { "$ref": "#/$defs/value" }, + { "type": "array", "items": { "$ref": "#/$defs/value" } } + ] + }, + "variables": { + "type": "object", + "propertyNames": { "pattern": "^[^\\s=,{}]+$" }, + "additionalProperties": { "$ref": "#/$defs/value" } + }, + "mode": { + "type": "object", + "additionalProperties": false, + "required": ["set"], + "properties": { + "set": { "description": "MODE_VAR = value", "type": "string", "pattern": "\\S\\s*=\\s*\\S" }, + "condition": { "$ref": "#/$defs/values" }, + "else": { "$ref": "#/$defs/value" } + } + }, + "behavior": { + "type": "object", + "additionalProperties": false, + "required": ["type"], + "properties": { + "type": { "type": "string", "pattern": "^\\S+$" }, + "name": { "$ref": "#/$defs/value" }, + "pwt": { "$ref": "#/$defs/value" }, + "condition": { "$ref": "#/$defs/values" }, + "updates": { "$ref": "#/$defs/value" }, + "templating": { "$ref": "#/$defs/value" }, + "duration": { "$ref": "#/$defs/value" }, + "duration_status": { "$ref": "#/$defs/value" }, + "duration_reset": { "$ref": "#/$defs/value" }, + "duration_idle_decay": { "$ref": "#/$defs/value" }, + "perpetual": { "$ref": "#/$defs/value" }, + "nostarve": { "$ref": "#/$defs/values" }, + "post_mapping": { "$ref": "#/$defs/values" }, + "comms_policy": { "$ref": "#/$defs/value" }, + "endflag": { "$ref": "#/$defs/values" }, + "runflag": { "$ref": "#/$defs/values" }, + "runxflag": { "$ref": "#/$defs/values" }, + "activeflag": { "$ref": "#/$defs/values" }, + "inactiveflag": { "$ref": "#/$defs/values" }, + "idleflag": { "$ref": "#/$defs/values" }, + "spawnflag": { "$ref": "#/$defs/values" }, + "spawnxflag": { "$ref": "#/$defs/values" }, + "configflag": { "$ref": "#/$defs/values" }, + "params": { + "description": "The behavior's own parameters, in order; a list for a parameter given more than once", + "type": "object", + "additionalProperties": { "$ref": "#/$defs/values" } + } + } + } + } +} diff --git a/src/bin/CMakeLists.txt b/src/bin/CMakeLists.txt index f162b61..85f9804 100644 --- a/src/bin/CMakeLists.txt +++ b/src/bin/CMakeLists.txt @@ -1,3 +1,5 @@ +add_subdirectory(bhv) + if(GOBY_FOUND) add_subdirectory(goby) endif() diff --git a/src/bin/bhv/CMakeLists.txt b/src/bin/bhv/CMakeLists.txt new file mode 100644 index 0000000..39231dc --- /dev/null +++ b/src/bin/bhv/CMakeLists.txt @@ -0,0 +1,6 @@ +add_executable(ivpico_bhv ivpico_bhv.cpp) +target_include_directories(ivpico_bhv PRIVATE ${PROJECT_SOURCE_DIR}/src/lib) +target_link_libraries(ivpico_bhv PRIVATE ivpico_helm) +install(TARGETS ivpico_bhv RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) +install(FILES ${PROJECT_SOURCE_DIR}/share/ivpico/bhv.schema.json + DESTINATION ${CMAKE_INSTALL_DATADIR}/ivpico) diff --git a/src/bin/bhv/ivpico_bhv.cpp b/src/bin/bhv/ivpico_bhv.cpp new file mode 100644 index 0000000..d2aa459 --- /dev/null +++ b/src/bin/bhv/ivpico_bhv.cpp @@ -0,0 +1,201 @@ +// Copyright 2026: +// GobySoft, LLC +// +// This file is part of IvPico. +// +// IvPico is free software: you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the Free Software +// Foundation, either version 2 of the License, or (at your option) any later +// version. +// +// IvPico is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with IvPico. If not, see . + +#include +#include +#include +#include +#include + +#include "helm/behavior_file.h" +#include "helm/helm.h" + +namespace +{ +const char* usage = R"(Usage: + ivpico_bhv convert [-o ] YAML to the official .bhv + ivpico_bhv import [-o ] .bhv to YAML (comments aren't kept) + ivpico_bhv check [--domain name:low:high:points]... + builds the behaviors and reports problems + ivpico_bhv schema the JSON Schema of .bhv.yml files +)"; + +const char* schema_url = + "https://raw.githubusercontent.com/GobySoft/IvPico/main/share/ivpico/bhv.schema.json"; + +int fail(const std::string& msg) +{ + std::cerr << msg << "\n"; + return 1; +} + +bool read_file(const std::string& file, std::string& text) +{ + std::ifstream in(file); + if (!in) + return false; + std::stringstream s; + s << in.rdbuf(); + text = s.str(); + return true; +} + +int write_output(const std::string& text, const std::string& output) +{ + if (output.empty()) + { + std::cout << text; + return 0; + } + std::ofstream out(output); + out << text; + return out ? 0 : fail("Couldn't write " + output); +} + +int report(const std::vector& errors) +{ + for (const auto& e : errors) std::cerr << e << "\n"; + return errors.empty() ? 0 : 1; +} + +int convert(const std::string& file, const std::string& output) +{ +#ifdef HAS_YAML + ivp::interface::HelmConfig cfg; + cfg.add_behavior_file(file); + std::vector errors; + const auto combined = ivpico::helm::combine_behaviors(cfg, errors); + if (!errors.empty()) + return report(errors); + return write_output(combined.text, output); +#else + (void)file, (void)output; + return fail("ivpico_bhv was built without yaml-cpp (enable_yaml), so it can't read YAML"); +#endif +} + +int import(const std::string& file, const std::string& output) +{ +#ifdef HAS_YAML + std::string text; + if (!read_file(file, text)) + return fail("Couldn't read " + file); + std::vector errors; + const auto set = ivpico::helm::read_bhv(text, file, errors); + if (!errors.empty()) + return report(errors); + return write_output(std::string("# yaml-language-server: $schema=") + schema_url + "\n" + + ivpico::helm::yaml_text(set), + output); +#else + (void)file, (void)output; + return fail("ivpico_bhv was built without yaml-cpp (enable_yaml), so it can't write YAML"); +#endif +} + +int check(const std::string& file, const std::vector& domains) +{ + ivp::interface::HelmConfig cfg; + cfg.add_behavior_file(file); + cfg.set_ownship("ownship"); + for (const auto& d : domains) + { + std::vector parts; + std::stringstream s(d); + for (std::string part; std::getline(s, part, ':');) parts.push_back(part); + auto& domain = *cfg.add_domain(); + try + { + if (parts.size() != 4) + throw std::invalid_argument(d); + domain.set_name(parts[0]); + domain.set_low(std::stod(parts[1])); + domain.set_high(std::stod(parts[2])); + domain.set_points(std::stoul(parts[3])); + } + catch (const std::exception&) + { + return fail("--domain takes name:low:high:points, e.g. course:0:359:360"); + } + } + if (domains.empty()) + { + for (const auto& [name, low, high, points] : + std::vector>{ + {"course", 0, 359, 360}, {"speed", 0, 5, 26}, {"depth", 0, 500, 501}}) + { + auto& domain = *cfg.add_domain(); + domain.set_name(name); + domain.set_low(low); + domain.set_high(high); + domain.set_points(points); + } + } + + // MOOS-IvP's libraries narrate their work on stdout and stderr + std::ostringstream quiet; + auto* out = std::cout.rdbuf(quiet.rdbuf()); + auto* err = std::cerr.rdbuf(quiet.rdbuf()); + std::vector warnings; + { + ivpico::helm::Helm helm(cfg); + warnings = helm.config_warnings(); + } + std::cout.rdbuf(out); + std::cerr.rdbuf(err); + + if (!warnings.empty()) + return report(warnings); + std::cout << file << ": OK\n"; + return 0; +} +} // namespace + +int main(int argc, char* argv[]) +{ + std::vector args(argv + 1, argv + argc); + if (args.empty() || args[0] == "-h" || args[0] == "--help") + { + std::cout << usage; + return args.empty() ? 1 : 0; + } + + const std::string command = args[0]; + std::string input, output; + std::vector domains; + for (std::size_t i = 1; i < args.size(); ++i) + { + if ((args[i] == "-o" || args[i] == "--domain") && i + 1 < args.size()) + (args[i] == "-o" ? output : domains.emplace_back()) = args[i + 1], ++i; + else if (input.empty() && args[i].rfind("-", 0) != 0) + input = args[i]; + else + return fail(std::string("Unexpected argument: ") + args[i] + "\n" + usage); + } + + if (command == "schema" && input.empty()) + return write_output(ivpico::helm::yaml_schema(), output); + if (input.empty()) + return fail(usage); + if (command == "convert") + return convert(input, output); + if (command == "import") + return import(input, output); + if (command == "check") + return check(input, domains); + return fail("Unknown command: " + command + "\n" + usage); +} diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt index 10ad4df..21e0b0f 100644 --- a/src/lib/CMakeLists.txt +++ b/src/lib/CMakeLists.txt @@ -2,20 +2,31 @@ add_library(ivpico_interface) protobuf_generate( TARGET ivpico_interface LANGUAGE cpp - PROTOS interface/options.proto interface/ivp.proto interface/config.proto + PROTOS interface/options.proto interface/ivp.proto interface/behaviors.proto interface/config.proto IMPORT_DIRS ${CMAKE_CURRENT_SOURCE_DIR} ${openocean_messages_PROTO_DIR} PROTOC_OUT_DIR ${CMAKE_CURRENT_BINARY_DIR}) target_include_directories(ivpico_interface PUBLIC $) target_link_libraries(ivpico_interface PUBLIC openocean_messages::openocean_messages) add_library(ivpico_helm helm/helm.cpp helm/conversions.cpp helm/status_strings.cpp - helm/contact_manager.cpp helm/contacts/CMAlert.cpp helm/contacts/PlatformAlertRecord.cpp) + helm/contact_manager.cpp helm/contacts/CMAlert.cpp helm/contacts/PlatformAlertRecord.cpp + helm/behavior_file.cpp) target_include_directories(ivpico_helm PUBLIC $) target_link_libraries(ivpico_helm PUBLIC ivpico_interface PRIVATE IVP::helmivp IVP::geodaid IVP::behaviors IVP::contacts IVP::logic IVP::ivpbuild IVP::ivpsolve IVP::ivpcore IVP::geometry IVP::mbutil) +if(enable_yaml) + target_sources(ivpico_helm PRIVATE helm/behavior_yaml.cpp) + target_compile_definitions(ivpico_helm PUBLIC HAS_YAML) + if(TARGET yaml-cpp::yaml-cpp) + target_link_libraries(ivpico_helm PRIVATE yaml-cpp::yaml-cpp) + else() + target_link_libraries(ivpico_helm PRIVATE yaml-cpp) + endif() +endif() + install(TARGETS ivpico_interface ivpico_helm LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) diff --git a/src/lib/helm/behavior_file.cpp b/src/lib/helm/behavior_file.cpp new file mode 100644 index 0000000..72b891a --- /dev/null +++ b/src/lib/helm/behavior_file.cpp @@ -0,0 +1,704 @@ +// Copyright 2026: +// GobySoft, LLC +// +// This file is part of IvPico. +// +// IvPico is free software: you can redistribute it and/or modify it under the +// terms of the GNU Lesser General Public License as published by the Free Software +// Foundation, either version 2.1 of the License, or (at your option) any later +// version. +// +// IvPico is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with IvPico. If not, see . + +#include "behavior_file.h" + +#include +#include +#include +#include + +#include + +#include "LogicCondition.h" + +using ivp::interface::BehaviorSet; +using Behavior = ivp::interface::BehaviorSet::Behavior; +using google::protobuf::FieldDescriptor; + +namespace +{ +std::string strip(const std::string& s) +{ + const auto begin = s.find_first_not_of(" \t\r"); + if (begin == std::string::npos) + return ""; + return s.substr(begin, s.find_last_not_of(" \t\r") - begin + 1); +} + +std::string lower(std::string s) +{ + for (auto& c : s) c = static_cast(std::tolower(static_cast(c))); + return s; +} + +bool starts_with_nocase(const std::string& s, const std::string& prefix) +{ + return s.size() >= prefix.size() && lower(s.substr(0, prefix.size())) == prefix; +} + +bool has_white(const std::string& s) { return s.find_first_of(" \t") != std::string::npos; } + +std::string indexed(const std::string& base, int i) { return base + "[" + std::to_string(i) + "]"; } + +// The parameters every behavior takes: Behavior's fields other than type and param +bool is_common(const FieldDescriptor* f) { return f->name() != "type" && f->name() != "param"; } + +// MBUtils' parseStringQ: split on sep outside double quotes +std::vector split_quoted(const std::string& s, char sep) +{ + std::vector parts(1); + bool quoted = false; + for (char c : s) + { + if (c == '"') + quoted = !quoted; + if (c == sep && !quoted) + parts.emplace_back(); + else + parts.back() += c; + } + return parts; +} + +// Values the Populator would read differently than given +void check_value(const std::string& value, const std::string& path, + std::vector& errors) +{ + if (value.find('\n') != std::string::npos) + errors.push_back(path + ": a .bhv value can't span lines"); + if (value.find("//") != std::string::npos) + errors.push_back(path + ": .bhv reads \"//\" as the start of a comment"); + if (!value.empty() && value.back() == '\\') + errors.push_back(path + ": .bhv joins a line ending in \"\\\" to the next"); + const std::string s = strip(value); + if (!s.empty() && (s.front() == '{' || s.front() == '}' || s.back() == '{')) + errors.push_back(path + ": .bhv splits a brace at the start or end of a line"); +} + +void check_word(const std::string& word, const std::string& what, const std::string& path, + std::vector& errors) +{ + if (strip(word).empty() || has_white(strip(word)) || + word.find_first_of("={},") != std::string::npos) + errors.push_back(path + ": \"" + word + "\" isn't a valid " + what); +} + +// The state machine of Populator_BehaviorSet::handleLine +class BhvReader +{ + public: + BhvReader(BehaviorSet& set, const std::string& filename, ivpico::helm::SourceMap* map) + : set_(set), filename_(filename), map_(map) + { + } + + bool handle(std::string line, int line_num) + { + line_num_ = line_num; + const auto comment = line.find("//"); + if (comment != std::string::npos) + line.erase(comment); + line = strip(line); + + if (line.empty()) + { + if (mode_ == Mode::MiscDefinedIsh) + mode_ = Mode::Top; + else if (mode_ == Mode::SetDefinedIsh) + close_set(); + return true; + } + if (line == "{") + { + if (mode_ == Mode::Top) + mode_ = Mode::MiscDefining; + else if (mode_ == Mode::BhvDeclared) + mode_ = Mode::BhvDefining; + else if (mode_ == Mode::SetDeclared) + mode_ = Mode::SetDefining; + else + return false; + return true; + } + if (line == "}") + { + if (mode_ == Mode::MiscDefining) + mode_ = Mode::MiscDefinedIsh; + else if (mode_ == Mode::BhvDefining) + mode_ = Mode::Top; + else if (mode_ == Mode::SetDefining) + mode_ = Mode::SetDefinedIsh; + else + return false; + return true; + } + + if (mode_ == Mode::Top || mode_ == Mode::MiscDefinedIsh || mode_ == Mode::SetDefinedIsh) + { + if (starts_with_nocase(line, "initialize ") || starts_with_nocase(line, "initialize_ ")) + { + if (mode_ == Mode::SetDefinedIsh) + close_set(); + mode_ = Mode::Top; + const auto space = line.find(' '); + const bool deferred = line.substr(0, space) == "initialize_"; + for (const auto& item : split_quoted(line.substr(space + 1), ',')) + { + const std::string pair = strip(item); + const auto eq = pair.find('='); + const std::string left = strip(pair.substr(0, eq)); + const std::string right = + eq == std::string::npos ? "" : strip(pair.substr(eq + 1)); + if (left.empty() || has_white(left) || right.empty()) + return false; + auto& init = *set_.add_initialize(); + init.set_var(left); + init.set_value(right); + init.set_deferred(deferred); + note(indexed("initialize", set_.initialize_size() - 1)); + } + return true; + } + if (starts_with_nocase(line, "behavior")) + { + if (mode_ == Mode::SetDefinedIsh) + close_set(); + const auto eq = line.find('='); + set_.add_behavior()->set_type(eq == std::string::npos ? "" + : strip(line.substr(eq + 1))); + note(indexed("behavior", set_.behavior_size() - 1)); + mode_ = Mode::BhvDeclared; + return true; + } + if (starts_with_nocase(line, "set ")) + { + if (mode_ == Mode::SetDefinedIsh) + close_set(); + const std::string head = line.substr(4); + const auto eq = head.find('='); + const std::string var = strip(head.substr(0, eq)); + const std::string val = eq == std::string::npos ? "" : strip(head.substr(eq + 1)); + if (var.empty() || val.empty()) + return false; + pending_mode_ = BehaviorSet::Mode(); + pending_mode_.set_set(var + " = " + val); + pending_line_ = line_num_; + mode_ = Mode::SetDeclared; + return true; + } + } + + switch (mode_) + { + case Mode::MiscDefining: return true; + case Mode::SetDefinedIsh: + if (has_white(line)) + return false; + pending_mode_.set_else_value(line); + pending_else_line_ = line_num_; + close_set(); + return true; + case Mode::BhvDefining: add_config(line); return true; + case Mode::SetDefining: + { + LogicCondition condition; + condition.setAllowDoubleEquals(false); + if (!condition.setCondition(line)) + return false; + pending_mode_.add_condition(line); + pending_condition_lines_.push_back(line_num_); + return true; + } + default: return false; + } + } + + private: + enum class Mode + { + Top, + MiscDefining, + MiscDefinedIsh, + BhvDeclared, + BhvDefining, + SetDeclared, + SetDefining, + SetDefinedIsh + }; + + void note(const std::string& path, int line = 0) + { + if (map_) + (*map_)[path] = {filename_, line ? line : line_num_, 0}; + } + + void close_set() + { + const std::string path = indexed("mode", set_.mode_size()); + *set_.add_mode() = pending_mode_; + note(path, pending_line_); + for (std::size_t i = 0; i < pending_condition_lines_.size(); ++i) + note(indexed(path + ".condition", static_cast(i)), pending_condition_lines_[i]); + if (pending_mode_.has_else_value()) + note(path + ".else_value", pending_else_line_); + pending_condition_lines_.clear(); + mode_ = Mode::Top; + } + + void add_config(const std::string& line) + { + auto& bhv = *set_.mutable_behavior(set_.behavior_size() - 1); + const std::string base = indexed("behavior", set_.behavior_size() - 1); + const auto eq = line.find('='); + const std::string key = strip(line.substr(0, eq)); + const std::string value = eq == std::string::npos ? "" : strip(line.substr(eq + 1)); + + const auto* f = Behavior::descriptor()->FindFieldByName(lower(key)); + const auto* refl = Behavior::GetReflection(); + if (f && is_common(f) && f->is_repeated()) + { + refl->AddString(&bhv, f, value); + note(indexed(base + "." + f->name(), refl->FieldSize(bhv, f) - 1)); + } + // a common parameter given twice keeps both, as a param + else if (f && is_common(f) && !refl->HasField(bhv, f)) + { + refl->SetString(&bhv, f, value); + note(base + "." + f->name()); + } + else + { + auto& param = *bhv.add_param(); + param.set_key(key); + param.set_value(value); + note(indexed(base + ".param", bhv.param_size() - 1)); + } + } + + BehaviorSet& set_; + std::string filename_; + ivpico::helm::SourceMap* map_; + Mode mode_{Mode::Top}; + int line_num_{0}; + BehaviorSet::Mode pending_mode_; + int pending_line_{0}; + int pending_else_line_{0}; + std::vector pending_condition_lines_; +}; +} // namespace + +std::string ivpico::helm::SourceLocation::str() const +{ + std::string s = file; + if (line > 0) + s += ":" + std::to_string(line); + if (line > 0 && column > 0) + s += ":" + std::to_string(column); + return s; +} + +std::optional ivpico::helm::locate(const SourceMap& map, + std::string path) +{ + while (true) + { + auto it = map.find(path); + if (it != map.end()) + return it->second; + const auto cut = path.find_last_of(".["); + if (cut == std::string::npos || cut == 0) + return std::nullopt; + path.erase(cut); + } +} + +bool ivpico::helm::is_yaml_file(const std::string& filename) +{ + const auto dot = filename.rfind('.'); + if (dot == std::string::npos) + return false; + const std::string ext = lower(filename.substr(dot)); + return ext == ".yml" || ext == ".yaml"; +} + +std::vector ivpico::helm::bhv_lines(const BehaviorSet& set, + std::vector& errors) +{ + std::vector lines; + auto add = [&](const std::string& text, const std::string& path) + { lines.push_back({text, path}); }; + + for (int i = 0; i < set.initialize_size(); ++i) + { + const auto& init = set.initialize(i); + const std::string path = indexed("initialize", i); + check_word(init.var(), "variable name", path, errors); + check_value(init.value(), path, errors); + if (strip(init.value()).empty()) + errors.push_back(path + ": initialize needs a value"); + const auto& value = init.value(); + if (value.find(',') != std::string::npos && + !(value.size() > 1 && value.front() == '"' && value.back() == '"')) + errors.push_back(path + ": .bhv splits an initialize value at a comma unless it's " + "in double quotes"); + add(std::string(init.deferred() ? "initialize_ " : "initialize ") + init.var() + " = " + + init.value(), + path); + } + if (set.initialize_size()) + add("", ""); + + for (int i = 0; i < set.mode_size(); ++i) + { + const auto& mode = set.mode(i); + const std::string path = indexed("mode", i); + const auto eq = mode.set().find('='); + if (eq == std::string::npos || strip(mode.set().substr(0, eq)).empty() || + strip(mode.set().substr(eq + 1)).empty()) + errors.push_back(path + ": set needs \"MODE_VAR = value\""); + check_value(mode.set(), path, errors); + add("set " + mode.set() + " {", path); + for (int j = 0; j < mode.condition_size(); ++j) + { + const std::string cpath = indexed(path + ".condition", j); + check_value(mode.condition(j), cpath, errors); + add(" " + mode.condition(j), cpath); + } + if (mode.has_else_value()) + { + if (strip(mode.else_value()).empty() || has_white(strip(mode.else_value()))) + errors.push_back(path + ".else_value: the else value must be one word"); + check_value(mode.else_value(), path + ".else_value", errors); + add("} " + mode.else_value(), path + ".else_value"); + } + else + { + add("}", path); + } + add("", ""); + } + + const auto* refl = Behavior::GetReflection(); + for (int i = 0; i < set.behavior_size(); ++i) + { + const auto& bhv = set.behavior(i); + const std::string path = indexed("behavior", i); + check_word(bhv.type(), "behavior type", path, errors); + add("Behavior = " + bhv.type(), path); + add("{", path); + for (int f = 0; f < Behavior::descriptor()->field_count(); ++f) + { + const auto* field = Behavior::descriptor()->field(f); + if (!is_common(field)) + continue; + const std::string fpath = path + "." + field->name(); + if (field->is_repeated()) + { + for (int j = 0; j < refl->FieldSize(bhv, field); ++j) + { + const std::string value = refl->GetRepeatedString(bhv, field, j); + check_value(value, indexed(fpath, j), errors); + add(" " + field->name() + " = " + value, indexed(fpath, j)); + } + } + else if (refl->HasField(bhv, field)) + { + const std::string value = refl->GetString(bhv, field); + check_value(value, fpath, errors); + add(" " + field->name() + " = " + value, fpath); + } + } + for (int j = 0; j < bhv.param_size(); ++j) + { + const auto& param = bhv.param(j); + const std::string ppath = indexed(path + ".param", j); + if (strip(param.key()).empty() || param.key().find_first_of("=\n") != std::string::npos) + errors.push_back(ppath + ": \"" + param.key() + "\" isn't a valid parameter name"); + check_value(param.key(), ppath, errors); + check_value(param.value(), ppath, errors); + add(" " + param.key() + " = " + param.value(), ppath); + } + add("}", path); + add("", ""); + } + return lines; +} + +std::string ivpico::helm::bhv_text(const std::vector& lines) +{ + std::string text; + for (const auto& line : lines) text += line.text + "\n"; + return text; +} + +BehaviorSet ivpico::helm::read_bhv(const std::string& text, const std::string& filename, + std::vector& errors, SourceMap* map) +{ + BehaviorSet set; + BhvReader reader(set, filename, map); + + // lines ending in "\" are joined to the next, as fileBufferSlash does + std::istringstream in(text); + std::string raw; + int line_num = 0; + while (std::getline(in, raw)) + { + const int first_line = ++line_num; + std::string line = strip(raw); + while (!line.empty() && line.back() == '\\' && std::getline(in, raw)) + { + ++line_num; + line.pop_back(); + line += strip(raw); + } + + // Populator_BehaviorSet::populate's brace separation + const bool comment = line.rfind("//", 0) == 0 || line.rfind("#", 0) == 0; + std::string pre, post; + if (!comment) + { + if (line.size() > 1 && (line.front() == '{' || line.front() == '}')) + { + pre = line.substr(0, 1); + line.erase(0, 1); + } + if (line.size() > 1 && line.back() == '{') + { + post = "{"; + line.pop_back(); + } + } + const bool ok = (pre.empty() || reader.handle(pre, first_line)) && + reader.handle(line, first_line) && + (post.empty() || reader.handle(post, first_line)); + if (!ok) + { + errors.push_back(SourceLocation{filename, first_line, 0}.str() + + ": can't read the line \"" + strip(raw) + "\""); + break; + } + } + return set; +} + +std::string ivpico::helm::yaml_schema() +{ + std::string behavior_props = + R"json( "type": { "type": "string", "pattern": "^\\S+$" })json"; + for (int f = 0; f < Behavior::descriptor()->field_count(); ++f) + { + const auto* field = Behavior::descriptor()->field(f); + if (!is_common(field)) + continue; + behavior_props += ",\n \"" + field->name() + "\": { \"$ref\": \"#/$defs/" + + (field->is_repeated() ? "values" : "value") + "\" }"; + } + behavior_props += R"json(, + "params": { + "description": "The behavior's own parameters, in order; a list for a parameter given more than once", + "type": "object", + "additionalProperties": { "$ref": "#/$defs/values" } + })json"; + + return R"json({ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "$id": "https://github.com/GobySoft/IvPico/blob/main/share/ivpico/bhv.schema.json", + "title": "IvPico behavior file (.bhv.yml)", + "description": "MOOS-IvP behaviors, modes and initial variables, as a .bhv file holds them", + "type": "object", + "additionalProperties": false, + "patternProperties": { + "^x-": { "description": "Ignored: a place for anchors to merge elsewhere" } + }, + "properties": { + "initialize": { + "description": "Variables posted when the helm starts (initialize)", + "$ref": "#/$defs/variables" + }, + "initialize_deferred": { + "description": "Variables posted unless another app has set them (initialize_)", + "$ref": "#/$defs/variables" + }, + "modes": { + "description": "Hierarchical modes (set), in order", + "type": "array", + "items": { "$ref": "#/$defs/mode" } + }, + "behaviors": { + "type": "array", + "items": { "$ref": "#/$defs/behavior" } + } + }, + "$defs": { + "value": { "type": ["string", "number", "boolean"] }, + "values": { + "oneOf": [ + { "$ref": "#/$defs/value" }, + { "type": "array", "items": { "$ref": "#/$defs/value" } } + ] + }, + "variables": { + "type": "object", + "propertyNames": { "pattern": "^[^\\s=,{}]+$" }, + "additionalProperties": { "$ref": "#/$defs/value" } + }, + "mode": { + "type": "object", + "additionalProperties": false, + "required": ["set"], + "properties": { + "set": { "description": "MODE_VAR = value", "type": "string", "pattern": "\\S\\s*=\\s*\\S" }, + "condition": { "$ref": "#/$defs/values" }, + "else": { "$ref": "#/$defs/value" } + } + }, + "behavior": { + "type": "object", + "additionalProperties": false, + "required": ["type"], + "properties": { +)json" + behavior_props + + R"json( + } + } + } +} +)json"; +} + +bool ivpico::helm::needs_combining(const ivp::interface::HelmConfig& cfg) +{ + return cfg.behaviors_size() > 0 || + std::any_of(cfg.behavior_file().begin(), cfg.behavior_file().end(), is_yaml_file); +} + +ivpico::helm::CombinedBehaviors +ivpico::helm::combine_behaviors(const ivp::interface::HelmConfig& cfg, + std::vector& errors) +{ + CombinedBehaviors combined; + auto add_set = [&](const BehaviorSet& set, const std::string& prefix, const SourceMap* map) + { + std::vector set_errors; + const auto lines = bhv_lines(set, set_errors); + auto where = [&](const std::string& path) -> SourceLocation + { + if (map) + if (auto loc = locate(*map, path)) + return *loc; + return {prefix + (path.empty() ? "" : "." + path), 0, 0}; + }; + for (const auto& e : set_errors) + { + const auto colon = e.find(": "); + errors.push_back(where(e.substr(0, colon)).str() + e.substr(colon)); + } + for (const auto& line : lines) + { + combined.text += line.text + "\n"; + combined.line_locations.push_back(where(line.path)); + } + }; + + for (const auto& file : cfg.behavior_file()) + { + std::ifstream in(file); + if (!in) + { + errors.push_back("Could not find File: " + file); + continue; + } + std::stringstream text; + text << in.rdbuf(); + + if (!is_yaml_file(file)) + { + // lines ending in "\" are joined here, so line numbers stay the file's own + std::istringstream lines(text.str()); + std::string line; + int line_num = 0; + while (std::getline(lines, line)) + { + const int first_line = ++line_num; + std::string joined = strip(line); + while (!joined.empty() && joined.back() == '\\' && std::getline(lines, line)) + { + ++line_num; + joined.pop_back(); + joined += strip(line); + } + combined.text += joined + "\n"; + combined.line_locations.push_back({file, first_line, 0}); + } + combined.text += "\n"; + combined.line_locations.push_back({file, 0, 0}); + continue; + } +#ifdef HAS_YAML + SourceMap map; + if (auto set = read_yaml(text.str(), file, errors, &map)) + add_set(*set, file, &map); +#else + errors.push_back(file + ": this IvPico was built without yaml-cpp (enable_yaml), so it " + "can't read YAML behavior files"); +#endif + } + for (int i = 0; i < cfg.behaviors_size(); ++i) + add_set(cfg.behaviors(i), "behaviors[" + std::to_string(i) + "]", nullptr); + return combined; +} + +std::string ivpico::helm::relocate_warning(const std::string& warning, const std::string& path, + const CombinedBehaviors& combined) +{ + auto location = [&](unsigned long line) -> std::string + { + if (line >= 1 && line <= combined.line_locations.size()) + return combined.line_locations[line - 1].str(); + return "behaviors"; + }; + + // Populator_BehaviorSet: a line it can't read, numbered from 1 + const std::string problem = "Problem with line "; + const std::string in_file = " in the BehaviorSet file: " + path + " Line: "; + const auto in_file_pos = warning.find(in_file); + if (warning.rfind(problem, 0) == 0 && in_file_pos != std::string::npos) + { + const std::string number = warning.substr(problem.size(), in_file_pos - problem.size()); + if (!number.empty() && std::all_of(number.begin(), number.end(), ::isdigit)) + return location(std::stoul(number)) + ": can't read the line " + + warning.substr(in_file_pos + in_file.size()); + } + + // BehaviorSet: a parameter the behavior rejected, numbered from 0 + const std::string prefix = path + ": Line "; + if (warning.rfind(prefix, 0) == 0) + { + const std::string rest = warning.substr(prefix.size()); + const auto colon = rest.find(": "); + if (colon != std::string::npos && colon > 0 && + std::all_of(rest.begin(), rest.begin() + colon, ::isdigit)) + return location(std::stoul(rest.substr(0, colon)) + 1) + ": the behavior rejected \"" + + rest.substr(colon + 2) + "\""; + } + + std::string out = warning; + for (auto pos = out.find(path); pos != std::string::npos; pos = out.find(path)) + out.replace(pos, path.size(), "behaviors"); + return out; +} diff --git a/src/lib/helm/behavior_file.h b/src/lib/helm/behavior_file.h new file mode 100644 index 0000000..22b0cfb --- /dev/null +++ b/src/lib/helm/behavior_file.h @@ -0,0 +1,107 @@ +// Copyright 2026: +// GobySoft, LLC +// +// This file is part of IvPico. +// +// IvPico is free software: you can redistribute it and/or modify it under the +// terms of the GNU Lesser General Public License as published by the Free Software +// Foundation, either version 2.1 of the License, or (at your option) any later +// version. +// +// IvPico is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with IvPico. If not, see . + +#ifndef IVPICO_HELM_BEHAVIOR_FILE_H +#define IVPICO_HELM_BEHAVIOR_FILE_H + +#include +#include +#include +#include + +#include "interface/behaviors.pb.h" +#include "interface/config.pb.h" + +namespace ivpico +{ +namespace helm +{ +struct SourceLocation +{ + std::string file; + // from 1; 0 if unknown + int line{0}; + int column{0}; + + std::string str() const; +}; + +// Locations of what a reader read, keyed on the path of each value in the +// BehaviorSet ("behavior[2].endflag[0]", "mode[1].condition[0]", ...) +using SourceMap = std::map; + +// The location of a path, or of the nearest enclosing path that has one +std::optional locate(const SourceMap& map, std::string path); + +// One line of .bhv text, and the path of the value it holds +struct BhvLine +{ + std::string text; + std::string path; +}; + +// The official .bhv text for a behavior set. Values that .bhv can't hold (a +// newline, "//", a brace where the Populator splits lines) go in errors as +// "path: message", and the lines are still written. +std::vector bhv_lines(const ivp::interface::BehaviorSet& set, + std::vector& errors); +std::string bhv_text(const std::vector& lines); + +// A .bhv file as Populator_BehaviorSet reads it. A line the Populator would +// reject ends the read with an error, as it does. +ivp::interface::BehaviorSet read_bhv(const std::string& text, const std::string& filename, + std::vector& errors, SourceMap* map = nullptr); + +// Whether a behavior file is YAML (.yml or .yaml) +bool is_yaml_file(const std::string& filename); + +#ifdef HAS_YAML +// A .bhv.yml file, checked against the schema; errors are "file:line:column: message" +std::optional read_yaml(const std::string& text, + const std::string& filename, + std::vector& errors, + SourceMap* map = nullptr); +std::string yaml_text(const ivp::interface::BehaviorSet& set); +#endif + +// Behaviors from YAML files or given inline can't go to the Populator as they +// are: they, and every .bhv file, are combined into one .bhv text instead +bool needs_combining(const ivp::interface::HelmConfig& cfg); + +// Each behavior file in the order given, then the inline sets, with the +// location each line came from. Errors (a missing file, YAML that doesn't +// pass the schema, values .bhv can't hold) mean nothing should be populated. +struct CombinedBehaviors +{ + std::string text; + std::vector line_locations; +}; +CombinedBehaviors combine_behaviors(const ivp::interface::HelmConfig& cfg, + std::vector& errors); + +// A Populator warning about the combined file at path, pointing at where the +// line came from instead +std::string relocate_warning(const std::string& warning, const std::string& path, + const CombinedBehaviors& combined); + +// The JSON Schema of .bhv.yml files +std::string yaml_schema(); + +} // namespace helm +} // namespace ivpico + +#endif diff --git a/src/lib/helm/behavior_yaml.cpp b/src/lib/helm/behavior_yaml.cpp new file mode 100644 index 0000000..ce5ae0f --- /dev/null +++ b/src/lib/helm/behavior_yaml.cpp @@ -0,0 +1,470 @@ +// Copyright 2026: +// GobySoft, LLC +// +// This file is part of IvPico. +// +// IvPico is free software: you can redistribute it and/or modify it under the +// terms of the GNU Lesser General Public License as published by the Free Software +// Foundation, either version 2.1 of the License, or (at your option) any later +// version. +// +// IvPico is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with IvPico. If not, see . + +#include +#include +#include + +#include +#include + +#include "behavior_file.h" + +using ivp::interface::BehaviorSet; +using Behavior = ivp::interface::BehaviorSet::Behavior; +using google::protobuf::FieldDescriptor; + +namespace +{ +std::string indexed(const std::string& base, int i) { return base + "[" + std::to_string(i) + "]"; } + +bool is_common(const FieldDescriptor* f) { return f->name() != "type" && f->name() != "param"; } + +class YamlReader +{ + public: + YamlReader(const std::string& filename, std::vector& errors, + ivpico::helm::SourceMap* map) + : filename_(filename), errors_(errors), map_(map) + { + } + + std::optional read(const std::string& text) + { + YAML::Node root; + try + { + root = YAML::Load(text); + } + catch (const YAML::Exception& e) + { + error(e.mark, e.msg); + return std::nullopt; + } + + const std::size_t errors_before = errors_.size(); + BehaviorSet set; + if (root.IsNull()) + return set; + if (!root.IsMap()) + { + error(root.Mark(), "a behavior file is a map of initialize, initialize_deferred, " + "modes and behaviors"); + return std::nullopt; + } + for (const auto& e : entries(root)) + { + if (e.key == "initialize" || e.key == "initialize_deferred") + read_initialize(e.value, e.key == "initialize_deferred", set); + else if (e.key == "modes") + read_modes(e.value, set); + else if (e.key == "behaviors") + read_behaviors(e.value, set); + // a place for anchors (&name) to merge (<<: *name) elsewhere, as in docker compose + else if (e.key.rfind("x-", 0) == 0) + continue; + else + error(e.mark, "unknown key \"" + e.key + + "\" (expected initialize, initialize_deferred, modes, " + "behaviors, or x-... for anchors)"); + } + if (errors_.size() != errors_before) + return std::nullopt; + return set; + } + + private: + struct Entry + { + std::string key; + YAML::Node value; + YAML::Mark mark; + }; + + void error(const YAML::Mark& mark, const std::string& msg) + { + errors_.push_back(location(mark).str() + ": " + msg); + } + + ivpico::helm::SourceLocation location(const YAML::Mark& mark) const + { + if (mark.is_null()) + return {filename_, 0, 0}; + return {filename_, mark.line + 1, mark.column + 1}; + } + + void note(const std::string& path, const YAML::Mark& mark) + { + if (map_) + (*map_)[path] = location(mark); + } + + // A map's entries in order, with merge keys ("<<") expanded; keys given in + // the map itself win over merged ones + std::vector entries(const YAML::Node& map) + { + std::set given; + for (const auto& kv : map) + if (kv.first.Scalar() != "<<" && !given.insert(kv.first.Scalar()).second) + error(kv.first.Mark(), "\"" + kv.first.Scalar() + "\" is given twice"); + + std::vector out; + std::set merged; + for (const auto& kv : map) + { + if (kv.first.Scalar() != "<<") + { + out.push_back({kv.first.Scalar(), kv.second, kv.first.Mark()}); + continue; + } + std::vector sources; + if (kv.second.IsSequence()) + for (const auto& s : kv.second) sources.push_back(s); + else + sources.push_back(kv.second); + for (const auto& source : sources) + { + if (!source.IsMap()) + { + error(kv.second.Mark(), "a merge (<<) takes a map or a list of maps"); + continue; + } + for (const auto& e : entries(source)) + if (!given.count(e.key) && merged.insert(e.key).second) + out.push_back(e); + } + } + return out; + } + + // key_mark: where to report a missing value, as yaml-cpp places null nodes poorly + bool scalar(const YAML::Node& node, const std::string& what, std::string& out, + const YAML::Mark& key_mark = YAML::Mark::null_mark()) + { + if (node.IsScalar()) + { + out = node.Scalar(); + return true; + } + if (node.IsNull()) + error(key_mark.is_null() ? node.Mark() : key_mark, + what + " needs a value (\"\" for an empty one)"); + else + error(node.Mark(), what + " takes one value, not a list or map"); + return false; + } + + // one value, or a list of them + std::vector> + scalars(const YAML::Node& node, const std::string& what, const YAML::Mark& key_mark) + { + std::vector> out; + if (node.IsSequence()) + { + for (const auto& item : node) + { + std::string value; + if (scalar(item, what, value)) + out.push_back({value, item.Mark()}); + } + } + else + { + std::string value; + if (scalar(node, what, value, key_mark)) + out.push_back({value, node.Mark()}); + } + return out; + } + + bool expect_map(const YAML::Node& node, const std::string& what) + { + if (node.IsMap()) + return true; + error(node.Mark(), what + " must be a map"); + return false; + } + + bool expect_list(const YAML::Node& node, const std::string& what) + { + if (node.IsSequence()) + return true; + error(node.Mark(), what + " must be a list"); + return false; + } + + void read_initialize(const YAML::Node& node, bool deferred, BehaviorSet& set) + { + const std::string what = deferred ? "initialize_deferred" : "initialize"; + if (!expect_map(node, what)) + return; + for (const auto& e : entries(node)) + { + std::string value; + if (!scalar(e.value, what + "." + e.key, value, e.mark)) + continue; + auto& init = *set.add_initialize(); + init.set_var(e.key); + init.set_value(value); + init.set_deferred(deferred); + note(indexed("initialize", set.initialize_size() - 1), e.mark); + } + } + + void read_modes(const YAML::Node& node, BehaviorSet& set) + { + if (!expect_list(node, "modes")) + return; + for (const auto& item : node) + { + if (!expect_map(item, "a mode")) + continue; + const std::string path = indexed("mode", set.mode_size()); + auto& mode = *set.add_mode(); + note(path, item.Mark()); + bool has_set = false; + for (const auto& e : entries(item)) + { + std::string value; + if (e.key == "set") + { + has_set = scalar(e.value, "set", value, e.mark); + mode.set_set(value); + note(path, e.value.Mark()); + } + else if (e.key == "condition") + { + for (const auto& [c, mark] : scalars(e.value, "condition", e.mark)) + { + note(indexed(path + ".condition", mode.condition_size()), mark); + mode.add_condition(c); + } + } + else if (e.key == "else") + { + if (scalar(e.value, "else", value, e.mark)) + mode.set_else_value(value); + note(path + ".else_value", e.value.Mark()); + } + else + { + error(e.mark, "unknown mode key \"" + e.key + + "\" (expected set, condition " + "or else)"); + } + } + if (!has_set) + error(item.Mark(), "a mode needs set: MODE_VAR = value"); + } + } + + void read_behaviors(const YAML::Node& node, BehaviorSet& set) + { + if (!expect_list(node, "behaviors")) + return; + const auto* refl = Behavior::GetReflection(); + for (const auto& item : node) + { + if (!expect_map(item, "a behavior")) + continue; + const std::string path = indexed("behavior", set.behavior_size()); + auto& bhv = *set.add_behavior(); + note(path, item.Mark()); + bool has_type = false; + for (const auto& e : entries(item)) + { + std::string value; + const auto* field = Behavior::descriptor()->FindFieldByName(e.key); + if (e.key == "type") + { + has_type = scalar(e.value, "type", value, e.mark); + bhv.set_type(value); + note(path, e.value.Mark()); + } + else if (e.key == "params") + { + if (!expect_map(e.value, "params")) + continue; + for (const auto& p : entries(e.value)) + { + for (const auto& [v, mark] : scalars(p.value, p.key, p.mark)) + { + note(indexed(path + ".param", bhv.param_size()), mark); + auto& param = *bhv.add_param(); + param.set_key(p.key); + param.set_value(v); + } + } + } + else if (field && is_common(field) && field->is_repeated()) + { + for (const auto& [v, mark] : scalars(e.value, e.key, e.mark)) + { + note(indexed(path + "." + e.key, refl->FieldSize(bhv, field)), mark); + refl->AddString(&bhv, field, v); + } + } + else if (field && is_common(field)) + { + if (scalar(e.value, e.key, value, e.mark)) + refl->SetString(&bhv, field, value); + note(path + "." + e.key, e.value.Mark()); + } + else + { + error(e.mark, "unknown behavior key \"" + e.key + + "\": the behavior's own parameters go under params"); + } + } + if (!has_type) + error(item.Mark(), "a behavior needs a type (BHV_Waypoint, ...)"); + } + } + + std::string filename_; + std::vector& errors_; + ivpico::helm::SourceMap* map_; +}; + +void emit_values(YAML::Emitter& out, const std::vector& values) +{ + if (values.size() == 1) + { + out << values.front(); + return; + } + out << YAML::BeginSeq; + for (const auto& v : values) out << v; + out << YAML::EndSeq; +} + +void emit_initialize(YAML::Emitter& out, const BehaviorSet& set, bool deferred) +{ + bool any = false; + for (const auto& init : set.initialize()) + { + if (init.deferred() != deferred) + continue; + if (!any) + out << YAML::Key << (deferred ? "initialize_deferred" : "initialize") << YAML::Value + << YAML::BeginMap; + any = true; + out << YAML::Key << init.var() << YAML::Value << init.value(); + } + if (any) + out << YAML::EndMap; +} +} // namespace + +std::optional ivpico::helm::read_yaml(const std::string& text, + const std::string& filename, + std::vector& errors, SourceMap* map) +{ + return YamlReader(filename, errors, map).read(text); +} + +std::string ivpico::helm::yaml_text(const BehaviorSet& set) +{ + YAML::Emitter out; + out.SetIndent(2); + out << YAML::BeginMap; + emit_initialize(out, set, false); + emit_initialize(out, set, true); + + if (set.mode_size()) + { + out << YAML::Key << "modes" << YAML::Value << YAML::BeginSeq; + for (const auto& mode : set.mode()) + { + out << YAML::BeginMap << YAML::Key << "set" << YAML::Value << mode.set(); + if (mode.condition_size()) + { + out << YAML::Key << "condition" << YAML::Value; + emit_values(out, {mode.condition().begin(), mode.condition().end()}); + } + if (mode.has_else_value()) + out << YAML::Key << "else" << YAML::Value << mode.else_value(); + out << YAML::EndMap; + } + out << YAML::EndSeq; + } + + if (set.behavior_size()) + { + const auto* refl = Behavior::GetReflection(); + out << YAML::Key << "behaviors" << YAML::Value << YAML::BeginSeq; + for (const auto& bhv : set.behavior()) + { + out << YAML::BeginMap << YAML::Key << "type" << YAML::Value << bhv.type(); + for (int f = 0; f < Behavior::descriptor()->field_count(); ++f) + { + const auto* field = Behavior::descriptor()->field(f); + if (!is_common(field)) + continue; + std::vector values; + if (field->is_repeated()) + for (int j = 0; j < refl->FieldSize(bhv, field); ++j) + values.push_back(refl->GetRepeatedString(bhv, field, j)); + else if (refl->HasField(bhv, field)) + values.push_back(refl->GetString(bhv, field)); + if (values.empty()) + continue; + out << YAML::Key << field->name() << YAML::Value; + emit_values(out, values); + } + + // a parameter given more than once becomes one list, where it first appears + std::vector>> params; + for (const auto& p : bhv.param()) + { + auto it = std::find_if(params.begin(), params.end(), + [&](const auto& kv) { return kv.first == p.key(); }); + if (it == params.end()) + params.push_back({p.key(), {p.value()}}); + else + it->second.push_back(p.value()); + } + if (!params.empty()) + { + out << YAML::Key << "params" << YAML::Value << YAML::BeginMap; + for (const auto& [key, values] : params) + { + out << YAML::Key << key << YAML::Value; + emit_values(out, values); + } + out << YAML::EndMap; + } + out << YAML::EndMap; + } + out << YAML::EndSeq; + } + out << YAML::EndMap; + + // a blank line between sections and between behaviors + std::istringstream in(out.c_str()); + std::string text, line, previous; + while (std::getline(in, line)) + { + const bool section = !line.empty() && line.front() != ' ' && line.front() != '-'; + const bool behavior = line.rfind(" - type:", 0) == 0 || line.rfind("- type:", 0) == 0; + if (!text.empty() && (section || (behavior && previous != "behaviors:"))) + text += "\n"; + text += line + "\n"; + previous = line; + } + return text; +} diff --git a/src/lib/helm/helm.cpp b/src/lib/helm/helm.cpp index 120fdbc..d51f71c 100644 --- a/src/lib/helm/helm.cpp +++ b/src/lib/helm/helm.cpp @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -46,6 +47,7 @@ #include "Populator_BehaviorSet.h" #include "VarDataPair.h" +#include "behavior_file.h" #include "contact_manager.h" #include "conversions.h" @@ -349,9 +351,35 @@ void Helm::Impl::build_behaviors() populator.setBHVDirNotFoundOK(cfg_.behavior_dir_not_found_ok()); populator.setOwnship(ownship_); for (const auto& dir : cfg_.behavior_dir()) populator.addBehaviorDir(dir); - bhv_set_ = populator.populate( - std::set(cfg_.behavior_file().begin(), cfg_.behavior_file().end())); - for (const auto& w : populator.getConfigWarnings()) config_warnings_.push_back(w); + if (!needs_combining(cfg_)) + { + bhv_set_ = populator.populate( + std::set(cfg_.behavior_file().begin(), cfg_.behavior_file().end())); + for (const auto& w : populator.getConfigWarnings()) config_warnings_.push_back(w); + } + else + { + const CombinedBehaviors combined = combine_behaviors(cfg_, config_warnings_); + if (!config_warnings_.empty()) + return; + // the Populator reads only files + std::string path = + (std::filesystem::temp_directory_path() / "ivpico_behaviors_XXXXXX").string(); + const int fd = mkstemp(path.data()); + if (fd < 0 || write(fd, combined.text.data(), combined.text.size()) != + static_cast(combined.text.size())) + { + config_warnings_.push_back("Couldn't write the combined behaviors to " + path); + if (fd >= 0) + close(fd); + return; + } + close(fd); + bhv_set_ = populator.populate(path); + std::filesystem::remove(path); + for (const auto& w : populator.getConfigWarnings()) + config_warnings_.push_back(relocate_warning(w, path, combined)); + } if (!bhv_set_) { diff --git a/src/lib/interface/behaviors.proto b/src/lib/interface/behaviors.proto new file mode 100644 index 0000000..05de0ae --- /dev/null +++ b/src/lib/interface/behaviors.proto @@ -0,0 +1,81 @@ +// Copyright 2026: +// GobySoft, LLC +// +// This file is part of IvPico. +// +// IvPico is free software: you can redistribute it and/or modify it under the +// terms of the GNU Lesser General Public License as published by the Free Software +// Foundation, either version 2.1 of the License, or (at your option) any later +// version. +// +// IvPico is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with IvPico. If not, see . + +syntax = "proto3"; + +package ivp.interface; + +// What one .bhv or .bhv.yml file holds. Values are kept as their source text, +// since the behaviors parse them. +message BehaviorSet +{ + // "initialize" (or "initialize_" if deferred) + message Initialize + { + string var = 1; + string value = 2; + bool deferred = 3; + } + repeated Initialize initialize = 1; + + // "set MODE = ACTIVE { conditions } else_value" + message Mode + { + string set = 1; + repeated string condition = 2; + optional string else_value = 3; + } + repeated Mode mode = 2; + + // The parameters every behavior takes are fields; the rest are params, in order + message Behavior + { + string type = 1; + optional string name = 2; + optional string pwt = 3; + repeated string condition = 4; + optional string updates = 5; + optional string templating = 6; + // seconds, or no-time-limit + optional string duration = 7; + optional string duration_status = 8; + optional string duration_reset = 9; + optional string duration_idle_decay = 10; + optional string perpetual = 11; + repeated string nostarve = 12; + repeated string post_mapping = 13; + optional string comms_policy = 14; + + repeated string endflag = 20; + repeated string runflag = 21; + repeated string runxflag = 22; + repeated string activeflag = 23; + repeated string inactiveflag = 24; + repeated string idleflag = 25; + repeated string spawnflag = 26; + repeated string spawnxflag = 27; + repeated string configflag = 28; + + message Param + { + string key = 1; + string value = 2; + } + repeated Param param = 100; + } + repeated Behavior behavior = 3; +} diff --git a/src/lib/interface/config.proto b/src/lib/interface/config.proto index 2a9a190..d30ddf5 100644 --- a/src/lib/interface/config.proto +++ b/src/lib/interface/config.proto @@ -19,6 +19,7 @@ syntax = "proto3"; package ivp.interface; +import "interface/behaviors.proto"; import "interface/options.proto"; import "openocean/messages/common.proto"; @@ -26,8 +27,11 @@ import "openocean/messages/common.proto"; // take pHelmIvP's defaults. message HelmConfig { - // .bhv files ("behaviors"), read in sorted order + // .bhv or .bhv.yml files ("behaviors"). Only .bhv files are read in + // sorted order, as pHelmIvP does; otherwise in the order given. repeated string behavior_file = 1; + // behaviors given here rather than in a file, read after behavior_file + repeated BehaviorSet behaviors = 8; message Domain { diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index cc162a6..6cd7b8e 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -6,6 +6,31 @@ add_executable(test_helm helm/test_helm.cpp) target_link_libraries(test_helm PRIVATE ivpico_helm IVP::contacts IVP::geometry IVP::mbutil) add_test(NAME helm COMMAND test_helm) +add_executable(test_behavior_file helm/test_behavior_file.cpp) +target_link_libraries(test_behavior_file PRIVATE ivpico_helm) +add_test(NAME behavior_file + COMMAND test_behavior_file ${CMAKE_CURRENT_SOURCE_DIR}/missions/fixtures) + +add_test(NAME bhv_schema + COMMAND ${CMAKE_COMMAND} -DTOOL=$ + -DSCHEMA=${PROJECT_SOURCE_DIR}/share/ivpico/bhv.schema.json -P ${CMAKE_CURRENT_SOURCE_DIR}/bhv/check_schema.cmake) + +# validating against the schema needs python3-jsonschema and python3-yaml +find_package(Python3 COMPONENTS Interpreter QUIET) +if(enable_yaml AND Python3_Interpreter_FOUND) + execute_process(COMMAND ${Python3_EXECUTABLE} -c "import jsonschema, yaml" + RESULT_VARIABLE python_modules_missing OUTPUT_QUIET ERROR_QUIET) + if(python_modules_missing) + message(STATUS "Skipping the bhv_schema_examples test: needs python3-jsonschema and python3-yaml") + else() + add_test(NAME bhv_schema_examples + COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/bhv/validate_schema.py + ${PROJECT_SOURCE_DIR}/share/ivpico/bhv.schema.json $ + ${CMAKE_CURRENT_SOURCE_DIR}/missions/fixtures + ${PROJECT_SOURCE_DIR}/launch/goby/alpha/config/alpha.bhv.yml) + endif() +endif() + add_executable(replay missions/replay.cpp) # conversions.h includes MOOS-IvP headers target_link_libraries(replay PRIVATE ivpico_helm IVP::contacts IVP::geometry IVP::mbutil) @@ -14,6 +39,9 @@ foreach(fixture ${fixtures}) if(IS_DIRECTORY ${fixture}) get_filename_component(mission ${fixture} NAME) add_test(NAME replay_${mission} COMMAND replay ${fixture}) + if(enable_yaml) + add_test(NAME replay_${mission}_yaml COMMAND replay ${fixture} --via-yaml) + endif() file(GLOB moos_file ${fixture}/*.moos) file(STRINGS ${moos_file} contact_manager REGEX "ProcessConfig *= *pContactMgrV20") if(contact_manager) @@ -33,8 +61,12 @@ if(GOBY_FOUND AND GOBY_EXECUTABLE) COMMAND ${GOBY_EXECUTABLE} test run ${CMAKE_CURRENT_SOURCE_DIR}/goby/helm_ivp/test.yml ${goby_test_defines}) + set(closed_loop_missions s1_alpha s2_bravo s3_charlie s4_delta m2_berta s1_alpha_inline) + if(enable_yaml) + list(APPEND closed_loop_missions s1_alpha_yaml) + endif() set(simulator_port 54400) - foreach(mission s1_alpha s2_bravo s3_charlie s4_delta m2_berta) + foreach(mission ${closed_loop_missions}) add_test(NAME closed_loop_${mission} COMMAND ${GOBY_EXECUTABLE} test run ${CMAKE_CURRENT_SOURCE_DIR}/missions/goby/${mission}/test.yml ${goby_test_defines} -D MISSION=${mission} -D PORT=${simulator_port}) diff --git a/src/test/bhv/check_schema.cmake b/src/test/bhv/check_schema.cmake new file mode 100644 index 0000000..043a82b --- /dev/null +++ b/src/test/bhv/check_schema.cmake @@ -0,0 +1,7 @@ +# Fails if the committed schema differs from what ivpico_bhv generates. +# Usage: cmake -DTOOL= -DSCHEMA= -P check_schema.cmake +execute_process(COMMAND ${TOOL} schema OUTPUT_VARIABLE generated RESULT_VARIABLE result) +file(READ ${SCHEMA} committed) +if(NOT result EQUAL 0 OR NOT generated STREQUAL committed) + message(FATAL_ERROR "${SCHEMA} is out of date: regenerate it with\n ivpico_bhv schema -o ${SCHEMA}") +endif() diff --git a/src/test/bhv/validate_schema.py b/src/test/bhv/validate_schema.py new file mode 100644 index 0000000..31ff8f8 --- /dev/null +++ b/src/test/bhv/validate_schema.py @@ -0,0 +1,64 @@ +#!/usr/bin/env python3 +"""Validates .bhv.yml files against IvPico's JSON Schema. + +Usage: validate_schema.py [file.bhv.yml ...] + +Every fixture .bhv is imported with ivpico_bhv and must validate, as must each +file given. A few files that ivpico_bhv's reader rejects must fail validation +too, so a schema that accepts anything can't pass. +""" + +import pathlib +import subprocess +import sys +import tempfile + +import jsonschema +import yaml + +REJECTED = { + "unknown top-level key": "behavior:\n - type: A\n", + "behavior without a type": "behaviors:\n - name: a\n", + "unknown behavior key": "behaviors:\n - type: A\n endflags: X = 1\n", + "list for a single value": "behaviors:\n - type: A\n pwt: [1, 2]\n", + "map for a parameter": "behaviors:\n - type: A\n params:\n speed: {a: 1}\n", + "mode without set": "modes:\n - condition: A = 1\n", + "initialize with a list": "initialize:\n A: [1, 2]\n", +} + + +def main(): + schema_file, tool, fixtures, *files = sys.argv[1:] + validator = jsonschema.Draft202012Validator( + yaml.safe_load(pathlib.Path(schema_file).read_text())) + failures = [] + + def validate(name, text): + errors = list(validator.iter_errors(yaml.safe_load(text))) + for e in errors: + failures.append(f"{name}: {'/'.join(map(str, e.path))}: {e.message}") + return not errors + + with tempfile.TemporaryDirectory() as tmp: + bhvs = sorted(pathlib.Path(fixtures).glob("*/*.bhv")) + for bhv in bhvs: + out = pathlib.Path(tmp) / f"{bhv.parent.name}.{bhv.name}.yml" + subprocess.run([tool, "import", str(bhv), "-o", str(out)], check=True) + validate(str(bhv), out.read_text()) + if len(bhvs) < 10: + failures.append(f"only {len(bhvs)} fixture .bhv files in {fixtures}") + + for f in files: + validate(f, pathlib.Path(f).read_text()) + + for what, text in REJECTED.items(): + if not list(validator.iter_errors(yaml.safe_load(text))): + failures.append(f"the schema accepts a file with a {what}") + + for f in failures: + print(f) + return 1 if failures else 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/src/test/helm/test_behavior_file.cpp b/src/test/helm/test_behavior_file.cpp new file mode 100644 index 0000000..d2d2674 --- /dev/null +++ b/src/test/helm/test_behavior_file.cpp @@ -0,0 +1,497 @@ +// Copyright 2026: +// GobySoft, LLC +// +// This file is part of IvPico. +// +// IvPico is free software: you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the Free Software +// Foundation, either version 2 of the License, or (at your option) any later +// version. +// +// IvPico is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with IvPico. If not, see . + +// Tests for behavior files: the .bhv reader and writer, the .bhv.yml reader and +// writer, and the helm loading YAML and inline behaviors. +// +// The reader must read a .bhv file as Populator_BehaviorSet does, and the +// writer must write text the Populator reads back into the same BehaviorSet. +// Given the fixtures directory, every fixture .bhv also goes .bhv → BehaviorSet +// → .bhv → BehaviorSet (and through YAML), which must give the same set. The +// replay__yaml tests then check the helm decides the same from it. +// +// Prints one line per failed check and exits non-zero if there were any. + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "helm/behavior_file.h" +#include "helm/helm.h" + +using google::protobuf::util::MessageDifferencer; +using ivp::interface::BehaviorSet; +using ivp::interface::HelmConfig; +using ivpico::helm::Helm; +namespace fs = std::filesystem; +namespace helm = ivpico::helm; + +namespace +{ +int errors = 0; + +void check(bool ok, const std::string& scenario, const std::string& what) +{ + if (!ok) + { + std::cout << "failed: " << scenario << ": " << what << "\n"; + ++errors; + } +} + +bool equal(const BehaviorSet& a, const BehaviorSet& b, const std::string& scenario) +{ + if (MessageDifferencer::Equals(a, b)) + return true; + std::cout << scenario << ": differ\n " << a.ShortDebugString() << "\n " + << b.ShortDebugString() << "\n"; + return false; +} + +std::string write_file(const std::string& name, const std::string& contents) +{ + const auto path = fs::temp_directory_path() / ("ivpico_test_" + name); + std::ofstream(path) << contents; + return path.string(); +} + +HelmConfig config() +{ + HelmConfig cfg; + cfg.set_ownship("alpha"); + cfg.set_start_in_drive(true); + for (const auto& [name, high, points] : std::vector>{ + {"course", 359, 360}, {"speed", 4, 41}}) + { + auto& d = *cfg.add_domain(); + d.set_name(name); + d.set_high(high); + d.set_points(points); + } + return cfg; +} + +// Every .bhv construct: initialize lists and deferral, modes with and without +// an else, a behavior opened on its own line, a common parameter given twice, +// comments, a continued line, and a block the Populator ignores. +const std::string constructs_bhv = R"(// comment +initialize DEPLOY = false, RETURN = false +initialize_ SPEED = 2 + +set MODE = ACTIVE { + DEPLOY = true +} INACTIVE + +set MODE = SURVEYING { + MODE = ACTIVE + RETURN = false +} + +{ + ignored by the Populator +} + +Behavior = BHV_Waypoint { + name = survey // trailing comment + pwt = 100 + pwt = 50 + condition = MODE = SURVEYING + endflag = RETURN = true + speed = 3 + points = 0,0:10,10:\ + 20,20 +} +)"; + +void read_bhv_constructs() +{ + const std::string scenario = "read_bhv_constructs"; + std::vector errs; + helm::SourceMap map; + const auto set = helm::read_bhv(constructs_bhv, "c.bhv", errs, &map); + check(errs.empty(), scenario, "errors reading"); + + check(set.initialize_size() == 3 && set.initialize(1).var() == "RETURN" && + !set.initialize(1).deferred() && set.initialize(2).deferred(), + scenario, "initialize: " + set.ShortDebugString()); + check(set.mode_size() == 2 && set.mode(0).set() == "MODE = ACTIVE" && + set.mode(0).else_value() == "INACTIVE" && !set.mode(1).has_else_value() && + set.mode(1).condition_size() == 2, + scenario, "modes"); + check(set.behavior_size() == 1, scenario, "one behavior"); + if (set.behavior_size() != 1) + return; + const auto& b = set.behavior(0); + check(b.type() == "BHV_Waypoint" && b.name() == "survey" && b.pwt() == "100", scenario, + "type, name and the first pwt"); + check(b.condition_size() == 1 && b.condition(0) == "MODE = SURVEYING" && b.endflag_size() == 1, + scenario, "condition and endflag"); + check(b.param_size() == 3 && b.param(0).key() == "pwt" && b.param(0).value() == "50" && + b.param(2).key() == "points" && b.param(2).value() == "0,0:10,10:20,20", + scenario, "params: " + b.ShortDebugString()); + + const auto endflag = helm::locate(map, "behavior[0].endflag[0]"); + check(endflag && endflag->line == 23, scenario, "endflag's line"); + const auto points = helm::locate(map, "behavior[0].param[2]"); + check(points && points->line == 25, scenario, "a continued line is at its first line"); + const auto unknown = helm::locate(map, "behavior[0].nostarve[0]"); + check(unknown && unknown->line == 18, scenario, + "a path without a line falls back to its behavior"); +} + +void read_bhv_rejects() +{ + const std::string scenario = "read_bhv_rejects"; + for (const auto& [text, line] : std::vector>{ + {"initialize A = 1\nnot a keyword\n", 2}, + {"set MODE = ACTIVE {\n DEPLOY = = true\n}\n", 2}, + {"set MODE = A {\n X = 1\n} TWO WORDS\n", 3}, + {"initialize A\n", 1}}) + { + std::vector errs; + helm::read_bhv(text, "r.bhv", errs); + check(errs.size() == 1 && errs[0].rfind("r.bhv:" + std::to_string(line) + ":", 0) == 0, + scenario, "line " + std::to_string(line) + " of: " + text); + } +} + +void bhv_round_trip(const std::string& text, const std::string& scenario) +{ + std::vector errs; + const auto set = helm::read_bhv(text, scenario, errs); + check(errs.empty(), scenario, "errors reading"); + std::vector write_errs; + const auto written = helm::bhv_text(helm::bhv_lines(set, write_errs)); + for (const auto& e : write_errs) check(false, scenario, "writing: " + e); + const auto again = helm::read_bhv(written, scenario, errs); + check(errs.empty(), scenario, "errors reading back"); + check(equal(set, again, scenario), scenario, ".bhv round trip"); +#ifdef HAS_YAML + const auto yaml = helm::read_yaml(helm::yaml_text(set), scenario, errs); + for (const auto& e : errs) check(false, scenario, "reading YAML back: " + e); + check(yaml && equal(set, *yaml, scenario), scenario, "YAML round trip"); +#endif +} + +void write_errors() +{ + const std::string scenario = "write_errors"; + BehaviorSet set; + auto& init = *set.add_initialize(); + init.set_var("A"); + init.set_value("1,2"); + set.add_mode()->set_set("MODE"); + auto& b = *set.add_behavior(); + b.set_type("BHV Waypoint"); + auto* p = b.add_param(); + p->set_key("url"); + p->set_value("http://example.com"); + p = b.add_param(); + p->set_key("note"); + p->set_value("two\nlines"); + + std::vector errs; + helm::bhv_lines(set, errs); + for (const auto& path : {"initialize[0]", "mode[0]", "behavior[0]", "behavior[0].param[0]", + "behavior[0].param[1]"}) + check(std::any_of(errs.begin(), errs.end(), [&](const std::string& e) + { return e.rfind(std::string(path) + ":", 0) == 0; }), + scenario, std::string("no error at ") + path); +} + +void relocate() +{ + const std::string scenario = "relocate"; + helm::CombinedBehaviors combined; + combined.line_locations = {{"a.bhv.yml", 3, 5}, {"a.bhv.yml", 7, 9}}; + check(helm::relocate_warning("Problem with line 2 in the BehaviorSet file: /tmp/x Line: [foo]", + "/tmp/x", combined) == "a.bhv.yml:7:9: can't read the line [foo]", + scenario, "a line the Populator can't read"); + check(helm::relocate_warning("/tmp/x: Line 0: speed = abc", "/tmp/x", combined) == + "a.bhv.yml:3:5: the behavior rejected \"speed = abc\"", + scenario, "a parameter a behavior rejected"); + check(helm::relocate_warning("Could not read /tmp/x", "/tmp/x", combined) == + "Could not read behaviors", + scenario, "another warning"); +} + +#ifdef HAS_YAML +void read_yaml_constructs() +{ + const std::string scenario = "read_yaml_constructs"; + const std::string yaml = R"(initialize: + DEPLOY: false + RETURN: false +initialize_deferred: + SPEED: 2 + +modes: + - set: MODE = ACTIVE + condition: DEPLOY = true + else: INACTIVE + - set: MODE = SURVEYING + condition: [MODE = ACTIVE, RETURN = false] + +behaviors: + - type: BHV_Waypoint + name: survey + pwt: 100 + condition: MODE = SURVEYING + endflag: RETURN = true + params: + pwt: 50 + speed: 3 + points: 0,0:10,10:20,20 +)"; + std::vector errs; + helm::SourceMap map; + const auto set = helm::read_yaml(yaml, "c.bhv.yml", errs, &map); + for (const auto& e : errs) check(false, scenario, e); + std::vector bhv_errs; + const auto expected = helm::read_bhv(constructs_bhv, "c.bhv", bhv_errs); + check(set && equal(*set, expected, scenario), scenario, "the same set as the .bhv"); + const auto speed = helm::locate(map, "behavior[0].param[1]"); + check(speed && speed->line == 22 && speed->column == 14, scenario, "speed's line and column"); +} + +void yaml_scalars_verbatim() +{ + const std::string scenario = "yaml_scalars_verbatim"; + const std::string yaml = R"(behaviors: + - type: BHV_Test + perpetual: yes + params: + a: 3.10 + b: on + c: 0x1 + d: "007" + e: 1e3 + f: "" +)"; + std::vector errs; + const auto set = helm::read_yaml(yaml, "v.bhv.yml", errs); + check(set.has_value(), scenario, "read"); + if (!set) + return; + std::string values; + for (const auto& p : set->behavior(0).param()) values += p.value() + "|"; + check(values == "3.10|on|0x1|007|1e3||", scenario, "values: " + values); + check(set->behavior(0).perpetual() == "yes", scenario, "perpetual: yes"); +} + +void yaml_errors() +{ + const std::string scenario = "yaml_errors"; + for (const auto& [yaml, where] : std::vector>{ + {"behaviors:\n - type: A\n endflags: X = 1\n", "e.bhv.yml:3:5"}, + {"behaviors:\n - type: A\n pwt: [1, 2]\n", "e.bhv.yml:3:10"}, + {"behaviors:\n - type: A\n params:\n speed:\n", "e.bhv.yml:4:7"}, + {"behaviors:\n - name: A\n", "e.bhv.yml:2:5"}, + {"modes:\n - condition: A = 1\n", "e.bhv.yml:2:5"}, + {"initialize:\n A: 1\n A: 2\n", "e.bhv.yml:3:3"}, + {"behavior:\n - type: A\n", "e.bhv.yml:1:1"}, + {"behaviors: [\n", "e.bhv.yml:"}, + {"- type: A\n", "e.bhv.yml:1:1"}}) + { + std::vector errs; + const auto set = helm::read_yaml(yaml, "e.bhv.yml", errs); + check(!set && !errs.empty() && errs[0].rfind(where, 0) == 0, scenario, + "at " + where + ": " + (errs.empty() ? "no error" : errs[0])); + } +} + +void yaml_merge() +{ + const std::string scenario = "yaml_merge"; + const std::string yaml = R"(x-waypoint: &waypoint + pwt: 100 + params: + speed: 3 +behaviors: + - type: BHV_Waypoint + <<: *waypoint + name: a + - type: BHV_Waypoint + <<: *waypoint + pwt: 50 +)"; + std::vector errs; + const auto set = helm::read_yaml(yaml, "m.bhv.yml", errs); + check(set && set->behavior_size() == 2 && set->behavior(0).pwt() == "100" && + set->behavior(1).pwt() == "50" && set->behavior(1).param_size() == 1, + scenario, "merged: " + (set ? set->ShortDebugString() : errs.front())); +} +#endif + +// A behavior that decides course 90 at speed 2, given inline +BehaviorSet heading_90() +{ + BehaviorSet set; + auto& b = *set.add_behavior(); + b.set_type("BHV_ConstantHeading"); + b.set_name("ch"); + b.set_duration("no-time-limit"); + auto* p = b.add_param(); + p->set_key("heading"); + p->set_value("90"); + auto& s = *set.add_behavior(); + s.set_type("BHV_ConstantSpeed"); + s.set_name("cs"); + s.set_duration("no-time-limit"); + p = s.add_param(); + p->set_key("speed"); + p->set_value("2"); + return set; +} + +bool decides_course_90(Helm& helm) +{ + ivp::interface::Navigation nav; + nav.set_self(true); + nav.mutable_nav()->mutable_enu()->set_e(0); + nav.mutable_nav()->mutable_enu()->set_n(0); + nav.mutable_nav()->mutable_attitude()->set_heading(0); + nav.mutable_nav()->add_speed()->set_value(0); + const double t0 = 1.7e9; + for (int i = 0; i < 12; ++i) + { + nav.mutable_nav()->set_time(static_cast((t0 + i * 0.25) * 1e6)); + helm.handle(nav); + const auto& out = helm.iterate(t0 + i * 0.25); + if (out.solution && out.solution->allstop() == ivp::interface::CLEAR) + return std::fabs(out.solution->desired().attitude().heading() * 180 / M_PI - 90) < 1; + } + return false; +} + +// Inline behaviors, alone and after a .bhv file; a parameter a behavior +// rejects is reported at its path in the configuration. +void helm_inline() +{ + const std::string scenario = "helm_inline"; + { + auto cfg = config(); + *cfg.add_behaviors() = heading_90(); + Helm helm(cfg); + check(helm.config_warnings().empty(), scenario, "config warnings"); + check(decides_course_90(helm), scenario, "inline behaviors didn't decide course 90"); + } + { + auto cfg = config(); + cfg.add_behavior_file(write_file("init.bhv", "initialize DEPLOY = true\n")); + *cfg.add_behaviors() = heading_90(); + Helm helm(cfg); + check(helm.config_warnings().empty(), scenario, "a .bhv file and inline behaviors"); + } + { + auto cfg = config(); + auto set = heading_90(); + auto* p = set.mutable_behavior(0)->add_param(); + p->set_key("not_a_param"); + p->set_value("1"); + *cfg.add_behaviors() = set; + Helm helm(cfg); + const auto& w = helm.config_warnings(); + check(!w.empty() && w[0].rfind("behaviors[0].behavior[0].param[1]: ", 0) == 0, scenario, + "rejected parameter: " + (w.empty() ? "no warning" : w[0])); + } +} + +void helm_yaml_file() +{ + const std::string scenario = "helm_yaml_file"; + const std::string yaml = R"(behaviors: + - type: BHV_ConstantHeading + name: ch + duration: no-time-limit + params: + heading: 90 + - type: BHV_ConstantSpeed + name: cs + duration: no-time-limit + params: + speed: 2 + not_a_param: 1 +)"; + auto cfg = config(); + cfg.add_behavior_file(write_file("helm.bhv.yml", yaml)); + Helm helm(cfg); +#ifdef HAS_YAML + const auto& w = helm.config_warnings(); + check(!w.empty() && w[0].find("ivpico_test_helm.bhv.yml:12:20: the behavior rejected") != + std::string::npos, + scenario, "rejected parameter: " + (w.empty() ? "no warning" : w[0])); + + auto fixed = yaml.substr(0, yaml.find(" not_a_param")); + cfg.set_behavior_file(0, write_file("helm.bhv.yml", fixed)); + Helm good(cfg); + check(good.config_warnings().empty(), scenario, "config warnings"); + check(decides_course_90(good), scenario, "YAML behaviors didn't decide course 90"); +#else + const auto& w = helm.config_warnings(); + check(!w.empty() && w[0].find("without yaml-cpp") != std::string::npos, scenario, + "no warning that YAML needs yaml-cpp"); +#endif +} + +// Every fixture .bhv round-trips through the writer, and through YAML +void fixtures(const fs::path& dir) +{ + int count = 0; + for (const auto& f : fs::recursive_directory_iterator(dir)) + { + if (f.path().extension() != ".bhv") + continue; + std::ifstream in(f.path()); + std::stringstream text; + text << in.rdbuf(); + bhv_round_trip(text.str(), "fixture " + f.path().parent_path().filename().string() + "/" + + f.path().filename().string()); + ++count; + } + check(count > 10, "fixtures", + "only " + std::to_string(count) + " .bhv fixtures in " + dir.string()); +} +} // namespace + +int main(int argc, char* argv[]) +{ + read_bhv_constructs(); + read_bhv_rejects(); + bhv_round_trip(constructs_bhv, "constructs_round_trip"); + write_errors(); + relocate(); +#ifdef HAS_YAML + read_yaml_constructs(); + yaml_scalars_verbatim(); + yaml_errors(); + yaml_merge(); +#endif + helm_inline(); + helm_yaml_file(); + if (argc > 1) + fixtures(argv[1]); + return errors == 0 ? 0 : 1; +} diff --git a/src/test/missions/goby/s1_alpha_inline/helm.pb.cfg b/src/test/missions/goby/s1_alpha_inline/helm.pb.cfg new file mode 100644 index 0000000..c5155f7 --- /dev/null +++ b/src/test/missions/goby/s1_alpha_inline/helm.pb.cfg @@ -0,0 +1,78 @@ +helm { + domain { name: "speed" low: 0 high: 4 points: 41 } + domain { name: "course" low: 0 high: 359 points: 360 } + # alpha.bhv, inline + behaviors { + initialize { var: "DEPLOY" value: "false" } + initialize { var: "RETURN" value: "false" } + behavior { + type: "BHV_Waypoint" + name: "waypt_survey" + pwt: "100" + condition: "RETURN = false" + condition: "DEPLOY = true" + updates: "WPT_UPDATE" + perpetual: "true" + endflag: "RETURN = true" + endflag: "SUMMARY=speed=$[SPEED],time=$[DUR_RUN_TIME]" + activeflag: "INFO=$[OWNSHIP]" + activeflag: "INFO=$[BHVNAME]" + activeflag: "INFO=$[BHVTYPE]" + configflag: "CRUISE_SPD = $[SPEED]" + param { key: "cycleflag" value: "CINFO=$[OSX],$[OSY]" } + param { key: "wptflag" value: "PREV=$(PX),$(PY)" } + param { key: "wptflag" value: "NEXT=$(NX),$(NY)" } + param { key: "wptflag" value: "TEST=$(X),$(Y)" } + param { key: "wptflag" value: "OSPOS=$(OSX),$(OSY)" } + param { key: "wptflag_on_start" value: "true" } + param { key: "speed_alt" value: "1.2" } + param { key: "use_alt_speed" value: "true" } + param { key: "lead" value: "8" } + param { key: "lead_damper" value: "1" } + param { key: "lead_to_start" value: "true" } + param { key: "speed" value: "3" } + param { key: "capture_line" value: "true" } + param { key: "capture_radius" value: "5.0" } + param { key: "slip_radius" value: "15.0" } + param { key: "efficiency_measure" value: "all" } + param { key: "polygon" value: "60,-40:60,-160:150,-160:180,-100:150,-40" } + param { key: "order" value: "normal" } + param { key: "repeat" value: "0" } + param { key: "visual_hints" value: "nextpt_color=yellow" } + param { key: "visual_hints" value: "nextpt_vertex_size=8" } + param { key: "visual_hints" value: "nextpt_lcolor=gray70" } + param { key: "visual_hints" value: "vertex_color=dodger_blue, edge_color=white" } + param { key: "visual_hints" value: "vertex_size=5, edge_size=1" } + } + behavior { + type: "BHV_Waypoint" + name: "waypt_return" + pwt: "100" + condition: "RETURN = true" + condition: "DEPLOY = true" + updates: "RETURN_UPDATE" + perpetual: "true" + endflag: "RETURN = false" + endflag: "DEPLOY = false" + endflag: "MISSION = complete" + param { key: "speed" value: "2.0" } + param { key: "capture_radius" value: "2.0" } + param { key: "slip_radius" value: "8.0" } + param { key: "points" value: "0,-20" } + } + behavior { + type: "BHV_ConstantSpeed" + name: "const_speed" + pwt: "200" + condition: "SPD=true" + condition: "DEPLOY = true" + updates: "SPEED_UPDATE" + duration: "10" + duration_reset: "CONST_SPD_RESET=true" + perpetual: "true" + endflag: "SPD = false" + param { key: "speed" value: "0.5" } + } + } +} +frontseat { enable: true } diff --git a/src/test/missions/goby/s1_alpha_inline/test.yml b/src/test/missions/goby/s1_alpha_inline/test.yml new file mode 100644 index 0000000..c49107e --- /dev/null +++ b/src/test/missions/goby/s1_alpha_inline/test.yml @@ -0,0 +1,2 @@ +# s1_alpha with its behaviors from helm.pb.cfg (inline) +include: [../s1_alpha/test.yml] diff --git a/src/test/missions/goby/s1_alpha_yaml/helm.pb.cfg b/src/test/missions/goby/s1_alpha_yaml/helm.pb.cfg new file mode 100644 index 0000000..f21150c --- /dev/null +++ b/src/test/missions/goby/s1_alpha_yaml/helm.pb.cfg @@ -0,0 +1,6 @@ +helm { + behavior_file: "../../../../launch/goby/alpha/config/alpha.bhv.yml" + domain { name: "speed" low: 0 high: 4 points: 41 } + domain { name: "course" low: 0 high: 359 points: 360 } +} +frontseat { enable: true } diff --git a/src/test/missions/goby/s1_alpha_yaml/test.yml b/src/test/missions/goby/s1_alpha_yaml/test.yml new file mode 100644 index 0000000..4b70465 --- /dev/null +++ b/src/test/missions/goby/s1_alpha_yaml/test.yml @@ -0,0 +1,2 @@ +# s1_alpha with its behaviors from the alpha launch's alpha.bhv.yml +include: [../s1_alpha/test.yml] diff --git a/src/test/missions/replay.cpp b/src/test/missions/replay.cpp index 46044fc..9c16412 100644 --- a/src/test/missions/replay.cpp +++ b/src/test/missions/replay.cpp @@ -19,7 +19,8 @@ // ivpico::helm::Helm, and checks that the helm makes the same decisions and // posts the same things. // -// Usage: replay [--alerts] [stream to print in full, e.g. "loiter LOITER_INDEX"] +// Usage: replay [--alerts] [--via-yaml] +// [stream to print in full, e.g. "loiter LOITER_INDEX"] // // The fixture holds the vehicle's .moos file (for pHelmIvP's configuration), // its .bhv files, and an alog trimmed to pHelmIvP's posts and the posts to @@ -82,6 +83,9 @@ #include #include +#include + +#include "helm/behavior_file.h" #include "helm/conversions.h" #include "helm/helm.h" #include "helm/status_strings.h" @@ -612,18 +616,66 @@ bool compare_desired(const std::string& var, double step, bool wrap, return ok; } +// --via-yaml: the same behaviors, loaded from .bhv.yml files converted from +// the fixture's .bhv files. pHelmIvP reads its .bhv files in name order, and +// the helm reads YAML files in the order given, so they're given sorted. +bool convert_to_yaml(HelmConfig& cfg) +{ +#ifdef HAS_YAML + std::vector files(cfg.behavior_file().begin(), cfg.behavior_file().end()); + std::sort(files.begin(), files.end()); + cfg.clear_behavior_file(); + const fs::path out_dir = + fs::temp_directory_path() / ("ivpico_replay_" + std::to_string(getpid())); + fs::create_directories(out_dir); + // kept until exit, since a restart reads the behaviors again + static const struct RemoveOnExit + { + fs::path dir; + ~RemoveOnExit() { fs::remove_all(dir); } + } remove_on_exit{out_dir}; + for (const auto& file : files) + { + std::ifstream in(file); + std::stringstream text; + text << in.rdbuf(); + std::vector errors; + const auto set = ivpico::helm::read_bhv(text.str(), file, errors); + for (const auto& e : errors) std::cerr << e << "\n"; + if (!errors.empty()) + return false; + const fs::path yaml = out_dir / (fs::path(file).filename().string() + ".yml"); + std::ofstream(yaml) << ivpico::helm::yaml_text(set); + cfg.add_behavior_file(yaml.string()); + std::cout << " behaviors from " << yaml.string() << "\n"; + } + return true; +#else + (void)cfg; + std::cerr << "--via-yaml needs IvPico built with yaml-cpp (enable_yaml)\n"; + return false; +#endif +} + } // namespace int main(int argc, char* argv[]) { std::vector args(argv + 1, argv + argc); - const auto alerts_arg = std::find(args.begin(), args.end(), "--alerts"); - const bool alerts = alerts_arg != args.end(); - if (alerts) - args.erase(alerts_arg); + auto take_flag = [&](const std::string& flag) + { + const auto it = std::find(args.begin(), args.end(), flag); + if (it == args.end()) + return false; + args.erase(it); + return true; + }; + const bool alerts = take_flag("--alerts"); + const bool via_yaml = take_flag("--via-yaml"); if (args.size() != 1 && args.size() != 2) { - std::cerr << "usage: replay [--alerts] [stream to print in full]\n"; + std::cerr << "usage: replay [--alerts] [--via-yaml] [stream to print " + "in full]\n"; return 2; } const std::string print_stream = args.size() == 2 ? args[1] : ""; @@ -644,6 +696,8 @@ int main(int argc, char* argv[]) HelmConfig cfg = read_config(moos_file, dir); cfg.set_contact_alerts(alerts); + if (via_yaml && !convert_to_yaml(cfg)) + return 2; const Alog alog = read_alog(alog_file); auto is_helm = [](const Entry& e) { return e.source.rfind("pHelmIvP", 0) == 0; };