<commit_before><%pre>
    #include "models/WebACL.h"
    #include "models/UserSession.h"

    # define DEBUG cout << "[" << __FILE__ << ":" << __LINE__ << "] " <<
    # define ERROR cerr << "[" << __FILE__ << ":" << __LINE__ << "] " <<
</%pre>

<%config>
</%config>

<%args>
    std::string login_name;
    std::string name;
    std::string mail;
    std::string password_a;
    std::string password_b;
    std::string userrolls[];
    bool is_inactive;
    std::string button_update_account;

    std::string edit_account_id;
    std::string delete_account_id;
    std::string affirmation_delete_account_id;
</%args>

<%session scope="global">
    UserSession userSession;
</%session>

<%session>
    std::string feedback;
    std::string session_account_id;
    std::string affirmation;
</%session>

<%cpp>

    // ACL Check
    if ( userSession.isInRole ( "admin" ) == false ) {
        return reply.redirect ( "/access_denied" );
    };

    std::vector<AccountData> accountList = WebACL::getAllAccounts();
    AccountData accountData;
    std::vector<std::string> userRolls;
    std::vector<std::string> allRolls;

    // if a account selected for editing?
    DEBUG "edit_account_id: " << edit_account_id << endl;
    if ( edit_account_id != "") {
        affirmation = "";
        feedback = "";
        accountData =  WebACL::getAccountsWithID ( edit_account_id );
        std::vector<std::string> userRolls = WebACL::getRoll ( accountData.getLogin_name() );
        allRolls = WebACL::getAllRolls();
        session_account_id = edit_account_id;
        DEBUG "accountData.getAccount_disable(): " << accountData.getAccount_disable() << endl;

    }

    // is button account data update pushed?
    if ( button_update_account == "Speichern" ) {
        affirmation = "";
        feedback = "";
        DEBUG "Speichern" << endl;
        DEBUG "session_account_id: " << session_account_id << endl;
        accountData.setID( session_account_id );
        DEBUG "login_name: " << login_name << endl;
        accountData.setLogin_name( login_name );
        DEBUG "name: " << name << endl;
        accountData.setReal_name( name );
        DEBUG "mail: " << mail << endl;
        accountData.setEmail( mail );
        DEBUG "is_inactive: " << is_inactive << endl;
        accountData.setAccount_disable( is_inactive );

        WebACL::reSetUserRolls( session_account_id, userrolls);
        accountData.saveUpdate();

        // check if new password set.
        if (password_a != "" ) {
            DEBUG "password_a" << password_a << endl;
            // check equal of new password.
            if ( password_a != password_b ) {
                feedback = "Das Passwort ist nicht gleich!";
            } else {
                // check is password empty.
                accountData.setNewPassword( password_a );
                session_account_id = "";
                feedback = "Die Daten wurden gespeichert und das Passwort neu gesetzt!";
            };
        } else {
            session_account_id = "";
            feedback = "Die Daten wurden gespeichert!";
        };
        accountList = WebACL::getAllAccounts();
    }

    // is button delete account pushed?
    if ( delete_account_id != "" ) {
        affirmation = "";
        feedback = "";
        session_account_id = delete_account_id;
        affirmation = "Account mit ID " + session_account_id + " und allen \
             zugehörigen Daten wirklich löschen?";

    }

    // if delete affirmation clicked.
    if ( affirmation_delete_account_id != "" ) {
        affirmation = "";
        feedback = "";
        DEBUG "will löschen: " << affirmation_delete_account_id << endl;
        feedback = "Der Account mit der Id " + affirmation_delete_account_id + " wurde gelöscht!";
        accountList = WebACL::getAllAccounts();
    }

</%cpp>

<commit_msg>Delete user function is ready now.<commit_after><%pre>
    #include "models/WebACL.h"
    #include "models/UserSession.h"

    # define DEBUG cout << "[" << __FILE__ << ":" << __LINE__ << "] " <<
    # define ERROR cerr << "[" << __FILE__ << ":" << __LINE__ << "] " <<
</%pre>

<%config>
</%config>

<%args>
    std::string login_name;
    std::string name;
    std::string mail;
    std::string password_a;
    std::string password_b;
    std::string userrolls[];
    bool is_inactive;
    std::string button_update_account;

    std::string edit_account_id;
    std::string delete_account_id;
    std::string affirmation_delete_account_id;
</%args>

<%session scope="global">
    UserSession userSession;
</%session>

<%session>
    std::string feedback;
    std::string session_account_id;
    std::string affirmation;
</%session>

<%cpp>

    // ACL Check
    if ( userSession.isInRole ( "admin" ) == false ) {
        return reply.redirect ( "/access_denied" );
    };

    std::vector<AccountData> accountList = WebACL::getAllAccounts();
    AccountData accountData;
    std::vector<std::string> userRolls;
    std::vector<std::string> allRolls;

    // if a account selected for editing?
    DEBUG "edit_account_id: " << edit_account_id << endl;
    if ( edit_account_id != "") {
        affirmation = "";
        feedback = "";
        accountData =  WebACL::getAccountsWithID ( edit_account_id );
        std::vector<std::string> userRolls = WebACL::getRoll ( accountData.getLogin_name() );
        allRolls = WebACL::getAllRolls();
        session_account_id = edit_account_id;
        DEBUG "accountData.getAccount_disable(): " << accountData.getAccount_disable() << endl;

    }

    // is button account data update pushed?
    if ( button_update_account == "Speichern" ) {
        affirmation = "";
        feedback = "";
        DEBUG "Speichern" << endl;
        DEBUG "session_account_id: " << session_account_id << endl;
        accountData.setID( session_account_id );
        DEBUG "login_name: " << login_name << endl;
        accountData.setLogin_name( login_name );
        DEBUG "name: " << name << endl;
        accountData.setReal_name( name );
        DEBUG "mail: " << mail << endl;
        accountData.setEmail( mail );
        DEBUG "is_inactive: " << is_inactive << endl;
        accountData.setAccount_disable( is_inactive );

        WebACL::reSetUserRolls( session_account_id, userrolls);
        accountData.saveUpdate();

        // check if new password set.
        if (password_a != "" ) {
            DEBUG "password_a" << password_a << endl;
            // check equal of new password.
            if ( password_a != password_b ) {
                feedback = "Das Passwort ist nicht gleich!";
            } else {
                // check is password empty.
                accountData.setNewPassword( password_a );
                session_account_id = "";
                feedback = "Die Daten wurden gespeichert und das Passwort neu gesetzt!";
            };
        } else {
            session_account_id = "";
            feedback = "Die Daten wurden gespeichert!";
        };
        accountList = WebACL::getAllAccounts();
    }

    // is button delete account pushed?
    if ( delete_account_id != "" ) {
        affirmation = "";
        feedback = "";
        session_account_id = delete_account_id;
        affirmation = "Account mit ID " + session_account_id + " und allen \
             zugehörigen Daten wirklich löschen?";

    }

    // if delete affirmation clicked.
    if ( affirmation_delete_account_id != "" ) {
        affirmation = "";
        feedback = "";
        DEBUG "will löschen: " << affirmation_delete_account_id << endl;
        AccountData accountData;
        accountData.setID( affirmation_delete_account_id );
        accountData.deleteAllData();
        feedback = "Der Account mit der Id " + affirmation_delete_account_id + " wurde gelöscht!";
        accountList = WebACL::getAllAccounts();
    }

</%cpp>

<|endoftext|>


<commit_before>/*
 * This file is open source software, licensed to you under the terms
 * of the Apache License, Version 2.0 (the "License").  See the NOTICE file
 * distributed with this work for additional information regarding copyright
 * ownership.  You may not use this file except in compliance with the License.
 *
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

/*
 * Copyright 2016 ScyllaDB
 */

#define BOOST_TEST_MODULE core

#include <boost/test/included/unit_test.hpp>
#include <seastar/util/log.hh>
#include <seastar/util/backtrace.hh>
#include <ostream>
#include <regex>

using namespace seastar;

// a class which is not derived from std::exception
// to play the part of the unknown object in the logging
// function.
class unknown_obj {
    sstring _message;
public:
    unknown_obj(std::string message) : _message(message) {}
};

// This functions generates an exception chain nesting_level+1 deep
// for each nesting level it throws one of two types of objects, an
// unknown (non std::exception) object or a runtime error which is
// derived from std::exception, it chooses the type of thrown object
// according to the bit in the `nesting_level` place in the
// `tests_instance` paramter or in other words according to:
// bool(test_instance & (1<<nesting_level))
void exception_generator(uint32_t test_instance, int nesting_level) {
    try {
        if (nesting_level > 0) {
            exception_generator(test_instance>>1, nesting_level-1);
        }
    } catch(...) {
        auto msg = format("Exception Level {}", nesting_level);
        if(test_instance&1) {
            // Throw a non std::exception derived type
            std::throw_with_nested(unknown_obj(msg));
        } else {
            std::throw_with_nested(std::runtime_error(msg));
        }
    }
    if (nesting_level == 0) {
        if (test_instance & 1) {
            throw unknown_obj(format("Exception Level {}", nesting_level));
        } else {
            throw std::runtime_error(format("Exception Level {}", nesting_level));
        }
    }
}

// This function generates the expected logging output string of an exception
// thrown by the  exception generator function with a specific output.
std::string exception_generator_str(uint32_t test_instance,int nesting_level) {
    std::ostringstream ret;
    const std::string runtime_err_str = "std::runtime_error";
    const std::string exception_level_fmt_str = "Exception Level {}";
    const std::string unknown_obj_str = "unknown_obj";
    const std::string nested_exception_with_unknown_obj_str = "std::_Nested_exception<unknown_obj>";
    const std::string nested_exception_with_runtime_err_str = "std::_Nested_exception<std::runtime_error>";

    for(; nesting_level > 0; nesting_level--) {
        if (test_instance & 1) {
            ret << nested_exception_with_unknown_obj_str;
        } else {
            ret << nested_exception_with_runtime_err_str << " (" <<
                    format(exception_level_fmt_str.c_str(), nesting_level) << ")";
        }
        ret << ": ";
        test_instance >>= 1;
    }

    if (test_instance & 1) {
        ret << unknown_obj_str;
    } else {
        ret << runtime_err_str << " (" << format(exception_level_fmt_str.c_str(), nesting_level) << ")";
    }
    return ret.str();
}

// Test all variations of nested exceptions of some
// depth
BOOST_AUTO_TEST_CASE(nested_exception_logging1) {

    constexpr int levels_to_test = 3;

    for(int level = 0; level < levels_to_test; level++) {
        for(int inst = (1 << (level + 1)) - 1; inst >= 0; inst--) {
            std::ostringstream log_msg;
            try {
                exception_generator(inst, level);
            } catch(...) {
                log_msg << std::current_exception();
            }
            BOOST_REQUIRE_EQUAL(log_msg.str(), exception_generator_str(inst, level));
        }
    }
}

// Test logging of nested exception not mixed in with anything
BOOST_AUTO_TEST_CASE(nested_exception_logging2) {
    std::ostringstream log_msg;
    try {
        throw std::nested_exception();
    } catch(...) {
        log_msg << std::current_exception();
    }

    BOOST_REQUIRE_EQUAL(log_msg.str(), std::string("std::nested_exception: <no exception>"));
}

class very_important_exception : public std::exception {
    const char* my_name = "very important information";

public:
    const char* what() const noexcept {
        return my_name;
    }
};

