Skip to content

Commit

Permalink
adr (#485)
Browse files Browse the repository at this point in the history
  • Loading branch information
felixguendling authored Mar 30, 2024
1 parent a44e823 commit 20568fc
Show file tree
Hide file tree
Showing 11 changed files with 224 additions and 18 deletions.
17 changes: 3 additions & 14 deletions .github/workflows/unix.yml
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ jobs:
--dataset.write_serialized false \
--nigiri.no_cache true \
--ris.db_max_size 1048576 \
--exclude_modules address osrm parking path ppr tiles tripbased gbfs transfers valhalla osr
--exclude_modules address osrm parking path ppr tiles tripbased gbfs transfers valhalla osr adr
- name: Run Tests
if: matrix.config.tests == 'On'
Expand Down Expand Up @@ -277,11 +277,6 @@ jobs:
artifact: linux-amd64
- preset: linux-amd64-noavx-release
artifact: linux-amd64-noavx
- preset: linux-x86-release
artifact: linux-x86
- preset: linux-arm-release
artifact: linux-arm
emulator: qemu-arm-static
- preset: linux-arm64-release
artifact: linux-arm64
emulator: qemu-aarch64-static
Expand Down Expand Up @@ -326,7 +321,7 @@ jobs:
--dataset.write_serialized false \
--nigiri.no_cache true \
--ris.db_max_size 1048576 \
--exclude_modules address osrm parking path ppr tiles tripbased gbfs transfers valhalla osr
--exclude_modules address osrm parking path ppr tiles tripbased gbfs transfers valhalla osr adr
- name: Run Tests
if: ${{ !matrix.config.skiptests }}
Expand Down Expand Up @@ -417,12 +412,6 @@ jobs:
os: macos-13
- artifact: linux-amd64
os: ubuntu-20.04
- artifact: linux-x86
os: ubuntu-20.04
- artifact: linux-arm
os: ubuntu-20.04
emulator: qemu-arm-static -L /usr/arm-linux-gnueabihf
packages: bzip2 unzip qemu-user-static
- artifact: linux-arm64
os: ubuntu-20.04
emulator: qemu-aarch64-static -L /usr/aarch64-linux-gnu
Expand Down Expand Up @@ -512,4 +501,4 @@ jobs:
context: .
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
platforms: linux/amd64,linux/arm/v7,linux/arm64
platforms: linux/amd64,linux/arm64
2 changes: 1 addition & 1 deletion .github/workflows/windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ jobs:
--dataset.begin 20151004
--dataset.write_serialized false
--nigiri.no_cache true
--exclude_modules address osrm parking path ppr tiles tripbased gbfs transfers valhalla
--exclude_modules address osrm parking path ppr tiles tripbased gbfs transfers valhalla osr adr
- name: Run Tests
if: matrix.config.webui == 'Off'
Expand Down
6 changes: 5 additions & 1 deletion .pkg
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,8 @@
[osr]
[email protected]:motis-project/osr.git
branch=master
commit=fb08e8acb813b64314458998a9290f4aa8e87d0b
commit=fb08e8acb813b64314458998a9290f4aa8e87d0b
[adr]
[email protected]:triptix-tech/adr.git
branch=master
commit=f044654571cfe5dd4cdd5260b16d9907dc270644
6 changes: 5 additions & 1 deletion .pkg.lock
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
2454708555057938301
11991521668100631100
cista 97228b567ac92f5a7e737f38719a0a0654aad72a
zlib fe8e13ffca867612951bc6baf114e5ac8b00f305
boost 1c3f21c1fa8b149da89e2f6bcb48b28fff30fa5e
Expand Down Expand Up @@ -60,3 +60,7 @@ microtar c71afe40887ae1a187ec24cedd85c765058d9978
robin-hood-hashing bd632059ea0bb5cf444b33bdfb4eba2118300c62
spatialite 7b8cd6447ac8632bd2c6c8ff9fbc10151742ef18
valhalla bcc77cb2746e3d234a34c73bd13880c32a96e6cc
FTXUI dd6a5d371fd7a3e2937bb579955003c54b727233
utf8proc 1cb28a66ca79a0845e99433fd1056257456cef8b
tg 80205a3b81c16459fa308d47e17b0b908a6683e3
adr 22d10698e2a881e0f761ecf5be53acf5c8bde53a
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ configure_file (
################################
if (MOTIS_MIMALLOC)
set(PPR_MIMALLOC ON)
set(ADR_MIMALLOC ON)
set(OSR_MIMALLOC ON)
if(WIN32)
set(MI_BUILD_SHARED ON)
endif()
Expand Down
2 changes: 1 addition & 1 deletion base/data/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ endif()
add_library(motis-data INTERFACE)
target_compile_features(motis-data INTERFACE cxx_std_17)
target_include_directories(motis-data INTERFACE include)
target_link_libraries(motis-data INTERFACE cista)
target_link_libraries(motis-data INTERFACE cista ${motis-mimalloc-lib})
if (MOTIS_SCHEDULE_MODE STREQUAL "raw")
target_compile_definitions(motis-data INTERFACE MOTIS_SCHEDULE_MODE_RAW)
else()
Expand Down
15 changes: 15 additions & 0 deletions modules/adr/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
cmake_minimum_required(VERSION 3.10)
project(motis)

include_directories(include)

file(GLOB_RECURSE motis-adr-files src/*.cc)
add_library(motis-adr STATIC ${motis-adr-files})
target_link_libraries(motis-adr
adr
motis-module
boost-thread
)
target_include_directories(motis-adr PUBLIC include)
target_compile_features(motis-adr PUBLIC cxx_std_23)
target_compile_options(motis-adr PRIVATE ${MOTIS_CXX_FLAGS})
30 changes: 30 additions & 0 deletions modules/adr/include/motis/adr/adr.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once

#include "motis/module/module.h"

namespace motis::adr {

struct adr : public motis::module::module {
adr();
~adr() override;

adr(adr const&) = delete;
adr& operator=(adr const&) = delete;

adr(adr&&) = delete;
adr& operator=(adr&&) = delete;

void import(motis::module::import_dispatcher&) override;
void init(motis::module::registry&) override;

bool import_successful() const override { return import_successful_; }

private:
motis::module::msg_ptr guess(motis::module::msg_ptr const&);

struct impl;
std::unique_ptr<impl> impl_;
bool import_successful_{false};
};

} // namespace motis::adr
157 changes: 157 additions & 0 deletions modules/adr/src/adr.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
#include "motis/adr/adr.h"

#include <filesystem>
#include <fstream>
#include <istream>
#include <ranges>
#include <regex>
#include <sstream>

#include "boost/thread/tss.hpp"

#include "cista/reflection/comparable.h"

#include "utl/to_vec.h"

#include "adr/adr.h"
#include "adr/typeahead.h"

#include "motis/core/common/logging.h"
#include "motis/core/conv/position_conv.h"
#include "motis/module/event_collector.h"
#include "motis/module/ini_io.h"

namespace fs = std::filesystem;
namespace mm = motis::module;
namespace a = adr;

namespace motis::adr {

struct import_state {
CISTA_COMPARABLE()
mm::named<std::string, MOTIS_NAME("path")> path_;
mm::named<cista::hash_t, MOTIS_NAME("hash")> hash_;
mm::named<size_t, MOTIS_NAME("size")> size_;
};

struct adr::impl {
explicit impl(cista::wrapped<a::typeahead> t) : t_{std::move(t)} {}

a::guess_context& get_guess_context() {
auto static ctx = boost::thread_specific_ptr<a::guess_context>{};
if (ctx.get() == nullptr) {
ctx.reset(new a::guess_context{cache_});
}
ctx->resize(*t_);
return *ctx;
}

cista::wrapped<a::typeahead> t_;
a::cache cache_{.n_strings_ = t_->strings_.size(), .max_size_ = 100U};
};

adr::adr() : module("Address Typeahead", "adr") {}

adr::~adr() = default;

void adr::import(motis::module::import_dispatcher& reg) {
std::make_shared<mm::event_collector>(
get_data_directory().generic_string(), "adr", reg,
[this](mm::event_collector::dependencies_map_t const& dependencies,
mm::event_collector::publish_fn_t const&) {
using import::OSMEvent;

auto const dir = get_data_directory() / "adr";
auto const osm = motis_content(OSMEvent, dependencies.at("OSM"));
auto const state = import_state{data_path(osm->path()->str()),
osm->hash(), osm->size()};

if (mm::read_ini<import_state>(dir / "import.ini") != state) {
fs::create_directories(dir);
a::extract(osm->path()->str(), (dir / "adr"), dir);
mm::write_ini(dir / "import.ini", state);
}

impl_ = std::make_unique<impl>(a::read(dir / "adr.t.adr", false));
import_successful_ = true;
})
->require("OSM", [](mm::msg_ptr const& msg) {
return msg->get()->content_type() == MsgContent_OSMEvent;
});
}

mm::msg_ptr adr::guess(mm::msg_ptr const& msg) {
using motis::address::AddressRequest;
auto const req = motis_content(AddressRequest, msg);

auto lang_indices = std::basic_string<a::language_idx_t>{{a::kDefaultLang}};
auto& ctx = impl_->get_guess_context();
a::get_suggestions<false>(*impl_->t_, geo::latlng{0, 0}, req->input()->view(),
10U, lang_indices, ctx);

mm::message_creator fbb;
fbb.create_and_finish(
MsgContent_AddressResponse,
CreateAddressResponse(
fbb,
fbb.CreateVector(utl::to_vec(
ctx.suggestions_,
[&](a::suggestion const& s) {
auto const pos = to_fbs(s.coordinates_.as_latlng());
auto const regions = fbb.CreateVector(utl::to_vec(
impl_->t_->area_sets_[s.area_set_] | std::views::reverse,
[&](a::area_idx_t const area_idx) {
auto const admin_lvl =
impl_->t_->area_admin_level_[area_idx];
auto const name =
impl_->t_
->strings_[impl_->t_->area_names_
[area_idx][a::kDefaultLangIdx]]
.view();
return address::CreateRegion(fbb, fbb.CreateString(name),
to_idx(admin_lvl));
}));

return std::visit(
utl::overloaded{
[&](a::place_idx_t) {
return address::CreateAddress(
fbb, &pos,
fbb.CreateString(
impl_->t_->strings_[s.str_].view()),
fbb.CreateString("place"), regions);
},
[&](a::address const addr) {
auto const is_address =
addr.house_number_ != a::address::kNoHouseNumber;
auto const name =
is_address
? fmt::format(
"{} {}",
impl_->t_->strings_[s.str_].view(),
impl_->t_
->strings_[impl_->t_->house_numbers_
[addr.street_]
[addr.house_number_]]
.view())
: fmt::format(
"{}",
impl_->t_->strings_[s.str_].view());
return address::CreateAddress(
fbb, &pos, fbb.CreateString(name),
fbb.CreateString(is_address ? "address"
: "street"),
regions);
}},
s.location_);
})))
.Union());
return make_msg(fbb);
}

void adr::init(mm::registry& reg) {
reg.register_op("/address",
[&](mm::msg_ptr const& msg) { return this->guess(msg); }, {});
}

} // namespace motis::adr
3 changes: 3 additions & 0 deletions modules/osr/include/motis/osr/osr.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ struct osr : public motis::module::module {
motis::module::msg_ptr via(motis::module::msg_ptr const&) const;
motis::module::msg_ptr ppr(motis::module::msg_ptr const&) const;

bool import_successful() const override { return import_successful_; }

bool import_successful_{false};
bool lock_{true};

struct impl;
Expand Down
2 changes: 2 additions & 0 deletions modules/osr/src/osr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,8 @@ void osr::import(mm::import_dispatcher& reg) {
}
auto l = std::make_unique<o::lookup>(*w);
impl_ = std::make_unique<impl>(std::move(w), std::move(l));

import_successful_ = true;
})
->require("OSM", [](mm::msg_ptr const& msg) {
return msg->get()->content_type() == MsgContent_OSMEvent;
Expand Down

0 comments on commit 20568fc

Please sign in to comment.