<commit_before>// //////////////////////////////////////////////////////////////////////
// Import section
// //////////////////////////////////////////////////////////////////////
// C
#include <assert.h>
// STL
#include <string>
// STDAIR
#include <stdair/STDAIR_Types.hpp>
#include <stdair/bom/BomStructureDummy.hpp>
#include <stdair/bom/BomContentDummy.hpp>
#include <stdair/bom/BomRootStructure.hpp>
#include <stdair/factory/FacBomContent.hpp>
// AIRSCHED
#include <airsched/bom/BomRoot.hpp>
#include <airsched/bom/Inventory.hpp>
#include <airsched/bom/FlightDate.hpp>
#include <airsched/bom/SegmentDate.hpp>
#include <airsched/bom/LegDate.hpp>
#include <airsched/bom/SegmentCabin.hpp>
#include <airsched/bom/LegCabin.hpp>
#include <airsched/bom/BookingClass.hpp>
#include <airsched/command/Simulator.hpp>
#include <airsched/service/Logger.hpp>

namespace AIRSCHED {

  // //////////////////////////////////////////////////////////////////////
  void Simulator::simulate () {
    // DEBUG
    AIRSCHED_LOG_DEBUG ("Welcome to Air-Schedule");

    // Step 0.0: initialisation
    // Create the root of the Bom tree (i.e., a BomRoot object)
    BomRoot& lBomRoot =
      stdair::FacBomContent::instance().createBomRoot<BomRoot>();

    // Step 0.1: Inventory level
    // Create an Inventory (BA)
    const stdair::AirlineCode_T lAirlineCode ("BA");
    const stdair::InventoryKey_T lInventoryKey (lAirlineCode);

    Inventory& lInventory =
      stdair::FacBomContent::
      instance().create<Inventory> (lBomRoot, lInventoryKey);

    // Display the inventory
    AIRSCHED_LOG_DEBUG ("Inventory: " << lInventory.toString());

    // Step 0.2: Flight-date level
    // Create a FlightDate (BA15/10-JUN-2010)
    const stdair::FlightNumber_T lFlightNumber = 15;
    const stdair::Date_T lDate (2010, 6, 10);
    const stdair::FlightDateKey_T lFlightDateKey (lFlightNumber, lDate);

    FlightDate& lFlightDate =
      stdair::FacBomContent::instance().create<FlightDate> (lInventory,
                                                            lFlightDateKey);

    // Display the flight-date
    AIRSCHED_LOG_DEBUG ("FlightDate: " << lFlightDate.toString());

    // Step 0.3: Segment-date level
    // Create a first SegmentDate (LHR-SYD)
    const stdair::AirportCode_T lLHR ("LHR");
    const stdair::AirportCode_T lSYD ("SYD");
    stdair::SegmentDateKey_T lSegmentDateKey (lLHR, lSYD);

    SegmentDate& lLHRSYDSegment =
      stdair::FacBomContent::instance().create<SegmentDate> (lFlightDate,
                                                             lSegmentDateKey);

    // Display the segment-date
    AIRSCHED_LOG_DEBUG ("SegmentDate: " << lLHRSYDSegment.toString());

    // Create a second SegmentDate (LHR-BKK)
    const stdair::AirportCode_T lBKK ("BKK");
    lSegmentDateKey = stdair::SegmentDateKey_T (lLHR, lBKK);

    SegmentDate& lLHRBKKSegment =
      stdair::FacBomContent::instance().create<SegmentDate> (lFlightDate,
                                                             lSegmentDateKey);

    // Display the segment-date
    AIRSCHED_LOG_DEBUG ("SegmentDate: " << lLHRBKKSegment.toString());

    // Create a third SegmentDate (BKK-SYD)
    lSegmentDateKey = stdair::SegmentDateKey_T (lBKK, lSYD);

    SegmentDate& lBKKSYDSegment =
      stdair::FacBomContent::instance().create<SegmentDate> (lFlightDate,
                                                             lSegmentDateKey);

    // Display the segment-date
    AIRSCHED_LOG_DEBUG ("SegmentDate: " << lBKKSYDSegment.toString());

    // Step 0.4: Leg-date level
    // Create a first LegDate (LHR)
    stdair::LegDateKey_T lLegDateKey (lLHR);

    LegDate& lLHRLeg =
      stdair::FacBomContent::instance().create<LegDate> (lFlightDate,
                                                         lLegDateKey);

    // Display the leg-date
    AIRSCHED_LOG_DEBUG ("LegDate: " << lLHRLeg.toString());

    // Create a second LegDate (BKK)
    lLegDateKey = stdair::LegDateKey_T (lBKK);

    LegDate& lBKKLeg =
      stdair::FacBomContent::instance().create<LegDate> (lFlightDate,
                                                         lLegDateKey);

    // Display the leg-date
    AIRSCHED_LOG_DEBUG ("LegDate: " << lBKKLeg.toString());

    // Step 0.5: segment-cabin level
    // Create a SegmentCabin (Y) of the Segment LHR-BKK;
    const stdair::CabinCode_T lY ("Y");
    stdair::SegmentCabinKey_T lYSegmentCabinKey (lY);

    SegmentCabin& lLHRBKKSegmentYCabin =
      stdair::FacBomContent::
      instance().create<SegmentCabin> (lLHRBKKSegment,
                                               lYSegmentCabinKey);

    // Display the segment-cabin
    AIRSCHED_LOG_DEBUG ("SegmentCabin: " << lLHRBKKSegmentYCabin.toString());

    // Create a SegmentCabin (Y) of the Segment BKK-SYD;
    SegmentCabin& lBKKSYDSegmentYCabin =
      stdair::FacBomContent::
      instance().create<SegmentCabin> (lBKKSYDSegment,
                                               lYSegmentCabinKey);

    // Display the segment-cabin
    AIRSCHED_LOG_DEBUG ("SegmentCabin: " << lBKKSYDSegmentYCabin.toString());

    // Create a SegmentCabin (Y) of the Segment LHR-SYD;
    SegmentCabin& lLHRSYDSegmentYCabin =
      stdair::FacBomContent::
      instance().create<SegmentCabin> (lLHRSYDSegment,
                                               lYSegmentCabinKey);

    // Display the segment-cabin
    AIRSCHED_LOG_DEBUG ("SegmentCabin: " << lLHRSYDSegmentYCabin.toString());

    // Step 0.6: leg-cabin level
    // Create a LegCabin (Y) of the Leg LHR-BKK;
    stdair::LegCabinKey_T lYLegCabinKey (lY);

    LegCabin& lLHRLegYCabin =
      stdair::FacBomContent::
      instance().create<LegCabin> (lLHRLeg, lYLegCabinKey);

    // Display the leg-cabin
    AIRSCHED_LOG_DEBUG ("LegCabin: " << lLHRLegYCabin.toString());

    // Create a LegCabin (Y) of the Leg BKK-SYD;
    LegCabin& lBKKLegYCabin =
      stdair::FacBomContent::
      instance().create<LegCabin> (lBKKLeg, lYLegCabinKey);

    // Display the leg-cabin
    AIRSCHED_LOG_DEBUG ("LegCabin: " << lBKKLegYCabin.toString());

    // Step 0.7: booking class level
    // Create a BookingClass (Q) of the Segment LHR-BKK, cabin Y;
    const stdair::ClassCode_T lQ ("Q");
    stdair::BookingClassKey_T lQBookingClassKey (lQ);

    BookingClass& lLHRBKKSegmentYCabinQClass =
      stdair::FacBomContent::
      instance().create<BookingClass> (lLHRBKKSegmentYCabin,
                                               lQBookingClassKey);

    // Display the booking class
    AIRSCHED_LOG_DEBUG ("BookingClass: "
                        << lLHRBKKSegmentYCabinQClass.toString());

    // Step 0.8: Initialisation results
    // Display the full structure and content
    AIRSCHED_LOG_DEBUG ("Full Inventory: " << lInventory.toString());

  }

}
<commit_msg>Removed some includes.<commit_after>// //////////////////////////////////////////////////////////////////////
// Import section
// //////////////////////////////////////////////////////////////////////
// C
#include <assert.h>
// STL
#include <string>
// STDAIR
#include <stdair/STDAIR_Types.hpp>
#include <stdair/bom/BomRootStructure.hpp>
#include <stdair/factory/FacBomContent.hpp>
// AIRSCHED
#include <airsched/bom/BomRoot.hpp>
#include <airsched/bom/Inventory.hpp>
#include <airsched/bom/FlightDate.hpp>
#include <airsched/bom/SegmentDate.hpp>
#include <airsched/bom/LegDate.hpp>
#include <airsched/bom/SegmentCabin.hpp>
#include <airsched/bom/LegCabin.hpp>
#include <airsched/bom/BookingClass.hpp>
#include <airsched/command/Simulator.hpp>
#include <airsched/service/Logger.hpp>

namespace AIRSCHED {

