Program Listing for File xml_testdata_loader.h

Return to documentation for file (include/pilz_industrial_motion_planner_testutils/xml_testdata_loader.h)

/*********************************************************************
 * Software License Agreement (BSD License)
 *
 *  Copyright (c) 2018 Pilz GmbH & Co. KG
 *  All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions
 *  are met:
 *
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above
 *     copyright notice, this list of conditions and the following
 *     disclaimer in the documentation and/or other materials provided
 *     with the distribution.
 *   * Neither the name of Pilz GmbH & Co. KG nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 *  POSSIBILITY OF SUCH DAMAGE.
 *********************************************************************/

#pragma once

#include <string>
#include <vector>
#include <functional>
#include <map>
#include <memory>

#include <boost/property_tree/ptree.hpp>

#include <pilz_industrial_motion_planner_testutils/testdata_loader.h>

namespace pt = boost::property_tree;
namespace pilz_industrial_motion_planner_testutils
{
class XmlTestdataLoader : public TestdataLoader
{
public:
  XmlTestdataLoader(const std::string& path_filename);
  XmlTestdataLoader(const std::string& path_filename, const moveit::core::RobotModelConstPtr& robot_model);
  ~XmlTestdataLoader() override;

public:
  JointConfiguration getJoints(const std::string& pos_name, const std::string& group_name) const override;

  CartesianConfiguration getPose(const std::string& pos_name, const std::string& group_name) const override;

  PtpJoint getPtpJoint(const std::string& cmd_name) const override;
  PtpCart getPtpCart(const std::string& cmd_name) const override;
  PtpJointCart getPtpJointCart(const std::string& cmd_name) const override;

  LinJoint getLinJoint(const std::string& cmd_name) const override;
  LinCart getLinCart(const std::string& cmd_name) const override;
  LinJointCart getLinJointCart(const std::string& cmd_name) const override;

  CircCenterCart getCircCartCenterCart(const std::string& cmd_name) const override;
  CircInterimCart getCircCartInterimCart(const std::string& cmd_name) const override;
  CircJointCenterCart getCircJointCenterCart(const std::string& cmd_name) const override;
  CircJointInterimCart getCircJointInterimCart(const std::string& cmd_name) const override;

  Sequence getSequence(const std::string& cmd_name) const override;

  Gripper getGripper(const std::string& cmd_name) const override;

private:
  const pt::ptree::value_type& findNodeWithName(const boost::property_tree::ptree& tree, const std::string& name,
                                                const std::string& key, const std::string& path = "") const;

  const pt::ptree::value_type& findCmd(const std::string& cmd_name, const std::string& cmd_path,
                                       const std::string& cmd_key) const;

  CartesianCenter getCartesianCenter(const std::string& cmd_name, const std::string& planning_group) const;

  CartesianInterim getCartesianInterim(const std::string& cmd_name, const std::string& planning_group) const;

private:
  JointConfiguration getJoints(const boost::property_tree::ptree& joints_tree, const std::string& group_name) const;

private:
  static inline std::vector<double> strVec2doubleVec(std::vector<std::string>& strVec);

public:
  class AbstractCmdGetterAdapter
  {
  public:
    AbstractCmdGetterAdapter() = default;
    AbstractCmdGetterAdapter(const AbstractCmdGetterAdapter&) = default;
    AbstractCmdGetterAdapter(AbstractCmdGetterAdapter&&) = default;
    AbstractCmdGetterAdapter& operator=(const AbstractCmdGetterAdapter&) = default;
    AbstractCmdGetterAdapter& operator=(AbstractCmdGetterAdapter&&) = default;
    virtual ~AbstractCmdGetterAdapter() = default;

    virtual CmdVariant getCmd(const std::string& /*cmd_name*/) const = 0;
  };

private:
  std::string path_filename_;
  pt::ptree tree_{};

  using AbstractCmdGetterUPtr = std::unique_ptr<AbstractCmdGetterAdapter>;

  std::map<std::string, AbstractCmdGetterUPtr> cmd_getter_funcs_;

private:
  const pt::ptree::value_type empty_value_type_{};
  const pt::ptree empty_tree_{};
};

std::vector<double> XmlTestdataLoader::strVec2doubleVec(std::vector<std::string>& strVec)
{
  std::vector<double> vec;

  vec.resize(strVec.size());
  std::transform(strVec.begin(), strVec.end(), vec.begin(), [](const std::string& val) { return std::stod(val); });

  return vec;
}

using XmlTestDataLoaderUPtr = std::unique_ptr<TestdataLoader>;
}  // namespace pilz_industrial_motion_planner_testutils