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

Non URI file scheme equal type name support #688

Merged
merged 22 commits into from
Mar 29, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 1 addition & 17 deletions include/cosim/uri.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,21 +274,5 @@ uri path_to_file_uri(const cosim::filesystem::path& path);
cosim::filesystem::path file_uri_to_path(const uri& fileUri);


/**
* Converts a URI with query parameter to a local filesystem path.
*
* \param [in] baseParentUri
* An (absolute) base parent URI.
* \param [in] queryUri
* An URI with arbitrary scheme component. Query file parameter path (if any)
* is specified after `file=`. Prefix `file:///` can be used for absolute path.
*
* \returns
* The local absolute path that corresponds to the `queryUri`, or the `baseParentUri`
* parent path if no file query is available.
*/
cosim::filesystem::path file_query_uri_to_path(const uri& baseParentUri, const uri& queryUri);


} // namespace cosim
#endif // header guard
#endif // header guard
14 changes: 14 additions & 0 deletions src/cosim/osp_config_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,20 @@ void connect_vector_sum_functions(
}
}

cosim::filesystem::path file_query_uri_to_path(
const cosim::uri& baseParentUri,
const cosim::uri& queryUri)
{
const auto query = queryUri.query();
if (query && query->find("file=") < query->size()) {
if (query->find("file=file:///") < query->size()) {
return file_uri_to_path(std::string(query->substr(5)));
}
return file_uri_to_path(std::string(baseParentUri.view()) + "/" + std::string(query->substr(5)));
}
return file_uri_to_path(baseParentUri);
}

} // namespace

osp_config load_osp_config(
Expand Down
15 changes: 0 additions & 15 deletions src/cosim/uri.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -559,19 +559,4 @@ cosim::filesystem::path file_uri_to_path(const uri& fileUri)
}


cosim::filesystem::path file_query_uri_to_path(
const cosim::uri& baseParentUri,
const cosim::uri& queryUri)
{
const auto query = queryUri.query();
if (query && query->find("file=") < query->size()) {
if (query->find("file=file:///") < query->size()) {
return file_uri_to_path(std::string(query->substr(5)));
}
return file_uri_to_path(std::string(baseParentUri.view()) + "/" + std::string(query->substr(5)));
}
return file_uri_to_path(baseParentUri);
}


} // namespace cosim
6 changes: 6 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,16 @@ if(LIBCOSIM_WITH_PROXYFMU)
"proxyfmu_integration_unittest"
"proxyfmu_library_unittest"
)
list(APPEND tests
"proxyfmu_osp_config_parser_test"
)
endif()

include("AddTestExecutable")
foreach(test IN LISTS tests)
if (LIBCOSIM_WITH_PROXYFMU)
list(APPEND dependencies "proxyfmu::proxyfmu-client")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line does nothing as dependencies is not used.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed in latest commit