// Test logging of nested exception that have std::system_error mixed with other exceptions
// so that std::system_error is in the middle of the exception chain.
BOOST_AUTO_TEST_CASE(nested_exception_logging3) {
    std::ostringstream log_msg;

    try {
        throw very_important_exception();
        } catch (...) {
        try {
            std::throw_with_nested(std::system_error(1, std::generic_category(), "my error"));
        } catch (...) {
            try {
                std::throw_with_nested(unknown_obj("This is an unknown object"));
            } catch (...) {
                log_msg << std::current_exception();
            }
        }
    }

    std::string expected_string("std::_Nested_exception<unknown_obj>: std::_Nested_exception<std::system_error> (error generic:1, my error: Operation not permitted): very_important_exception (very important information)");

    BOOST_REQUIRE_EQUAL(log_msg.str(), expected_string);
}

BOOST_AUTO_TEST_CASE(unknown_object_thrown_test) {
    std::ostringstream log_msg;
    try {
        throw unknown_obj("This is an unknown object");
    } catch(...) {
        log_msg << std::current_exception();
    }

    BOOST_REQUIRE_EQUAL(log_msg.str(), std::string("unknown_obj"));

}

BOOST_AUTO_TEST_CASE(format_error_test) {
    static seastar::logger l("format_error_test");

    std::ostringstream log_msg;
    l.set_ostream(log_msg);

    const char* fmt = "bad format string: {}";
    l.error(fmt);

    BOOST_TEST_MESSAGE(log_msg.str());
    BOOST_REQUIRE_NE(log_msg.str().find(__builtin_FILE()), std::string::npos);
    BOOST_REQUIRE_NE(log_msg.str().find(__builtin_FUNCTION()), std::string::npos);
    BOOST_REQUIRE_NE(log_msg.str().find(fmt), std::string::npos);
}

BOOST_AUTO_TEST_CASE(throw_with_backtrace_exception_logging) {
    std::ostringstream log_msg;
    try {
        throw_with_backtrace<std::runtime_error>("throw_with_backtrace_exception_logging");
    } catch(...) {
        log_msg << std::current_exception();
    }

    auto regex_str = "backtraced<std::runtime_error> \\(throw_with_backtrace_exception_logging Backtrace:(\\s+(\\S+\\+)?0x[0-9a-f]+)+\\)";
    std::regex expected_msg_re(regex_str, std::regex_constants::ECMAScript | std::regex_constants::icase);
    BOOST_REQUIRE(std::regex_search(log_msg.str(), expected_msg_re));
}
<commit_msg>test: exception_logging_test: test throw_with_backtrace nested exception combinations<commit_after>/*
 * This file is open source software, licensed to you under the terms
 * of the Apache License, Version 2.0 (the "License").  See the NOTICE file
 * distributed with this work for additional information regarding copyright
 * ownership.  You may not use this file except in compliance with the License.
 *
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

/*
 * Copyright 2016 ScyllaDB
 */

#include <exception>
#define BOOST_TEST_MODULE core

#include <boost/test/included/unit_test.hpp>
#include <seastar/util/log.hh>
#include <seastar/util/backtrace.hh>
#include <ostream>
#include <regex>

using namespace seastar;

// a class which is not derived from std::exception
// to play the part of the unknown object in the logging
// function.
class unknown_obj {
    sstring _message;
public:
    unknown_obj(std::string message) : _message(message) {}
};

// This functions generates an exception chain nesting_level+1 deep
// for each nesting level it throws one of two types of objects, an
// unknown (non std::exception) object or a runtime error which is
// derived from std::exception, it chooses the type of thrown object
// according to the bit in the `nesting_level` place in the
// `tests_instance` paramter or in other words according to:
// bool(test_instance & (1<<nesting_level))
void exception_generator(uint32_t test_instance, int nesting_level) {
    try {
        if (nesting_level > 0) {
            exception_generator(test_instance>>1, nesting_level-1);
        }
    } catch(...) {
        auto msg = format("Exception Level {}", nesting_level);
        if(test_instance&1) {
            // Throw a non std::exception derived type
            std::throw_with_nested(unknown_obj(msg));
        } else {
            std::throw_with_nested(std::runtime_error(msg));
        }
    }
    if (nesting_level == 0) {
        if (test_instance & 1) {
            throw unknown_obj(format("Exception Level {}", nesting_level));
        } else {
            throw std::runtime_error(format("Exception Level {}", nesting_level));
        }
    }
}

// This function generates the expected logging output string of an exception
// thrown by the  exception generator function with a specific output.
std::string exception_generator_str(uint32_t test_instance,int nesting_level) {
    std::ostringstream ret;
    const std::string runtime_err_str = "std::runtime_error";
    const std::string exception_level_fmt_str = "Exception Level {}";
    const std::string unknown_obj_str = "unknown_obj";
    const std::string nested_exception_with_unknown_obj_str = "std::_Nested_exception<unknown_obj>";
    const std::string nested_exception_with_runtime_err_str = "std::_Nested_exception<std::runtime_error>";

    for(; nesting_level > 0; nesting_level--) {
        if (test_instance & 1) {
            ret << nested_exception_with_unknown_obj_str;
        } else {
            ret << nested_exception_with_runtime_err_str << " (" <<
                    format(exception_level_fmt_str.c_str(), nesting_level) << ")";
        }
        ret << ": ";
        test_instance >>= 1;
    }

    if (test_instance & 1) {
        ret << unknown_obj_str;
    } else {
        ret << runtime_err_str << " (" << format(exception_level_fmt_str.c_str(), nesting_level) << ")";
    }
    return ret.str();
}

// Test all variations of nested exceptions of some
// depth
BOOST_AUTO_TEST_CASE(nested_exception_logging1) {

    constexpr int levels_to_test = 3;

    for(int level = 0; level < levels_to_test; level++) {
        for(int inst = (1 << (level + 1)) - 1; inst >= 0; inst--) {
            std::ostringstream log_msg;
            try {
                exception_generator(inst, level);
            } catch(...) {
                log_msg << std::current_exception();
            }
            BOOST_REQUIRE_EQUAL(log_msg.str(), exception_generator_str(inst, level));
        }
    }
}

// Test logging of nested exception not mixed in with anything
BOOST_AUTO_TEST_CASE(nested_exception_logging2) {
    std::ostringstream log_msg;
    try {
        throw std::nested_exception();
    } catch(...) {
        log_msg << std::current_exception();
    }

    BOOST_REQUIRE_EQUAL(log_msg.str(), std::string("std::nested_exception: <no exception>"));
}

class very_important_exception : public std::exception {
    const char* my_name = "very important information";

public:
    const char* what() const noexcept {
        return my_name;
    }
};

// Test logging of nested exception that have std::system_error mixed with other exceptions
// so that std::system_error is in the middle of the exception chain.
BOOST_AUTO_TEST_CASE(nested_exception_logging3) {
    std::ostringstream log_msg;

    try {
        throw very_important_exception();
        } catch (...) {
        try {
            std::throw_with_nested(std::system_error(1, std::generic_category(), "my error"));
        } catch (...) {
            try {
                std::throw_with_nested(unknown_obj("This is an unknown object"));
            } catch (...) {
                log_msg << std::current_exception();
            }
        }
    }

    std::string expected_string("std::_Nested_exception<unknown_obj>: std::_Nested_exception<std::system_error> (error generic:1, my error: Operation not permitted): very_important_exception (very important information)");

    BOOST_REQUIRE_EQUAL(log_msg.str(), expected_string);
}

BOOST_AUTO_TEST_CASE(unknown_object_thrown_test) {
    std::ostringstream log_msg;
    try {
        throw unknown_obj("This is an unknown object");
    } catch(...) {
        log_msg << std::current_exception();
    }

    BOOST_REQUIRE_EQUAL(log_msg.str(), std::string("unknown_obj"));

}

BOOST_AUTO_TEST_CASE(format_error_test) {
    static seastar::logger l("format_error_test");

    std::ostringstream log_msg;
    l.set_ostream(log_msg);

    const char* fmt = "bad format string: {}";
    l.error(fmt);

    BOOST_TEST_MESSAGE(log_msg.str());
    BOOST_REQUIRE_NE(log_msg.str().find(__builtin_FILE()), std::string::npos);
    BOOST_REQUIRE_NE(log_msg.str().find(__builtin_FUNCTION()), std::string::npos);
    BOOST_REQUIRE_NE(log_msg.str().find(fmt), std::string::npos);
}

BOOST_AUTO_TEST_CASE(throw_with_backtrace_exception_logging) {
    std::ostringstream log_msg;
    try {
        throw_with_backtrace<std::runtime_error>("throw_with_backtrace_exception_logging");
    } catch(...) {
        log_msg << std::current_exception();
    }

    auto regex_str = "backtraced<std::runtime_error> \\(throw_with_backtrace_exception_logging Backtrace:(\\s+(\\S+\\+)?0x[0-9a-f]+)+\\)";
    std::regex expected_msg_re(regex_str, std::regex_constants::ECMAScript | std::regex_constants::icase);
    BOOST_REQUIRE(std::regex_search(log_msg.str(), expected_msg_re));
}

BOOST_AUTO_TEST_CASE(throw_with_backtrace_nested_exception_logging) {
    std::ostringstream log_msg;
    try {
        throw_with_backtrace<std::runtime_error>("outer");
    } catch(...) {
        try {
            std::throw_with_nested(unknown_obj("This is an unknown object"));
        } catch (...) {
            log_msg << std::current_exception();
        }
    }

    auto regex_str = "std::_Nested_exception<unknown_obj>.*backtraced<std::runtime_error> \\(outer Backtrace:(\\s+(\\S+\\+)?0x[0-9a-f]+)+\\)";
    std::regex expected_msg_re(regex_str, std::regex_constants::ECMAScript | std::regex_constants::icase);
    BOOST_REQUIRE(std::regex_search(log_msg.str(), expected_msg_re));
}

BOOST_AUTO_TEST_CASE(throw_with_backtrace_seastar_nested_exception_logging) {
    std::ostringstream log_msg;
    try {
        throw unknown_obj("This is an unknown object");
    } catch (...) {
        auto outer = std::current_exception();
        try {
            throw_with_backtrace<std::runtime_error>("inner");
        } catch (...) {
            auto inner = std::current_exception();
            try {
                throw seastar::nested_exception(std::move(inner), std::move(outer));
            } catch (...) {
                log_msg << std::current_exception();
            }
        }
    }

    auto regex_str = "seastar::nested_exception:.*backtraced<std::runtime_error> \\(inner Backtrace:(\\s+(\\S+\\+)?0x[0-9a-f]+)+\\)"
            " \\(while cleaning up after unknown_obj\\)";
    std::regex expected_msg_re(regex_str, std::regex_constants::ECMAScript | std::regex_constants::icase);
    BOOST_REQUIRE(std::regex_search(log_msg.str(), expected_msg_re));
}

BOOST_AUTO_TEST_CASE(double_throw_with_backtrace_seastar_nested_exception_logging) {
    std::ostringstream log_msg;
    try {
        throw_with_backtrace<std::runtime_error>("outer");
    } catch (...) {
        auto outer = std::current_exception();
        try {
            throw_with_backtrace<std::runtime_error>("inner");
        } catch (...) {
            auto inner = std::current_exception();
            try {
                throw seastar::nested_exception(std::move(inner), std::move(outer));
            } catch (...) {
                log_msg << std::current_exception();
            }
        }
    }

    auto regex_str = "seastar::nested_exception:.*backtraced<std::runtime_error> \\(inner Backtrace:(\\s+(\\S+\\+)?0x[0-9a-f]+)+\\)"
            " \\(while cleaning up after .*backtraced<std::runtime_error> \\(outer Backtrace:(\\s+(\\S+\\+)?0x[0-9a-f]+)+\\)\\)";
    std::regex expected_msg_re(regex_str, std::regex_constants::ECMAScript | std::regex_constants::icase);
    BOOST_REQUIRE(std::regex_search(log_msg.str(), expected_msg_re));
}
<|endoftext|>


