Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support yaml scenarios #275

Merged
merged 28 commits into from
Nov 12, 2019
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
56c5f6e
Replace json with yaml for scenarios
markaren Jun 18, 2019
3559b05
Missing a comma somehow
markaren Jun 18, 2019
98aa8a0
Making Jenkins great again!
Jun 20, 2019
2cd1f60
Fix ubuntu dependecies
markaren Jun 21, 2019
f064fd2
Merge remote-tracking branch 'origin/feature/271-yaml-scenario-parser…
markaren Jun 21, 2019
d4f5bdb
Merge branch 'master' into feature/271-yaml-scenario-parser
markaren Jul 4, 2019
d224ef1
Merge remote-tracking branch 'origin/master' into feature/271-yaml-sc…
markaren Jul 4, 2019
aa0d25b
Merge branch 'master' into feature/271-yaml-scenario-parser
markaren Jul 4, 2019
2b8dae7
Merge branch 'master' into feature/271-yaml-scenario-parser
markaren Sep 10, 2019
cd02cc2
Replace nlohmann_json with yaml-cpp in find_dependency
markaren Sep 10, 2019
007a76b
Update conanfile
markaren Sep 10, 2019
c59fc73
Fix copy of non-existent file
markaren Sep 10, 2019
aab5c71
Remove json reference
markaren Sep 10, 2019
f56c219
Merge branch 'master' into feature/271-yaml-scenario-parser
markaren Sep 24, 2019
fc15d63
Merge branch 'master' into feature/271-yaml-scenario-parser
Oct 8, 2019
f415a0b
Add Findyaml-cpp.cmake
Oct 8, 2019
6dc0d1f
Revert change to yaml-cpp require
Oct 8, 2019
8c37de2
Merge branch 'master' into feature/271-yaml-scenario-parser
markaren Nov 1, 2019
1324895
Merge branch 'master' into feature/271-yaml-scenario-parser
markaren Nov 1, 2019
abdce2f
Fix copy paste error
markaren Nov 1, 2019
11f9db0
fix error
markaren Nov 1, 2019
27b2cec
Merge branch 'master' into feature/271-yaml-scenario-parser
markaren Nov 11, 2019
facae6f
Test both .json and .yml
markaren Nov 11, 2019
14d91da
Fix typo
markaren Nov 11, 2019
8d2488f
Remove type and causality from .yml test file
markaren Nov 11, 2019
e441145
yaml-cpp is now found in conan-center
markaren Nov 11, 2019
504dbdb
Update cmake/Findyaml-cpp.cmake
markaren Nov 11, 2019
8f61fdf
Update doc: yaml -> json or yaml
markaren Nov 12, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .dockerfiles/Dockerfile.build
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ RUN apt-get update && \
libevent-dev \
libzip-dev \
libmsgsl-dev \
nlohmann-json-dev \
libyaml-cpp-dev \
libxerces-c-dev \
libxerces-c3.2 \
&& rm -rf /var/lib/apt/lists/*
Expand Down
3 changes: 1 addition & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ find_package(MS_GSL REQUIRED)
find_package(Boost REQUIRED COMPONENTS ${Boost_components})
find_package(FMILibrary REQUIRED)
find_package(LIBZIP REQUIRED)
find_package(nlohmann_json REQUIRED)
find_package(yaml-cpp REQUIRED)
find_package(XercesC REQUIRED)
if(CSECORE_WITH_FMUPROXY)
find_package(THRIFT REQUIRED)
Expand Down Expand Up @@ -225,7 +225,6 @@ install(FILES
"${CMAKE_SOURCE_DIR}/cmake/FindFMILibrary.cmake"
"${CMAKE_SOURCE_DIR}/cmake/FindLIBZIP.cmake"
"${CMAKE_SOURCE_DIR}/cmake/FindMS_GSL.cmake"
"${CMAKE_SOURCE_DIR}/cmake/Findnlohmann_json.cmake"
"${CMAKE_SOURCE_DIR}/cmake/FindTHRIFT.cmake"
DESTINATION
"${CSECORE_CMAKE_INSTALL_DIR}"
Expand Down
13 changes: 0 additions & 13 deletions cmake/Findnlohmann_json.cmake

This file was deleted.

36 changes: 36 additions & 0 deletions cmake/Findyaml-cpp.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Find yaml-cpp
#
# Find the native yaml-cpp headers and libraries.
#
# YAML_CPP_INCLUDE_DIRS - where to find yaml-cpp/yaml.h
# YAML_CPP_LIBRARIES - List of libraries when using yaml-cpp.
# YAML_CPP_FOUND - True if yaml-cpp found.
#

find_path(YAML_CPP_INCLUDE_DIR NAMES yaml-cpp/yaml.h)
mark_as_advanced(YAML_CPP_INCLUDE_DIR)

find_library(YAML_CPP_LIBRARY NAMES yaml-cpp libyaml-cpp yaml-cppmd libyaml-cppmd)
markaren marked this conversation as resolved.
Show resolved Hide resolved
mark_as_advanced(YAML_CPP_LIBRARY)

include(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(YAML_CPP
REQUIRED_VARS YAML_CPP_LIBRARY YAML_CPP_INCLUDE_DIR)

if (YAML_CPP_FOUND)

set(YAML_CPP_INCLUDE_DIRS ${YAML_CPP_INCLUDE_DIR})

if (NOT YAML_CPP_LIBRARIES)
set(YAML_CPP_LIBRARIES ${YAML_CPP_LIBRARY})
endif()

if (NOT TARGET yaml-cpp)
add_library(yaml-cpp UNKNOWN IMPORTED)
set_target_properties(yaml-cpp PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${YAML_CPP_INCLUDE_DIR}")
set_property(TARGET yaml-cpp APPEND PROPERTY
IMPORTED_LOCATION "${YAML_CPP_LIBRARY}")
endif()

endif()
2 changes: 1 addition & 1 deletion cmake/project-config.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ find_dependency(MS_GSL REQUIRED)
find_dependency(Boost REQUIRED COMPONENTS date_time fiber filesystem log)
find_dependency(FMILibrary REQUIRED)
find_dependency(LIBZIP REQUIRED)
find_dependency(nlohmann_json REQUIRED)
find_dependency(yaml-cpp REQUIRED)
find_dependency(XercesC REQUIRED)

if(@CSECORE_WITH_FMUPROXY@)
Expand Down
2 changes: 1 addition & 1 deletion conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class CSECoreConan(ConanFile):
"gsl_microsoft/20171020@bincrafters/stable",
"libzip/1.5.2@bincrafters/stable",
"zlib/1.2.11",
"jsonformoderncpp/3.5.0@vthiery/stable",
"yaml-cpp/0.6.2@bincrafters/stable",
"bzip2/1.0.8",
"xerces-c/3.2.2"
)
Expand Down
2 changes: 1 addition & 1 deletion include/cse/scenario_parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace cse
* Parses a scenario from file.
*
* \param scenarioFile
* The path to a proprietary `json` file defining the scenario.
* The path to a proprietary `yaml` file defining the scenario.
*
* \param simulators
* A map containing the simulators currently loaded in the execution.
Expand Down
2 changes: 1 addition & 1 deletion src/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ target_link_libraries(csecorecpp
gsl
XercesC::XercesC
PRIVATE
nlohmann_json
yaml-cpp
)

if(CSECORE_WITH_FMUPROXY)
Expand Down
54 changes: 31 additions & 23 deletions src/cpp/scenario_parser.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "cse/scenario_parser.hpp"

#include <boost/filesystem/fstream.hpp>
#include <nlohmann/json.hpp>

#include <cstdlib>
#include <functional>
Expand All @@ -12,6 +11,16 @@
#include <string>
#include <system_error>

#ifdef _MSC_VER
# pragma warning(push)
# pragma warning(disable : 4127)
#endif
#include <yaml-cpp/yaml.h>
#ifdef _MSC_VER
# pragma warning(pop)
#endif


namespace cse
{

Expand Down Expand Up @@ -65,12 +74,12 @@ cse::variable_description find_variable(
template<typename T>
std::function<T(T)> generate_modifier(
const std::string& kind,
const nlohmann::json& event)
const YAML::Node& event)
{
if ("reset" == kind) {
return nullptr;
}
T value = event.at("value").get<T>();
T value = event["value"].as<T>();
if ("bias" == kind) {
return [value](T original) { return original + value; };
} else if ("override" == kind) {
Expand All @@ -83,12 +92,12 @@ std::function<T(T)> generate_modifier(

std::function<std::string(std::string_view)> generate_string_modifier(
const std::string& kind,
const nlohmann::json& event)
const YAML::Node& event)
{
if ("reset" == kind) {
return nullptr;
}
auto value = event.at("value").get<std::string>();
auto value = event["value"].as<std::string>();
if ("override" == kind) {
return [value](std::string_view /*original*/) { return value; };
}
Expand All @@ -98,7 +107,7 @@ std::function<std::string(std::string_view)> generate_string_modifier(
}