  // //////////////////////////////////////////////////////////////////////
  void Simulator::simulate () {
    // DEBUG
    AIRSCHED_LOG_DEBUG ("Welcome to Air-Schedule");

    // Step 0.0: initialisation
    // Create the root of the Bom tree (i.e., a BomRoot object)
    BomRoot& lBomRoot =
      stdair::FacBomContent::instance().createBomRoot<BomRoot>();

    // Step 0.1: Inventory level
    // Create an Inventory (BA)
    const stdair::AirlineCode_T lAirlineCode ("BA");
    const stdair::InventoryKey_T lInventoryKey (lAirlineCode);

    Inventory& lInventory =
      stdair::FacBomContent::
      instance().create<Inventory> (lBomRoot, lInventoryKey);

    // Display the inventory
    AIRSCHED_LOG_DEBUG ("Inventory: " << lInventory.toString());

    // Step 0.2: Flight-date level
    // Create a FlightDate (BA15/10-JUN-2010)
    const stdair::FlightNumber_T lFlightNumber = 15;
    const stdair::Date_T lDate (2010, 6, 10);
    const stdair::FlightDateKey_T lFlightDateKey (lFlightNumber, lDate);

    FlightDate& lFlightDate =
      stdair::FacBomContent::instance().create<FlightDate> (lInventory,
                                                            lFlightDateKey);

    // Display the flight-date
    AIRSCHED_LOG_DEBUG ("FlightDate: " << lFlightDate.toString());

    // Step 0.3: Segment-date level
    // Create a first SegmentDate (LHR-SYD)
    const stdair::AirportCode_T lLHR ("LHR");
    const stdair::AirportCode_T lSYD ("SYD");
    stdair::SegmentDateKey_T lSegmentDateKey (lLHR, lSYD);

    SegmentDate& lLHRSYDSegment =
      stdair::FacBomContent::instance().create<SegmentDate> (lFlightDate,
                                                             lSegmentDateKey);

    // Display the segment-date
    AIRSCHED_LOG_DEBUG ("SegmentDate: " << lLHRSYDSegment.toString());

    // Create a second SegmentDate (LHR-BKK)
    const stdair::AirportCode_T lBKK ("BKK");
    lSegmentDateKey = stdair::SegmentDateKey_T (lLHR, lBKK);

    SegmentDate& lLHRBKKSegment =
      stdair::FacBomContent::instance().create<SegmentDate> (lFlightDate,
                                                             lSegmentDateKey);

    // Display the segment-date
    AIRSCHED_LOG_DEBUG ("SegmentDate: " << lLHRBKKSegment.toString());

    // Create a third SegmentDate (BKK-SYD)
    lSegmentDateKey = stdair::SegmentDateKey_T (lBKK, lSYD);

    SegmentDate& lBKKSYDSegment =
      stdair::FacBomContent::instance().create<SegmentDate> (lFlightDate,
                                                             lSegmentDateKey);

    // Display the segment-date
    AIRSCHED_LOG_DEBUG ("SegmentDate: " << lBKKSYDSegment.toString());

    // Step 0.4: Leg-date level
    // Create a first LegDate (LHR)
    stdair::LegDateKey_T lLegDateKey (lLHR);

    LegDate& lLHRLeg =
      stdair::FacBomContent::instance().create<LegDate> (lFlightDate,
                                                         lLegDateKey);

    // Display the leg-date
    AIRSCHED_LOG_DEBUG ("LegDate: " << lLHRLeg.toString());

    // Create a second LegDate (BKK)
    lLegDateKey = stdair::LegDateKey_T (lBKK);

    LegDate& lBKKLeg =
      stdair::FacBomContent::instance().create<LegDate> (lFlightDate,
                                                         lLegDateKey);

    // Display the leg-date
    AIRSCHED_LOG_DEBUG ("LegDate: " << lBKKLeg.toString());

    // Step 0.5: segment-cabin level
    // Create a SegmentCabin (Y) of the Segment LHR-BKK;
    const stdair::CabinCode_T lY ("Y");
    stdair::SegmentCabinKey_T lYSegmentCabinKey (lY);

    SegmentCabin& lLHRBKKSegmentYCabin =
      stdair::FacBomContent::
      instance().create<SegmentCabin> (lLHRBKKSegment,
                                               lYSegmentCabinKey);

    // Display the segment-cabin
    AIRSCHED_LOG_DEBUG ("SegmentCabin: " << lLHRBKKSegmentYCabin.toString());

    // Create a SegmentCabin (Y) of the Segment BKK-SYD;
    SegmentCabin& lBKKSYDSegmentYCabin =
      stdair::FacBomContent::
      instance().create<SegmentCabin> (lBKKSYDSegment,
                                               lYSegmentCabinKey);

    // Display the segment-cabin
    AIRSCHED_LOG_DEBUG ("SegmentCabin: " << lBKKSYDSegmentYCabin.toString());

    // Create a SegmentCabin (Y) of the Segment LHR-SYD;
    SegmentCabin& lLHRSYDSegmentYCabin =
      stdair::FacBomContent::
      instance().create<SegmentCabin> (lLHRSYDSegment,
                                               lYSegmentCabinKey);

    // Display the segment-cabin
    AIRSCHED_LOG_DEBUG ("SegmentCabin: " << lLHRSYDSegmentYCabin.toString());

    // Step 0.6: leg-cabin level
    // Create a LegCabin (Y) of the Leg LHR-BKK;
    stdair::LegCabinKey_T lYLegCabinKey (lY);

    LegCabin& lLHRLegYCabin =
      stdair::FacBomContent::
      instance().create<LegCabin> (lLHRLeg, lYLegCabinKey);

    // Display the leg-cabin
    AIRSCHED_LOG_DEBUG ("LegCabin: " << lLHRLegYCabin.toString());

    // Create a LegCabin (Y) of the Leg BKK-SYD;
    LegCabin& lBKKLegYCabin =
      stdair::FacBomContent::
      instance().create<LegCabin> (lBKKLeg, lYLegCabinKey);

    // Display the leg-cabin
    AIRSCHED_LOG_DEBUG ("LegCabin: " << lBKKLegYCabin.toString());

    // Step 0.7: booking class level
    // Create a BookingClass (Q) of the Segment LHR-BKK, cabin Y;
    const stdair::ClassCode_T lQ ("Q");
    stdair::BookingClassKey_T lQBookingClassKey (lQ);

    BookingClass& lLHRBKKSegmentYCabinQClass =
      stdair::FacBomContent::
      instance().create<BookingClass> (lLHRBKKSegmentYCabin,
                                               lQBookingClassKey);

    // Display the booking class
    AIRSCHED_LOG_DEBUG ("BookingClass: "
                        << lLHRBKKSegmentYCabinQClass.toString());

    // Step 0.8: Initialisation results
    // Display the full structure and content
    AIRSCHED_LOG_DEBUG ("Full Inventory: " << lInventory.toString());

  }

}
<|endoftext|>


<commit_before>/*************************************************************************
 *
 *  OpenOffice.org - a multi-platform office productivity suite
 *
 *  $RCSfile: propertyanimationnode.hxx,v $
 *
 *  $Revision: 1.4 $
 *
 *  last change: $Author: rt $ $Date: 2006-07-26 07:36:34 $
 *
 *  The Contents of this file are made available subject to
 *  the terms of GNU Lesser General Public License Version 2.1.
 *
 *
 *    GNU Lesser General Public License Version 2.1
 *    =============================================
 *    Copyright 2005 by Sun Microsystems, Inc.
 *    901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License version 2.1, as published by the Free Software Foundation.
 *
 *    This library 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
 *    Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public
 *    License along with this library; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *    MA  02111-1307  USA
 *
 ************************************************************************/
#ifndef INCLUDED_SLIDESHOW_PROPERTYANIMATIONNODE_HXX
#define INCLUDED_SLIDESHOW_PROPERTYANIMATIONNODE_HXX

#include "animationbasenode.hxx"

namespace presentation {
namespace internal {

class PropertyAnimationNode : public AnimationBaseNode
{
public:
    PropertyAnimationNode(
        ::com::sun::star::uno::Reference<
        ::com::sun::star::animations::XAnimationNode> const& xNode,
        ::boost::shared_ptr<BaseContainerNode> const& pParent,
        NodeContext const& rContext )
        : AnimationBaseNode( xNode, pParent, rContext ) {}

#if defined(VERBOSE) && defined(DBG_UTIL)
    virtual const char* getDescription() const
        { return "PropertyAnimationNode"; }
#endif

private:
    virtual AnimationActivitySharedPtr createActivity() const;
};

} // namespace internal
} // namespace presentation

#endif /* INCLUDED_SLIDESHOW_PROPERTYANIMATIONNODE_HXX */

<commit_msg>INTEGRATION: CWS presfixes09 (1.3.18); FILE MERGED 2006/09/15 22:16:49 thb 1.3.18.2: RESYNC: (1.3-1.4); FILE MERGED 2006/03/24 18:23:19 thb 1.3.18.1: #i37778# Moved whole slideshow engine from namespace presentation (which conflicts with one of the UNO subnamespaces) to slideshow<commit_after>/*************************************************************************
 *
 *  OpenOffice.org - a multi-platform office productivity suite
 *
 *  $RCSfile: propertyanimationnode.hxx,v $
 *
 *  $Revision: 1.5 $
 *
 *  last change: $Author: kz $ $Date: 2006-12-13 15:34:40 $
 *
 *  The Contents of this file are made available subject to
 *  the terms of GNU Lesser General Public License Version 2.1.
 *
 *
 *    GNU Lesser General Public License Version 2.1
 *    =============================================
 *    Copyright 2005 by Sun Microsystems, Inc.
 *    901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License version 2.1, as published by the Free Software Foundation.
 *
 *    This library 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
 *    Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public
 *    License along with this library; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *    MA  02111-1307  USA
 *
 ************************************************************************/
#ifndef INCLUDED_SLIDESHOW_PROPERTYANIMATIONNODE_HXX
#define INCLUDED_SLIDESHOW_PROPERTYANIMATIONNODE_HXX

#include "animationbasenode.hxx"

namespace slideshow {
namespace internal {

class PropertyAnimationNode : public AnimationBaseNode
{
public:
    PropertyAnimationNode(
        ::com::sun::star::uno::Reference<
        ::com::sun::star::animations::XAnimationNode> const& xNode,
        ::boost::shared_ptr<BaseContainerNode> const& pParent,
        NodeContext const& rContext )
        : AnimationBaseNode( xNode, pParent, rContext ) {}

#if defined(VERBOSE) && defined(DBG_UTIL)
    virtual const char* getDescription() const
        { return "PropertyAnimationNode"; }
#endif

private:
    virtual AnimationActivitySharedPtr createActivity() const;
};

} // namespace internal
} // namespace slideshow

#endif /* INCLUDED_SLIDESHOW_PROPERTYANIMATIONNODE_HXX */

<|endoftext|>


<commit_before>// Jubatus: Online machine learning framework for distributed environment
// Copyright (C) 2011,2012 Preferred Infrastructure and Nippon Telegraph and Telephone Corporation.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License version 2.1 as published by the Free Software Foundation.
//
// This library 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

#ifndef JUBATUS_SERVER_FRAMEWORK_SERVER_HELPER_HPP_
#define JUBATUS_SERVER_FRAMEWORK_SERVER_HELPER_HPP_

#include <sys/types.h>
#include <unistd.h>

#include <map>
#include <string>
#include <glog/logging.h>
#include "jubatus/util/lang/bind.h"
#include "jubatus/util/lang/shared_ptr.h"
#include "jubatus/util/system/sysstat.h"
#include "jubatus/util/system/time_util.h"

#include "jubatus/core/common/jsonconfig.hpp"
#include "mixer/mixer.hpp"
#include "server_util.hpp"
#include "../../config.hpp"
#include "../common/lock_service.hpp"
#include "../common/mprpc/rpc_server.hpp"
#include "../common/signals.hpp"
#include "../common/config.hpp"

using jubatus::util::system::time::clock_time;
using jubatus::util::system::time::get_clock_time;

namespace jubatus {
namespace server {
namespace framework {

class server_helper_impl {
 public:
  explicit server_helper_impl(const server_argv& a);
  ~server_helper_impl();

  void prepare_for_start(const server_argv& a, bool use_cht);
  void prepare_for_run(const server_argv& a, bool use_cht);
  void get_config_lock(const server_argv& a, int retry);

  jubatus::util::lang::shared_ptr<common::lock_service> zk() const {
    return zk_;
  }

 private:
  jubatus::util::lang::shared_ptr<common::lock_service> zk_;
  jubatus::util::lang::shared_ptr<common::try_lockable> zk_config_lock_;
};

template<typename Server>
class server_helper {
 public:
  typedef typename Server::status_t status_t;

  explicit server_helper(const server_argv& a, bool use_cht = false)
      : impl_(a),
        start_time_(get_clock_time()),
        use_cht_(use_cht) {
    impl_.prepare_for_start(a, use_cht);
    server_.reset(new Server(a, impl_.zk()));

    impl_.get_config_lock(a, 3);

    try {
      server_->set_config(get_conf(a));
    } catch (const core::common::jsonconfig::cast_check_error& e) {
      core::common::config_exception config_error;
      const core::common::jsonconfig::config_error_list& errors = e.errors();
      for (core::common::jsonconfig::config_error_list::const_iterator
          it = errors.begin(), end = errors.end(); it != end; ++it) {
        config_error << core::common::exception::error_message((*it)->what());
      }
      // send error message to caller
      throw JUBATUS_EXCEPTION(config_error);
    } catch (const jubatus::util::lang::parse_error& e) {
      // exit immediately on JSON parse error with exit-code 1
      std::string msg =
          std::string("syntax error in configuration: ") +
          (a.is_standalone() ?
           a.configpath :
           std::string("<zookeeper>")) + ":" +
          jubatus::util::lang::lexical_cast<std::string>(e.lineno()) + ":" +
          jubatus::util::lang::lexical_cast<std::string>(e.pos()) + " " +
          e.msg();

      LOG(ERROR) << msg;
      exit(1);
    }

    try {
      // standalone only, is it desirable?
      if (a.is_standalone() && !a.modelpath.empty()) {
        server_->load_file(a.modelpath);
      }
    } catch (const std::runtime_error& e) {
      exit(1);
    }
  }