<commit_before>//#define LOG_AT_W  /* warning only */
#include "stinger_core/stinger_error.h"

#include "stinger_core/stinger_atomics.h"
#include "stinger_core/xmalloc.h"
#include "rapidjson/document.h"
#include "json_rpc_server.h"
#include "json_rpc.h"

using namespace gt::stinger;

int64_t
JSON_RPC_conversation_id_by_user::operator()(rapidjson::Value * params, rapidjson::Value & result, rapidjson::MemoryPoolAllocator<rapidjson::CrtAllocator> & allocator)
{
  int64_t source;
  bool strings;

  rpc_params_t p[] = {
    {"source", TYPE_VERTEX, &source, false, 0},
    {"strings", TYPE_BOOL, &strings, true, 0},
    {NULL, TYPE_NONE, NULL, false, 0}
  };

  if (!contains_params(p, params)) {
    return json_rpc_error(-32602, result, allocator);
  }

  StingerAlgState * alg_state = server_state->get_alg("influence_aggregator");
  if (!alg_state) {
    LOG_E ("Algorithm is not running");
    return json_rpc_error(-32003, result, allocator);
  }

  stinger_t * S = server_state->get_stinger();
  if (!S) {
    LOG_E ("STINGER pointer is invalid");
    return json_rpc_error(-32603, result, allocator);
  }

  /* read-only data arrays */
  int64_t * component_map = (int64_t *)alg_state->data;
  int64_t * component_size  = component_map + S->max_nv;

  rapidjson::Value src, src_str;
  rapidjson::Value name, vtx_phys;
  rapidjson::Value vtx_id (rapidjson::kArrayType);
  rapidjson::Value vtx_str (rapidjson::kArrayType);

  /* this part of the response needs to be set no matter what */
  src.SetInt64(source);
  result.AddMember("source", src, allocator);
  if (strings) {
    char * physID;
    uint64_t len;
    if (-1 == stinger_mapping_physid_direct(S, source, &physID, &len)) {
      physID = (char *) "";
      len = 0;
    }
    src_str.SetString(physID, len, allocator);
    result.AddMember("source_str", src_str, allocator);
  }

  int64_t tweets_etype = stinger_etype_names_lookup_type(S, "tweets");
  if (tweets_etype < 0) {
    return -1;
  }

  /* this is a pretty expensive way of unique-ing the conversation IDs */
  /* maybe consider doing something different (i.e. sorting) */
  int64_t nv = S->max_nv;
  int64_t * conv_ids = (int64_t *) xmalloc (nv * sizeof(int64_t));
  for (int64_t i = 0; i < nv; i++) {
    conv_ids[i] = 0;
  }

  /* find unique conversation IDs */
  STINGER_FORALL_EDGES_OF_TYPE_OF_VTX_BEGIN(S, tweets_etype, source) {
    int64_t tweet_id = STINGER_EDGE_DEST;
    int64_t conversation_id = component_map[tweet_id];
    conv_ids[conversation_id] = 1;
  } STINGER_FORALL_EDGES_OF_TYPE_OF_VTX_END();

  for (int64_t i = 0; i < nv; i++) {
    if (conv_ids[i]) {
      name.SetInt64(i);
      vtx_id.PushBack(name, allocator);
      if (strings) {
	char * physID;
	uint64_t len;
	if (-1 == stinger_mapping_physid_direct(S, i, &physID, &len)) {
	  physID = (char *) "";
	  len = 0;
	}
	vtx_phys.SetString(physID, len, allocator);
	vtx_str.PushBack(vtx_phys, allocator);
      }
    }
  }

  /* configure the response */
  result.AddMember("vertex_id", vtx_id, allocator);
  if (strings) {
    result.AddMember("vertex_str", vtx_str, allocator);
  }

  free (conv_ids);

  return 0;
}
<commit_msg>This should be connected components, not the influence aggregator<commit_after>//#define LOG_AT_W  /* warning only */
#include "stinger_core/stinger_error.h"

#include "stinger_core/stinger_atomics.h"
#include "stinger_core/xmalloc.h"
#include "rapidjson/document.h"
#include "json_rpc_server.h"
#include "json_rpc.h"

using namespace gt::stinger;

int64_t
JSON_RPC_conversation_id_by_user::operator()(rapidjson::Value * params, rapidjson::Value & result, rapidjson::MemoryPoolAllocator<rapidjson::CrtAllocator> & allocator)
{
  int64_t source;
  bool strings;

  rpc_params_t p[] = {
    {"source", TYPE_VERTEX, &source, false, 0},
    {"strings", TYPE_BOOL, &strings, true, 0},
    {NULL, TYPE_NONE, NULL, false, 0}
  };

  if (!contains_params(p, params)) {
    return json_rpc_error(-32602, result, allocator);
  }

  StingerAlgState * alg_state = server_state->get_alg("weakly_connected_components");
  if (!alg_state) {
    LOG_E ("Algorithm is not running");
    return json_rpc_error(-32003, result, allocator);
  }

  stinger_t * S = server_state->get_stinger();
  if (!S) {
    LOG_E ("STINGER pointer is invalid");
    return json_rpc_error(-32603, result, allocator);
  }

  /* read-only data arrays */
  int64_t * component_map = (int64_t *)alg_state->data;
  int64_t * component_size  = component_map + S->max_nv;

  rapidjson::Value src, src_str;
  rapidjson::Value name, vtx_phys;
  rapidjson::Value vtx_id (rapidjson::kArrayType);
  rapidjson::Value vtx_str (rapidjson::kArrayType);

  /* this part of the response needs to be set no matter what */
  src.SetInt64(source);
  result.AddMember("source", src, allocator);
  if (strings) {
    char * physID;
    uint64_t len;
    if (-1 == stinger_mapping_physid_direct(S, source, &physID, &len)) {
      physID = (char *) "";
      len = 0;
    }
    src_str.SetString(physID, len, allocator);
    result.AddMember("source_str", src_str, allocator);
  }

  int64_t tweets_etype = stinger_etype_names_lookup_type(S, "tweets");
  if (tweets_etype < 0) {
    return -1;
  }

  /* this is a pretty expensive way of unique-ing the conversation IDs */
  /* maybe consider doing something different (i.e. sorting) */
  int64_t nv = S->max_nv;
  int64_t * conv_ids = (int64_t *) xmalloc (nv * sizeof(int64_t));
  for (int64_t i = 0; i < nv; i++) {
    conv_ids[i] = 0;
  }

  /* find unique conversation IDs */
  STINGER_FORALL_EDGES_OF_TYPE_OF_VTX_BEGIN(S, tweets_etype, source) {
    int64_t tweet_id = STINGER_EDGE_DEST;
    int64_t conversation_id = component_map[tweet_id];
    conv_ids[conversation_id] = 1;
  } STINGER_FORALL_EDGES_OF_TYPE_OF_VTX_END();

  for (int64_t i = 0; i < nv; i++) {
    if (conv_ids[i]) {
      name.SetInt64(i);
      vtx_id.PushBack(name, allocator);
      if (strings) {
	char * physID;
	uint64_t len;
	if (-1 == stinger_mapping_physid_direct(S, i, &physID, &len)) {
	  physID = (char *) "";
	  len = 0;
	}
	vtx_phys.SetString(physID, len, allocator);
	vtx_str.PushBack(vtx_phys, allocator);
      }
    }
  }

  /* configure the response */
  result.AddMember("vertex_id", vtx_id, allocator);
  if (strings) {
    result.AddMember("vertex_str", vtx_str, allocator);
  }

  free (conv_ids);

  return 0;
}
<|endoftext|>


<commit_before>#include "dialog.h"

#include <Tempest/Application>
#include <Tempest/Layout>
#include <Tempest/Painter>
#include <Tempest/UiOverlay>

using namespace Tempest;

struct Dialog::LayShadow : Tempest::Layout {
  bool hasLay = false;

  void applyLayout(){
    Widget& ow    = *owner();
    size_t  count = ow.widgetsCount();

    if(hasLay || ow.size().isEmpty())
      return;

    const bool sendShowEvent = !hasLay;
    hasLay = true;

    for(size_t i=0;i<count;++i){
      Widget& w = ow.widget(i);

      if(sendShowEvent) {
        if(auto d = dynamic_cast<Dialog*>(&w))
          d->showEvent();
        }

      Point pos = w.pos();
      if(w.x()+w.w()>ow.w())
        pos.x = ow.w()-w.w();
      if(w.y()+w.h()>ow.h())
        pos.y = ow.h()-w.h();
      if(pos.x<=0)
        pos.x = 0;
      if(pos.y<=0)
        pos.y = 0;

      w.setPosition(pos);
      }
    }
  };

struct Dialog::Overlay : public Tempest::UiOverlay {
  Dialog& dlg;

  Overlay(Dialog& dlg):dlg(dlg){}

  void mouseDownEvent(MouseEvent& e) override {
    if(dlg.modal)
      e.accept(); else
      e.ignore();
    if(dlg.popup)
      dlg.close();
    }

  void mouseMoveEvent(MouseEvent& e) override {
    e.accept();
    }

  void mouseWheelEvent(MouseEvent& e) override {
    if(dlg.modal)
      e.accept(); else
      e.ignore();
    }

  void paintEvent(PaintEvent& e) override {
    dlg.paintShadow(e);
    }

  void keyDownEvent(Tempest::KeyEvent& e) override {
    dlg.keyDownEvent(e);
    e.accept();
    }

  void keyUpEvent(Tempest::KeyEvent& e) override {
    dlg.keyUpEvent(e);
    e.accept();
    }

  void closeEvent(Tempest::CloseEvent& e) override {
    dlg.closeEvent(e);
    }
  };

Dialog::Dialog() {
  resize(300,200);
  }

Dialog::~Dialog() {
  close();
  }

int Dialog::exec() {
  if(owner_ov==nullptr){
    owner_ov = new Overlay(*this);

    SystemApi::addOverlay(std::move(owner_ov));
    owner_ov->setLayout(new LayShadow());
    owner_ov->addWidget(this);
    }

  setVisible(true);
  showEvent();
  while(owner_ov && Application::isRunning()) {
    Application::processEvents();
    }
  return 0;
  }

void Dialog::close() {
  if(owner_ov==nullptr)
    return;
  Tempest::UiOverlay* ov = owner_ov;
  owner_ov = nullptr;

  setVisible(false);
  CloseEvent e;
  this->closeEvent(e);

  ov->takeWidget(this);
  delete ov;
  }

bool Dialog::isOpen() const {
  return owner_ov!=nullptr;
  }

void Dialog::setModal(bool m) {
  modal = m;
  }

bool Dialog::isModal() const {
  return modal;
  }

void Dialog::setPopup(bool p) {
  popup = p;
  }

bool Dialog::isPopup() const {
  return popup;
  }

void Dialog::closeEvent(CloseEvent&) {
  if(owner_ov!=nullptr)
    close();
  }

void Dialog::keyDownEvent(KeyEvent &e) {
  if(e.key==KeyEvent::K_ESCAPE)
    close();
  }

void Dialog::keyUpEvent(KeyEvent& e) {
  e.ignore();
  }

void Dialog::paintEvent(PaintEvent& e) {
  Tempest::Painter p(e);
  style().draw(p,this,Style::E_Background,state(),Rect(0,0,w(),h()),Style::Extra(*this));
  }

