ConditionCheck.h
Go to the documentation of this file.
1 /*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5 *
6 * ArmarX is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * ArmarX is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * @package ArmarX::Core
19 * @author Kai Welke (welke _at_ kit _dot_ edu)
20 * @date 2011
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24 
25 #pragma once
26 
27 #include <map>
28 #include <memory>
29 #include <string>
30 
32 #include <ArmarXCore/interface/observers/ConditionCheckBase.h>
33 #include <ArmarXCore/interface/observers/ObserverInterface.h>
34 #include <ArmarXCore/interface/observers/VariantBase.h>
37 
38 namespace armarx
39 {
40  using VariantTypeIdList = std::vector<VariantTypeId>;
41 
42  class ConditionCheck;
44 
45  /**
46  * @class ConditionCheck
47  * @ingroup Conditions
48  *
49  * A ConditionCheck implements a check on the sensor data stream of a Sensor-Actor Unit.
50  * ConditionCheck instances can be arbitrarily combined to complex Terms.
51  *
52  * All custom condition checks should inherit from this base class.
53  */
54  class ARMARXCORE_IMPORT_EXPORT ConditionCheck : virtual public ConditionCheckBase
55  {
56  public:
57  /**
58  * Creates and initializes a ConditionCheck instance
59  */
61 
62  /**
63  * Destructor
64  */
65  ~ConditionCheck() override
66  {
67  }
68 
69  /**
70  * Creates a new ConditionCheck instance.
71  * It checks if channel and datafield named in configuration are availabel and if the type of the datafield
72  * is supported by the check.
73  *
74  * @param configuration The configuration of the check to instantiate
75  * @param channelRegistry The current registry of available channels
76  *
77  * @throws InvalidConditionException if the requested channel or datafield could not be found.
78  *
79  * @return Pointer to a ConditionCheck instance
80  */
81  //using ChannelRegistry = std::unordered_map<std::string, armarx::ChannelRegistryEntry>;
82  using ChannelRegistry = armarx::ChannelRegistry;
83  virtual ConditionCheckPtr createInstance(const CheckConfiguration& configuration,
84  const ChannelRegistry& channelRegistry);
85 
86  /**
87  * Clones the current check. Implement this in each subclass of the check.
88  *
89  * @return new instance
90  */
91  virtual ConditionCheck*
93  {
94  return new ConditionCheck();
95  }
96 
97  /**
98  * Resets the status flags of the check such as check results.
99  */
100  void reset();
101 
102  /**
103  * Evaluates the condition based on the given registry of data fields.
104  *
105  * The result of the evaluation can be accessed by calling getFulFilled.
106  *
107  * @param dataFields The registry of data fields to consider
108  */
109  void evaluateCondition(const DataFieldRegistry& dataFields);
110 
111  /**
112  * Retrieve result of a condition evaluation initiated by evaluateCondition
113  *
114  * @return true if test was successful, false otherwise
115  */
116  bool getFulFilled();
117 
118  friend std::ostream&
119  operator<<(std::ostream& stream, const ConditionCheck& rhs)
120  {
121  // stream << ElementaryConditionPtr::dynamicCast(rhs.condition) << " = " << rhs.fulFilled;
122  return stream;
123  }
124 
125  friend std::ostream&
126  operator<<(std::ostream& stream, const ConditionCheckPtr& rhs)
127  {
128  // stream << ElementaryConditionPtr::dynamicCast(rhs->condition) << " = " << rhs->fulFilled;
129  return stream;
130  }
131 
132  protected:
133  /**
134  * Sets the number of paramaters required for this check
135  *
136  * @param numberParameters number of parameters
137  */
138  void setNumberParameters(int numberParameters);
139 
140  /**
141  * Add a supported type for elementary condition check
142  * marks pairs of (dataFieldType,EvaluationTypes)
143  *
144  * @param dataFieldType valid type for the dataField variant. Set to 0 for: all types are supported.
145  * @param parameterTypes types of the parameters
146  */
147  void addSupportedType(VariantTypeId dataFieldType = 0,
148  ParameterTypeList parameterTypes = ParameterTypeList());
149 
150  /**
151  * Retrieve parameters of check
152  *
153  * @param index of the parameter
154  *
155  * @return reference to check parameter
156  */
157  const Variant& getParameter(int index);
158 
159  /**
160  * Evaluate the condition based on the current data field values.
161  * This method should be overwritten in order to create a custom condition check.
162  *
163  * @param dataFields The values of the relevant data fields to check against
164  *
165  * @return In this implementation, returns always true
166  */
167  virtual bool
168  evaluate(const StringVariantMap& dataFields)
169  {
170  return true;
171  }
172 
173  static ParameterTypeList createParameterTypeList(int numberTypes, ...);
174 
175  private:
176  void assureTypeSupported(const CheckConfiguration& configuration,
177  VariantTypeId dataFieldType);
178 
179  bool firstEval;
180  };
181 } // namespace armarx
182 
183 namespace armarx::checks
184 {
185  class CheckBase;
186  using CheckBasePtr = std::shared_ptr<CheckBase>;
187  using CheckBaseConstPtr = std::shared_ptr<const CheckBase>;
188 
189  class CheckBase
190  {
191  public:
192  virtual ~CheckBase()
193  {
194  }
195 
196  virtual const std::string&
197  getCheckStr() const
198  {
199  return _checkStr;
200  }
201 
202  static CheckBasePtr
203  create(const std::string& check)
204  {
205  return CheckBasePtr(new CheckBase(check));
206  }
207 
208  private:
209  const std::string _checkStr;
210 
211  CheckBase()
212  {
213  }
214 
215  CheckBase(std::string check) : _checkStr(check)
216  {
217  }
218  };
219 
220 } // namespace armarx::checks
221 
222 #define ARMARX_CREATE_CHECK(OFFERER, NEWCHECK) \
223  namespace checks \
224  { \
225  namespace OFFERER \
226  { \
227  const CheckBaseConstPtr NEWCHECK = CheckBase::create(#NEWCHECK); \
228  } \
229  }
230 
231 extern template class ::IceInternal::Handle<::armarx::ConditionCheck>;
armarx::Variant
The Variant class is described here: Variants.
Definition: Variant.h:223
armarx::checks::CheckBaseConstPtr
std::shared_ptr< const CheckBase > CheckBaseConstPtr
Definition: ConditionCheck.h:187
armarx::StringVariantMap
std::map< std::string, Variant > StringVariantMap
Definition: Variant.h:748
index
uint8_t index
Definition: EtherCATFrame.h:59
armarx::checks::CheckBase::getCheckStr
virtual const std::string & getCheckStr() const
Definition: ConditionCheck.h:197
armarx::checks::CheckBase::create
static CheckBasePtr create(const std::string &check)
Definition: ConditionCheck.h:203
DataFieldIdentifier.h
armarx::ConditionCheck::evaluate
virtual bool evaluate(const StringVariantMap &dataFields)
Evaluate the condition based on the current data field values.
Definition: ConditionCheck.h:168
armarx::checks::CheckBasePtr
std::shared_ptr< CheckBase > CheckBasePtr
Definition: ConditionCheck.h:186
armarx::checks
Definition: ConditionCheck.h:183
IceInternal::Handle
Definition: forward_declarations.h:8
armarx::ConditionCheck::clone
virtual ConditionCheck * clone()
Clones the current check.
Definition: ConditionCheck.h:92
armarx::ConditionCheck::~ConditionCheck
~ConditionCheck() override
Destructor.
Definition: ConditionCheck.h:65
armarx::VariantTypeId
Ice::Int VariantTypeId
Definition: Variant.h:43
armarx::checks::CheckBase::~CheckBase
virtual ~CheckBase()
Definition: ConditionCheck.h:192
armarx::ConditionCheck
Definition: ConditionCheck.h:54
armarx::ConditionCheck::ChannelRegistry
armarx::ChannelRegistry ChannelRegistry
Creates a new ConditionCheck instance.
Definition: ConditionCheck.h:82
armarx::operator<<
std::ostream & operator<<(std::ostream &os, const PythonApplicationManager::Paths &paths)
Definition: PythonApplicationManager.cpp:285
armarx::VariantTypeIdList
std::vector< VariantTypeId > VariantTypeIdList
Definition: ConditionCheck.h:40
ImportExport.h
ARMARXCORE_IMPORT_EXPORT
#define ARMARXCORE_IMPORT_EXPORT
Definition: ImportExport.h:38
Variant.h
armarx::checks::CheckBase
Definition: ConditionCheck.h:189
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27