<commit_before>// -*- coding: utf-8 -*-
// Copyright (C) 2012-2016 MUJIN Inc. <<EMAIL>>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// 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.
#include "common.h"
#include "controllerclientimpl.h"
#if BOOST_VERSION > 104800
#include <boost/algorithm/string/replace.hpp>
#endif
#include <boost/property_tree/ptree.hpp>
#include <boost/thread.hpp> // for sleep
#include "mujincontrollerclient/binpickingtask.h"

#ifdef MUJIN_USEZMQ
#include "mujincontrollerclient/zmq.hpp"
#endif

#ifdef _WIN32
#include <float.h>
#define isnan _isnan
#endif

#include <cmath>

#include "logging.h"

#include <rapidjson/document.h>
#include <rapidjson/writer.h>
#include <rapidjson/stringbuffer.h>

MUJIN_LOGGER("mujin.controllerclientcpp.binpickingtask");

namespace mujinclient {
using namespace utils;

BinPickingResultResource::BinPickingResultResource(ControllerClientPtr controller, const std::string& pk) : PlanningResultResource(controller,"binpickingresult", pk)
{
}

BinPickingResultResource::~BinPickingResultResource()
{
}

    BinPickingTaskResource::BinPickingTaskResource(ControllerClientPtr pcontroller, const std::string& pk, const std::string& scenepk, const std::string& tasktype) : TaskResource(pcontroller,pk), _zmqPort(-1), _heartbeatPort(-1), _tasktype(tasktype), _bIsInitialized(false)
{
    _scenepk = scenepk;
    // get hostname from uri
    GETCONTROLLERIMPL();
    const std::string baseuri = controller->GetBaseUri();
    std::string::const_iterator uriend = baseuri.end();
    // query start
    std::string::const_iterator querystart = std::find(baseuri.begin(), uriend, '?');
    // protocol
    std::string protocol;
    std::string::const_iterator protocolstart = baseuri.begin();
    std::string::const_iterator protocolend = std::find(protocolstart, uriend, ':');
    if (protocolend != uriend) {
        std::string p = &*(protocolend);
        if ((p.length() > 3) & (p.substr(0,3) == "://")) {
            protocol = std::string(protocolstart, protocolend);
            protocolend +=3;
        } else {
            protocolend = baseuri.begin();
        }
    } else {
        protocolend = baseuri.begin();
    }
    // host
    std::string::const_iterator hoststart = protocolend;
    std::string::const_iterator pathstart = std::find(hoststart, uriend, '/');
    std::string::const_iterator hostend = std::find(protocolend, (pathstart != uriend) ? pathstart : querystart, ':');
    _mujinControllerIp = std::string(hoststart, hostend);

    /// HACK until can think of proper way to send sceneparams
    std::stringstream ss;
    ss.str("");
    ss << "{";
    ss << GetJsonString("scenetype", "mujincollada") << ", ";

    std::string scenebasename = pcontroller->GetNameFromPrimaryKey_UTF8(scenepk);
    ss << GetJsonString("sceneuri", std::string("mujin:/")+scenebasename) << ", ";

    // should stop sending scenefilename since it is a hack!
    std::string MUJIN_MEDIA_ROOT_DIR = "/var/www/media/u";
    char* pMUJIN_MEDIA_ROOT_DIR = getenv("MUJIN_MEDIA_ROOT_DIR");
    if( !!pMUJIN_MEDIA_ROOT_DIR ) {
        MUJIN_MEDIA_ROOT_DIR = pMUJIN_MEDIA_ROOT_DIR;
    }

    std::string scenefilename = MUJIN_MEDIA_ROOT_DIR + std::string("/") + pcontroller->GetUserName() + std::string("/") + scenebasename;
    ss << GetJsonString("scenefilename", scenefilename);
    // ss << GetJsonString("scale", "[1.0, 1.0,1.0]");
    ss << "}";
    _sceneparams_json = ss.str();
}

BinPickingTaskResource::~BinPickingTaskResource()
{
    _bShutdownHeartbeatMonitor = true;
    if (!!_pHeartbeatMonitorThread) {
        _pHeartbeatMonitorThread->join();
    }
}

void BinPickingTaskResource::Initialize(const std::string& defaultTaskParameters, const double timeout, const std::string& userinfo, const std::string& slaverequestid)
{
    if( defaultTaskParameters.size() > 0 ) {
        rapidjson::Document d;
        d.Parse(defaultTaskParameters.c_str());
        for (rapidjson::Value::ConstMemberIterator it = d.MemberBegin(); it != d.MemberEnd(); ++it) {
            rapidjson::StringBuffer stringbuffer;
            rapidjson::Writer<rapidjson::StringBuffer> writer(stringbuffer);
            it->value.Accept(writer);
            _mapTaskParameters[it->name.GetString()] = std::string(stringbuffer.GetString(), stringbuffer.GetSize());
        }
    }

    _bIsInitialized = true;
    _userinfo_json = userinfo;
    _slaverequestid = slaverequestid;
}

#ifdef MUJIN_USEZMQ
void BinPickingTaskResource::Initialize(const std::string& defaultTaskParameters, const int zmqPort, const int heartbeatPort, boost::shared_ptr<zmq::context_t> zmqcontext, const bool initializezmq, const double reinitializetimeout, const double timeout, const std::string& userinfo, const std::string& slaverequestid)
{
    if( defaultTaskParameters.size() > 0 ) {
        boost::property_tree::ptree pt;
        std::stringstream ss(defaultTaskParameters);
#if defined(_WIN32) || defined(_WIN64)
        ParsePropertyTreeWin(ss.str(), pt);
#else
        boost::property_tree::read_json(ss, pt);
#endif
        FOREACH(itchild, pt) {
            const std::string value = itchild->second.data();
            // hack?!
            if( value.size() > 0 ) {
                _mapTaskParameters[itchild->first] = GetJsonString(value);
            }
            else {
                std::stringstream ssvalue;
#if BOOST_VERSION > 104800
                write_json(ssvalue, itchild->second, false); // pretty print false
#else
                write_json(ssvalue, itchild->second);
#endif
                _mapTaskParameters[itchild->first] = ssvalue.str();
            }
            //std::cout << "initialize " << std::string(itchild->first) << "=" << _mapTaskParameters[itchild->first] << std::endl;
        }
    }

    _zmqPort = zmqPort;
    _heartbeatPort = heartbeatPort;
    _bIsInitialized = true;
    _zmqcontext = zmqcontext;
    _userinfo_json = userinfo;
    _slaverequestid = slaverequestid;
}
#endif

boost::property_tree::ptree BinPickingResultResource::GetResultPtree() const
{
    GETCONTROLLERIMPL();
    boost::property_tree::ptree pt;
    controller->CallGet(boost::str(boost::format("%s/%s/?format=json&limit=1")%GetResourceName()%GetPrimaryKey()), pt);
    return pt.get_child("output");
}

std::string utils::GetJsonString(const std::string& str)
{
    std::string newstr = str;
#if BOOST_VERSION > 104800
    boost::replace_all(newstr, "\"", "\\\"");
#else
    std::vector< std::pair<std::string, std::string> > serachpairs(1);
    serachpairs[0].first = "\""; serachpairs[0].second = "\\\"";
    SearchAndReplace(newstr, str, serachpairs);
#endif
    return "\""+newstr+"\"";
}<commit_msg>Fix the other intialize.<commit_after>// -*- coding: utf-8 -*-
// Copyright (C) 2012-2016 MUJIN Inc. <<EMAIL>>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// 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.
#include "common.h"
#include "controllerclientimpl.h"
#if BOOST_VERSION > 104800
#include <boost/algorithm/string/replace.hpp>
#endif
#include <boost/property_tree/ptree.hpp>
#include <boost/thread.hpp> // for sleep
#include "mujincontrollerclient/binpickingtask.h"

#ifdef MUJIN_USEZMQ
#include "mujincontrollerclient/zmq.hpp"
#endif

#ifdef _WIN32
#include <float.h>
#define isnan _isnan
#endif

#include <cmath>

#include "logging.h"

#include <rapidjson/document.h>
#include <rapidjson/writer.h>
#include <rapidjson/stringbuffer.h>

MUJIN_LOGGER("mujin.controllerclientcpp.binpickingtask");

namespace mujinclient {
using namespace utils;

BinPickingResultResource::BinPickingResultResource(ControllerClientPtr controller, const std::string& pk) : PlanningResultResource(controller,"binpickingresult", pk)
{
}

BinPickingResultResource::~BinPickingResultResource()
{
}