  std::map<std::string, std::string> get_loads() const {
    std::map<std::string, std::string> result;
    {
      jubatus::util::system::sysstat::sysstat_ret sys;
      get_sysstat(sys);
      result["loadavg"] =
          jubatus::util::lang::lexical_cast<std::string>(sys.loadavg);
      result["total_memory"] = jubatus::util::lang::lexical_cast<std::string>(
          sys.total_memory);
      result["free_memory"] = jubatus::util::lang::lexical_cast<std::string>(
          sys.free_memory);
    }
    return result;
  }

  std::map<std::string, status_t> get_status() const {
    std::map<std::string, status_t> status;
    const server_argv& a = server_->argv();
    status_t& data = status[get_server_identifier(a)];

    const clock_time ct = get_clock_time();
    data["clock_time"] =
       jubatus::util::lang::lexical_cast<std::string>(ct.sec);
    data["start_time"] =
       jubatus::util::lang::lexical_cast<std::string>(start_time_.sec);
    data["uptime"] =
       jubatus::util::lang::lexical_cast<std::string>((ct - start_time_).sec);

    common::machine_status_t mt;
    common::get_machine_status(mt);
    data["VIRT"] =
        jubatus::util::lang::lexical_cast<std::string>(mt.vm_size);
    data["RSS"] =
        jubatus::util::lang::lexical_cast<std::string>(mt.vm_resident);
    data["SHR"] =
        jubatus::util::lang::lexical_cast<std::string>(mt.vm_share);

    data["timeout"] = jubatus::util::lang::lexical_cast<std::string>(a.timeout);
    data["threadnum"] =
        jubatus::util::lang::lexical_cast<std::string>(a.threadnum);
    data["datadir"] = a.datadir;
    data["is_standalone"] = jubatus::util::lang::lexical_cast<std::string>(
        a.is_standalone());
    data["VERSION"] = JUBATUS_VERSION;
    data["PROGNAME"] = a.program_name;
    data["type"] = a.type;
    data["logdir"] = a.logdir;
    data["loglevel"] = google::GetLogSeverityName(a.loglevel);

    std::string configpath;
    if (a.is_standalone()) {
      configpath = a.configpath;
    } else {
#ifdef HAVE_ZOOKEEPER_H
      // return zookeeper node name
      jubatus::server::common::build_config_path(configpath, a.type, a.name);
#endif
    }
    data["configpath"] = configpath;

    data["pid"] =
        jubatus::util::lang::lexical_cast<std::string>(getpid());
    data["user"] = jubatus::server::common::get_user_name();

    data["update_count"] = jubatus::util::lang::lexical_cast<std::string>(
        server_->update_count());

    data["last_saved"] =
        jubatus::util::lang::lexical_cast<std::string>
        (server_->last_saved_sec());
    data["last_saved_path"] = server_->last_saved_path();
    data["last_loaded"] =
        jubatus::util::lang::lexical_cast<std::string>
        (server_->last_loaded_sec());
    data["last_loaded_path"] = server_->last_loaded_path();

    server_->get_status(data);

    // distributed mode only
    if (!a.is_standalone()) {
      data["zk"] = a.z;
      data["name"] = a.name;
      data["interval_sec"] =
          jubatus::util::lang::lexical_cast<std::string>(a.interval_sec);
      data["interval_count"] = jubatus::util::lang::lexical_cast<std::string>(
          a.interval_count);
      data["zookeeper_timeout"] =
          jubatus::util::lang::lexical_cast<std::string>(a.zookeeper_timeout);
      data["interconnect_timeout"] =
          jubatus::util::lang::lexical_cast<std::string>
          (a.interconnect_timeout);
      data["connected_zookeeper"] = impl_.zk()->get_connected_host_and_port();
      data["use_cht"] = jubatus::util::lang::lexical_cast<std::string>(
          use_cht_);

      data["mixer"] = a.mixer;
      server_->get_mixer()->get_status(data);
    }

    return status;
  }

  int start(common::mprpc::rpc_server& serv) {
    const server_argv& a = server_->argv();

    if (!a.is_standalone()) {
      server_->get_mixer()->start();
    }

    try {
      serv.listen(a.port, a.bind_address);
      LOG(INFO) << "start listening at port " << a.port;

      start_time_ = get_clock_time();
      serv.start(a.threadnum, true);

      // RPC server started, then register group membership
      impl_.prepare_for_run(a, use_cht_);
      LOG(INFO) << common::get_program_name() << " RPC server startup";

      // Stop RPC server when terminate signal is sent
      common::set_action_on_term(
          jubatus::util::lang::bind(
              &server_helper::stop, this, jubatus::util::lang::ref(serv)));

      // wait for termination
      serv.join();

      // RPC server stopped, then stop mixer
      if (!a.is_standalone()) {
        server_->get_mixer()->stop();
      }

      return 0;
    } catch (const mp::system_error& e) {
      if (e.code == EADDRINUSE) {
        LOG(FATAL) << "server failed to start: any process using port "
            << a.port << "?";
      } else {
        LOG(FATAL) << "server failed to start: " << e.what();
      }
    } catch (jubatus::core::common::exception::jubatus_exception&) {
      throw;
    } catch (const std::exception& e) {
      LOG(FATAL) << "server failed to start: " << e.what();
    }
    return -1;
  }

  void stop(common::mprpc::rpc_server& serv) {
    LOG(INFO) << "stopping RPC server";
    serv.end();
  }

  jubatus::util::lang::shared_ptr<Server> server() const {
    return server_;
  }

  jubatus::util::concurrent::rw_mutex& rw_mutex() {
    return server_->rw_mutex();
  }

 private:
  jubatus::util::lang::shared_ptr<Server> server_;
  server_helper_impl impl_;
  clock_time start_time_;
  const bool use_cht_;
};

}  // namespace framework
}  // namespace server
}  // namespace jubatus

#define JRLOCK_(p) \
  ::jubatus::util::concurrent::scoped_rlock lk((p)->rw_mutex())

#define JWLOCK_(p) \
  ::jubatus::util::concurrent::scoped_wlock lk((p)->rw_mutex()); \
  (p)->server()->event_model_updated()

#define NOLOCK_(p)

#endif  // JUBATUS_SERVER_FRAMEWORK_SERVER_HELPER_HPP_
<commit_msg>Fix jubatus servers to stop mixer before RPC server stopped<commit_after>// Jubatus: Online machine learning framework for distributed environment
// Copyright (C) 2011,2012 Preferred Infrastructure and Nippon Telegraph and Telephone Corporation.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License version 2.1 as published by the Free Software Foundation.
//
// This library 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

#ifndef JUBATUS_SERVER_FRAMEWORK_SERVER_HELPER_HPP_
#define JUBATUS_SERVER_FRAMEWORK_SERVER_HELPER_HPP_

#include <sys/types.h>
#include <unistd.h>

#include <map>
#include <string>
#include <glog/logging.h>
#include "jubatus/util/lang/bind.h"
#include "jubatus/util/lang/shared_ptr.h"
#include "jubatus/util/system/sysstat.h"
#include "jubatus/util/system/time_util.h"

#include "jubatus/core/common/jsonconfig.hpp"
#include "mixer/mixer.hpp"
#include "server_util.hpp"
#include "../../config.hpp"
#include "../common/lock_service.hpp"
#include "../common/mprpc/rpc_server.hpp"
#include "../common/signals.hpp"
#include "../common/config.hpp"

using jubatus::util::system::time::clock_time;
using jubatus::util::system::time::get_clock_time;

namespace jubatus {
namespace server {
namespace framework {

class server_helper_impl {
 public:
  explicit server_helper_impl(const server_argv& a);
  ~server_helper_impl();

  void prepare_for_start(const server_argv& a, bool use_cht);
  void prepare_for_run(const server_argv& a, bool use_cht);
  void get_config_lock(const server_argv& a, int retry);

  jubatus::util::lang::shared_ptr<common::lock_service> zk() const {
    return zk_;
  }

 private:
  jubatus::util::lang::shared_ptr<common::lock_service> zk_;
  jubatus::util::lang::shared_ptr<common::try_lockable> zk_config_lock_;
};

template<typename Server>
class server_helper {
 public:
  typedef typename Server::status_t status_t;

  explicit server_helper(const server_argv& a, bool use_cht = false)
      : impl_(a),
        start_time_(get_clock_time()),
        use_cht_(use_cht) {
    impl_.prepare_for_start(a, use_cht);
    server_.reset(new Server(a, impl_.zk()));

    impl_.get_config_lock(a, 3);

    try {
      server_->set_config(get_conf(a));
    } catch (const core::common::jsonconfig::cast_check_error& e) {
      core::common::config_exception config_error;
      const core::common::jsonconfig::config_error_list& errors = e.errors();
      for (core::common::jsonconfig::config_error_list::const_iterator
          it = errors.begin(), end = errors.end(); it != end; ++it) {
        config_error << core::common::exception::error_message((*it)->what());
      }
      // send error message to caller
      throw JUBATUS_EXCEPTION(config_error);
    } catch (const jubatus::util::lang::parse_error& e) {
      // exit immediately on JSON parse error with exit-code 1
      std::string msg =
          std::string("syntax error in configuration: ") +
          (a.is_standalone() ?
           a.configpath :
           std::string("<zookeeper>")) + ":" +
          jubatus::util::lang::lexical_cast<std::string>(e.lineno()) + ":" +
          jubatus::util::lang::lexical_cast<std::string>(e.pos()) + " " +
          e.msg();

      LOG(ERROR) << msg;
      exit(1);
    }

    try {
      // standalone only, is it desirable?
      if (a.is_standalone() && !a.modelpath.empty()) {
        server_->load_file(a.modelpath);
      }
    } catch (const std::runtime_error& e) {
      exit(1);
    }
  }

  std::map<std::string, std::string> get_loads() const {
    std::map<std::string, std::string> result;
    {
      jubatus::util::system::sysstat::sysstat_ret sys;
      get_sysstat(sys);
      result["loadavg"] =
          jubatus::util::lang::lexical_cast<std::string>(sys.loadavg);
      result["total_memory"] = jubatus::util::lang::lexical_cast<std::string>(
          sys.total_memory);
      result["free_memory"] = jubatus::util::lang::lexical_cast<std::string>(
          sys.free_memory);
    }
    return result;
  }