endif()
add_test_executable(
"cpp_${test}"
FOLDER "C++ tests"
Expand Down
45 changes: 45 additions & 0 deletions tests/data/msmi/OspSystemStructure_proxyfmu.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="utf-8" ?>
<OspSystemStructure
xmlns="http://opensimulationplatform.com/MSMI/OSPSystemStructure"
version="0.1">
<StartTime>0.0</StartTime>
<BaseStepSize>1e-4</BaseStepSize>
<Algorithm>fixedStep</Algorithm>
<Simulators>
<Simulator name="CraneController" source="proxyfmu://localhost?file=../ssp/demo/CraneController.fmu" stepSize="2e-4">
<InitialValues>
<InitialValue variable="cl1_min">
<Real value="2.2"/>
</InitialValue>
<InitialValue variable="cl1_max">
<Real value="3.8"/>
</InitialValue>
</InitialValues>
</Simulator>
<Simulator name="KnuckleBoomCrane" source="proxyfmu://localhost?file=../ssp/demo/KnuckleBoomCrane.fmu" stepSize="2e-4"/>
<Simulator name="TrueIdentity" source="proxyfmu://localhost?file=../fmi1/identity.fmu" stepSize="2.03e-4">
<InitialValues>
<InitialValue variable="booleanIn">
<Boolean value="true"/>
</InitialValue>
</InitialValues>
</Simulator>
<Simulator name="OneIdentity" source="proxyfmu://localhost?file=../fmi1/identity.fmu" stepSize="2.03e-4">
<InitialValues>
<InitialValue variable="booleanIn">
<Boolean value="1"/>
</InitialValue>
</InitialValues>
</Simulator>
</Simulators>
<Connections>
<VariableGroupConnection>
<VariableGroup simulator="KnuckleBoomCrane" name="actuatorLimits"/>
<VariableGroup simulator="CraneController" name="actuatorLimits"/>
</VariableGroupConnection>
<VariableGroupConnection>
<VariableGroup simulator="KnuckleBoomCrane" name="linear mechanical port"/>
<VariableGroup simulator="CraneController" name="linear mechanical port"/>
</VariableGroupConnection>
</Connections>
</OspSystemStructure>
58 changes: 58 additions & 0 deletions tests/proxyfmu_osp_config_parser_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <cosim/algorithm/fixed_step_algorithm.hpp>
#include <cosim/fs_portability.hpp>
#include <cosim/log/simple.hpp>
#include <cosim/observer/last_value_observer.hpp>
#include <cosim/orchestration.hpp>
#include <cosim/osp_config_parser.hpp>

#include <cstdlib>
#include <exception>


#define REQUIRE(test) \
if (!(test)) throw std::runtime_error("Requirement not satisfied: " #test)

void test(const cosim::filesystem::path& configPath, size_t expectedNumConnections)
{
auto resolver = cosim::default_model_uri_resolver();
const auto config = cosim::load_osp_config(configPath, *resolver);
auto execution = cosim::execution(
config.start_time,
std::make_shared<cosim::fixed_step_algorithm>(config.step_size));

const auto entityMaps = cosim::inject_system_structure(
execution, config.system_structure, config.initial_values);
REQUIRE(entityMaps.simulators.size() == 4);
REQUIRE(boost::size(config.system_structure.connections()) == expectedNumConnections);

auto obs = std::make_shared<cosim::last_value_observer>();
execution.add_observer(obs);

auto result = execution.simulate_until(cosim::to_time_point(1e-3));
REQUIRE(result.get());

const auto simIndex = entityMaps.simulators.at("CraneController");
const auto varReference =
config.system_structure.get_variable_description({"CraneController", "cl1_min"}).reference;
double realValue = -1.0;
obs->get_real(simIndex, gsl::make_span(&varReference, 1), gsl::make_span(&realValue, 1));

double magicNumberFromConf = 2.2;
REQUIRE(std::fabs(realValue - magicNumberFromConf) < 1e-9);
}

int main()
{
try {
cosim::log::setup_simple_console_logging();
cosim::log::set_global_output_level(cosim::log::info);

const auto testDataDir = std::getenv("TEST_DATA_DIR");
REQUIRE(testDataDir);
test(cosim::filesystem::path(testDataDir) / "msmi" / "OspSystemStructure_proxyfmu.xml", 9);
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what();
return 1;
}
return 0;
}
24 changes: 0 additions & 24 deletions tests/uri_unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,27 +222,3 @@ BOOST_AUTO_TEST_CASE(file_uri_conversions)
BOOST_CHECK_THROW(file_uri_to_path("http://foo/bar"), std::invalid_argument);
BOOST_CHECK_THROW(file_uri_to_path("file://foo/bar"), std::invalid_argument);
}


BOOST_AUTO_TEST_CASE(file_query_uri_conversions)
{
// From URI file query to path
const auto baseParentURI = uri("file:///c:/foo");
#ifdef _WIN32
BOOST_TEST(file_query_uri_to_path(baseParentURI, "proxyfmu://foo%20bar/bar?file=baz.txt") == "c:\\foo\\baz.txt");
BOOST_TEST(file_query_uri_to_path(baseParentURI, "proxyfmu://foo%20bar/bar/foo?file=bar/baz.txt") == "c:\\foo\\bar\\baz.txt");
BOOST_TEST(file_query_uri_to_path(baseParentURI, "http://foo%20bar/bar?file=baz.txt") == "c:\\foo\\baz.txt");
BOOST_TEST(file_query_uri_to_path(baseParentURI, "http://foo%20bar/foo/bar?file=bar%20foo/baz.txt") == "c:\\foo\\bar foo\\baz.txt");
BOOST_TEST(file_query_uri_to_path(baseParentURI, "proxyfmu://foo/bar?file=file:///c:/baz.txt") == "c:\\baz.txt");
BOOST_TEST(file_query_uri_to_path(baseParentURI, "http://foo%20baz/bar?file=file:///c:/foo/bar/baz.txt") == "c:\\foo\\bar\\baz.txt");
BOOST_TEST(file_query_uri_to_path(baseParentURI, "http://foo%20baz/bar?foo=baz.txt") == "c:\\foo");
#else
BOOST_TEST(file_query_uri_to_path(baseParentURI, "proxyfmu://foo%20bar/bar?file=baz.txt") == "/c:/foo/baz.txt");
BOOST_TEST(file_query_uri_to_path(baseParentURI, "proxyfmu://foo%20bar/bar/foo?file=bar/baz.txt") == "/c:/foo/bar/baz.txt");
BOOST_TEST(file_query_uri_to_path(baseParentURI, "http://foo%20bar/bar?file=baz.txt") == "/c:/foo/baz.txt");
BOOST_TEST(file_query_uri_to_path(baseParentURI, "http://foo%20bar/foo/bar?file=bar%20foo/baz.txt") == "/c:/foo/bar foo/baz.txt");
BOOST_TEST(file_query_uri_to_path(baseParentURI, "proxyfmu://foo/bar?file=file:///c:/baz.txt") == "/c:/baz.txt");
BOOST_TEST(file_query_uri_to_path(baseParentURI, "http://foo%20baz/bar?file=file:///c:/foo/bar/baz.txt") == "/c:/foo/bar/baz.txt");
BOOST_TEST(file_query_uri_to_path(baseParentURI, "http://foo%20baz/bar?foo=baz.txt") == "/c:/foo");
#endif
}