From e13ba8ba11574aea01955da5f6e385b94bbc5d4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20=C5=81oskot?= Date: Wed, 8 Mar 2017 11:27:38 +0100 Subject: [PATCH] Add basic facades for Extractor and Contractor run methods. Based on idea suggested in comments to #3776, simplifies use of extractor and contractor as libraries. --- CMakeLists.txt | 4 +-- include/osrm/contractor.hpp | 50 ++++++++++++++++++++++++++++++ include/osrm/contractor_config.hpp | 38 +++++++++++++++++++++++ include/osrm/extractor.hpp | 50 ++++++++++++++++++++++++++++++ include/osrm/extractor_config.hpp | 38 +++++++++++++++++++++++ src/osrm/contractor.cpp | 12 +++++++ src/osrm/extractor.cpp | 17 ++++++++++ unit_tests/CMakeLists.txt | 25 ++++++++++++++- unit_tests/library/contract.cpp | 21 +++++++++++++ unit_tests/library/extract.cpp | 27 ++++++++++++++++ 10 files changed, 279 insertions(+), 3 deletions(-) create mode 100644 include/osrm/contractor.hpp create mode 100644 include/osrm/contractor_config.hpp create mode 100644 include/osrm/extractor.hpp create mode 100644 include/osrm/extractor_config.hpp create mode 100644 src/osrm/contractor.cpp create mode 100644 src/osrm/extractor.cpp create mode 100644 unit_tests/library/contract.cpp create mode 100644 unit_tests/library/extract.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 7c3396fa5..ea32ec110 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -143,10 +143,10 @@ add_executable(osrm-contract src/tools/contract.cpp) add_executable(osrm-routed src/tools/routed.cpp $ $) add_executable(osrm-datastore src/tools/store.cpp $) add_library(osrm src/osrm/osrm.cpp $ $ $) -add_library(osrm_extract $ $) +add_library(osrm_contract src/osrm/contractor.cpp $ $) +add_library(osrm_extract src/osrm/extractor.cpp $ $) add_library(osrm_partition $ $) add_library(osrm_customize $ $) -add_library(osrm_contract $ $) add_library(osrm_store $ $) if(ENABLE_GOLD_LINKER) diff --git a/include/osrm/contractor.hpp b/include/osrm/contractor.hpp new file mode 100644 index 000000000..9e6b67261 --- /dev/null +++ b/include/osrm/contractor.hpp @@ -0,0 +1,50 @@ +/* + +Copyright (c) 2017, Project OSRM contributors +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + +Redistributions of source code must retain the above copyright notice, this list +of conditions and the following disclaimer. +Redistributions in binary form must reproduce the above copyright notice, this +list of conditions and the following disclaimer in the documentation and/or +other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +*/ + +#ifndef OSRM_CONTRACTOR_HPP +#define OSRM_CONTRACTOR_HPP + +namespace osrm +{ +namespace contractor +{ +struct ContractorConfig; + +} // ns contractor + +/** + * Runs contraction hierarchy computation process. + * + * \param config The user-provided contraction configuration. + * \throws osrm::util::exception + * \see Contractor, ContractorConfig + */ +void contract(const contractor::ContractorConfig &config); + +} // ns osrm + +#endif // OSRM_CONTRACTOR_HPP diff --git a/include/osrm/contractor_config.hpp b/include/osrm/contractor_config.hpp new file mode 100644 index 000000000..d509e2937 --- /dev/null +++ b/include/osrm/contractor_config.hpp @@ -0,0 +1,38 @@ +/* + +Copyright (c) 2017, Project OSRM contributors +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + +Redistributions of source code must retain the above copyright notice, this list +of conditions and the following disclaimer. +Redistributions in binary form must reproduce the above copyright notice, this +list of conditions and the following disclaimer in the documentation and/or +other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +*/ + +#ifndef GLOBAL_CONTRACTOR_CONFIG_HPP +#define GLOBAL_CONTRACTOR_CONFIG_HPP + +#include "contractor/contractor_config.hpp" + +namespace osrm +{ +using contractor::ContractorConfig; +} + +#endif diff --git a/include/osrm/extractor.hpp b/include/osrm/extractor.hpp new file mode 100644 index 000000000..a598980b0 --- /dev/null +++ b/include/osrm/extractor.hpp @@ -0,0 +1,50 @@ +/* + +Copyright (c) 2017, Project OSRM contributors +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + +Redistributions of source code must retain the above copyright notice, this list +of conditions and the following disclaimer. +Redistributions in binary form must reproduce the above copyright notice, this +list of conditions and the following disclaimer in the documentation and/or +other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +*/ + +#ifndef OSRM_EXTRACTOR_HPP +#define OSRM_EXTRACTOR_HPP + +namespace osrm +{ +namespace extractor +{ +struct ExtractorConfig; + +} // ns contractor + +/** + * Runs extraction process. + * + * \param config The user-provided extraction configuration. + * \throws osrm::util::exception, osmium::io_error + * \see Extractor, ExtractorConfig + */ +void extract(const extractor::ExtractorConfig &config); + +} // ns osrm + +#endif // OSRM_EXTRACTOR_HPP diff --git a/include/osrm/extractor_config.hpp b/include/osrm/extractor_config.hpp new file mode 100644 index 000000000..f0e340624 --- /dev/null +++ b/include/osrm/extractor_config.hpp @@ -0,0 +1,38 @@ +/* + +Copyright (c) 2017, Project OSRM contributors +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + +Redistributions of source code must retain the above copyright notice, this list +of conditions and the following disclaimer. +Redistributions in binary form must reproduce the above copyright notice, this +list of conditions and the following disclaimer in the documentation and/or +other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +*/ + +#ifndef GLOBAL_EXTRACTOR_CONFIG_HPP +#define GLOBAL_EXTRACTOR_CONFIG_HPP + +#include "extractor/extractor_config.hpp" + +namespace osrm +{ +using extractor::ExtractorConfig; +} + +#endif diff --git a/src/osrm/contractor.cpp b/src/osrm/contractor.cpp new file mode 100644 index 000000000..a91c542ab --- /dev/null +++ b/src/osrm/contractor.cpp @@ -0,0 +1,12 @@ +#include "osrm/contractor.hpp" +#include "contractor/contractor.hpp" +#include "contractor/contractor_config.hpp" + +namespace osrm +{ + +// Pimpl-like facade + +void contract(const contractor::ContractorConfig &config) { contractor::Contractor(config).Run(); } + +} // ns osrm diff --git a/src/osrm/extractor.cpp b/src/osrm/extractor.cpp new file mode 100644 index 000000000..2e50c7f75 --- /dev/null +++ b/src/osrm/extractor.cpp @@ -0,0 +1,17 @@ +#include "osrm/extractor.hpp" +#include "extractor/extractor.hpp" +#include "extractor/extractor_config.hpp" +#include "extractor/scripting_environment_lua.hpp" + +namespace osrm +{ + +// Pimpl-like facade + +void extract(const extractor::ExtractorConfig &config) +{ + extractor::Sol2ScriptingEnvironment scripting_environment(config.profile_path.string().c_str()); + extractor::Extractor(config).run(scripting_environment); +} + +} // ns osrm diff --git a/unit_tests/CMakeLists.txt b/unit_tests/CMakeLists.txt index 5facf62a3..f5999e761 100644 --- a/unit_tests/CMakeLists.txt +++ b/unit_tests/CMakeLists.txt @@ -13,6 +13,15 @@ file(GLOB PartitionTestsSources file(GLOB LibraryTestsSources library_tests.cpp library/*.cpp) +list(REMOVE_ITEM LibraryTestsSources ${CMAKE_CURRENT_SOURCE_DIR}/library/extract.cpp ${CMAKE_CURRENT_SOURCE_DIR}/library/contract.cpp) + +file(GLOB LibraryExtractTestsSources + library_tests.cpp + library/extract.cpp) + +file(GLOB LibraryContractTestsSources + library_tests.cpp + library/contract.cpp) file(GLOB ServerTestsSources server_tests.cpp @@ -41,6 +50,14 @@ add_executable(library-tests EXCLUDE_FROM_ALL ${LibraryTestsSources}) +add_executable(library-extract-tests + EXCLUDE_FROM_ALL + ${LibraryExtractTestsSources}) + +add_executable(library-contract-tests + EXCLUDE_FROM_ALL + ${LibraryContractTestsSources}) + add_executable(server-tests EXCLUDE_FROM_ALL ${ServerTestsSources} @@ -65,9 +82,13 @@ endif() target_compile_definitions(extractor-tests PRIVATE COMPILE_DEFINITIONS OSRM_FIXTURES_DIR="${CMAKE_SOURCE_DIR}/unit_tests/fixtures") target_compile_definitions(library-tests PRIVATE COMPILE_DEFINITIONS OSRM_TEST_DATA_DIR="${TEST_DATA_DIR}") +target_compile_definitions(library-extract-tests PRIVATE COMPILE_DEFINITIONS OSRM_TEST_DATA_DIR="${TEST_DATA_DIR}") +target_compile_definitions(library-contract-tests PRIVATE COMPILE_DEFINITIONS OSRM_TEST_DATA_DIR="${TEST_DATA_DIR}") target_include_directories(engine-tests PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) target_include_directories(library-tests PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) +target_include_directories(library-extract-tests PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) +target_include_directories(library-contract-tests PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) target_include_directories(util-tests PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) target_include_directories(partition-tests PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) @@ -75,8 +96,10 @@ target_link_libraries(engine-tests ${ENGINE_LIBRARIES} ${Boost_UNIT_TEST_FRAMEWO target_link_libraries(extractor-tests ${EXTRACTOR_LIBRARIES} ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY}) target_link_libraries(partition-tests ${PARTITIONER_LIBRARIES} ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY}) target_link_libraries(library-tests osrm ${ENGINE_LIBRARIES} ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY}) +target_link_libraries(library-extract-tests osrm_extract ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY}) +target_link_libraries(library-contract-tests osrm_contract ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY}) target_link_libraries(server-tests osrm ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY}) target_link_libraries(util-tests ${UTIL_LIBRARIES} ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY}) add_custom_target(tests - DEPENDS engine-tests extractor-tests partition-tests library-tests server-tests util-tests) + DEPENDS engine-tests extractor-tests partition-tests library-tests library-extract-tests server-tests util-tests) diff --git a/unit_tests/library/contract.cpp b/unit_tests/library/contract.cpp new file mode 100644 index 000000000..d7dbdcadc --- /dev/null +++ b/unit_tests/library/contract.cpp @@ -0,0 +1,21 @@ +#include +#include + +#include "osrm/contractor.hpp" +#include "osrm/contractor_config.hpp" + +#include // default_num_threads + +BOOST_AUTO_TEST_SUITE(library_contract) + +BOOST_AUTO_TEST_CASE(test_contract_with_invalid_config) +{ + using namespace osrm; + + osrm::ContractorConfig config; + config.requested_num_threads = tbb::task_scheduler_init::default_num_threads(); + BOOST_CHECK_THROW(osrm::contract(config), + std::exception); // including osrm::util::exception, etc. +} + +BOOST_AUTO_TEST_SUITE_END() diff --git a/unit_tests/library/extract.cpp b/unit_tests/library/extract.cpp new file mode 100644 index 000000000..b65f4b9c3 --- /dev/null +++ b/unit_tests/library/extract.cpp @@ -0,0 +1,27 @@ +#include +#include + +#include "osrm/extractor.hpp" +#include "osrm/extractor_config.hpp" + +#include // default_num_threads + +BOOST_AUTO_TEST_SUITE(library_extract) + +BOOST_AUTO_TEST_CASE(test_extract_with_invalid_config) +{ + osrm::ExtractorConfig config; + config.requested_num_threads = tbb::task_scheduler_init::default_num_threads(); + BOOST_CHECK_THROW(osrm::extract(config), + std::exception); // including osrm::util::exception, osmium::io_error, etc. +} + +BOOST_AUTO_TEST_CASE(test_extract_with_valid_config) +{ + osrm::ExtractorConfig config; + config.input_path = {OSRM_TEST_DATA_DIR "/monaco.osm.pbf"}; + config.requested_num_threads = tbb::task_scheduler_init::default_num_threads(); + BOOST_CHECK_NO_THROW(osrm::extract(config)); +} + +BOOST_AUTO_TEST_SUITE_END()