  std::map<std::string, status_t> get_status() const {
    std::map<std::string, status_t> status;
    const server_argv& a = server_->argv();
    status_t& data = status[get_server_identifier(a)];

    const clock_time ct = get_clock_time();
    data["clock_time"] =
       jubatus::util::lang::lexical_cast<std::string>(ct.sec);
    data["start_time"] =
       jubatus::util::lang::lexical_cast<std::string>(start_time_.sec);
    data["uptime"] =
       jubatus::util::lang::lexical_cast<std::string>((ct - start_time_).sec);

    common::machine_status_t mt;
    common::get_machine_status(mt);
    data["VIRT"] =
        jubatus::util::lang::lexical_cast<std::string>(mt.vm_size);
    data["RSS"] =
        jubatus::util::lang::lexical_cast<std::string>(mt.vm_resident);
    data["SHR"] =
        jubatus::util::lang::lexical_cast<std::string>(mt.vm_share);

    data["timeout"] = jubatus::util::lang::lexical_cast<std::string>(a.timeout);
    data["threadnum"] =
        jubatus::util::lang::lexical_cast<std::string>(a.threadnum);
    data["datadir"] = a.datadir;
    data["is_standalone"] = jubatus::util::lang::lexical_cast<std::string>(
        a.is_standalone());
    data["VERSION"] = JUBATUS_VERSION;
    data["PROGNAME"] = a.program_name;
    data["type"] = a.type;
    data["logdir"] = a.logdir;
    data["loglevel"] = google::GetLogSeverityName(a.loglevel);

    std::string configpath;
    if (a.is_standalone()) {
      configpath = a.configpath;
    } else {
#ifdef HAVE_ZOOKEEPER_H
      // return zookeeper node name
      jubatus::server::common::build_config_path(configpath, a.type, a.name);
#endif
    }
    data["configpath"] = configpath;

    data["pid"] =
        jubatus::util::lang::lexical_cast<std::string>(getpid());
    data["user"] = jubatus::server::common::get_user_name();

    data["update_count"] = jubatus::util::lang::lexical_cast<std::string>(
        server_->update_count());

    data["last_saved"] =
        jubatus::util::lang::lexical_cast<std::string>
        (server_->last_saved_sec());
    data["last_saved_path"] = server_->last_saved_path();
    data["last_loaded"] =
        jubatus::util::lang::lexical_cast<std::string>
        (server_->last_loaded_sec());
    data["last_loaded_path"] = server_->last_loaded_path();

    server_->get_status(data);

    // distributed mode only
    if (!a.is_standalone()) {
      data["zk"] = a.z;
      data["name"] = a.name;
      data["interval_sec"] =
          jubatus::util::lang::lexical_cast<std::string>(a.interval_sec);
      data["interval_count"] = jubatus::util::lang::lexical_cast<std::string>(
          a.interval_count);
      data["zookeeper_timeout"] =
          jubatus::util::lang::lexical_cast<std::string>(a.zookeeper_timeout);
      data["interconnect_timeout"] =
          jubatus::util::lang::lexical_cast<std::string>
          (a.interconnect_timeout);
      data["connected_zookeeper"] = impl_.zk()->get_connected_host_and_port();
      data["use_cht"] = jubatus::util::lang::lexical_cast<std::string>(
          use_cht_);

      data["mixer"] = a.mixer;
      server_->get_mixer()->get_status(data);
    }

    return status;
  }

  int start(common::mprpc::rpc_server& serv) {
    const server_argv& a = server_->argv();

    if (!a.is_standalone()) {
      server_->get_mixer()->start();
    }

    try {
      serv.listen(a.port, a.bind_address);
      LOG(INFO) << "start listening at port " << a.port;

      start_time_ = get_clock_time();
      serv.start(a.threadnum, true);

      // RPC server started, then register group membership
      impl_.prepare_for_run(a, use_cht_);
      LOG(INFO) << common::get_program_name() << " RPC server startup";

      // Stop RPC server when terminate signal is sent
      common::set_action_on_term(
          jubatus::util::lang::bind(
              &server_helper::stop, this, jubatus::util::lang::ref(serv)));

      // wait for termination
      serv.join();

      return 0;
    } catch (const mp::system_error& e) {
      if (e.code == EADDRINUSE) {
        LOG(FATAL) << "server failed to start: any process using port "
            << a.port << "?";
      } else {
        LOG(FATAL) << "server failed to start: " << e.what();
      }
    } catch (jubatus::core::common::exception::jubatus_exception&) {
      throw;
    } catch (const std::exception& e) {
      LOG(FATAL) << "server failed to start: " << e.what();
    }
    return -1;
  }

  void stop(common::mprpc::rpc_server& serv) {
    // stop mixer before RPC server stopped for avoiding mixer RPC call to the
    // server itself
    LOG(INFO) << "stopping mixer thread";
    if (!server_->argv().is_standalone()) {
      server_->get_mixer()->stop();
    }

    LOG(INFO) << "stopping RPC server";
    serv.end();
  }

  jubatus::util::lang::shared_ptr<Server> server() const {
    return server_;
  }

  jubatus::util::concurrent::rw_mutex& rw_mutex() {
    return server_->rw_mutex();
  }

 private:
  jubatus::util::lang::shared_ptr<Server> server_;
  server_helper_impl impl_;
  clock_time start_time_;
  const bool use_cht_;
};

}  // namespace framework
}  // namespace server
}  // namespace jubatus

#define JRLOCK_(p) \
  ::jubatus::util::concurrent::scoped_rlock lk((p)->rw_mutex())

#define JWLOCK_(p) \
  ::jubatus::util::concurrent::scoped_wlock lk((p)->rw_mutex()); \
  (p)->server()->event_model_updated()

#define NOLOCK_(p)

#endif  // JUBATUS_SERVER_FRAMEWORK_SERVER_HELPER_HPP_
<|endoftext|>


<commit_before>//..............................................................................
//
//  This file is part of the Jancy toolkit.
//
//  Jancy is distributed under the MIT license.
//  For details see accompanying license.txt file,
//  the public copy of which is also available at:
//  http://tibbo.com/downloads/archive/jancy/license.txt
//
//..............................................................................

#include "pch.h"
#include "jnc_ct_ArrayType.h"
#include "jnc_ct_Module.h"
#include "jnc_rt_GcHeap.h"

namespace jnc {
namespace ct {

//..............................................................................

ArrayType::ArrayType()
{
	m_typeKind = TypeKind_Array;
	m_flags = TypeFlag_StructRet;
	m_elementType = NULL;
	m_rootType = NULL;
	m_elementCount = -1;
	m_getDynamicSizeFunction = NULL;
	m_parentUnit = NULL;
	m_parentNamespace = NULL;
}

Type*
ArrayType::getRootType()
{
	if (!m_rootType)
		m_rootType = m_elementType->getTypeKind() == TypeKind_Array ?
			((ArrayType*)m_elementType)->getRootType() :
			m_elementType;

	return m_rootType;
}

void
ArrayType::prepareTypeString()
{
	TypeStringTuple* tuple = getTypeStringTuple();
	tuple->m_typeStringPrefix = getRootType()->getTypeString();
	tuple->m_typeStringSuffix = createDimensionString();
}

void
ArrayType::prepareDoxyLinkedText()
{
	TypeStringTuple* tuple = getTypeStringTuple();
	tuple->m_doxyLinkedTextPrefix = getRootType()->getDoxyLinkedTextPrefix();
	tuple->m_doxyLinkedTextSuffix = createDimensionString();
}

sl::String
ArrayType::createDimensionString()
{
	sl::String string;

	if (m_elementCount == -1)
		string = "[]";
	else
		string.format("[%d]", m_elementCount);

	Type* elementType = m_elementType;
	while (elementType->getTypeKind() == TypeKind_Array)
	{
		ArrayType* arrayType = (ArrayType*)elementType;
		string.appendFormat(" [%d]", arrayType->m_elementCount);
		elementType = arrayType->m_elementType;
	}

	return string;
}

bool
ArrayType::ensureDynamicLayout(
	StructType* dynamicStruct,
	Field* dynamicField
	)
{
	bool result;

	if (m_flags & ModuleItemFlag_LayoutReady)
		return true;

	result = calcLayoutImpl(dynamicStruct, dynamicField);
	if (!result)
		return false;

	m_flags |= ModuleItemFlag_LayoutReady;
	return true;
}

bool
ArrayType::calcLayoutImpl(
	StructType* dynamicStruct,
	Field* dynamicField
	)
{
	bool result = m_elementType->ensureLayout();
	if (!result)
		return false;

	if (m_elementType->getTypeKind() == TypeKind_Class ||
		m_elementType->getFlags() & TypeFlag_Dynamic)
	{
		err::setFormatStringError("'%s' cannot be an element of an array", m_elementType->getTypeString().sz());
		return false;
	}

	// ensure update

	m_rootType = NULL;
	if (m_typeStringTuple)
	{
		AXL_MEM_DELETE(m_typeStringTuple);
		m_typeStringTuple = NULL;
	}

	uint_t rootTypeFlags = getRootType()->getFlags();
	if (rootTypeFlags & TypeFlag_Pod)
		m_flags |= TypeFlag_Pod;
	else if (rootTypeFlags & TypeFlag_GcRoot)
		m_flags |= TypeFlag_GcRoot;

	m_alignment = m_elementType->getAlignment();

	// calculate size

	if (!m_elementCountInitializer.isEmpty())
	{
		ASSERT(m_parentUnit && m_parentNamespace);

		Unit* prevUnit = m_module->m_unitMgr.setCurrentUnit(m_parentUnit);
		m_module->m_namespaceMgr.openNamespace(m_parentNamespace);

		int64_t value = 0;
		result = m_module->m_operatorMgr.parseConstIntegerExpression(m_elementCountInitializer, &value);

		m_module->m_namespaceMgr.closeNamespace();
		m_module->m_unitMgr.setCurrentUnit(prevUnit);

		if (!result)
		{
			if (!dynamicStruct)
				return false;

			sl::String qualifiedName = sl::formatString(
				"%s.%s.getDynamicSize",
				dynamicStruct->getQualifiedName().sz(),
				dynamicField->getName().sz()
				);

			Type* returnType = m_module->m_typeMgr.getPrimitiveType(TypeKind_SizeT);
			FunctionType* type = m_module->m_typeMgr.getFunctionType(returnType, NULL, 0);

			m_getDynamicSizeFunction = m_module->m_functionMgr.createInternalFunction<GetDynamicSizeFunction>(
				qualifiedName,
				type
				);

			m_getDynamicSizeFunction->m_arrayType = this;
			m_getDynamicSizeFunction->m_storageKind = StorageKind_Member;
			m_getDynamicSizeFunction->convertToMemberMethod(dynamicStruct);
			m_module->markForCompile(m_getDynamicSizeFunction);

			m_flags |= TypeFlag_Dynamic;
			m_elementCount = 0;
			m_size = 0;
			return true;
		}

		if (value <= 0)
		{
			err::setFormatStringError("invalid array size '%lld'\n", value);
			lex::pushSrcPosError(
				m_parentUnit->getFilePath(),
				m_elementCountInitializer.getHead()->m_pos
				);

			return false;
		}

#if (JNC_PTR_SIZE == 4)
		if (value >= (uint32_t) -1)
		{
			err::setFormatStringError("array size '%lld' is too big\n", value);
			lex::pushSrcPosError(
				m_parentUnit->getFilePath(),
				m_elementCountInitializer.getHead()->m_pos
				);

			return false;
		}
#endif

		m_elementCount = (size_t)value;
	}

	m_size = m_elementType->getSize() * m_elementCount;
	if (m_size > TypeSizeLimit_StackAllocSize)
		m_flags |= TypeFlag_NoStack;

	return true;
}

sl::String
ArrayType::getValueString(
	const void* p0,
	const char* formatSpec
	)
{
	if (m_flags & TypeFlag_Dynamic)
	{
		AXL_TODO("format dynamic arrays")
		return "{ dynamic-array }";
	}

	const char* p = (char*)p0;

	if (m_elementType->getTypeKind() == TypeKind_Char)
	{
		const char* null = (const char*)memchr(p, 0, m_elementCount);
		return sl::String(p, null ? null - p : m_elementCount);
	}

	if (!m_elementCount)
		return "{}";

	sl::String string = "{ " + m_elementType->getValueString(p);

	for (size_t i = 1; i < m_elementCount; i++)
	{
		p += m_elementType->getSize();

		string += ", ";
		string += m_elementType->getValueString(p, formatSpec);
	}

	string += " }";
	return string;
}

void
ArrayType::markGcRoots(
	const void* p,
	rt::GcHeap* gcHeap
	)
{
	ASSERT(m_flags & TypeFlag_GcRoot);
	gcHeap->addRootArray(p, m_elementType, m_elementCount);
}

void
ArrayType::prepareLlvmDiType()
{
	m_llvmDiType = m_module->m_llvmDiBuilder.createArrayType(this);
}

bool
ArrayType::compileGetDynamicSizeFunction(Function* function)
{
	ASSERT(function == m_getDynamicSizeFunction);

	bool result;

	Unit* parentUnit = function->getParentUnit();
	if (parentUnit)
		m_module->m_unitMgr.setCurrentUnit(parentUnit);

	m_module->m_namespaceMgr.openNamespace(function->getParentNamespace());
	m_module->m_functionMgr.internalPrologue(function);
	m_module->m_functionMgr.createThisValue();

	Value resultValue;
	result = m_module->m_operatorMgr.parseExpression(m_elementCountInitializer, &resultValue);
	if (!result)
		return false;

	size_t size = m_elementType->getSize();
	if (size != 1)
	{
		Value sizeValue(size, m_module->m_typeMgr.getPrimitiveType(TypeKind_SizeT));
		result = m_module->m_operatorMgr.binaryOperator(
			BinOpKind_Mul,
			&resultValue,
			sizeValue
			);

		if (!result)
			return false;
	}

	result = m_module->m_controlFlowMgr.ret(resultValue);
	if (!result)
		return false;

	m_module->m_functionMgr.internalEpilogue();
	m_module->m_namespaceMgr.closeNamespace();
	return true;
}

//..............................................................................

} // namespace ct
} // namespace jnc
<commit_msg>[jnc_ct] respect formatSpec for char literals in ArrayType::getValueString<commit_after>//..............................................................................
//
//  This file is part of the Jancy toolkit.
//
//  Jancy is distributed under the MIT license.
//  For details see accompanying license.txt file,
//  the public copy of which is also available at:
//  http://tibbo.com/downloads/archive/jancy/license.txt
//
//..............................................................................

