RcsPySim
A robot control and simulation library
ControlPolicy.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2  Copyright (c) 2020, Fabio Muratore, Honda Research Institute Europe GmbH, and
3  Technical University of Darmstadt.
4  All rights reserved.
5 
6  Redistribution and use in source and binary forms, with or without
7  modification, are permitted provided that the following conditions are met:
8  1. Redistributions of source code must retain the above copyright
9  notice, this list of conditions and the following disclaimer.
10  2. Redistributions in binary form must reproduce the above copyright
11  notice, this list of conditions and the following disclaimer in the
12  documentation and/or other materials provided with the distribution.
13  3. Neither the name of Fabio Muratore, Honda Research Institute Europe GmbH,
14  or Technical University of Darmstadt, nor the names of its contributors may
15  be used to endorse or promote products derived from this software without
16  specific prior written permission.
17 
18  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  DISCLAIMED. IN NO EVENT SHALL FABIO MURATORE, HONDA RESEARCH INSTITUTE EUROPE GMBH,
22  OR TECHNICAL UNIVERSITY OF DARMSTADT BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
26  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  POSSIBILITY OF SUCH DAMAGE.
29 *******************************************************************************/
30 
31 #include "ControlPolicy.h"
32 #include "../config/PropertySource.h"
33 
34 #include <Rcs_macros.h>
35 #include <Rcs_resourcePath.h>
36 
37 #include <map>
38 #include <sstream>
39 
40 namespace Rcs
41 {
42 
43 // The policy type registry
44 static std::map<std::string, ControlPolicy::ControlPolicyCreateFunction> registry;
45 
47 {
48  // Store in registry
49  registry[name] = creator;
50 }
51 
52 ControlPolicy* ControlPolicy::create(const char* name, const char* dataFile)
53 {
54  // Lookup factory for type
55  auto iter = registry.find(name);
56  if (iter == registry.end()) {
57  std::ostringstream os;
58  os << "Unknown control policy type '" << name << "'!";
59  throw std::invalid_argument(os.str());
60  }
61 
62  // Find data file
63  char filepath[256];
64  bool found = Rcs_getAbsoluteFileName(dataFile, filepath);
65  if (!found) {
66  // file does not exist
67  Rcs_printResourcePath();
68  std::ostringstream os;
69  os << "Policy file '" << dataFile << "' does not exist!";
70  throw std::invalid_argument(os.str());
71  }
72 
73  // Create instance
74  return iter->second(filepath);
75 }
76 
78 {
79  std::string policyType;
80  std::string policyFile;
81  RCHECK(config->getProperty(policyType, "type"));
82  RCHECK(config->getProperty(policyFile, "file"));
83  return Rcs::ControlPolicy::create(policyType.c_str(), policyFile.c_str());
84 }
85 
86 std::vector<std::string> ControlPolicy::getTypeNames()
87 {
88  std::vector<std::string> names;
89  for (auto& elem : registry) {
90  names.push_back(elem.first);
91  }
92  return names;
93 }
94 
96 {
97  // Does nothing
98 }
99 
101 {
102  // Does nothing
103 }
104 
106 {
107  // Does nothing by default
108 }
109 
110 } /* namespace Rcs */
virtual bool getProperty(std::string &out, const char *property)=0
static std::vector< std::string > getTypeNames()
List available policy names.
static void registerType(const char *name, ControlPolicyCreateFunction creator)
virtual void reset()
static std::map< std::string, ControlPolicy::ControlPolicyCreateFunction > registry
ControlPolicy *(* ControlPolicyCreateFunction)(const char *)
Policy factory function. Should read a policy from a file.
Definition: ControlPolicy.h:53
static ControlPolicy * create(const char *name, const char *dataFile)