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 
28 
29 #include <ArmarXCore/interface/observers/VariantBase.h>
30 #include <ArmarXCore/interface/observers/ConditionCheckBase.h>
31 #include <ArmarXCore/interface/observers/ObserverInterface.h>
32 
35 
36 #include <string>
37 #include <memory>
38 #include <map>
39 
40 namespace armarx
41 {
42  using VariantTypeIdList = std::vector<VariantTypeId>;
43 
44  class ConditionCheck;
46 
47  /**
48  * @class ConditionCheck
49  * @ingroup Conditions
50  *
51  * A ConditionCheck implements a check on the sensor data stream of a Sensor-Actor Unit.
52  * ConditionCheck instances can be arbitrarily combined to complex Terms.
53  *
54  * All custom condition checks should inherit from this base class.
55  */
57  virtual public ConditionCheckBase
58  {
59  public:
60  /**
61  * Creates and initializes a ConditionCheck instance
62  */
64 
65  /**
66  * Destructor
67  */
68  ~ConditionCheck() override {}
69 
70  /**
71  * Creates a new ConditionCheck instance.
72  * It checks if channel and datafield named in configuration are availabel and if the type of the datafield
73  * is supported by the check.
74  *
75  * @param configuration The configuration of the check to instantiate
76  * @param channelRegistry The current registry of available channels
77  *
78  * @throws InvalidConditionException if the requested channel or datafield could not be found.
79  *
80  * @return Pointer to a ConditionCheck instance
81  */
82  //using ChannelRegistry = std::unordered_map<std::string, armarx::ChannelRegistryEntry>;
83  using ChannelRegistry = armarx::ChannelRegistry;
84  virtual ConditionCheckPtr createInstance(const CheckConfiguration& configuration, 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* clone()
92  {
93  return new ConditionCheck();
94  }
95 
96  /**
97  * Resets the status flags of the check such as check results.
98  */
99  void reset();
100 
101  /**
102  * Evaluates the condition based on the given registry of data fields.
103  *
104  * The result of the evaluation can be accessed by calling getFulFilled.
105  *
106  * @param dataFields The registry of data fields to consider
107  */
108  void evaluateCondition(const DataFieldRegistry& dataFields);
109 
110  /**
111  * Retrieve result of a condition evaluation initiated by evaluateCondition
112  *
113  * @return true if test was successful, false otherwise
114  */
115  bool getFulFilled();
116 
117  friend std::ostream& operator<<(std::ostream& stream, const ConditionCheck& rhs)
118  {
119  // stream << ElementaryConditionPtr::dynamicCast(rhs.condition) << " = " << rhs.fulFilled;
120  return stream;
121  }
122 
123  friend std::ostream& operator<<(std::ostream& stream, const ConditionCheckPtr& rhs)
124  {
125  // stream << ElementaryConditionPtr::dynamicCast(rhs->condition) << " = " << rhs->fulFilled;
126  return stream;
127  }
128 
129  protected:
130  /**
131  * Sets the number of paramaters required for this check
132  *
133  * @param numberParameters number of parameters
134  */
135  void setNumberParameters(int numberParameters);
136 
137  /**
138  * Add a supported type for elementary condition check
139  * marks pairs of (dataFieldType,EvaluationTypes)
140  *
141  * @param dataFieldType valid type for the dataField variant. Set to 0 for: all types are supported.
142  * @param parameterTypes types of the parameters
143  */
144  void addSupportedType(VariantTypeId dataFieldType = 0, ParameterTypeList parameterTypes = ParameterTypeList());
145 
146  /**
147  * Retrieve parameters of check
148  *
149  * @param index of the parameter
150  *
151  * @return reference to check parameter
152  */
153  const Variant& getParameter(int index);
154 
155  /**
156  * Evaluate the condition based on the current data field values.
157  * This method should be overwritten in order to create a custom condition check.
158  *
159  * @param dataFields The values of the relevant data fields to check against
160  *
161  * @return In this implementation, returns always true
162  */
163  virtual bool evaluate(const StringVariantMap& dataFields)
164  {
165  return true;
166  }
167 
168  static ParameterTypeList createParameterTypeList(int numberTypes, ...);
169 
170  private:
171  void assureTypeSupported(const CheckConfiguration& configuration, VariantTypeId dataFieldType);
172 
173  bool firstEval;
174  };
175 }
176 namespace armarx::checks
177 {
178  class CheckBase;
179  using CheckBasePtr = std::shared_ptr<CheckBase>;
180  using CheckBaseConstPtr = std::shared_ptr<const CheckBase>;
181 
182  class CheckBase
183  {
184  public:
185  virtual ~CheckBase()
186  {
187  }
188  virtual const std::string& getCheckStr() const
189  {
190  return _checkStr;
191  }
192 
193  static CheckBasePtr create(const std::string& check)
194  {
195  return CheckBasePtr(new CheckBase(check));
196  }
197  private:
198  const std::string _checkStr;
199  CheckBase() {}
200  CheckBase(std::string check) : _checkStr(check) {}
201  };
202 
203 }
204 
205 #define ARMARX_CREATE_CHECK(OFFERER,NEWCHECK) namespace checks { namespace OFFERER { \
206  const CheckBaseConstPtr NEWCHECK = CheckBase::create(#NEWCHECK);\
207  }}
208 
209 extern template class ::IceInternal::Handle<::armarx::ConditionCheck>;
armarx::Variant
The Variant class is described here: Variants.
Definition: Variant.h:224
armarx::checks::CheckBaseConstPtr
std::shared_ptr< const CheckBase > CheckBaseConstPtr
Definition: ConditionCheck.h:180
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:188
armarx::checks::CheckBase::create
static CheckBasePtr create(const std::string &check)
Definition: ConditionCheck.h:193
DataFieldIdentifier.h
armarx::ConditionCheck::evaluate
virtual bool evaluate(const StringVariantMap &dataFields)
Evaluate the condition based on the current data field values.
Definition: ConditionCheck.h:163
armarx::checks::CheckBasePtr
std::shared_ptr< CheckBase > CheckBasePtr
Definition: ConditionCheck.h:179
armarx::checks
Definition: ConditionCheck.h:176
IceInternal::Handle
Definition: forward_declarations.h:8
armarx::ConditionCheck::clone
virtual ConditionCheck * clone()
Clones the current check.
Definition: ConditionCheck.h:91
armarx::ConditionCheck::operator<<
friend std::ostream & operator<<(std::ostream &stream, const ConditionCheck &rhs)
Definition: ConditionCheck.h:117
armarx::ConditionCheck::~ConditionCheck
~ConditionCheck() override
Destructor.
Definition: ConditionCheck.h:68
armarx::VariantTypeId
Ice::Int VariantTypeId
Definition: Variant.h:44
armarx::ConditionCheck::operator<<
friend std::ostream & operator<<(std::ostream &stream, const ConditionCheckPtr &rhs)
Definition: ConditionCheck.h:123
armarx::checks::CheckBase::~CheckBase
virtual ~CheckBase()
Definition: ConditionCheck.h:185
armarx::ConditionCheck
Definition: ConditionCheck.h:56
armarx::ConditionCheck::ChannelRegistry
armarx::ChannelRegistry ChannelRegistry
Creates a new ConditionCheck instance.
Definition: ConditionCheck.h:83
armarx::VariantTypeIdList
std::vector< VariantTypeId > VariantTypeIdList
Definition: ConditionCheck.h:42
ImportExport.h
ARMARXCORE_IMPORT_EXPORT
#define ARMARXCORE_IMPORT_EXPORT
Definition: ImportExport.h:38
Variant.h
armarx::checks::CheckBase
Definition: ConditionCheck.h:182
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28