diff --git a/cmake/generate_fmu.cmake b/cmake/generate_fmu.cmake index b45d228..cf0add1 100644 --- a/cmake/generate_fmu.cmake +++ b/cmake/generate_fmu.cmake @@ -1,5 +1,5 @@ -function(generateFMU modelIdentifier) +function(generateFMU modelIdentifier resourceFolder) target_sources(${modelIdentifier} PRIVATE "$") target_include_directories("${modelIdentifier}" PRIVATE "${PROJECT_SOURCE_DIR}/export/include") @@ -26,10 +26,25 @@ function(generateFMU modelIdentifier) WORKING_DIRECTORY "${CMAKE_BINARY_DIR}" COMMAND descriptionGenerator ${modelIdentifier} "${outputDir}/$") + if (resourceFolder STREQUAL "") + add_custom_command(TARGET ${modelIdentifier} POST_BUILD + WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/${modelIdentifier}" + COMMAND ${CMAKE_COMMAND} -E tar "c" "${modelIdentifier}.fmu" --format=zip + "${CMAKE_BINARY_DIR}/${modelIdentifier}/binaries" + "${CMAKE_BINARY_DIR}/${modelIdentifier}/modelDescription.xml") + + else () + message("[generateFMU] Using resourceFolder=${resourceFolder} for ${modelIdentifier}") + + file(COPY "${resourceFolder}/" DESTINATION "${CMAKE_BINARY_DIR}/${modelIdentifier}/resources") + + add_custom_command(TARGET ${modelIdentifier} POST_BUILD + WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/${modelIdentifier}" + COMMAND ${CMAKE_COMMAND} -E tar "c" "${modelIdentifier}.fmu" --format=zip + "resources" + "${CMAKE_BINARY_DIR}/${modelIdentifier}/binaries" + "${CMAKE_BINARY_DIR}/${modelIdentifier}/modelDescription.xml") + endif () + - add_custom_command(TARGET ${modelIdentifier} POST_BUILD - WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/${modelIdentifier}" - COMMAND ${CMAKE_COMMAND} -E tar "c" "${modelIdentifier}.fmu" --format=zip - "${CMAKE_BINARY_DIR}/${modelIdentifier}/binaries" - "${CMAKE_BINARY_DIR}/${modelIdentifier}/modelDescription.xml") endfunction() diff --git a/export/examples/BouncingBall/CMakeLists.txt b/export/examples/BouncingBall/CMakeLists.txt index e252b7d..4da8531 100644 --- a/export/examples/BouncingBall/CMakeLists.txt +++ b/export/examples/BouncingBall/CMakeLists.txt @@ -3,4 +3,4 @@ add_library(bouncing_ball SHARED "bouncing_ball.cpp" ) -generateFMU("bouncing_ball") +generateFMU("bouncing_ball" "") diff --git a/export/examples/BouncingBall/bouncing_ball.cpp b/export/examples/BouncingBall/bouncing_ball.cpp index 15dff22..ceec26d 100644 --- a/export/examples/BouncingBall/bouncing_ball.cpp +++ b/export/examples/BouncingBall/bouncing_ball.cpp @@ -7,7 +7,7 @@ using namespace fmu4cpp; class BouncingBall : public fmu_base { public: - BouncingBall(const std::string &instanceName, const std::string &resources) + BouncingBall(const std::string &instanceName, const std::filesystem::path &resources) : fmu_base(instanceName, resources) { register_variable( @@ -79,6 +79,6 @@ model_info fmu4cpp::get_model_info() { return info; } -std::unique_ptr fmu4cpp::createInstance(const std::string &instanceName, const std::string &fmuResourceLocation) { +std::unique_ptr fmu4cpp::createInstance(const std::string &instanceName, const std::filesystem::path &fmuResourceLocation) { return std::make_unique(instanceName, fmuResourceLocation); } diff --git a/export/examples/CMakeLists.txt b/export/examples/CMakeLists.txt index d0eb5fa..f9aced6 100644 --- a/export/examples/CMakeLists.txt +++ b/export/examples/CMakeLists.txt @@ -1,3 +1,4 @@ add_subdirectory(BouncingBall) add_subdirectory(SimplePendulum) +add_subdirectory(Resource) diff --git a/export/examples/Resource/CMakeLists.txt b/export/examples/Resource/CMakeLists.txt new file mode 100644 index 0000000..d4be233 --- /dev/null +++ b/export/examples/Resource/CMakeLists.txt @@ -0,0 +1,6 @@ + +add_library(resource SHARED + "resource.cpp" +) + +generateFMU("resource" "${CMAKE_CURRENT_SOURCE_DIR}/resources") diff --git a/export/examples/Resource/resource.cpp b/export/examples/Resource/resource.cpp new file mode 100644 index 0000000..e378a8f --- /dev/null +++ b/export/examples/Resource/resource.cpp @@ -0,0 +1,52 @@ + + +#include + +#include + +using namespace fmu4cpp; + + +class Resource : public fmu_base { + +public: + Resource(const std::string &instanceName, const std::filesystem::path &resources) + : fmu_base(instanceName, resources) { + + std::ifstream ifs(resources / "file.txt"); + + std::string line; + std::getline(ifs, line); + + register_variable( + string( + "content", [line] { return line; }) + .setVariability(variability_t::CONSTANT) + .setCausality(causality_t::OUTPUT)); + + Resource::reset(); + } + + bool do_step(double currentTime, double dt) override { + + debugLog(fmi2OK, get_string_variable("content")->get()); + return true; + } + + void reset() override { + // do nothing + } + +}; + +model_info fmu4cpp::get_model_info() { + model_info info; + info.modelName = "Resource"; + info.description = "A model with resources"; + info.modelIdentifier = FMU4CPP_MODEL_IDENTIFIER; + return info; +} + +std::unique_ptr fmu4cpp::createInstance(const std::string &instanceName, const std::filesystem::path &fmuResourceLocation) { + return std::make_unique(instanceName, fmuResourceLocation); +} diff --git a/export/examples/Resource/resources/file.txt b/export/examples/Resource/resources/file.txt new file mode 100644 index 0000000..980a0d5 --- /dev/null +++ b/export/examples/Resource/resources/file.txt @@ -0,0 +1 @@ +Hello World! diff --git a/export/examples/SimplePendulum/CMakeLists.txt b/export/examples/SimplePendulum/CMakeLists.txt index d357a95..22e1618 100644 --- a/export/examples/SimplePendulum/CMakeLists.txt +++ b/export/examples/SimplePendulum/CMakeLists.txt @@ -3,4 +3,4 @@ add_library(simple_pendulum SHARED "simple_pendulum.cpp" ) -generateFMU(simple_pendulum) +generateFMU(simple_pendulum "") diff --git a/export/examples/SimplePendulum/simple_pendulum.cpp b/export/examples/SimplePendulum/simple_pendulum.cpp index d878b9a..e55cbcd 100644 --- a/export/examples/SimplePendulum/simple_pendulum.cpp +++ b/export/examples/SimplePendulum/simple_pendulum.cpp @@ -9,7 +9,7 @@ using namespace fmu4cpp; class SimplePendulum : public fmu_base { public: - SimplePendulum(const std::string &instanceName, const std::string &resources) + SimplePendulum(const std::string &instanceName, const std::filesystem::path &resources) : fmu_base(instanceName, resources) { register_variable(real("angle", @@ -74,6 +74,6 @@ model_info fmu4cpp::get_model_info() { return info; } -std::unique_ptr fmu4cpp::createInstance(const std::string &instanceName, const std::string &fmuResourceLocation) { +std::unique_ptr fmu4cpp::createInstance(const std::string &instanceName, const std::filesystem::path &fmuResourceLocation) { return std::make_unique(instanceName, fmuResourceLocation); } diff --git a/export/include/fmu4cpp/fmu_base.hpp b/export/include/fmu4cpp/fmu_base.hpp index 43ad06a..384e802 100644 --- a/export/include/fmu4cpp/fmu_base.hpp +++ b/export/include/fmu4cpp/fmu_base.hpp @@ -14,12 +14,14 @@ #include "logger.hpp" #include "model_info.hpp" +#include + namespace fmu4cpp { class fmu_base { public: - fmu_base(std::string instance_name, std::string resourceLocation) + fmu_base(std::string instance_name, std::filesystem::path resourceLocation) : instanceName_(std::move(instance_name)), resourceLocation_(std::move(resourceLocation)) {} fmu_base(const fmu_base &) = delete; @@ -29,7 +31,7 @@ namespace fmu4cpp { return instanceName_; } - [[nodiscard]] std::string resourceLocation() const { + [[nodiscard]] const std::filesystem::path& resourceLocation() const { return resourceLocation_; } @@ -181,7 +183,7 @@ namespace fmu4cpp { size_t numVariables_{1}; std::string instanceName_; - std::string resourceLocation_; + std::filesystem::path resourceLocation_; std::vector integers_; std::vector reals_; @@ -193,7 +195,7 @@ namespace fmu4cpp { model_info get_model_info(); - std::unique_ptr createInstance(const std::string &instanceName, const std::string &fmuResourceLocation); + std::unique_ptr createInstance(const std::string &instanceName, const std::filesystem::path &fmuResourceLocation); }// namespace fmu4cpp diff --git a/export/tests/basic_test.cpp b/export/tests/basic_test.cpp index f8e34ec..00d2d51 100644 --- a/export/tests/basic_test.cpp +++ b/export/tests/basic_test.cpp @@ -7,7 +7,7 @@ class Model : public fmu4cpp::fmu_base { public: - Model(const std::string &instanceName, const std::string &resources) + Model(const std::string &instanceName, const std::filesystem::path &resources) : fmu_base(instanceName, resources) { register_variable(real("myReal", [this] { return real_; }) @@ -50,7 +50,7 @@ fmu4cpp::model_info fmu4cpp::get_model_info() { return m; } -std::unique_ptr fmu4cpp::createInstance(const std::string &instanceName, const std::string &fmuResourceLocation) { +std::unique_ptr fmu4cpp::createInstance(const std::string &instanceName, const std::filesystem::path &fmuResourceLocation) { return std::make_unique(instanceName, fmuResourceLocation); } diff --git a/resources/readme.txt b/resources/readme.txt new file mode 100644 index 0000000..724da85 --- /dev/null +++ b/resources/readme.txt @@ -0,0 +1 @@ +place fmu4cpp resources in this folder \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e783b88..88a8e14 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -5,7 +5,7 @@ add_library(${modelIdentifier} SHARED ) # Generate FMU -generateFMU("${modelIdentifier}") +generateFMU("${modelIdentifier}" "${PROJECT_SOURCE_DIR}/resources") set(OUTPUT_FILE "${CMAKE_BINARY_DIR}/model.txt") file(WRITE ${OUTPUT_FILE} "${modelIdentifier}") diff --git a/src/model.cpp b/src/model.cpp index 9e90460..f0069f7 100644 --- a/src/model.cpp +++ b/src/model.cpp @@ -11,7 +11,7 @@ using namespace fmu4cpp; class Model : public fmu_base { public: - Model(const std::string &instanceName, const std::string &resources) + Model(const std::string &instanceName, const std::filesystem::path &resources) : fmu_base(instanceName, resources) { register_variable(integer( @@ -96,6 +96,6 @@ model_info fmu4cpp::get_model_info() { } std::unique_ptr fmu4cpp::createInstance(const std::string &instanceName, - const std::string &fmuResourceLocation) { + const std::filesystem::path &fmuResourceLocation) { return std::make_unique(instanceName, fmuResourceLocation); }