void Dialog::paintShadow(PaintEvent &e) {
  if(owner_ov==nullptr)
    return;
  Painter p(e);
  style().draw(p,this,owner_ov,Style::E_Background,state(),
               rect(),Style::Extra(*this),Rect(0,0,owner_ov->w(),owner_ov->h()));
  }

void Dialog::showEvent() {
  auto& ow = *owner();
  Point pos;
  pos.x = (ow.w()-this->w())/2;
  pos.y = (ow.h()-this->h())/2;
  setPosition(pos);
  }
<commit_msg>update popup dialogs<commit_after>#include "dialog.h"

#include <Tempest/Application>
#include <Tempest/Layout>
#include <Tempest/Painter>
#include <Tempest/UiOverlay>

using namespace Tempest;

struct Dialog::LayShadow : Tempest::Layout {
  bool hasLay = false;

  void applyLayout(){
    Widget& ow    = *owner();
    size_t  count = ow.widgetsCount();

    if(hasLay || ow.size().isEmpty())
      return;

    const bool sendShowEvent = !hasLay;
    hasLay = true;

    for(size_t i=0;i<count;++i){
      Widget& w = ow.widget(i);

      if(sendShowEvent) {
        if(auto d = dynamic_cast<Dialog*>(&w))
          d->showEvent();
        }

      Point pos = w.pos();
      if(w.x()+w.w()>ow.w())
        pos.x = ow.w()-w.w();
      if(w.y()+w.h()>ow.h())
        pos.y = ow.h()-w.h();
      if(pos.x<=0)
        pos.x = 0;
      if(pos.y<=0)
        pos.y = 0;

      w.setPosition(pos);
      }
    }
  };

struct Dialog::Overlay : public Tempest::UiOverlay {
  Dialog& dlg;

  Overlay(Dialog& dlg):dlg(dlg){}

  void mouseDownEvent(MouseEvent& e) override {
    if(dlg.modal)
      e.accept(); else
      e.ignore();
    if(dlg.popup) {
      CloseEvent e;
      e.accept();
      dlg.closeEvent(e);
      if(e.isAccepted()) {
        dlg.close();
        } else {
        e.accept();
        }
      }
    }

  void mouseMoveEvent(MouseEvent& e) override {
    if(dlg.modal)
      e.accept(); else
      e.ignore();
    }

  void mouseWheelEvent(MouseEvent& e) override {
    if(dlg.modal)
      e.accept(); else
      e.ignore();
    }

  void paintEvent(PaintEvent& e) override {
    dlg.paintShadow(e);
    }

  void keyDownEvent(Tempest::KeyEvent& e) override {
    dlg.keyDownEvent(e);
    e.accept();
    }

  void keyUpEvent(Tempest::KeyEvent& e) override {
    dlg.keyUpEvent(e);
    e.accept();
    }

  void closeEvent(Tempest::CloseEvent& e) override {
    dlg.closeEvent(e);
    }
  };

Dialog::Dialog() {
  resize(300,200);
  }

Dialog::~Dialog() {
  close();
  }

int Dialog::exec() {
  if(owner_ov==nullptr){
    owner_ov = new Overlay(*this);

    SystemApi::addOverlay(std::move(owner_ov));
    owner_ov->setLayout(new LayShadow());
    owner_ov->addWidget(this);
    }

  setVisible(true);
  showEvent();
  while(owner_ov && Application::isRunning()) {
    Application::processEvents();
    }
  return 0;
  }

void Dialog::close() {
  if(owner_ov==nullptr)
    return;
  Tempest::UiOverlay* ov = owner_ov;
  owner_ov = nullptr;

  setVisible(false);
  CloseEvent e;
  this->closeEvent(e);

  ov->takeWidget(this);
  delete ov;
  }

bool Dialog::isOpen() const {
  return owner_ov!=nullptr;
  }

void Dialog::setModal(bool m) {
  modal = m;
  }

bool Dialog::isModal() const {
  return modal;
  }

void Dialog::setPopup(bool p) {
  popup = p;
  }

bool Dialog::isPopup() const {
  return popup;
  }

void Dialog::closeEvent(CloseEvent&) {
  if(owner_ov!=nullptr)
    close();
  }

void Dialog::keyDownEvent(KeyEvent &e) {
  if(e.key==KeyEvent::K_ESCAPE)
    close();
  }

void Dialog::keyUpEvent(KeyEvent& e) {
  e.ignore();
  }

void Dialog::paintEvent(PaintEvent& e) {
  Tempest::Painter p(e);
  style().draw(p,this,Style::E_Background,state(),Rect(0,0,w(),h()),Style::Extra(*this));
  }

void Dialog::paintShadow(PaintEvent &e) {
  if(owner_ov==nullptr)
    return;
  Painter p(e);
  style().draw(p,this,owner_ov,Style::E_Background,state(),
               rect(),Style::Extra(*this),Rect(0,0,owner_ov->w(),owner_ov->h()));
  }

void Dialog::showEvent() {
  auto& ow = *owner();
  Point pos;
  pos.x = (ow.w()-this->w())/2;
  pos.y = (ow.h()-this->h())/2;
  setPosition(pos);
  }
<|endoftext|>


<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 "autocomplete.h"
#include "type/pt_data.h"
namespace navitia { namespace autocomplete {

void compute_score_poi(type::PT_Data&, georef::GeoRef &georef) {
    for (auto it = georef.fl_poi.word_quality_list.begin(); it != georef.fl_poi.word_quality_list.end(); ++it){
        for (navitia::georef::Admin* admin : georef.pois[it->first]->admin_list){
            if(admin->level == 8){
                it->second.score = georef.fl_admin.word_quality_list.at(admin->idx).score;
            }
        }
    }
}

void compute_score_way(type::PT_Data&, georef::GeoRef &georef) {
    //The scocre of each admin(level 8) is attributed to all its ways
    for (auto it = georef.fl_way.word_quality_list.begin(); it != georef.fl_way.word_quality_list.end(); ++it){
        for (navitia::georef::Admin* admin : georef.ways[it->first]->admin_list){
            if (admin->level == 8){
                it->second.score = georef.fl_admin.word_quality_list.at(admin->idx).score;
            }
        }
    }
}

void compute_score_stop_point(type::PT_Data &pt_data, georef::GeoRef &georef) {
    //The scocre of each admin(level 8) is attributed to all its stop_points
    for (auto it = pt_data.stop_point_autocomplete.word_quality_list.begin(); it != pt_data.stop_point_autocomplete.word_quality_list.end(); ++it){
        for(navitia::georef::Admin* admin : pt_data.stop_points[it->first]->admin_list){
            if (admin->level == 8){
                it->second.score = georef.fl_admin.word_quality_list.at(admin->idx).score;
            }
        }
    }
}

static size_t admin_score(const std::vector<navitia::georef::Admin*>& admins, const georef::GeoRef &georef) {
    for (const auto* admin: admins) {
        if (admin->level == 8) return georef.fl_admin.word_quality_list.at(admin->idx).score;
    }
    return 0;
}

void compute_score_stop_area(type::PT_Data & pt_data, const georef::GeoRef &georef){
    //The scocre of each admin(level 8) is attributed to all its stop_areas also
    //Find the stop-point count in all stop_areas and keep the highest;
    size_t max_score = 0;

    for (navitia::type::StopArea* sa : pt_data.stop_areas){
        max_score = std::max(max_score, sa->stop_point_list.size());
    }

    //Ajust the score of each stop_area from 0 to 100 using maximum score (max_score)
    if (max_score > 0){
        for (auto & it : pt_data.stop_area_autocomplete.word_quality_list){
            const size_t ad_score = admin_score(pt_data.stop_areas[it.first]->admin_list, georef);
            it.second.score = ad_score + (pt_data.stop_areas[it.first]->stop_point_list.size() * 100)/max_score;
        }
    }
}

/*
 Use natural logarithm to compute admin score as explained below.
 City       stop_point_count    log(n+2)*10
 Paris      3065                80
 Lyon       1000                69
 Versailles 418                 60
 St Denis   248                 55
 Melun      64                  42
 Pouzioux   1                   11
 Pampa      0                   7
*/
void compute_score_admin(type::PT_Data &pt_data, georef::GeoRef &georef) {
    //For each stop_point increase the score of it's admin(level 8) by 1.
    for (navitia::type::StopPoint* sp : pt_data.stop_points){
        for (navitia::georef::Admin * admin : sp->admin_list){
            if (admin->level == 8){
                georef.fl_admin.word_quality_list.at(admin->idx).score++;
            }
        }
    }

    //Ajust the score of each admin using natural logarithm as : log(n+2)*10
    for (auto it = georef.fl_admin.word_quality_list.begin(); it != georef.fl_admin.word_quality_list.end(); ++it){
        it->second.score = log(it->second.score + 2) * 10;
    }
}

template<>
void Autocomplete<type::idx_t>::compute_score(type::PT_Data &pt_data, georef::GeoRef &georef,
                   const type::Type_e type) {
    switch(type){
        case type::Type_e::StopArea:
            compute_score_stop_area(pt_data, georef);
            break;
        case type::Type_e::StopPoint:
            compute_score_stop_point(pt_data, georef);
            break;
        case type::Type_e::Admin:
            compute_score_admin(pt_data, georef);
            break;
        case type::Type_e::Way:
            compute_score_way(pt_data, georef);
            break;
        case type::Type_e::POI:
            compute_score_poi(pt_data, georef);
            break;
        default:
            break;
    }
}

}}
<commit_msg>Kraken: autocomplete: take the number of ways in account in autocomplete<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 "autocomplete.h"
#include "type/pt_data.h"
namespace navitia { namespace autocomplete {

void compute_score_poi(type::PT_Data&, georef::GeoRef &georef) {
    for (auto it = georef.fl_poi.word_quality_list.begin(); it != georef.fl_poi.word_quality_list.end(); ++it){
        for (navitia::georef::Admin* admin : georef.pois[it->first]->admin_list){
            if(admin->level == 8){
                it->second.score = georef.fl_admin.word_quality_list.at(admin->idx).score;
            }
        }
    }
}

void compute_score_way(type::PT_Data&, georef::GeoRef &georef) {
    //The scocre of each admin(level 8) is attributed to all its ways
    for (auto it = georef.fl_way.word_quality_list.begin(); it != georef.fl_way.word_quality_list.end(); ++it){
        for (navitia::georef::Admin* admin : georef.ways[it->first]->admin_list){
            if (admin->level == 8){
                it->second.score = georef.fl_admin.word_quality_list.at(admin->idx).score;
            }
        }
    }
}

void compute_score_stop_point(type::PT_Data &pt_data, georef::GeoRef &georef) {
    //The scocre of each admin(level 8) is attributed to all its stop_points
    for (auto it = pt_data.stop_point_autocomplete.word_quality_list.begin(); it != pt_data.stop_point_autocomplete.word_quality_list.end(); ++it){
        for(navitia::georef::Admin* admin : pt_data.stop_points[it->first]->admin_list){
            if (admin->level == 8){
                it->second.score = georef.fl_admin.word_quality_list.at(admin->idx).score;
            }
        }
    }
}

static size_t admin_score(const std::vector<navitia::georef::Admin*>& admins, const georef::GeoRef &georef) {
    for (const auto* admin: admins) {
        if (admin->level == 8) return georef.fl_admin.word_quality_list.at(admin->idx).score;
    }
    return 0;
}

void compute_score_stop_area(type::PT_Data & pt_data, const georef::GeoRef &georef){
    //The scocre of each admin(level 8) is attributed to all its stop_areas also
    //Find the stop-point count in all stop_areas and keep the highest;
    size_t max_score = 0;

    for (navitia::type::StopArea* sa : pt_data.stop_areas){
        max_score = std::max(max_score, sa->stop_point_list.size());
    }

    //Ajust the score of each stop_area from 0 to 100 using maximum score (max_score)
    if (max_score > 0){
        for (auto & it : pt_data.stop_area_autocomplete.word_quality_list){
            const size_t ad_score = admin_score(pt_data.stop_areas[it.first]->admin_list, georef);
            it.second.score = ad_score + (pt_data.stop_areas[it.first]->stop_point_list.size() * 100)/max_score;
        }
    }
}

/*
 Use natural logarithm to compute admin score as explained below.
 City       stop_point_count    log(n+2)*10
 Paris      3065                80
 Lyon       1000                69
 Versailles 418                 60
 St Denis   248                 55
 Melun      64                  42
 Pouzioux   1                   11
 Pampa      0                   7
*/
void compute_score_admin(type::PT_Data &pt_data, georef::GeoRef &georef) {
    //For each stop_point increase the score of it's admin(level 8) by 1.
    for (navitia::georef::Way* way: georef.ways) {
        for (navitia::georef::Admin * admin : way->admin_list){
            if (admin->level == 8){
                georef.fl_admin.word_quality_list.at(admin->idx).score++;
            }
        }
    }
    std::set<navitia::georef::Admin*> without_way_admins;
    for(auto admin : georef.admins) {
        if (admin->level == 8 && georef.fl_admin.word_quality_list.at(admin->idx).score == 0) {
            without_way_admins.insert(admin);
        }
    }
    for (navitia::type::StopPoint* sp : pt_data.stop_points){
        for (navitia::georef::Admin * admin : sp->admin_list){
            if (admin->level == 8 && without_way_admins.count(admin) > 0){
                georef.fl_admin.word_quality_list.at(admin->idx).score++;
            }
        }
    }

    //Ajust the score of each admin using natural logarithm as : log(n+2)*10
    for (auto it = georef.fl_admin.word_quality_list.begin(); it != georef.fl_admin.word_quality_list.end(); ++it){
        it->second.score = log(it->second.score + 2) * 10;
    }
}

template<>
void Autocomplete<type::idx_t>::compute_score(type::PT_Data &pt_data, georef::GeoRef &georef,
                   const type::Type_e type) {
    switch(type){
        case type::Type_e::StopArea:
            compute_score_stop_area(pt_data, georef);
            break;
        case type::Type_e::StopPoint:
            compute_score_stop_point(pt_data, georef);
            break;
        case type::Type_e::Admin:
            compute_score_admin(pt_data, georef);
            break;
        case type::Type_e::Way:
            compute_score_way(pt_data, georef);
            break;
        case type::Type_e::POI:
            compute_score_poi(pt_data, georef);
            break;
        default:
            break;
    }
}

}}
<|endoftext|>