#include "pch.h"
#include "jnc_ct_ArrayType.h"
#include "jnc_ct_Module.h"
#include "jnc_rt_GcHeap.h"

namespace jnc {
namespace ct {

//..............................................................................

ArrayType::ArrayType()
{
	m_typeKind = TypeKind_Array;
	m_flags = TypeFlag_StructRet;
	m_elementType = NULL;
	m_rootType = NULL;
	m_elementCount = -1;
	m_getDynamicSizeFunction = NULL;
	m_parentUnit = NULL;
	m_parentNamespace = NULL;
}

Type*
ArrayType::getRootType()
{
	if (!m_rootType)
		m_rootType = m_elementType->getTypeKind() == TypeKind_Array ?
			((ArrayType*)m_elementType)->getRootType() :
			m_elementType;

	return m_rootType;
}

void
ArrayType::prepareTypeString()
{
	TypeStringTuple* tuple = getTypeStringTuple();
	tuple->m_typeStringPrefix = getRootType()->getTypeString();
	tuple->m_typeStringSuffix = createDimensionString();
}

void
ArrayType::prepareDoxyLinkedText()
{
	TypeStringTuple* tuple = getTypeStringTuple();
	tuple->m_doxyLinkedTextPrefix = getRootType()->getDoxyLinkedTextPrefix();
	tuple->m_doxyLinkedTextSuffix = createDimensionString();
}

sl::String
ArrayType::createDimensionString()
{
	sl::String string;

	if (m_elementCount == -1)
		string = "[]";
	else
		string.format("[%d]", m_elementCount);

	Type* elementType = m_elementType;
	while (elementType->getTypeKind() == TypeKind_Array)
	{
		ArrayType* arrayType = (ArrayType*)elementType;
		string.appendFormat(" [%d]", arrayType->m_elementCount);
		elementType = arrayType->m_elementType;
	}

	return string;
}

bool
ArrayType::ensureDynamicLayout(
	StructType* dynamicStruct,
	Field* dynamicField
	)
{
	bool result;

	if (m_flags & ModuleItemFlag_LayoutReady)
		return true;

	result = calcLayoutImpl(dynamicStruct, dynamicField);
	if (!result)
		return false;

	m_flags |= ModuleItemFlag_LayoutReady;
	return true;
}

bool
ArrayType::calcLayoutImpl(
	StructType* dynamicStruct,
	Field* dynamicField
	)
{
	bool result = m_elementType->ensureLayout();
	if (!result)
		return false;

	if (m_elementType->getTypeKind() == TypeKind_Class ||
		m_elementType->getFlags() & TypeFlag_Dynamic)
	{
		err::setFormatStringError("'%s' cannot be an element of an array", m_elementType->getTypeString().sz());
		return false;
	}

	// ensure update

	m_rootType = NULL;
	if (m_typeStringTuple)
	{
		AXL_MEM_DELETE(m_typeStringTuple);
		m_typeStringTuple = NULL;
	}

	uint_t rootTypeFlags = getRootType()->getFlags();
	if (rootTypeFlags & TypeFlag_Pod)
		m_flags |= TypeFlag_Pod;
	else if (rootTypeFlags & TypeFlag_GcRoot)
		m_flags |= TypeFlag_GcRoot;

	m_alignment = m_elementType->getAlignment();

	// calculate size

	if (!m_elementCountInitializer.isEmpty())
	{
		ASSERT(m_parentUnit && m_parentNamespace);

		Unit* prevUnit = m_module->m_unitMgr.setCurrentUnit(m_parentUnit);
		m_module->m_namespaceMgr.openNamespace(m_parentNamespace);

		int64_t value = 0;
		result = m_module->m_operatorMgr.parseConstIntegerExpression(m_elementCountInitializer, &value);

		m_module->m_namespaceMgr.closeNamespace();
		m_module->m_unitMgr.setCurrentUnit(prevUnit);

		if (!result)
		{
			if (!dynamicStruct)
				return false;

			sl::String qualifiedName = sl::formatString(
				"%s.%s.getDynamicSize",
				dynamicStruct->getQualifiedName().sz(),
				dynamicField->getName().sz()
				);

			Type* returnType = m_module->m_typeMgr.getPrimitiveType(TypeKind_SizeT);
			FunctionType* type = m_module->m_typeMgr.getFunctionType(returnType, NULL, 0);

			m_getDynamicSizeFunction = m_module->m_functionMgr.createInternalFunction<GetDynamicSizeFunction>(
				qualifiedName,
				type
				);

			m_getDynamicSizeFunction->m_arrayType = this;
			m_getDynamicSizeFunction->m_storageKind = StorageKind_Member;
			m_getDynamicSizeFunction->convertToMemberMethod(dynamicStruct);
			m_module->markForCompile(m_getDynamicSizeFunction);

			m_flags |= TypeFlag_Dynamic;
			m_elementCount = 0;
			m_size = 0;
			return true;
		}

		if (value <= 0)
		{
			err::setFormatStringError("invalid array size '%lld'\n", value);
			lex::pushSrcPosError(
				m_parentUnit->getFilePath(),
				m_elementCountInitializer.getHead()->m_pos
				);

			return false;
		}

#if (JNC_PTR_SIZE == 4)
		if (value >= (uint32_t) -1)
		{
			err::setFormatStringError("array size '%lld' is too big\n", value);
			lex::pushSrcPosError(
				m_parentUnit->getFilePath(),
				m_elementCountInitializer.getHead()->m_pos
				);

			return false;
		}
#endif

		m_elementCount = (size_t)value;
	}

	m_size = m_elementType->getSize() * m_elementCount;
	if (m_size > TypeSizeLimit_StackAllocSize)
		m_flags |= TypeFlag_NoStack;

	return true;
}

sl::String
ArrayType::getValueString(
	const void* p0,
	const char* formatSpec
	)
{
	if (m_flags & TypeFlag_Dynamic)
	{
		AXL_TODO("format dynamic arrays")
		return "{ dynamic-array }";
	}

	const char* p = (char*)p0;

	if (m_elementType->getTypeKind() == TypeKind_Char)
	{
		const char* null = (const char*)memchr(p, 0, m_elementCount);
		sl::String string(p, null ? null - p : m_elementCount);
		return formatSpec ? sl::formatString(formatSpec, string.sz()) : string;
	}

	if (!m_elementCount)
		return "{}";

	sl::String string = "{ " + m_elementType->getValueString(p);

	for (size_t i = 1; i < m_elementCount; i++)
	{
		p += m_elementType->getSize();

		string += ", ";
		string += m_elementType->getValueString(p, formatSpec);
	}

	string += " }";
	return string;
}

void
ArrayType::markGcRoots(
	const void* p,
	rt::GcHeap* gcHeap
	)
{
	ASSERT(m_flags & TypeFlag_GcRoot);
	gcHeap->addRootArray(p, m_elementType, m_elementCount);
}

void
ArrayType::prepareLlvmDiType()
{
	m_llvmDiType = m_module->m_llvmDiBuilder.createArrayType(this);
}

bool
ArrayType::compileGetDynamicSizeFunction(Function* function)
{
	ASSERT(function == m_getDynamicSizeFunction);

	bool result;

	Unit* parentUnit = function->getParentUnit();
	if (parentUnit)
		m_module->m_unitMgr.setCurrentUnit(parentUnit);

	m_module->m_namespaceMgr.openNamespace(function->getParentNamespace());
	m_module->m_functionMgr.internalPrologue(function);
	m_module->m_functionMgr.createThisValue();

	Value resultValue;
	result = m_module->m_operatorMgr.parseExpression(m_elementCountInitializer, &resultValue);
	if (!result)
		return false;

	size_t size = m_elementType->getSize();
	if (size != 1)
	{
		Value sizeValue(size, m_module->m_typeMgr.getPrimitiveType(TypeKind_SizeT));
		result = m_module->m_operatorMgr.binaryOperator(
			BinOpKind_Mul,
			&resultValue,
			sizeValue
			);

		if (!result)
			return false;
	}

	result = m_module->m_controlFlowMgr.ret(resultValue);
	if (!result)
		return false;

	m_module->m_functionMgr.internalEpilogue();
	m_module->m_namespaceMgr.closeNamespace();
	return true;
}

//..............................................................................

} // namespace ct
} // namespace jnc
<|endoftext|>