    BinPickingTaskResource::BinPickingTaskResource(ControllerClientPtr pcontroller, const std::string& pk, const std::string& scenepk, const std::string& tasktype) : TaskResource(pcontroller,pk), _zmqPort(-1), _heartbeatPort(-1), _tasktype(tasktype), _bIsInitialized(false)
{
    _scenepk = scenepk;
    // get hostname from uri
    GETCONTROLLERIMPL();
    const std::string baseuri = controller->GetBaseUri();
    std::string::const_iterator uriend = baseuri.end();
    // query start
    std::string::const_iterator querystart = std::find(baseuri.begin(), uriend, '?');
    // protocol
    std::string protocol;
    std::string::const_iterator protocolstart = baseuri.begin();
    std::string::const_iterator protocolend = std::find(protocolstart, uriend, ':');
    if (protocolend != uriend) {
        std::string p = &*(protocolend);
        if ((p.length() > 3) & (p.substr(0,3) == "://")) {
            protocol = std::string(protocolstart, protocolend);
            protocolend +=3;
        } else {
            protocolend = baseuri.begin();
        }
    } else {
        protocolend = baseuri.begin();
    }
    // host
    std::string::const_iterator hoststart = protocolend;
    std::string::const_iterator pathstart = std::find(hoststart, uriend, '/');
    std::string::const_iterator hostend = std::find(protocolend, (pathstart != uriend) ? pathstart : querystart, ':');
    _mujinControllerIp = std::string(hoststart, hostend);

    /// HACK until can think of proper way to send sceneparams
    std::stringstream ss;
    ss.str("");
    ss << "{";
    ss << GetJsonString("scenetype", "mujincollada") << ", ";

    std::string scenebasename = pcontroller->GetNameFromPrimaryKey_UTF8(scenepk);
    ss << GetJsonString("sceneuri", std::string("mujin:/")+scenebasename) << ", ";

    // should stop sending scenefilename since it is a hack!
    std::string MUJIN_MEDIA_ROOT_DIR = "/var/www/media/u";
    char* pMUJIN_MEDIA_ROOT_DIR = getenv("MUJIN_MEDIA_ROOT_DIR");
    if( !!pMUJIN_MEDIA_ROOT_DIR ) {
        MUJIN_MEDIA_ROOT_DIR = pMUJIN_MEDIA_ROOT_DIR;
    }

    std::string scenefilename = MUJIN_MEDIA_ROOT_DIR + std::string("/") + pcontroller->GetUserName() + std::string("/") + scenebasename;
    ss << GetJsonString("scenefilename", scenefilename);
    // ss << GetJsonString("scale", "[1.0, 1.0,1.0]");
    ss << "}";
    _sceneparams_json = ss.str();
}

BinPickingTaskResource::~BinPickingTaskResource()
{
    _bShutdownHeartbeatMonitor = true;
    if (!!_pHeartbeatMonitorThread) {
        _pHeartbeatMonitorThread->join();
    }
}

void BinPickingTaskResource::Initialize(const std::string& defaultTaskParameters, const double timeout, const std::string& userinfo, const std::string& slaverequestid)
{
    if( defaultTaskParameters.size() > 0 ) {
        rapidjson::Document d;
        d.Parse(defaultTaskParameters.c_str());
        for (rapidjson::Value::ConstMemberIterator it = d.MemberBegin(); it != d.MemberEnd(); ++it) {
            rapidjson::StringBuffer stringbuffer;
            rapidjson::Writer<rapidjson::StringBuffer> writer(stringbuffer);
            it->value.Accept(writer);
            _mapTaskParameters[it->name.GetString()] = std::string(stringbuffer.GetString(), stringbuffer.GetSize());
        }
    }

    _bIsInitialized = true;
    _userinfo_json = userinfo;
    _slaverequestid = slaverequestid;
}

#ifdef MUJIN_USEZMQ
void BinPickingTaskResource::Initialize(const std::string& defaultTaskParameters, const int zmqPort, const int heartbeatPort, boost::shared_ptr<zmq::context_t> zmqcontext, const bool initializezmq, const double reinitializetimeout, const double timeout, const std::string& userinfo, const std::string& slaverequestid)
{
    
#if defined(_WIN32) || defined(_WIN64)
    if( defaultTaskParameters.size() > 0 ) {
        boost::property_tree::ptree pt;
        std::stringstream ss(defaultTaskParameters);
        ParsePropertyTreeWin(ss.str(), pt);
        FOREACH(itchild, pt) {
            const std::string value = itchild->second.data();
            // hack?!
            if( value.size() > 0 ) {
                _mapTaskParameters[itchild->first] = GetJsonString(value);
            }
            else {
                std::stringstream ssvalue;
#if BOOST_VERSION > 104800
                write_json(ssvalue, itchild->second, false); // pretty print false
#else
                write_json(ssvalue, itchild->second);
#endif
                _mapTaskParameters[itchild->first] = ssvalue.str();
            }
            //std::cout << "initialize " << std::string(itchild->first) << "=" << _mapTaskParameters[itchild->first] << std::endl;
        }
    }
#else
    if( defaultTaskParameters.size() > 0 ) {
        rapidjson::Document d;
        d.Parse(defaultTaskParameters.c_str());
        for (rapidjson::Value::ConstMemberIterator it = d.MemberBegin(); it != d.MemberEnd(); ++it) {
            rapidjson::StringBuffer stringbuffer;
            rapidjson::Writer<rapidjson::StringBuffer> writer(stringbuffer);
            it->value.Accept(writer);
            _mapTaskParameters[it->name.GetString()] = std::string(stringbuffer.GetString(), stringbuffer.GetSize());
        }
    }
#endif

    _zmqPort = zmqPort;
    _heartbeatPort = heartbeatPort;
    _bIsInitialized = true;
    _zmqcontext = zmqcontext;
    _userinfo_json = userinfo;
    _slaverequestid = slaverequestid;
}
#endif

boost::property_tree::ptree BinPickingResultResource::GetResultPtree() const
{
    GETCONTROLLERIMPL();
    boost::property_tree::ptree pt;
    controller->CallGet(boost::str(boost::format("%s/%s/?format=json&limit=1")%GetResourceName()%GetPrimaryKey()), pt);
    return pt.get_child("output");
}

std::string utils::GetJsonString(const std::string& str)
{
    std::string newstr = str;
#if BOOST_VERSION > 104800
    boost::replace_all(newstr, "\"", "\\\"");
#else
    std::vector< std::pair<std::string, std::string> > serachpairs(1);
    serachpairs[0].first = "\""; serachpairs[0].second = "\\\"";
    SearchAndReplace(newstr, str, serachpairs);
#endif
    return "\""+newstr+"\"";
}
{'fraction_non_alphanumeric': 0.1406207459842091, 'fraction_numerical': 0.007827389055268173, 'mean_word_length': 3.8895174708818634, 'pattern_counts': {'":': 2, '<': 84, '<?xml version=': 0, '>': 77, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6681835', 'n_tokens_mistral': 4699, 'n_tokens_neox': 4556, 'n_words': 940}