<commit_before>#include <parser/addressparser.h>
#include <parser/documentlocation.h>
#include <parser/attributes.h>

#include <citygml/address.h>
#include <citygml/citygmllogger.h>

#include <unordered_set>
#include <unordered_map>
#include <functional>

namespace citygml {
	void setCountry(Address* address, std::string const &country)
	{
		address->setCountry(country);
	}

	void setLocality(Address* address, std::string const &locality)
	{
		address->setLocality(locality);
	}

	void setThoroughfareName(Address* address, std::string const &thoroughfareName)
	{
		address->setThoroughfareName(thoroughfareName);
	}

	void setThoroughfareNumber(Address *address, std::string const &thoroughfareNumber)
	{
		address->setThoroughfareNumber(thoroughfareNumber);
	}

	void setPostalCode(Address* address, std::string const &postalCode)
	{
		address->setPostalCode(postalCode);
	}

    namespace {

        template<typename T, typename... Args>
        std::unique_ptr<T> make_unique(Args&&... args) {
            return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
        }

        auto k_rootElements = std::unordered_set<NodeType::XMLNode>();
        auto k_subElements = std::unordered_set<NodeType::XMLNode>();
        auto k_dataElements = std::unordered_map<NodeType::XMLNode, std::function<void(Address*, const std::string&)>>();

        std::mutex g_nodeSetMutex;
        bool g_nodeSetsInitialized = false;

        void initializeNodeSets() {
            if (!g_nodeSetsInitialized) {
                std::lock_guard<std::mutex> lock(g_nodeSetMutex);
                if (!g_nodeSetsInitialized) {
                    k_rootElements = {
                        NodeType::CORE_AddressNode,
                        NodeType::BLDG_AddressNode,
                        NodeType::CORE_XalAddressNode
                    };

                    k_subElements = {
                        NodeType::XAL_AddressDetailsNode,
                        NodeType::XAL_CountryNode,
                        NodeType::XAL_LocalityNode,
                        NodeType::XAL_PostalCodeNode,
                        NodeType::XAL_ThoroughfareNode
                    };

                    //k_dataElements = {
                    //    { NodeType::XAL_CountryNameNode, &Address::setCountry },
                    //    { NodeType::XAL_LocalityNameNode, &Address::setLocality },
                    //    { NodeType::XAL_ThoroughfareNameNode, &Address::setThoroughfareName },
                    //    { NodeType::XAL_ThoroughfareNumberNode, &Address::setThoroughfareNumber },
                    //    { NodeType::XAL_PostalCodeNumberNode, &Address::setPostalCode }
                    //};
					k_dataElements[NodeType::XAL_CountryNameNode] = &setCountry;
					k_dataElements[NodeType::XAL_LocalityNameNode] = &setLocality;
					k_dataElements[NodeType::XAL_ThoroughfareNameNode] = &setThoroughfareName;
					k_dataElements[NodeType::XAL_ThoroughfareNumberNode] = &setThoroughfareNumber;
					k_dataElements[NodeType::XAL_PostalCodeNumberNode] = &setPostalCode;

                    g_nodeSetsInitialized = true;
                }
            }
        }

    }  // anonymous namespace

    AddressParser::AddressParser(CityGMLDocumentParser& documentParser, CityGMLFactory& factory, std::shared_ptr<CityGMLLogger> logger, const Callback& callback)
    : CityGMLElementParser(documentParser, factory, logger)
    , m_callback(callback)
    {
        initializeNodeSets();
    }

    std::string AddressParser::elementParserName() const
    {
        return "AddressParser";
    }

    bool AddressParser::handlesElement(const NodeType::XMLNode& node) const
    {
        return k_rootElements.count(node) > 0;
    }

    bool AddressParser::parseElementStartTag(const NodeType::XMLNode& node, Attributes& attributes)
    {
        if (k_rootElements.count(node) == 0) {
            CITYGML_LOG_ERROR(m_logger, "Expected an address start tag but got <" << node << "> at " << getDocumentLocation());
            throw std::runtime_error("Unexpected start tag found.");
        }

        m_address = std::make_unique<Address>(attributes.getCityGMLIDAttribute());
        return true;
    }

    bool AddressParser::parseElementEndTag(const NodeType::XMLNode& node, const std::string& characters)
    {
        m_callback(std::move(m_address));
        m_address = nullptr;
        return k_rootElements.count(node) > 0;
    }

    bool AddressParser::parseChildElementStartTag(const NodeType::XMLNode& node, Attributes& attributes)
    {
        return k_subElements.count(node) > 0 || k_dataElements.count(node) > 0 || k_rootElements.count(node) > 0;
    }

    bool AddressParser::parseChildElementEndTag(const NodeType::XMLNode& node, const std::string& characters)
    {
        const auto itr = k_dataElements.find(node);
        if (itr != k_dataElements.end()) {
            itr->second(m_address.get(), characters);
            return true;
        }

        return k_subElements.count(node) > 0 || k_dataElements.count(node) > 0 || k_rootElements.count(node) > 0;
    }

} /* namespace citygml */
<commit_msg>Removed some code relicts<commit_after>#include <parser/addressparser.h>
#include <parser/documentlocation.h>
#include <parser/attributes.h>

#include <citygml/address.h>
#include <citygml/citygmllogger.h>

#include <unordered_set>
#include <unordered_map>
#include <functional>

namespace citygml {
	void setCountry(Address* address, std::string const &country)
	{
		address->setCountry(country);
	}

	void setLocality(Address* address, std::string const &locality)
	{
		address->setLocality(locality);
	}

	void setThoroughfareName(Address* address, std::string const &thoroughfareName)
	{
		address->setThoroughfareName(thoroughfareName);
	}

	void setThoroughfareNumber(Address *address, std::string const &thoroughfareNumber)
	{
		address->setThoroughfareNumber(thoroughfareNumber);
	}

	void setPostalCode(Address* address, std::string const &postalCode)
	{
		address->setPostalCode(postalCode);
	}

    namespace {

        template<typename T, typename... Args>
        std::unique_ptr<T> make_unique(Args&&... args) {
            return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
        }

        auto k_rootElements = std::unordered_set<NodeType::XMLNode>();
        auto k_subElements = std::unordered_set<NodeType::XMLNode>();
        auto k_dataElements = std::unordered_map<NodeType::XMLNode, std::function<void(Address*, const std::string&)>>();

        std::mutex g_nodeSetMutex;
        bool g_nodeSetsInitialized = false;

        void initializeNodeSets() {
            if (!g_nodeSetsInitialized) {
                std::lock_guard<std::mutex> lock(g_nodeSetMutex);
                if (!g_nodeSetsInitialized) {
                    k_rootElements = {
                        NodeType::CORE_AddressNode,
                        NodeType::BLDG_AddressNode,
                        NodeType::CORE_XalAddressNode
                    };

                    k_subElements = {
                        NodeType::XAL_AddressDetailsNode,
                        NodeType::XAL_CountryNode,
                        NodeType::XAL_LocalityNode,
                        NodeType::XAL_PostalCodeNode,
                        NodeType::XAL_ThoroughfareNode
                    };

					k_dataElements[NodeType::XAL_CountryNameNode] = &setCountry;
					k_dataElements[NodeType::XAL_LocalityNameNode] = &setLocality;
					k_dataElements[NodeType::XAL_ThoroughfareNameNode] = &setThoroughfareName;
					k_dataElements[NodeType::XAL_ThoroughfareNumberNode] = &setThoroughfareNumber;
					k_dataElements[NodeType::XAL_PostalCodeNumberNode] = &setPostalCode;

                    g_nodeSetsInitialized = true;
                }
            }
        }

    }  // anonymous namespace

    AddressParser::AddressParser(CityGMLDocumentParser& documentParser, CityGMLFactory& factory, std::shared_ptr<CityGMLLogger> logger, const Callback& callback)
    : CityGMLElementParser(documentParser, factory, logger)
    , m_callback(callback)
    {
        initializeNodeSets();
    }

    std::string AddressParser::elementParserName() const
    {
        return "AddressParser";
    }

    bool AddressParser::handlesElement(const NodeType::XMLNode& node) const
    {
        return k_rootElements.count(node) > 0;
    }

    bool AddressParser::parseElementStartTag(const NodeType::XMLNode& node, Attributes& attributes)
    {
        if (k_rootElements.count(node) == 0) {
            CITYGML_LOG_ERROR(m_logger, "Expected an address start tag but got <" << node << "> at " << getDocumentLocation());
            throw std::runtime_error("Unexpected start tag found.");
        }

        m_address = std::make_unique<Address>(attributes.getCityGMLIDAttribute());
        return true;
    }

    bool AddressParser::parseElementEndTag(const NodeType::XMLNode& node, const std::string& characters)
    {
        m_callback(std::move(m_address));
        m_address = nullptr;
        return k_rootElements.count(node) > 0;
    }