<commit_before>// calculate capacitance in unit area of an ideal 1D planar detector
using namespace GeFiCa;
void calculateC()
{
   // configure detector
   Planar1D *detector = new Planar1D;
   detector->Thickness=1*cm;
   detector->V0=0*volt;
   detector->V1=800*volt;

   // calculate capacitance
   double c = detector->GetC()/pF;
   cout<<"capacitance is "<<c<<" pF/cm2"<<endl;
}
<commit_msg>started to use GetBoundaryConditionFrom<commit_after>// calculate capacitance in unit area of an ideal planar detector
using namespace GeFiCa;
void calculateC()
{
   Planar detector;
   detector.Height=1*cm;
   detector.Bias[0]=0*volt;
   detector.Bias[1]=800*volt;
   detector.SetAverageImpurity(1e10/cm3);

   X grid;
   grid.GetBoundaryConditionFrom(detector);
   double c = grid.GetC()/pF;
   cout<<"capacitance is "<<c<<" pF/cm2"<<endl;
}
<|endoftext|>


<commit_before>/*
Resembla: Word-based Japanese similar sentence search library
https://github.com/tuem/resembla

Copyright 2017 Takashi Uemura

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 <iostream>
#include <fstream>
#include <string>
#include <unordered_map>
#include <set>
#include <stdexcept>

#include <paramset.hpp>
#include <resembla/resembla_util.hpp>

#include <resembla/simstring/simstring.h>
#include <json.hpp>

#include <resembla/measure/word_sequence_builder.hpp>
#include <resembla/measure/pronunciation_sequence_builder.hpp>
#include <resembla/measure/romaji_sequence_builder.hpp>
#include <resembla/measure/keyword_match_preprocessor.hpp>

using namespace resembla;

template<typename Preprocessor>
void create_index(const std::string corpus_path, const std::string db_path, const std::string inverse_path,
        int n, Preprocessor preprocess, size_t text_col, size_t features_col = 0)
{
    simstring::ngram_generator gen(n, false);
    simstring::writer_base<string_type> dbw(gen, db_path);
    std::unordered_map<string_type, std::set<string_type>> inserted;
    std::basic_ifstream<string_type::value_type> ifs(corpus_path);
    if(ifs.fail()){
        throw std::runtime_error("input file is not available: " + corpus_path);
    }
    while(ifs.good()){
        string_type line;
        std::getline(ifs, line);
        if(ifs.eof() || line.length() == 0){
            break;
        }

        auto columns = split(line, L'\t');
        if(text_col > columns.size()){
            continue;
        }

        auto original = columns[text_col - 1];
        auto s = preprocess.index(original);

        if(features_col > 0 && features_col - 1 < columns.size()){
            original += L"\t" + columns[features_col - 1];
        }

        if(inserted.count(s) == 0){
            dbw.insert(s);
            inserted[s] = {original};
        }
        else{
            inserted[s].insert(original);
        }
    }
    dbw.close();
    std::basic_ofstream<string_type::value_type> ofs;
    ofs.open(inverse_path);
    for(auto p: inserted){
        for(auto original: p.second){
            nlohmann::json preprocessed = preprocess(original);
            ofs << p.first << L'\t' << original L'\t' << preprocessed << std::endl;
        }
    }
}

int main(int argc, char* argv[])
{
    init_locale();

    paramset::definitions defs = {
        {"simstring_ngram_unit", 2, {"simstring", "ngram_unit"}, "simstring-ngram-unit", 'N', "Unit of N-gram for SimString"},
        {"simstring_text_preprocess", "asis", {"simstring", "text_preprocess"}, "simstring-text-preprocess", 'P', "preprocessing method for texts to create index"},
        {"ed_simstring_ngram_unit", -1, {"edit_distance", "simstring_ngram_unit"}, "ed-simstring-ngram-unit", 0, "Unit of N-gram for input text"},
        {"wwed_simstring_ngram_unit", -1, {"weighted_word_edit_distance", "simstring_ngram_unit"}, "wwed-simstring-ngram-unit", 0, "Unit of N-gram for input text"},
        {"wwed_mecab_options", "", {"weighted_word_edit_distance", "mecab_options"}, "wwed-mecab-options", 0, "MeCab options for weighted word edit distance"},
        {"wped_simstring_ngram_unit", -1, {"weighted_pronunciation_edit_distance", "simstring_ngram_unit"}, "wped-simstring-ngram-unit", 0, "Unit of N-gram for pronunciation of input text"},
        {"wped_mecab_options", "", {"weighted_pronunciation_edit_distance", "mecab_options"}, "wped-mecab-options", 0, "MeCab options for weighted pronunciation edit distance"},
        {"wped_mecab_feature_pos", 7, {"weighted_pronunciation_edit_distance", "mecab_feature_pos"}, "wped-mecab-feature-pos", 0, "Position of pronunciation in feature for weighted pronunciation edit distance"},
        {"wped_mecab_pronunciation_of_marks", "", {"weighted_pronunciation_edit_distance", "mecab_pronunciation_of_marks"}, "wped-mecab-pronunciation-of-marks", 0, "pronunciation in MeCab features when input is a mark"},
        {"wred_simstring_ngram_unit", -1, {"weighted_romaji_edit_distance", "simstring_ngram_unit"}, "wred-simstring-ngram-unit", 0, "Unit of N-gram for romaji notation of input text"},
        {"wred_mecab_options", "", {"weighted_romaji_edit_distance", "mecab_options"}, "wred-mecab-options", 0, "MeCab options for weighted romaji edit distance"},
        {"wred_mecab_feature_pos", 7, {"weighted_romaji_edit_distance", "mecab_feature_pos"}, "wred-mecab-feature-pos", 0, "Position of pronunciation in feature for weighted romaji edit distance"},
        {"wred_mecab_pronunciation_of_marks", "", {"weighted_romaji_edit_distance", "mecab_pronunciation_of_marks"}, "wred-mecab-pronunciation-of-marks", 0, "pronunciation in MeCab features when input is a mark"},
        {"corpus_path", "", {"common", "corpus_path"}},
        {"text_col", 1, {"common", "text_col"}, "text-col", 0, "column mumber of text in corpus rows"},
        {"features_col", 0, {"common", "features_col"}, "features-col", 0, "column number of features in corpus rows"},
        {"verbose", false, {"common", "verbose"}, 'v', "show more information"},
        {"conf_path", "", "config", 'c', "config file path"}
    };
    paramset::manager pm(defs);

    try{
        pm.load(argc, argv, "config");

        std::string corpus_path = read_value_with_rest(pm, "corpus_path", ""); // must not be empty
        int default_simstring_ngram_unit = pm["simstring_ngram_unit"];
        int wwed_simstring_ngram_unit = pm.get<int>("wwed_simstring_ngram_unit") != -1 ?
            pm.get<int>("wwed_simstring_ngram_unit") : default_simstring_ngram_unit;
        int wped_simstring_ngram_unit = pm.get<int>("wped_simstring_ngram_unit") != -1 ?
            pm.get<int>("wped_simstring_ngram_unit") : default_simstring_ngram_unit;
        int wred_simstring_ngram_unit = pm.get<int>("wred_simstring_ngram_unit") != -1 ?
            pm.get<int>("wred_simstring_ngram_unit") : default_simstring_ngram_unit;
        auto simstring_text_preprocesses = split_to_text_preprocesses(pm.get<std::string>("simstring_text_preprocess"));

        if(pm.get<bool>("verbose")){
            std::cerr << "Configurations:" << std::endl;
            std::cerr << "  Common:" << std::endl;
            std::cerr << "    corpus_path=" << corpus_path << std::endl;
            std::cerr << "    text_col=" << pm.get<int>("text_col") << std::endl;
            std::cerr << "    features_col=" << pm.get<int>("features_col") << std::endl;
            std::cerr << "  SimString:" << std::endl;
            std::cerr << "    ngram_unit=" << default_simstring_ngram_unit << std::endl;
            for(auto simstring_text_preprocess: simstring_text_preprocesses){
                if(simstring_text_preprocess == asis){
                    std::cerr << "  text_preprocess=" << STR(asis) << std::endl;
                    std::cerr << "    simstring_ngram_unit=" << wwed_simstring_ngram_unit << std::endl;
                    std::cerr << "    mecab_options=" << pm.get<std::string>("wwed_mecab_options") << std::endl;
                }
                else if(simstring_text_preprocess == pronunciation){
                    std::cerr << "  text_preprocess=" << STR(pronunciation) << std::endl;
                    std::cerr << "    simstring_ngram_unit=" << wped_simstring_ngram_unit << std::endl;
                    std::cerr << "    mecab_options=" << pm.get<std::string>("wped_mecab_options") << std::endl;
                    std::cerr << "    mecab_feature_pos=" << pm.get<int>("wped_mecab_feature_pos") << std::endl;
                    std::cerr << "    mecab_pronunciation_of_marks=" << pm.get<std::string>("wped_mecab_pronunciation_of_marks") << std::endl;
                }
                else if(simstring_text_preprocess == romaji){
                    std::cerr << "  text_preprocess=" << STR(romaji) << std::endl;
                    std::cerr << "    simstring_ngram_unit=" << wred_simstring_ngram_unit << std::endl;
                    std::cerr << "    mecab_options=" << pm.get<std::string>("wred_mecab_options") << std::endl;
                    std::cerr << "    mecab_feature_pos=" << pm.get<int>("wred_mecab_feature_pos") << std::endl;
                    std::cerr << "    mecab_pronunciation_of_marks=" << pm.get<std::string>("wred_mecab_pronunciation_of_marks") << std::endl;
                }
            }
        }

        for(auto simstring_text_preprocess: simstring_text_preprocesses){
            std::string db_path = db_path_from_simstring_text_preprocess(corpus_path, simstring_text_preprocess);
            std::string inverse_path = inverse_path_from_simstring_text_preprocess(corpus_path, simstring_text_preprocess);

            if(simstring_text_preprocess == asis){
                WordSequenceBuilder builder(pm.get<std::string>("wwed_mecab_options"));
                create_index(corpus_path, db_path, inverse_path, wwed_simstring_ngram_unit, builder,
                        pm.get<int>("text_col"), pm.get<int>("features_col"));
            }
            else if(simstring_text_preprocess == pronunciation){
                PronunciationSequenceBuilder builder(pm.get<std::string>("wped_mecab_options"),
                        pm.get<int>("wped_mecab_feature_pos"), pm.get<std::string>("wped_mecab_pronunciation_of_marks"));
                create_index(corpus_path, db_path, inverse_path, wped_simstring_ngram_unit,
                        builder, pm.get<int>("text_col"), pm.get<int>("features_col"));
            }
            else if(simstring_text_preprocess == romaji){
                RomajiSequenceBuilder builder(pm.get<std::string>("wred_mecab_options"),
                        pm.get<int>("wred_mecab_feature_pos"), pm.get<std::string>("wred_mecab_pronunciation_of_marks"));
                create_index(corpus_path, db_path, inverse_path, wred_simstring_ngram_unit,
                        builder, pm.get<int>("text_col"), pm.get<int>("features_col"));
            }
            else if(simstring_text_preprocess == keyword){
                create_index(corpus_path, db_path, inverse_path, wred_simstring_ngram_unit,
                        KeywordMatchPreprocessor<string_type>(), pm.get<int>("text_col"), pm.get<int>("features_col"));
            }

            std::cerr << "database saved to " << db_path << std::endl;
        }
    }
    catch(const std::exception& e){
        std::cerr << "error: " << e.what() << std::endl;
        exit(1);
    }

    return 0;
}
<commit_msg>store preprocessed data in wwed, wped and wred<commit_after>/*
Resembla: Word-based Japanese similar sentence search library
https://github.com/tuem/resembla

Copyright 2017 Takashi Uemura

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 <iostream>
#include <fstream>
#include <string>
#include <unordered_map>
#include <set>
#include <stdexcept>

#include <paramset.hpp>
#include <resembla/resembla_util.hpp>

#include <resembla/simstring/simstring.h>
#include <json.hpp>

#include <resembla/measure/word_sequence_builder.hpp>
#include <resembla/measure/pronunciation_sequence_builder.hpp>
#include <resembla/measure/romaji_sequence_builder.hpp>
#include <resembla/measure/keyword_match_preprocessor.hpp>

#include <resembla/measure/weighted_sequence_builder.hpp>

#include <resembla/measure/feature_match_weight.hpp>
#include <resembla/measure/surface_match_cost.hpp>

#include <resembla/measure/romaji_match_weight.hpp>
#include <resembla/measure/romaji_match_cost.hpp>

#include <resembla/measure/weighted_sequence_serializer.hpp>

using namespace resembla;

template<typename Preprocessor>
void create_index(const std::string corpus_path, const std::string db_path, const std::string inverse_path,
        int n, Preprocessor preprocess, size_t text_col, size_t features_col = 0)
{
    simstring::ngram_generator gen(n, false);
    simstring::writer_base<string_type> dbw(gen, db_path);
    std::unordered_map<string_type, std::set<string_type>> inserted;
    std::basic_ifstream<string_type::value_type> ifs(corpus_path);
    if(ifs.fail()){
        throw std::runtime_error("input file is not available: " + corpus_path);
    }
    while(ifs.good()){
        string_type line;
        std::getline(ifs, line);
        if(ifs.eof() || line.length() == 0){
            break;
        }

        auto columns = split(line, L'\t');
        if(text_col > columns.size()){
            continue;
        }

        auto original = columns[text_col - 1];
        auto s = preprocess.index(original);

        if(features_col > 0 && features_col - 1 < columns.size()){
            original += L"\t" + columns[features_col - 1];
        }

        if(inserted.count(s) == 0){
            dbw.insert(s);
            inserted[s] = {original};
        }
        else{
            inserted[s].insert(original);
        }
    }
    dbw.close();
    std::basic_ofstream<string_type::value_type> ofs;
    ofs.open(inverse_path);
    for(auto p: inserted){
        for(auto original: p.second){
            nlohmann::json j = preprocess(original);
            auto preprocessed = cast_string<string_type>(j.dump());
            ofs << p.first << L'\t' << original << L'\t' << preprocessed << std::endl;
        }
    }
}

int main(int argc, char* argv[])
{
    init_locale();

    paramset::definitions defs = {
        {"simstring_ngram_unit", 2, {"simstring", "ngram_unit"}, "simstring-ngram-unit", 'N', "Unit of N-gram for SimString"},
        {"simstring_text_preprocess", "asis", {"simstring", "text_preprocess"}, "simstring-text-preprocess", 'P', "preprocessing method for texts to create index"},
        {"ed_simstring_ngram_unit", -1, {"edit_distance", "simstring_ngram_unit"}, "ed-simstring-ngram-unit", 0, "Unit of N-gram for input text"},
        {"wwed_simstring_ngram_unit", -1, {"weighted_word_edit_distance", "simstring_ngram_unit"}, "wwed-simstring-ngram-unit", 0, "Unit of N-gram for input text"},
        {"wwed_mecab_options", "", {"weighted_word_edit_distance", "mecab_options"}, "wwed-mecab-options", 0, "MeCab options for weighted word edit distance"},
        {"wwed_base_weight", 1L, {"weighted_word_edit_distance", "base_weight"}, "wwed-base-weight", 0, "base weight for weighted word edit distance"},
        {"wwed_delete_insert_ratio", 10L, {"weighted_word_edit_distance", "delete_insert_ratio"}, "wwed-del-ins-ratio", 0, "cost ratio of deletion and insertion for weighted word edit distance"},
        {"wwed_noun_coefficient", 10L, {"weighted_word_edit_distance", "noun_coefficient"}, "wwed-noun-coefficient", 0, "coefficient of nouns for weighted word edit distance"},
        {"wwed_verb_coefficient", 10L, {"weighted_word_edit_distance", "verb_coefficient"}, "wwed-verb-coefficient", 0, "coefficient of verbs for weighted word edit distance"},
        {"wwed_adj_coefficient", 5L, {"weighted_word_edit_distance", "adj_coefficient"}, "wwed-adj-coefficient", 0, "coefficient of adjectives for weighted word edit distance"},
        {"wped_simstring_ngram_unit", -1, {"weighted_pronunciation_edit_distance", "simstring_ngram_unit"}, "wped-simstring-ngram-unit", 0, "Unit of N-gram for pronunciation of input text"},
        {"wped_mecab_options", "", {"weighted_pronunciation_edit_distance", "mecab_options"}, "wped-mecab-options", 0, "MeCab options for weighted pronunciation edit distance"},
        {"wped_mecab_feature_pos", 7, {"weighted_pronunciation_edit_distance", "mecab_feature_pos"}, "wped-mecab-feature-pos", 0, "Position of pronunciation in feature for weighted pronunciation edit distance"},
        {"wped_mecab_pronunciation_of_marks", "", {"weighted_pronunciation_edit_distance", "mecab_pronunciation_of_marks"}, "wped-mecab-pronunciation-of-marks", 0, "pronunciation in MeCab features when input is a mark"},
        {"wped_base_weight", 1L, {"weighted_pronunciation_edit_distance", "base_weight"}, "wped-base-weight", 0, "base weight for weighted pronunciation edit distance"},
        {"wped_delete_insert_ratio", 10L, {"weighted_pronunciation_edit_distance", "delete_insert_ratio"}, "wped-del-ins-ratio", 0, "cost ratio of deletion and insertion for weighted pronunciation edit distance"},
        {"wred_simstring_ngram_unit", -1, {"weighted_romaji_edit_distance", "simstring_ngram_unit"}, "wred-simstring-ngram-unit", 0, "Unit of N-gram for romaji notation of input text"},
        {"wred_mecab_options", "", {"weighted_romaji_edit_distance", "mecab_options"}, "wred-mecab-options", 0, "MeCab options for weighted romaji edit distance"},
        {"wred_mecab_feature_pos", 7, {"weighted_romaji_edit_distance", "mecab_feature_pos"}, "wred-mecab-feature-pos", 0, "Position of pronunciation in feature for weighted romaji edit distance"},
        {"wred_mecab_pronunciation_of_marks", "", {"weighted_romaji_edit_distance", "mecab_pronunciation_of_marks"}, "wred-mecab-pronunciation-of-marks", 0, "pronunciation in MeCab features when input is a mark"},
        {"wred_base_weight", 1L, {"weighted_romaji_edit_distance", "base_weight"}, "wred-base-weight", 0, "base weight for weighted romaji edit distance"},
        {"wred_delete_insert_ratio", 10L, {"weighted_romaji_edit_distance", "delete_insert_ratio"}, "wred-del-ins-ratio", 0, "cost ratio of deletion and insertion for weighted romaji edit distance"},
        {"wred_uppercase_coefficient", 1L, {"weighted_romaji_edit_distance", "uppercase_coefficient"}, "wred-uppercase-coefficient", 0, "coefficient for uppercase letters for weighted romaji edit distance"},
        {"wred_lowercase_coefficient", 1L, {"weighted_romaji_edit_distance", "lowercase_coefficient"}, "wred-lowercase-coefficient", 0, "coefficient for lowercase letters for weighted romaji edit distance"},
        {"wred_vowel_coefficient", 1L, {"weighted_romaji_edit_distance", "vowel_coefficient"}, "wred-vowel-coefficient", 0, "coefficient for vowels for weighted romaji edit distance"},
        {"wred_consonant_coefficient", 1L, {"weighted_romaji_edit_distance", "consonant_coefficient"}, "wred-consonant-coefficient", 0, "coefficient for consonants for weighted romaji edit distance"},
        {"wred_case_mismatch_cost", 1L, {"weighted_romaji_edit_distance", "case_mismatch_cost"}, "wred-case-mismatch-cost", 0, "cost to replace case mismatches for weighted romaji edit distance"},
        {"wred_similar_letter_cost", 1L, {"weighted_romaji_edit_distance", "similar_letter_cost"}, "wred-similar-letter-cost", 0, "cost to replace similar letters for weighted romaji edit distance"},
        {"wred_mismatch_cost_path", "", {"weighted_romaji_edit_distance", "mismatch_cost_path"}, "wred-mismatch-cost-path", 0, "costs to replace similar letters for weighted romaji edit distance"},
        {"corpus_path", "", {"common", "corpus_path"}},
        {"text_col", 1, {"common", "text_col"}, "text-col", 0, "column mumber of text in corpus rows"},
        {"features_col", 0, {"common", "features_col"}, "features-col", 0, "column number of features in corpus rows"},
        {"verbose", false, {"common", "verbose"}, 'v', "show more information"},
        {"conf_path", "", "config", 'c', "config file path"}
    };
    paramset::manager pm(defs);

    try{
        pm.load(argc, argv, "config");

        std::string corpus_path = read_value_with_rest(pm, "corpus_path", ""); // must not be empty
        int default_simstring_ngram_unit = pm["simstring_ngram_unit"];
        int wwed_simstring_ngram_unit = pm.get<int>("wwed_simstring_ngram_unit") != -1 ?
            pm.get<int>("wwed_simstring_ngram_unit") : default_simstring_ngram_unit;
        int wped_simstring_ngram_unit = pm.get<int>("wped_simstring_ngram_unit") != -1 ?
            pm.get<int>("wped_simstring_ngram_unit") : default_simstring_ngram_unit;
        int wred_simstring_ngram_unit = pm.get<int>("wred_simstring_ngram_unit") != -1 ?
            pm.get<int>("wred_simstring_ngram_unit") : default_simstring_ngram_unit;
        auto simstring_text_preprocesses = split_to_text_preprocesses(pm.get<std::string>("simstring_text_preprocess"));

        if(pm.get<bool>("verbose")){
            std::cerr << "Configurations:" << std::endl;
            std::cerr << "  Common:" << std::endl;
            std::cerr << "    corpus_path=" << corpus_path << std::endl;
            std::cerr << "    text_col=" << pm.get<int>("text_col") << std::endl;
            std::cerr << "    features_col=" << pm.get<int>("features_col") << std::endl;
            std::cerr << "  SimString:" << std::endl;
            std::cerr << "    ngram_unit=" << default_simstring_ngram_unit << std::endl;
            for(auto simstring_text_preprocess: simstring_text_preprocesses){
                if(simstring_text_preprocess == asis){
                    std::cerr << "  text_preprocess=" << STR(asis) << std::endl;
                    std::cerr << "    simstring_ngram_unit=" << wwed_simstring_ngram_unit << std::endl;
                    std::cerr << "    mecab_options=" << pm.get<std::string>("wwed_mecab_options") << std::endl;
                }
                else if(simstring_text_preprocess == pronunciation){
                    std::cerr << "  text_preprocess=" << STR(pronunciation) << std::endl;
                    std::cerr << "    simstring_ngram_unit=" << wped_simstring_ngram_unit << std::endl;
                    std::cerr << "    mecab_options=" << pm.get<std::string>("wped_mecab_options") << std::endl;
                    std::cerr << "    mecab_feature_pos=" << pm.get<int>("wped_mecab_feature_pos") << std::endl;
                    std::cerr << "    mecab_pronunciation_of_marks=" << pm.get<std::string>("wped_mecab_pronunciation_of_marks") << std::endl;
                }
                else if(simstring_text_preprocess == romaji){
                    std::cerr << "  text_preprocess=" << STR(romaji) << std::endl;
                    std::cerr << "    simstring_ngram_unit=" << wred_simstring_ngram_unit << std::endl;
                    std::cerr << "    mecab_options=" << pm.get<std::string>("wred_mecab_options") << std::endl;
                    std::cerr << "    mecab_feature_pos=" << pm.get<int>("wred_mecab_feature_pos") << std::endl;
                    std::cerr << "    mecab_pronunciation_of_marks=" << pm.get<std::string>("wred_mecab_pronunciation_of_marks") << std::endl;
                }
            }
        }

        for(auto simstring_text_preprocess: simstring_text_preprocesses){
            std::string db_path = db_path_from_simstring_text_preprocess(corpus_path, simstring_text_preprocess);
            std::string inverse_path = inverse_path_from_simstring_text_preprocess(corpus_path, simstring_text_preprocess);

            if(simstring_text_preprocess == asis){
                //WordSequenceBuilder builder(pm.get<std::string>("wwed_mecab_options"));
                WeightedSequenceBuilder<WordSequenceBuilder, FeatureMatchWeight> builder(
                    WordSequenceBuilder(pm.get<std::string>("wwed_mecab_options")),
                    FeatureMatchWeight(pm.get<double>("wwed_base_weight"),
                        pm.get<double>("wwed_delete_insert_ratio"), pm.get<double>("wwed_noun_coefficient"),
                        pm.get<double>("wwed_verb_coefficient"), pm.get<double>("wwed_adj_coefficient")));
                create_index(corpus_path, db_path, inverse_path, wwed_simstring_ngram_unit, builder,
                        pm.get<int>("text_col"), pm.get<int>("features_col"));
            }
            else if(simstring_text_preprocess == pronunciation){
                PronunciationSequenceBuilder builder(pm.get<std::string>("wped_mecab_options"),
                        pm.get<int>("wped_mecab_feature_pos"), pm.get<std::string>("wped_mecab_pronunciation_of_marks"));
                create_index(corpus_path, db_path, inverse_path, wped_simstring_ngram_unit,
                        builder, pm.get<int>("text_col"), pm.get<int>("features_col"));
            }
            else if(simstring_text_preprocess == romaji){
                //RomajiSequenceBuilder builder(pm.get<std::string>("wred_mecab_options"),
                //        pm.get<int>("wred_mecab_feature_pos"), pm.get<std::string>("wred_mecab_pronunciation_of_marks"));
                WeightedSequenceBuilder<RomajiSequenceBuilder, RomajiMatchWeight> builder(
                    RomajiSequenceBuilder(pm.get<std::string>("wred_mecab_options"),
                        pm.get<int>("wred_mecab_feature_pos"), pm.get<std::string>("wred_mecab_pronunciation_of_marks")),
                    RomajiMatchWeight(pm.get<double>("wred_base_weight"), pm.get<double>("wred_delete_insert_ratio"),
                        pm.get<double>("wred_uppercase_coefficient"), pm.get<double>("wred_lowercase_coefficient"),
                        pm.get<double>("wred_vowel_coefficient"), pm.get<double>("wred_consonant_coefficient")));
                create_index(corpus_path, db_path, inverse_path, wred_simstring_ngram_unit,
                        builder, pm.get<int>("text_col"), pm.get<int>("features_col"));
            }
            else if(simstring_text_preprocess == keyword){
                create_index(corpus_path, db_path, inverse_path, wred_simstring_ngram_unit,
                        KeywordMatchPreprocessor<string_type>(), pm.get<int>("text_col"), pm.get<int>("features_col"));
            }

            std::cerr << "database saved to " << db_path << std::endl;
        }
    }
    catch(const std::exception& e){
        std::cerr << "error: " << e.what() << std::endl;
        exit(1);
    }

    return 0;
}
<|endoftext|>


<commit_before>// Author: Enrico Guiraud, Danilo Piparo, Massimo Tumolo CERN  06/2018

/*************************************************************************
 * Copyright (C) 1995-2018, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

#ifndef ROOT_RDFBOOKEDCUSTOMCOLUMNS
#define ROOT_RDFBOOKEDCUSTOMCOLUMNS

#include <memory>
#include <map>
#include <vector>
#include <string>
#include <algorithm>
#include "TString.h"

namespace ROOT {
namespace Detail {
namespace RDF {
class RDefineBase;
}
}

namespace Internal {
namespace RDF {

namespace RDFDetail = ROOT::Detail::RDF;

/**
 * \class ROOT::Internal::RDF::RBookedDefines
 * \ingroup dataframe
 * \brief Encapsulates the columns defined by the user
 */