cse::scenario::variable_action generate_action(
const nlohmann::json& event,
const YAML::Node& event,
const std::string& mode,
cse::simulator_index sim,
cse::variable_type type,
Expand Down Expand Up @@ -141,20 +150,20 @@ struct defaults
};

std::optional<std::string> parse_element(
const nlohmann::json& j,
const YAML::Node& j,
const std::string& name)
{
if (j.count(name)) {
return j.at(name).get<std::string>();
if (j[name]) {
return j[name].as<std::string>();
} else {
return std::nullopt;
}
}

defaults parse_defaults(const nlohmann::json& scenario)
defaults parse_defaults(const YAML::Node& scenario)
{
if (scenario.count("defaults")) {
auto j = scenario.at("defaults");
if (scenario["defaults"]) {
auto j = scenario["defaults"];
return defaults{
parse_element(j, "model"),
parse_element(j, "variable"),
Expand All @@ -164,12 +173,12 @@ defaults parse_defaults(const nlohmann::json& scenario)
}

std::string specified_or_default(
const nlohmann::json& j,
const YAML::Node& j,
const std::string& name,
std::optional<std::string> defaultOption)
{
if (j.count(name)) {
return j.at(name).get<std::string>();
if (j[name]) {
return j[name].as<std::string>();
} else if (defaultOption.has_value()) {
return *defaultOption;
}
Expand All @@ -178,10 +187,10 @@ std::string specified_or_default(
throw std::invalid_argument(oss.str());
}

std::optional<cse::time_point> parse_end_time(const nlohmann::json& j)
std::optional<cse::time_point> parse_end_time(const YAML::Node& j)
{
if (j.count("end")) {
auto endTime = j.at("end").get<double>();
if (j["end"]) {
auto endTime = j["end"].as<double>();
return to_time_point(endTime);
}
return std::nullopt;
Expand All @@ -201,15 +210,14 @@ scenario::scenario parse_scenario(
oss << "Cannot load scenario. Failed to open file " << scenarioFile;
throw std::system_error(errno, std::system_category(), oss.str());
}
nlohmann::json j;
i >> j;
YAML::Node j = YAML::Load(i);

std::vector<scenario::event> events;
defaults defaultOpts = parse_defaults(j);

for (auto& event : j.at("events")) {
auto trigger = event.at("time");
auto triggerTime = trigger.get<double>();
for (const auto& event : j["events"]) {
auto trigger = event["time"];
eidekrist marked this conversation as resolved.
Show resolved Hide resolved
auto triggerTime = trigger.as<double>();
auto time = to_time_point(triggerTime);

const auto& [index, simulator] =
Expand Down
2 changes: 1 addition & 1 deletion test/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ set(tests
"trend_buffer_test"
"monitor_modified_variables_test"
"scenario_manager_test"
"scenario_parser_test"
"synchronized_xy_series_test"
)

Expand All @@ -28,6 +27,7 @@ set(unittests
"fmi_v2_fmu_unittest"
"model_unittest"
"orchestration_unittest"
"scenario_parser_unittest"
"ssp_parser_unittest"
"uri_unittest"
"utility_concurrency_unittest"
Expand Down
86 changes: 0 additions & 86 deletions test/cpp/scenario_parser_test.cpp

This file was deleted.

Loading