    bool AddressParser::parseChildElementStartTag(const NodeType::XMLNode& node, Attributes& attributes)
    {
        return k_subElements.count(node) > 0 || k_dataElements.count(node) > 0 || k_rootElements.count(node) > 0;
    }

    bool AddressParser::parseChildElementEndTag(const NodeType::XMLNode& node, const std::string& characters)
    {
        const auto itr = k_dataElements.find(node);
        if (itr != k_dataElements.end()) {
            itr->second(m_address.get(), characters);
            return true;
        }

        return k_subElements.count(node) > 0 || k_dataElements.count(node) > 0 || k_rootElements.count(node) > 0;
    }

} /* namespace citygml */
<|endoftext|>


<commit_before><commit_msg>constraint_solver: remove usage of gtl:ContainsKey<commit_after><|endoftext|>


<commit_before>#include <qpdf/QPDF.hh>
#include <qpdf/QPDFWriter.hh>
#include <qpdf/QUtil.hh>
#include <qpdf/BufferInputSource.hh>
#include <qpdf/Buffer.hh>
#include <qpdf/Pl_Discard.hh>
#include <qpdf/QPDFPageDocumentHelper.hh>
#include <qpdf/QPDFPageObjectHelper.hh>
#include <qpdf/QPDFPageLabelDocumentHelper.hh>
#include <qpdf/QPDFOutlineDocumentHelper.hh>
#include <qpdf/QPDFAcroFormDocumentHelper.hh>

class DiscardContents: public QPDFObjectHandle::ParserCallbacks
{
  public:
    virtual ~DiscardContents() {}
    virtual void handleObject(QPDFObjectHandle) {}
    virtual void handleEOF() {}
};

class FuzzHelper
{
  public:
    FuzzHelper(unsigned char const* data, size_t size);
    void run();

  private:
    PointerHolder<QPDF> getQpdf();
    PointerHolder<QPDFWriter> getWriter(PointerHolder<QPDF>);
    void doWrite(PointerHolder<QPDFWriter> w);
    void testWrite();
    void testPages();
    void testOutlines();
    void doChecks();

    Buffer input_buffer;
    Pl_Discard discard;
};

FuzzHelper::FuzzHelper(unsigned char const* data, size_t size) :
    // We do not modify data, so it is safe to remove the const for Buffer
    input_buffer(const_cast<unsigned char*>(data), size)
{
}

PointerHolder<QPDF>
FuzzHelper::getQpdf()
{
    PointerHolder<InputSource> is =
        new BufferInputSource("fuzz input", &this->input_buffer);
    PointerHolder<QPDF> qpdf = new QPDF();
    qpdf->processInputSource(is);
    return qpdf;
}

PointerHolder<QPDFWriter>
FuzzHelper::getWriter(PointerHolder<QPDF> qpdf)
{
    PointerHolder<QPDFWriter> w = new QPDFWriter(*qpdf);
    w->setOutputPipeline(&this->discard);
    w->setDeterministicID(true);
    w->setDecodeLevel(qpdf_dl_all);
    w->setCompressStreams(false);
    return w;
}

void
FuzzHelper::doWrite(PointerHolder<QPDFWriter> w)
{
    try
    {
        w->write();
    }
    catch (QPDFExc const& e)
    {
        std::cerr << e.what() << std::endl;
    }
}

void
FuzzHelper::testWrite()
{
    // Write in various ways to exercise QPDFWriter

    PointerHolder<QPDF> q;
    PointerHolder<QPDFWriter> w;

    q = getQpdf();
    w = getWriter(q);
    doWrite(w);

    q = getQpdf();
    w = getWriter(q);
    w->setLinearization(true);
    doWrite(w);

    q = getQpdf();
    w = getWriter(q);
    w->setObjectStreamMode(qpdf_o_disable);
    doWrite(w);

    q = getQpdf();
    w = getWriter(q);
    w->setObjectStreamMode(qpdf_o_generate);
    doWrite(w);
}

void
FuzzHelper::testPages()
{
    // Parse all content streams, and exercise some helpers that
    // operate on pages.
    PointerHolder<QPDF> q = getQpdf();
    QPDFPageDocumentHelper pdh(*q);
    QPDFPageLabelDocumentHelper pldh(*q);
    QPDFOutlineDocumentHelper odh(*q);
    QPDFAcroFormDocumentHelper afdh(*q);
    std::vector<QPDFPageObjectHelper> pages = pdh.getAllPages();
    DiscardContents discard_contents;
    int pageno = 0;
    for (std::vector<QPDFPageObjectHelper>::iterator iter =
             pages.begin();
         iter != pages.end(); ++iter)
    {
        QPDFPageObjectHelper& page(*iter);
        ++pageno;
        try
        {
            page.parsePageContents(&discard_contents);
            page.getPageImages();
            pldh.getLabelForPage(pageno);
            odh.getOutlinesForPage(page.getObjectHandle().getObjGen());

            std::vector<QPDFAnnotationObjectHelper> annotations =
                afdh.getWidgetAnnotationsForPage(page);
            for (std::vector<QPDFAnnotationObjectHelper>::iterator annot_iter =
                     annotations.begin();
                 annot_iter != annotations.end(); ++annot_iter)
            {
                QPDFAnnotationObjectHelper& aoh = *annot_iter;
                afdh.getFieldForAnnotation(aoh);
            }
        }
        catch (QPDFExc& e)
        {
            std::cerr << "page " << pageno << ": "
                      << e.what() << std::endl;
        }
    }
}

void
FuzzHelper::testOutlines()
{
    PointerHolder<QPDF> q = getQpdf();
    std::list<std::list<QPDFOutlineObjectHelper> > queue;
    QPDFOutlineDocumentHelper odh(*q);
    queue.push_back(odh.getTopLevelOutlines());
    while (! queue.empty())
    {
        std::list<QPDFOutlineObjectHelper>& outlines = *(queue.begin());
        for (std::list<QPDFOutlineObjectHelper>::iterator iter =
                 outlines.begin();
             iter != outlines.end(); ++iter)
        {
            QPDFOutlineObjectHelper& ol = *iter;
            ol.getDestPage();
            queue.push_back(ol.getKids());
        }
        queue.pop_front();
    }
}

void
FuzzHelper::doChecks()
{
    // Get as much coverage as possible in parts of the library that
    // might benefit from fuzzing.
    testWrite();
    testPages();
    testOutlines();
}

void
FuzzHelper::run()
{
    // The goal here is that you should be able to throw anything at
    // libqpdf and it will respond without any memory errors and never
    // do anything worse than throwing a QPDFExc or
    // std::runtime_error. Throwing any other kind of exception,
    // segfaulting, or having a memory error (when built with
    // appropriate sanitizers) will all cause abnormal exit.
    try
    {
        doChecks();
    }
    catch (QPDFExc const& e)
    {
        std::cerr << "QPDFExc: " << e.what() << std::endl;
    }
    catch (std::runtime_error const& e)
    {
        std::cerr << "runtime_error: " << e.what() << std::endl;
    }
}

extern "C" int LLVMFuzzerTestOneInput(unsigned char const* data, size_t size)
{
    FuzzHelper f(data, size);
    f.run();
    return 0;
}
<commit_msg>Update qpdf_fuzzer to increase coverage<commit_after>#include <qpdf/QPDF.hh>
#include <qpdf/QPDFWriter.hh>
#include <qpdf/QUtil.hh>
#include <qpdf/BufferInputSource.hh>
#include <qpdf/Buffer.hh>
#include <qpdf/Pl_Discard.hh>
#include <qpdf/QPDFPageDocumentHelper.hh>
#include <qpdf/QPDFPageObjectHelper.hh>
#include <qpdf/QPDFPageLabelDocumentHelper.hh>
#include <qpdf/QPDFOutlineDocumentHelper.hh>
#include <qpdf/QPDFAcroFormDocumentHelper.hh>

class DiscardContents: public QPDFObjectHandle::ParserCallbacks
{
  public:
    virtual ~DiscardContents() {}
    virtual void handleObject(QPDFObjectHandle) {}
    virtual void handleEOF() {}
};

class FuzzHelper
{
  public:
    FuzzHelper(unsigned char const* data, size_t size);
    void run();

  private:
    PointerHolder<QPDF> getQpdf();
    PointerHolder<QPDFWriter> getWriter(PointerHolder<QPDF>);
    void doWrite(PointerHolder<QPDFWriter> w);
    void testWrite();
    void testPages();
    void testOutlines();
    void doChecks();

    Buffer input_buffer;
    Pl_Discard discard;
};

FuzzHelper::FuzzHelper(unsigned char const* data, size_t size) :
    // We do not modify data, so it is safe to remove the const for Buffer
    input_buffer(const_cast<unsigned char*>(data), size)
{
}

PointerHolder<QPDF>
FuzzHelper::getQpdf()
{
    PointerHolder<InputSource> is =
        new BufferInputSource("fuzz input", &this->input_buffer);
    PointerHolder<QPDF> qpdf = new QPDF();
    qpdf->processInputSource(is);
    return qpdf;
}

PointerHolder<QPDFWriter>
FuzzHelper::getWriter(PointerHolder<QPDF> qpdf)
{
    PointerHolder<QPDFWriter> w = new QPDFWriter(*qpdf);
    w->setOutputPipeline(&this->discard);
    w->setDecodeLevel(qpdf_dl_all);
    return w;
}

void
FuzzHelper::doWrite(PointerHolder<QPDFWriter> w)
{
    try
    {
        w->write();
    }
    catch (QPDFExc const& e)
    {
        std::cerr << e.what() << std::endl;
    }
    catch (std::runtime_error const& e)
    {
        std::cerr << e.what() << std::endl;
    }
}

void
FuzzHelper::testWrite()
{
    // Write in various ways to exercise QPDFWriter

    PointerHolder<QPDF> q;
    PointerHolder<QPDFWriter> w;

    q = getQpdf();
    w = getWriter(q);
    w->setDeterministicID(true);
    w->setQDFMode(true);
    doWrite(w);

    q = getQpdf();
    w = getWriter(q);
    w->setStaticID(true);
    w->setLinearization(true);
    w->setR6EncryptionParameters(
        "u", "o", true, true, true, true, true, true, qpdf_r3p_full, true);
    doWrite(w);

    q = getQpdf();
    w = getWriter(q);
    w->setStaticID(true);
    w->setObjectStreamMode(qpdf_o_disable);
    w->setR3EncryptionParameters(
	"u", "o", true, true, qpdf_r3p_full, qpdf_r3m_all);
    doWrite(w);

    q = getQpdf();
    w = getWriter(q);
    w->setDeterministicID(true);
    w->setObjectStreamMode(qpdf_o_generate);
    w->setLinearization(true);
    doWrite(w);
}

void
FuzzHelper::testPages()
{
    // Parse all content streams, and exercise some helpers that
    // operate on pages.
    PointerHolder<QPDF> q = getQpdf();
    QPDFPageDocumentHelper pdh(*q);
    QPDFPageLabelDocumentHelper pldh(*q);
    QPDFOutlineDocumentHelper odh(*q);
    QPDFAcroFormDocumentHelper afdh(*q);
    afdh.generateAppearancesIfNeeded();
    pdh.flattenAnnotations();
    std::vector<QPDFPageObjectHelper> pages = pdh.getAllPages();
    DiscardContents discard_contents;
    int pageno = 0;
    for (std::vector<QPDFPageObjectHelper>::iterator iter =
             pages.begin();
         iter != pages.end(); ++iter)
    {
        QPDFPageObjectHelper& page(*iter);
        ++pageno;
        try
        {
            page.coalesceContentStreams();
            page.parsePageContents(&discard_contents);
            page.getPageImages();
            pldh.getLabelForPage(pageno);
            QPDFObjectHandle page_obj(page.getObjectHandle());
            page_obj.getJSON(true).unparse();
            odh.getOutlinesForPage(page_obj.getObjGen());

            std::vector<QPDFAnnotationObjectHelper> annotations =
                afdh.getWidgetAnnotationsForPage(page);
            for (std::vector<QPDFAnnotationObjectHelper>::iterator annot_iter =
                     annotations.begin();
                 annot_iter != annotations.end(); ++annot_iter)
            {
                QPDFAnnotationObjectHelper& aoh = *annot_iter;
                afdh.getFieldForAnnotation(aoh);
            }
        }
        catch (QPDFExc& e)
        {
            std::cerr << "page " << pageno << ": "
                      << e.what() << std::endl;
        }
    }
}