class RBookedDefines {
   using RDefineBasePtrMap_t = std::map<std::string, std::shared_ptr<RDFDetail::RDefineBase>>;
   using ColumnNames_t = std::vector<std::string>;

   // Since RBookedDefines is meant to be an immutable, copy-on-write object, the actual values are set as const
   using RDefineBasePtrMapPtr_t = std::shared_ptr<const RDefineBasePtrMap_t>;
   using ColumnNamesPtr_t = std::shared_ptr<const ColumnNames_t>;

private:
   RDefineBasePtrMapPtr_t fDefines;
   ColumnNamesPtr_t fDefinesNames;

public:
   ////////////////////////////////////////////////////////////////////////////
   /// \brief Copy-ctor for RBookedDefines.
   RBookedDefines(const RBookedDefines &) = default;

   ////////////////////////////////////////////////////////////////////////////
   /// \brief Move-ctor for RBookedDefines.
   RBookedDefines(RBookedDefines &&) = default;

   ////////////////////////////////////////////////////////////////////////////
   /// \brief Copy-assignment operator for RBookedDefines.
   RBookedDefines &operator=(const RBookedDefines &) = default;

   ////////////////////////////////////////////////////////////////////////////
   /// \brief Creates the object starting from the provided maps
   RBookedDefines(RDefineBasePtrMapPtr_t defines, ColumnNamesPtr_t defineNames)
      : fDefines(defines), fDefinesNames(defineNames)
   {
   }

