<commit_before>/* Copyright © 2001-2014, Canal TP and/or its affiliates. All rights reserved.

This file is part of Navitia,
    the software to build cool stuff with public transport.

Hope you'll enjoy and contribute to this project,
    powered by Canal TP (www.canaltp.fr).
Help us simplify mobility and open public transport:
    a non ending quest to the responsive locomotion way of traveling!

LICENCE: This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.

Stay tuned using
twitter @navitia
IRC #navitia on freenode
https://groups.google.com/d/forum/navitia
www.navitia.io
*/
#include "make_disruption_from_chaos.h"
#include "apply_disruption.h"
#include "utils/logger.h"
#include <boost/make_shared.hpp>

namespace bt = boost::posix_time;

namespace navitia {

static boost::shared_ptr<nt::disruption::Tag>
make_tag(const chaos::Tag& chaos_tag, nt::disruption::DisruptionHolder& holder) {
    auto from_posix = navitia::from_posix_timestamp;

    auto& weak_tag = holder.tags[chaos_tag.id()];
    if (auto tag = weak_tag.lock()) { return tag; }

    auto tag = boost::make_shared<nt::disruption::Tag>();
    tag->uri = chaos_tag.id();
    tag->name = chaos_tag.name();
    tag->created_at = from_posix(chaos_tag.created_at());
    tag->updated_at = from_posix(chaos_tag.updated_at());

    weak_tag = tag;
    return tag;
}

static boost::shared_ptr<nt::disruption::Cause>
make_cause(const chaos::Cause& chaos_cause, nt::disruption::DisruptionHolder& holder) {
    auto from_posix = navitia::from_posix_timestamp;

    auto& weak_cause = holder.causes[chaos_cause.id()];
    if (auto cause = weak_cause.lock()) { return cause; }

    auto cause = boost::make_shared<nt::disruption::Cause>();
    cause->uri = chaos_cause.id();
    cause->wording = chaos_cause.wording();
    cause->created_at = from_posix(chaos_cause.created_at());
    cause->updated_at = from_posix(chaos_cause.updated_at());

    weak_cause = cause;
    return cause;

}

static boost::shared_ptr<nt::disruption::Severity>
make_severity(const chaos::Severity& chaos_severity, nt::disruption::DisruptionHolder& holder) {
    namespace tr = transit_realtime;
    namespace new_disr = nt::disruption;
    auto from_posix = navitia::from_posix_timestamp;

    auto& weak_severity = holder.severities[chaos_severity.id()];
    if (auto severity = weak_severity.lock()) { return severity; }

    auto severity = boost::make_shared<new_disr::Severity>();
    severity->uri = chaos_severity.id();
    severity->wording = chaos_severity.wording();
    severity->created_at = from_posix(chaos_severity.created_at());
    severity->updated_at = from_posix(chaos_severity.updated_at());
    severity->color = chaos_severity.color();
    severity->priority = chaos_severity.priority();
    switch (chaos_severity.effect()) {
#define EFFECT_ENUM_CONVERSION(e) \
        case tr::Alert_Effect_##e: severity->effect = new_disr::Effect::e; break

        EFFECT_ENUM_CONVERSION(NO_SERVICE);
        EFFECT_ENUM_CONVERSION(REDUCED_SERVICE);
        EFFECT_ENUM_CONVERSION(SIGNIFICANT_DELAYS);
        EFFECT_ENUM_CONVERSION(DETOUR);
        EFFECT_ENUM_CONVERSION(ADDITIONAL_SERVICE);
        EFFECT_ENUM_CONVERSION(MODIFIED_SERVICE);
        EFFECT_ENUM_CONVERSION(OTHER_EFFECT);
        EFFECT_ENUM_CONVERSION(UNKNOWN_EFFECT);
        EFFECT_ENUM_CONVERSION(STOP_MOVED);

#undef EFFECT_ENUM_CONVERSION
    }

    weak_severity = severity;
    return severity;
}

static boost::optional<nt::disruption::LineSection>
make_line_section(const chaos::PtObject& chaos_section,
                  nt::PT_Data& pt_data,
                  const boost::shared_ptr<nt::disruption::Impact>& impact) {
    if (!chaos_section.has_pt_line_section()) {
        LOG4CPLUS_WARN(log4cplus::Logger::getInstance("log"),
                       "fill_disruption_from_chaos: LineSection invalid!");
        return boost::none;
    }
    const auto& pb_section = chaos_section.pt_line_section();
    nt::disruption::LineSection line_section;
    auto* line = find_or_default(pb_section.line().uri(), pt_data.lines_map);
    if (line) {
        line_section.line = line;
    } else {
        LOG4CPLUS_WARN(log4cplus::Logger::getInstance("log"),
                       "fill_disruption_from_chaos: line id "
                       << pb_section.line().uri() << " in LineSection invalid!");
        return boost::none;
    }
    if (auto* start = find_or_default(pb_section.start_point().uri(), pt_data.stop_areas_map)) {
        line_section.start_point = start;
    } else {
        LOG4CPLUS_WARN(log4cplus::Logger::getInstance("log"),
                       "fill_disruption_from_chaos: start_point id "
                       << pb_section.start_point().uri() << " in LineSection invalid!");
        return boost::none;
    }
    if (auto* end = find_or_default(pb_section.end_point().uri(), pt_data.stop_areas_map)) {
        line_section.end_point = end;
    } else {
        LOG4CPLUS_WARN(log4cplus::Logger::getInstance("log"),
                       "fill_disruption_from_chaos: end_point id "
                       << pb_section.end_point().uri() << " in LineSection invalid!");
        return boost::none;
    }
    if(pb_section.routes().size()) {
        for(auto pb_route: pb_section.routes()) {
            if (auto* route = find_or_default(pb_route.uri(), pt_data.routes_map)) {
                line_section.routes.push_back(route);
            } else {
                LOG4CPLUS_WARN(log4cplus::Logger::getInstance("log"),
                               "fill_disruption_from_chaos: route id "
                               << pb_route.uri() << " in LineSection invalid!");
                return boost::none;
            }
        }
    } else {
        LOG4CPLUS_TRACE(log4cplus::Logger::getInstance("trace"),
                        "Empty routes for line_section on line" << line_section.line->uri <<
                        ". Applying disruptions on all routes");
        line_section.routes = line_section.line->route_list;
    }

    if(impact) {
        for(auto* route: line_section.routes) {
            route->add_impact(impact);
        }
    }

    return line_section;
}
<commit_msg>kraken: use const auto& in pb loop<commit_after>/* Copyright © 2001-2014, Canal TP and/or its affiliates. All rights reserved.

This file is part of Navitia,
    the software to build cool stuff with public transport.

Hope you'll enjoy and contribute to this project,
    powered by Canal TP (www.canaltp.fr).
Help us simplify mobility and open public transport:
    a non ending quest to the responsive locomotion way of traveling!

LICENCE: This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.

Stay tuned using
twitter @navitia
IRC #navitia on freenode
https://groups.google.com/d/forum/navitia
www.navitia.io
*/
#include "make_disruption_from_chaos.h"
#include "apply_disruption.h"
#include "utils/logger.h"
#include <boost/make_shared.hpp>

namespace bt = boost::posix_time;

namespace navitia {

static boost::shared_ptr<nt::disruption::Tag>
make_tag(const chaos::Tag& chaos_tag, nt::disruption::DisruptionHolder& holder) {
    auto from_posix = navitia::from_posix_timestamp;

    auto& weak_tag = holder.tags[chaos_tag.id()];
    if (auto tag = weak_tag.lock()) { return tag; }

    auto tag = boost::make_shared<nt::disruption::Tag>();
    tag->uri = chaos_tag.id();
    tag->name = chaos_tag.name();
    tag->created_at = from_posix(chaos_tag.created_at());
    tag->updated_at = from_posix(chaos_tag.updated_at());

    weak_tag = tag;
    return tag;
}

static boost::shared_ptr<nt::disruption::Cause>
make_cause(const chaos::Cause& chaos_cause, nt::disruption::DisruptionHolder& holder) {
    auto from_posix = navitia::from_posix_timestamp;

    auto& weak_cause = holder.causes[chaos_cause.id()];
    if (auto cause = weak_cause.lock()) { return cause; }

    auto cause = boost::make_shared<nt::disruption::Cause>();
    cause->uri = chaos_cause.id();
    cause->wording = chaos_cause.wording();
    cause->created_at = from_posix(chaos_cause.created_at());
    cause->updated_at = from_posix(chaos_cause.updated_at());

    weak_cause = cause;
    return cause;

}

static boost::shared_ptr<nt::disruption::Severity>
make_severity(const chaos::Severity& chaos_severity, nt::disruption::DisruptionHolder& holder) {
    namespace tr = transit_realtime;
    namespace new_disr = nt::disruption;
    auto from_posix = navitia::from_posix_timestamp;

    auto& weak_severity = holder.severities[chaos_severity.id()];
    if (auto severity = weak_severity.lock()) { return severity; }

    auto severity = boost::make_shared<new_disr::Severity>();
    severity->uri = chaos_severity.id();
    severity->wording = chaos_severity.wording();
    severity->created_at = from_posix(chaos_severity.created_at());
    severity->updated_at = from_posix(chaos_severity.updated_at());
    severity->color = chaos_severity.color();
    severity->priority = chaos_severity.priority();
    switch (chaos_severity.effect()) {
#define EFFECT_ENUM_CONVERSION(e) \
        case tr::Alert_Effect_##e: severity->effect = new_disr::Effect::e; break

        EFFECT_ENUM_CONVERSION(NO_SERVICE);
        EFFECT_ENUM_CONVERSION(REDUCED_SERVICE);
        EFFECT_ENUM_CONVERSION(SIGNIFICANT_DELAYS);
        EFFECT_ENUM_CONVERSION(DETOUR);
        EFFECT_ENUM_CONVERSION(ADDITIONAL_SERVICE);
        EFFECT_ENUM_CONVERSION(MODIFIED_SERVICE);
        EFFECT_ENUM_CONVERSION(OTHER_EFFECT);
        EFFECT_ENUM_CONVERSION(UNKNOWN_EFFECT);
        EFFECT_ENUM_CONVERSION(STOP_MOVED);

#undef EFFECT_ENUM_CONVERSION
    }

    weak_severity = severity;
    return severity;
}

static boost::optional<nt::disruption::LineSection>
make_line_section(const chaos::PtObject& chaos_section,
                  nt::PT_Data& pt_data,
                  const boost::shared_ptr<nt::disruption::Impact>& impact) {
    if (!chaos_section.has_pt_line_section()) {
        LOG4CPLUS_WARN(log4cplus::Logger::getInstance("log"),
                       "fill_disruption_from_chaos: LineSection invalid!");
        return boost::none;
    }
    const auto& pb_section = chaos_section.pt_line_section();
    nt::disruption::LineSection line_section;
    auto* line = find_or_default(pb_section.line().uri(), pt_data.lines_map);
    if (line) {
        line_section.line = line;
    } else {
        LOG4CPLUS_WARN(log4cplus::Logger::getInstance("log"),
                       "fill_disruption_from_chaos: line id "
                       << pb_section.line().uri() << " in LineSection invalid!");
        return boost::none;
    }
    if (auto* start = find_or_default(pb_section.start_point().uri(), pt_data.stop_areas_map)) {
        line_section.start_point = start;
    } else {
        LOG4CPLUS_WARN(log4cplus::Logger::getInstance("log"),
                       "fill_disruption_from_chaos: start_point id "
                       << pb_section.start_point().uri() << " in LineSection invalid!");
        return boost::none;
    }
    if (auto* end = find_or_default(pb_section.end_point().uri(), pt_data.stop_areas_map)) {
        line_section.end_point = end;
    } else {
        LOG4CPLUS_WARN(log4cplus::Logger::getInstance("log"),
                       "fill_disruption_from_chaos: end_point id "
                       << pb_section.end_point().uri() << " in LineSection invalid!");
        return boost::none;
    }
    if(!pb_section.routes().empty()) {
        for(const auto& pb_route: pb_section.routes()) {
            if (auto* route = find_or_default(pb_route.uri(), pt_data.routes_map)) {
                line_section.routes.push_back(route);
            } else {
                LOG4CPLUS_WARN(log4cplus::Logger::getInstance("log"),
                               "fill_disruption_from_chaos: route id "
                               << pb_route.uri() << " in LineSection invalid!");
                return boost::none;
            }
        }
    } else {
        LOG4CPLUS_TRACE(log4cplus::Logger::getInstance("trace"),
                        "Empty routes for line_section on line" << line_section.line->uri <<
                        ". Applying disruptions on all routes");
        line_section.routes = line_section.line->route_list;
    }

    if(impact) {
        for(auto* route: line_section.routes) {
            route->add_impact(impact);
        }
    }

    return line_section;
}

{'fraction_non_alphanumeric': 0.11325229765787133, 'fraction_numerical': 0.003112955825674474, 'mean_word_length': 3.361021331609567, 'pattern_counts': {'":': 0, '<': 63, '<?xml version=': 0, '>': 59, 'https://': 2, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6261699', 'n_tokens_mistral': 4302, 'n_tokens_neox': 4179, 'n_words': 889}