void
FuzzHelper::testOutlines()
{
    PointerHolder<QPDF> q = getQpdf();
    std::list<std::list<QPDFOutlineObjectHelper> > queue;
    QPDFOutlineDocumentHelper odh(*q);
    queue.push_back(odh.getTopLevelOutlines());
    while (! queue.empty())
    {
        std::list<QPDFOutlineObjectHelper>& outlines = *(queue.begin());
        for (std::list<QPDFOutlineObjectHelper>::iterator iter =
                 outlines.begin();
             iter != outlines.end(); ++iter)
        {
            QPDFOutlineObjectHelper& ol = *iter;
            ol.getDestPage();
            queue.push_back(ol.getKids());
        }
        queue.pop_front();
    }
}

void
FuzzHelper::doChecks()
{
    // Get as much coverage as possible in parts of the library that
    // might benefit from fuzzing.
    testWrite();
    testPages();
    testOutlines();
}

void
FuzzHelper::run()
{
    // The goal here is that you should be able to throw anything at
    // libqpdf and it will respond without any memory errors and never
    // do anything worse than throwing a QPDFExc or
    // std::runtime_error. Throwing any other kind of exception,
    // segfaulting, or having a memory error (when built with
    // appropriate sanitizers) will all cause abnormal exit.
    try
    {
        doChecks();
    }
    catch (QPDFExc const& e)
    {
        std::cerr << "QPDFExc: " << e.what() << std::endl;
    }
    catch (std::runtime_error const& e)
    {
        std::cerr << "runtime_error: " << e.what() << std::endl;
    }
}

extern "C" int LLVMFuzzerTestOneInput(unsigned char const* data, size_t size)
{
    FuzzHelper f(data, size);
    f.run();
    return 0;
}
<|endoftext|>


<commit_before>/*********************************************************************
 * Software License Agreement (BSD License)
 *
 *  Copyright (c) 2014, Willow Garage, Inc.
 *  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.
 *   * Neither the name of Willow Garage nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 *  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 OWNER 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.
 *********************************************************************/

/* Author: Arjun Menon */

#include <ros/ros.h>

#include <moveit/move_group_interface/move_group.h>

#include <moveit/warehouse/planning_scene_storage.h>

#include <boost/foreach.hpp>
#include <boost/program_options.hpp>
#include <boost/filesystem.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/format.hpp>
#include <boost/regex.hpp>

#include <view_controller_msgs/CameraPlacement.h>

#include "moveit_recorder/trajectory_retimer.h"
#include "moveit_recorder/animation_recorder.h"
#include "moveit_recorder/utils.h"

#include <algorithm>

#include "moveit_recorder/trajectory_video_lookup.h"

int main(int argc, char** argv)
{
  ros::init(argc, argv, "playback");
  ros::NodeHandle node_handle;
  ros::AsyncSpinner spinner(1);
  spinner.start();

  sleep(20); // to let RVIZ come up

  boost::program_options::options_description desc;
  desc.add_options()
    ("help", "Show help message")
    ("host", boost::program_options::value<std::string>(), "Host for the MongoDB.")
    ("port", boost::program_options::value<std::size_t>(), "Port for the MongoDB.")
    ("views",boost::program_options::value<std::string>(), "Bag file for viewpoints")
    ("scene_regex",boost::program_options::value<std::string>(), "Regex for matching the scenes")
    ("query_regex",boost::program_options::value<std::string>(), "Regex for matching the queries")
    ("camera_topic",boost::program_options::value<std::string>(), "Topic for publishing to the camera position")
    ("planning_scene_topic",boost::program_options::value<std::string>(), "Topic for publishing the planning scene for recording")
    ("display_traj_topic",boost::program_options::value<std::string>(), "Topic for publishing the trajectory for recorder")
    ("animation_status_topic",boost::program_options::value<std::string>(), "Topic for listening to the completion of the replay")
    ("save_dir",boost::program_options::value<std::string>(), "Directory for saving videos");

  boost::program_options::variables_map vm;
  boost::program_options::parsed_options po = boost::program_options::parse_command_line(argc, argv, desc);
  boost::program_options::store(po, vm);
  boost::program_options::notify(vm);

  if (vm.count("help") || argc == 1) // show help if no parameters passed
  {
    std::cout << desc << std::endl;
    return 1;
  }
  try
  {
    //connect to the DB
    std::string host = vm.count("host") ? vm["host"].as<std::string>() : "";
    size_t port = vm.count("port") ? vm["port"].as<std::size_t>() : 0;
    moveit_warehouse::PlanningSceneStorage pss(host, port);
    ROS_INFO("Connected to Warehouse DB at host (%s) and port (%d)", host.c_str(), (int)port);

    // set up the storage directory
    boost::filesystem::path storage_dir( vm.count("save_dir") ? vm["save_dir"].as<std::string>() : "/tmp" );
    boost::filesystem::create_directories( storage_dir );

    // track associated videos with the plans that created them
    TrajectoryVideoLookup video_lookup_table;

    // load the viewpoints
    std::string bagfilename = vm.count("views") ? vm["views"].as<std::string>() : "";
    std::vector<view_controller_msgs::CameraPlacement> views;
    utils::rosbag_storage::getViewsFromBag( bagfilename, views );
    ROS_INFO("%d views loaded",(int)views.size());

    //TODO change these to params
    std::string camera_placement_topic = utils::get_option(vm, "camera_topic", "/rviz/camera_placement");
    std::string planning_scene_topic =utils:: get_option(vm, "planning_scene_topic", "planning_scene");
    std::string display_traj_topic = utils::get_option(vm, "display_traj_topic", "/move_group/display_planned_path");
    std::string anim_status_topic = utils::get_option(vm, "animation_status_topic", "animation_status");

    boost::regex scene_regex( utils::get_option(vm, "scene_regex", ".*") );
    boost::regex query_regex( utils::get_option(vm, "query_regex", ".*") );

    AnimationRecorder recorder( camera_placement_topic,
                                planning_scene_topic,
                                display_traj_topic,
                                anim_status_topic,
                                node_handle);

    // ask the warehouse for the scenes
    std::vector<std::string> ps_names;
    pss.getPlanningSceneNames( ps_names );
    ROS_INFO("%d available scenes to display", (int)ps_names.size());

    // iterate over scenes
    std::vector<std::string>::iterator scene_name = ps_names.begin();
    for(; scene_name!=ps_names.end(); ++scene_name)
    {
      boost::cmatch matches;
      if(!boost::regex_match( scene_name->c_str(), matches, scene_regex ))
        continue;

      ROS_INFO("Retrieving scene %s", scene_name->c_str());
      moveit_warehouse::PlanningSceneWithMetadata pswm;
      pss.getPlanningScene(pswm, *scene_name);
      moveit_msgs::PlanningScene ps_msg = static_cast<const moveit_msgs::PlanningScene&>(*pswm);

      // ask qarehosue for the queries
      std::vector<std::string> pq_names;
      pss.getPlanningQueriesNames( pq_names, *scene_name);
      ROS_INFO("%d available queries to display", (int)pq_names.size());

      // iterate over the queries
      std::vector<std::string>::iterator query_name = pq_names.begin();
      for(; query_name!=pq_names.end(); ++query_name)
      {
        if(!boost::regex_match( query_name->c_str(), matches, query_regex ))
          continue;
        ROS_INFO("Retrieving query %s", query_name->c_str());
        moveit_warehouse::MotionPlanRequestWithMetadata mprwm;
        pss.getPlanningQuery(mprwm, *scene_name, *query_name);
        moveit_msgs::MotionPlanRequest mpr_msg = static_cast<const moveit_msgs::MotionPlanRequest&>(*mprwm);

        // ask warehouse for stored trajectories
        std::vector<moveit_warehouse::RobotTrajectoryWithMetadata> planning_results;
        pss.getPlanningResults(planning_results, *scene_name, *query_name);
        ROS_INFO("Loaded %d trajectories", (int)planning_results.size());

        // animate each trajectory
        std::vector<moveit_warehouse::RobotTrajectoryWithMetadata>::iterator traj_w_mdata = planning_results.begin();
        for(; traj_w_mdata!=planning_results.end(); ++traj_w_mdata)
        {
          moveit_msgs::RobotTrajectory rt_msg;
          rt_msg = static_cast<const moveit_msgs::RobotTrajectory&>(**traj_w_mdata);
          // retime it and post process it
          TrajectoryRetimer retimer( "robot_description", mpr_msg.group_name );
          retimer.configure(ps_msg, mpr_msg);
          bool result = retimer.retime(rt_msg);
          ROS_INFO("Retiming success? %s", result? "yes" : "no" );
          retimer.addTimeToStartandGoal(rt_msg);

          //date and time based filename
          boost::posix_time::ptime now = boost::posix_time::second_clock::local_time();
          std::string time_as_string = boost::posix_time::to_simple_string(now);

          // create trajectory ID
          std::string trajectory_id = time_as_string;
          std::replace(trajectory_id.begin(), trajectory_id.end(), '/','_');
          std::replace(trajectory_id.begin(), trajectory_id.end(), '-','_');
          std::replace(trajectory_id.begin(), trajectory_id.end(), ' ','_');
          std::replace(trajectory_id.begin(), trajectory_id.end(), ':','_');

          // save into lookup
          video_lookup_table.put( trajectory_id, ps_msg );
          video_lookup_table.put( trajectory_id, mpr_msg );
          video_lookup_table.put( trajectory_id, rt_msg );

          int view_counter=0;
          std::vector<view_controller_msgs::CameraPlacement>::iterator view_msg;
          for(view_msg=views.begin(); view_msg!=views.end(); ++view_msg)
          {
            // create filename
            std::string filename = boost::str(boost::format("%s-%d.%s") % trajectory_id % ++view_counter % "ogv");
            std::string view_id = boost::str(boost::format("%s%d") % "view" % view_counter);

            //Animation request data
            AnimationRequest req;

            view_msg->time_from_start = ros::Duration(0.001);
            ros::Time t_now = ros::Time::now();
            view_msg->eye.header.stamp = t_now;
            view_msg->focus.header.stamp = t_now;
            view_msg->up.header.stamp = t_now;

            req.camera_placement = *view_msg;
            req.planning_scene = ps_msg;
            req.motion_plan_request = mpr_msg;
            req.robot_trajectory = rt_msg;
            req.filepath = (storage_dir/filename).string();

            video_lookup_table.putVideoFile( trajectory_id, view_id, req.filepath );

            recorder.record(req);
            recorder.startCapture();
            ROS_INFO("Recording Done");
          }//view
        }//traj
      }//query
    }//scene

    video_lookup_table.saveToBag( (storage_dir/"video_lookup.bag").string() );
  }
  catch(mongo_ros::DbConnectException &ex)
  {
    ROS_ERROR_STREAM("Unable to connect to warehouse. If you just created the database, it could take a while for initial setup. Please try to run the benchmark again."
        << std::endl << ex.what());
  }

  ROS_INFO("Successfully performed trajectory playback");
  ros::shutdown();
  return 0;
}
<commit_msg>overhauled this to move initialization to different node<commit_after>/*********************************************************************
 * Software License Agreement (BSD License)
 *
 *  Copyright (c) 2014, Willow Garage, Inc.
 *  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.
 *   * Neither the name of Willow Garage nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 *  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 OWNER 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.
 *********************************************************************/