   ////////////////////////////////////////////////////////////////////////////
   /// \brief Creates a new wrapper with empty maps
   RBookedDefines()
      : fDefines(std::make_shared<RDefineBasePtrMap_t>()),
        fDefinesNames(std::make_shared<ColumnNames_t>())
   {
   }

   ////////////////////////////////////////////////////////////////////////////
   /// \brief Returns the list of the names of the defined columns
   ColumnNames_t GetNames() const { return *fDefinesNames; }

   ////////////////////////////////////////////////////////////////////////////
   /// \brief Returns the list of the pointers to the defined columns
   const RDefineBasePtrMap_t &GetColumns() const { return *fDefines; }

   ////////////////////////////////////////////////////////////////////////////
   /// \brief Check if the provided name is tracked in the names list
   bool HasName(std::string_view name) const;

   ////////////////////////////////////////////////////////////////////////////
   /// \brief Internally it recreates the map with the new column, and swaps with the old one.
   void AddColumn(const std::shared_ptr<RDFDetail::RDefineBase> &column, std::string_view name);

   ////////////////////////////////////////////////////////////////////////////
   /// \brief Internally it recreates the map with the new column name, and swaps with the old one.
   void AddName(std::string_view name);
};

} // Namespace RDF
} // Namespace Internal
} // Namespace ROOT

#endif
<commit_msg>[DF][NFC] Improve comments in RBookedDefines<commit_after>// Author: Enrico Guiraud, Danilo Piparo, Massimo Tumolo CERN  06/2018

/*************************************************************************
 * Copyright (C) 1995-2018, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

#ifndef ROOT_RDFBOOKEDCUSTOMCOLUMNS
#define ROOT_RDFBOOKEDCUSTOMCOLUMNS

#include <memory>
#include <map>
#include <vector>
#include <string>
#include <algorithm>
#include "TString.h"

namespace ROOT {
namespace Detail {
namespace RDF {
class RDefineBase;
}
}

namespace Internal {
namespace RDF {

namespace RDFDetail = ROOT::Detail::RDF;

/**
 * \class ROOT::Internal::RDF::RBookedDefines
 * \ingroup dataframe
 * \brief Encapsulates the columns defined by the user
 */

class RBookedDefines {
   using RDefineBasePtrMap_t = std::map<std::string, std::shared_ptr<RDFDetail::RDefineBase>>;
   using ColumnNames_t = std::vector<std::string>;

   // Since RBookedDefines is meant to be an immutable, copy-on-write object, the actual values are set as const
   using RDefineBasePtrMapPtr_t = std::shared_ptr<const RDefineBasePtrMap_t>;
   using ColumnNamesPtr_t = std::shared_ptr<const ColumnNames_t>;

private:
   RDefineBasePtrMapPtr_t fDefines;
   ColumnNamesPtr_t fDefinesNames;  // also abused to keep track of aliases for each branch of the computation graph

public:
   ////////////////////////////////////////////////////////////////////////////
   /// \brief Copy-ctor for RBookedDefines.
   RBookedDefines(const RBookedDefines &) = default;

   ////////////////////////////////////////////////////////////////////////////
   /// \brief Move-ctor for RBookedDefines.
   RBookedDefines(RBookedDefines &&) = default;

   ////////////////////////////////////////////////////////////////////////////
   /// \brief Copy-assignment operator for RBookedDefines.
   RBookedDefines &operator=(const RBookedDefines &) = default;

   ////////////////////////////////////////////////////////////////////////////
   /// \brief Creates the object starting from the provided maps
   RBookedDefines(RDefineBasePtrMapPtr_t defines, ColumnNamesPtr_t defineNames)
      : fDefines(defines), fDefinesNames(defineNames)
   {
   }

   ////////////////////////////////////////////////////////////////////////////
   /// \brief Creates a new wrapper with empty maps
   RBookedDefines()
      : fDefines(std::make_shared<RDefineBasePtrMap_t>()),
        fDefinesNames(std::make_shared<ColumnNames_t>())
   {
   }

   ////////////////////////////////////////////////////////////////////////////
   /// \brief Returns the list of the names of the defined columns
   ColumnNames_t GetNames() const { return *fDefinesNames; }

   ////////////////////////////////////////////////////////////////////////////
   /// \brief Returns the list of the pointers to the defined columns
   const RDefineBasePtrMap_t &GetColumns() const { return *fDefines; }

   ////////////////////////////////////////////////////////////////////////////
   /// \brief Check if the provided name is tracked in the names list
   bool HasName(std::string_view name) const;

   ////////////////////////////////////////////////////////////////////////////
   /// \brief Add a new booked column.
   /// Internally it recreates the map with the new column, and swaps it with the old one.
   void AddColumn(const std::shared_ptr<RDFDetail::RDefineBase> &column, std::string_view name);

   ////////////////////////////////////////////////////////////////////////////
   /// \brief Add a new name to the list returned by `GetNames` without booking a new column.
   ///
   /// This is needed because we abuse fDefinesNames to also keep track of the aliases defined
   /// in each branch of the computation graph.
   /// Internally it recreates the vector with the new name, and swaps it with the old one.
   void AddName(std::string_view name);
};

} // Namespace RDF
} // Namespace Internal
} // Namespace ROOT

#endif
<|endoftext|>