/* Author: Arjun Menon */

#include <ros/ros.h>

#include <moveit/move_group_interface/move_group.h>

#include <moveit/warehouse/planning_scene_storage.h>

#include <boost/foreach.hpp>
#include <boost/program_options.hpp>
#include <boost/filesystem.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/format.hpp>
#include <boost/regex.hpp>

#include <view_controller_msgs/CameraPlacement.h>

#include "moveit_recorder/trajectory_retimer.h"
#include "moveit_recorder/animation_recorder.h"
#include "moveit_recorder/utils.h"

#include <algorithm>

#include "moveit_recorder/trajectory_video_lookup.h"

int main(int argc, char** argv)
{
  ros::init(argc, argv, "playback");
  ros::NodeHandle node_handle;
  ros::AsyncSpinner spinner(1);
  spinner.start();

  sleep(20); // to let RVIZ come up

  boost::program_options::options_description desc;
  desc.add_options()
    ("help", "Show help message")
    ("host", boost::program_options::value<std::string>(), "Host for the MongoDB.")
    ("port", boost::program_options::value<std::size_t>(), "Port for the MongoDB.")
    ("views",boost::program_options::value<std::string>(), "Bag file for viewpoints")
    ("scene_regex",boost::program_options::value<std::string>(), "Regex for matching the scenes")
    ("query_regex",boost::program_options::value<std::string>(), "Regex for matching the queries")
    ("camera_topic",boost::program_options::value<std::string>(), "Topic for publishing to the camera position")
    ("planning_scene_topic",boost::program_options::value<std::string>(), "Topic for publishing the planning scene for recording")
    ("display_traj_topic",boost::program_options::value<std::string>(), "Topic for publishing the trajectory for recorder")
    ("animation_status_topic",boost::program_options::value<std::string>(), "Topic for listening to the completion of the replay")
    ("save_dir",boost::program_options::value<std::string>(), "Directory for saving videos");

  boost::program_options::variables_map vm;
  boost::program_options::parsed_options po = boost::program_options::parse_command_line(argc, argv, desc);
  boost::program_options::store(po, vm);
  boost::program_options::notify(vm);

  if (vm.count("help") || argc == 1) // show help if no parameters passed
  {
    std::cout << desc << std::endl;
    return 1;
  }
  try
  {
    //connect to the DB
    std::string host = vm.count("host") ? vm["host"].as<std::string>() : "";
    size_t port = vm.count("port") ? vm["port"].as<std::size_t>() : 0;
    moveit_warehouse::PlanningSceneStorage pss(host, port);
    ROS_INFO("Connected to Warehouse DB at host (%s) and port (%d)", host.c_str(), (int)port);

    // set up the storage directory
    boost::filesystem::path storage_dir( vm.count("save_dir") ? vm["save_dir"].as<std::string>() : "/tmp" );
    boost::filesystem::create_directories( storage_dir );

    // track associated videos with the plans that created them
    TrajectoryVideoLookup video_lookup_table;
    video_lookup_table.loadFromBag( storage_dir.string() );

    //TODO change these to params
    std::string camera_placement_topic = utils::get_option(vm, "camera_topic", "/rviz/camera_placement");
    std::string planning_scene_topic =utils:: get_option(vm, "planning_scene_topic", "planning_scene");
    std::string display_traj_topic = utils::get_option(vm, "display_traj_topic", "/move_group/display_planned_path");
    std::string anim_status_topic = utils::get_option(vm, "animation_status_topic", "animation_status");

    boost::regex scene_regex( utils::get_option(vm, "scene_regex", ".*") );
    boost::regex query_regex( utils::get_option(vm, "query_regex", ".*") );

    AnimationRecorder recorder( camera_placement_topic,
                                planning_scene_topic,
                                display_traj_topic,
                                anim_status_topic,
                                node_handle);

    TrajectoryVideoLookup::iterator traj_it = video_lookup_table.begin();
    // for each traj
    for(;traj_it!=video_lookup_table.end();++traj_it)
    {
      // match regexes
      boost::cmatch matches;
      if(!boost::regex_match( traj_it->second.ps.name.c_str(), matches, scene_regex ))
        continue;

      // post process trajectory (Add timing, add pauses to start and goal states)
      moveit_msgs::RobotTrajectory post_processed_rt = traj_it->second.rt;
      TrajectoryRetimer retimer( "robot_description", traj_it->second.mpr.group_name );
      retimer.configure(traj_it->second.ps, traj_it->second.mpr);
      bool result = retimer.retime(post_processed_rt);
      retimer.addTimeToStartandGoal(post_processed_rt);
      ROS_INFO("Post processing success? %s", result? "yes" : "no" );

      //make request
      AnimationRequest req;
      req.planning_scene = traj_it->second.ps;
      req.motion_plan_request = traj_it->second.mpr;
      req.robot_trajectory = post_processed_rt;

      // for each view
      std::vector<view_controller_msgs::CameraPlacement>::iterator view_it = traj_it->second.views.begin();
      size_t view_counter=0;
      for(;view_it!=traj_it->second.views.end();++view_it)
      {
        view_controller_msgs::CameraPlacement view_msg = *view_it;
        // create filename
        std::string filename = boost::str(boost::format("%s-%d.%s") % traj_it->first % ++view_counter % "ogv");
        std::string view_id = boost::str(boost::format("%s%d") % "view" % view_counter);

        //Animation request view
        view_msg.time_from_start = ros::Duration(0.001);
        ros::Time t_now = ros::Time::now();
        view_msg.eye.header.stamp = t_now;
        view_msg.focus.header.stamp = t_now;
        view_msg.up.header.stamp = t_now;

        // view specific
        req.camera_placement = view_msg;
        req.filepath = (storage_dir/filename).string();

        video_lookup_table.putVideoFile( traj_it->first, view_id, req.filepath );

        recorder.record(req);
        recorder.startCapture();
        ROS_INFO("Recording view #%d for trajectory id=\"%s\" complete.", (int)view_counter, traj_it->first.c_str());
      }
      ROS_INFO("Recording for trajectory id=\"%s\" complete.", traj_it->first.c_str());
    }

    /*
    // ask the warehouse for the scenes
    std::vector<std::string> ps_names;
    pss.getPlanningSceneNames( ps_names );
    ROS_INFO("%d available scenes to display", (int)ps_names.size());

    // iterate over scenes
    std::vector<std::string>::iterator scene_name = ps_names.begin();
    for(; scene_name!=ps_names.end(); ++scene_name)
    {
      boost::cmatch matches;
      if(!boost::regex_match( scene_name->c_str(), matches, scene_regex ))
        continue;

      ROS_INFO("Retrieving scene %s", scene_name->c_str());
      moveit_warehouse::PlanningSceneWithMetadata pswm;
      pss.getPlanningScene(pswm, *scene_name);
      moveit_msgs::PlanningScene ps_msg = static_cast<const moveit_msgs::PlanningScene&>(*pswm);

      // ask qarehosue for the queries
      std::vector<std::string> pq_names;
      pss.getPlanningQueriesNames( pq_names, *scene_name);
      ROS_INFO("%d available queries to display", (int)pq_names.size());

      // iterate over the queries
      std::vector<std::string>::iterator query_name = pq_names.begin();
      for(; query_name!=pq_names.end(); ++query_name)
      {
        if(!boost::regex_match( query_name->c_str(), matches, query_regex ))
          continue;
        ROS_INFO("Retrieving query %s", query_name->c_str());
        moveit_warehouse::MotionPlanRequestWithMetadata mprwm;
        pss.getPlanningQuery(mprwm, *scene_name, *query_name);
        moveit_msgs::MotionPlanRequest mpr_msg = static_cast<const moveit_msgs::MotionPlanRequest&>(*mprwm);

        // ask warehouse for stored trajectories
        std::vector<moveit_warehouse::RobotTrajectoryWithMetadata> planning_results;
        pss.getPlanningResults(planning_results, *scene_name, *query_name);
        ROS_INFO("Loaded %d trajectories", (int)planning_results.size());

        // animate each trajectory
        std::vector<moveit_warehouse::RobotTrajectoryWithMetadata>::iterator traj_w_mdata = planning_results.begin();
        for(; traj_w_mdata!=planning_results.end(); ++traj_w_mdata)
        {
          moveit_msgs::RobotTrajectory rt_msg;
          rt_msg = static_cast<const moveit_msgs::RobotTrajectory&>(**traj_w_mdata);
          // retime it and post process it
          TrajectoryRetimer retimer( "robot_description", mpr_msg.group_name );
          retimer.configure(ps_msg, mpr_msg);
          bool result = retimer.retime(rt_msg);
          ROS_INFO("Retiming success? %s", result? "yes" : "no" );
          retimer.addTimeToStartandGoal(rt_msg);

          //date and time based filename
          boost::posix_time::ptime now = boost::posix_time::second_clock::local_time();
          std::string time_as_string = boost::posix_time::to_simple_string(now);

          // create trajectory ID
          std::string trajectory_id = time_as_string;
          std::replace(trajectory_id.begin(), trajectory_id.end(), '/','_');
          std::replace(trajectory_id.begin(), trajectory_id.end(), '-','_');
          std::replace(trajectory_id.begin(), trajectory_id.end(), ' ','_');
          std::replace(trajectory_id.begin(), trajectory_id.end(), ':','_');

          // save into lookup
          video_lookup_table.put( trajectory_id, ps_msg );
          video_lookup_table.put( trajectory_id, mpr_msg );
          video_lookup_table.put( trajectory_id, rt_msg );

          int view_counter=0;
          std::vector<view_controller_msgs::CameraPlacement>::iterator view_msg;
          for(view_msg=views.begin(); view_msg!=views.end(); ++view_msg)
          {
            // create filename
            std::string filename = boost::str(boost::format("%s-%d.%s") % trajectory_id % ++view_counter % "ogv");
            std::string view_id = boost::str(boost::format("%s%d") % "view" % view_counter);

            //Animation request data
            AnimationRequest req;

            view_msg->time_from_start = ros::Duration(0.001);
            ros::Time t_now = ros::Time::now();
            view_msg->eye.header.stamp = t_now;
            view_msg->focus.header.stamp = t_now;
            view_msg->up.header.stamp = t_now;

            req.camera_placement = *view_msg;
            req.planning_scene = ps_msg;
            req.motion_plan_request = mpr_msg;
            req.robot_trajectory = rt_msg;
            req.filepath = (storage_dir/filename).string();

            video_lookup_table.putVideoFile( trajectory_id, view_id, req.filepath );

            recorder.record(req);
            recorder.startCapture();
            ROS_INFO("Recording Done");
          }//view
        }//traj
      }//query
    }//scene
    */
    video_lookup_table.saveToBag( (storage_dir/"video_lookup.bag").string() );
  }
  catch(mongo_ros::DbConnectException &ex)
  {
    ROS_ERROR_STREAM("Unable to connect to warehouse. If you just created the database, it could take a while for initial setup. Please try to run the benchmark again."
        << std::endl << ex.what());
  }

  ROS_INFO("Successfully performed trajectory playback");
  ros::shutdown();
  return 0;
}
<|endoftext|>