ConditionHandler.cpp
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 #include "ConditionHandler.h"
29 
30 #include <SimoxUtility/algorithm/string/string_tools.h>
31 
32 #include <sstream>
33 
34 namespace armarx
35 {
38  {
39  defineOptionalProperty<std::string>("Observers", "", "Comma seperated observer list");
40  defineOptionalProperty<int>("HistoryLength", 1000, "Length of condition history kept by the conditionhandler");
41  }
42 
43 
44  // *******************************************************
45  // implementation of ObserverInterface
46  // *******************************************************
47  ConditionIdentifier ConditionHandler::installCondition(const EventListenerInterfacePrx& listener, const TermImplBasePtr& expression, const EventBasePtr& e, bool onlyFireOnce, bool reportDatafields, const DatafieldRefList& refs, const Ice::Current& c)
48  {
49  return installConditionWithDescription(listener, expression, e, "", onlyFireOnce, reportDatafields, refs, c);
50  }
51 
52  ConditionIdentifier ConditionHandler::installConditionWithDescription(const EventListenerInterfacePrx& listener, const TermImplBasePtr& expression, const EventBasePtr& e, const std::string& desc, bool onlyFireOnce, bool reportDatafields, const DatafieldRefList& refs, const Ice::Current& c)
53  {
54  // generate root expression
55  ConditionRootPtr root = new ConditionRoot(listener, e, desc, onlyFireOnce, refs);
56  ARMARX_DEBUG << "Installing condition: " << root << std::endl;
57  root->addChild(expression);
58 
59  // extract checks
60  std::vector<LiteralImplPtr> literals = ConditionRoot::ExtractLiterals(root);
61 
62  // forward the onlyfireonce flag to all literals
63  for (LiteralImplPtr& literal : literals)
64  {
65  literal->checkConfig.reportAlways = !onlyFireOnce;
66  literal->checkConfig.reportDatafieldValues = reportDatafields;
67  }
68 
69  std::unique_lock lock(conditionRegistryMutex);
70 
71  // go through literals and install checks
72  installChecks(literals, c);
73 
74  // store in registry
75  std::pair<int, ConditionRootBasePtr> entry;
76  entry.first = generateId();
77  entry.second = root;
78 
79  conditionRegistry.insert(entry);
80 
81  ConditionIdentifier id;
82  id.uniqueId = entry.first;
83 
84  ARMARX_DEBUG << "Installed condition: " << root << std::endl;
85  setMetaInfo("conditionCount", new Variant((int)conditionRegistry.size()));
86  return id;
87  }
88 
89 
90  void ConditionHandler::removeCondition(const ConditionIdentifier& id, const Ice::Current& c)
91  {
92  ARMARX_DEBUG << "Removing condition " << id.uniqueId << std::endl;
93 
94  std::unique_lock lock(conditionRegistryMutex);
95  ConditionRegistry::iterator iter = conditionRegistry.find(id.uniqueId);
96 
97  if (iter == conditionRegistry.end())
98  {
99  return;
100  }
101 
102  ConditionRootPtr root = ConditionRootPtr::dynamicCast(iter->second);
103 
104  // extract checks
105  std::vector<LiteralImplPtr> literals = ConditionRoot::ExtractLiterals(root);
106 
107  // go through literals and remove checks
108  removeChecks(literals, c);
109 
110  // remove from registry
111  std::pair<int, ConditionRootBasePtr> tmp = *iter;
112  conditionRegistry.erase(iter);
113 
114  // insert into history
115  std::unique_lock lockHistory(conditionHistoryMutex);
116  conditionHistory.insert(tmp);
117 
118  if (int(conditionHistory.size()) > historyLength)
119  {
120  conditionHistory.erase(conditionHistory.begin());
121  }
122  setMetaInfo("conditionHistory", new Variant((int)conditionHistory.size()));
123  setMetaInfo("conditionCount", new Variant((int)conditionRegistry.size()));
124 
125  }
126 
127  void ConditionHandler::removeAllConditions(const Ice::Current& c)
128  {
129  ConditionRegistry::iterator iter;
130  std::vector<int> conditionIds;
131 
132  {
133  std::unique_lock lock(conditionRegistryMutex);
134 
135  if (!conditionRegistry.empty())
136  {
137  for (iter = conditionRegistry.begin(); iter != conditionRegistry.end(); iter++)
138  {
139  conditionIds.push_back(iter->first);
140  }
141  }
142  }
143 
144  if (!conditionIds.empty())
145  {
146  for (int id : conditionIds)
147  {
148  removeCondition(ConditionIdentifier {id}, c);
149  }
150  }
151 
152  std::unique_lock lockHistory(conditionHistoryMutex);
153  conditionHistory.clear();
154  setMetaInfo("conditionHistory", new Variant((int)conditionHistory.size()));
155  setMetaInfo("conditionCount", new Variant((int)conditionRegistry.size()));
156 
157  }
158 
159  ConditionRegistry ConditionHandler::getActiveConditions(const Ice::Current& c)
160  {
161  std::unique_lock lock(conditionRegistryMutex);
162 
163  return conditionRegistry;
164  }
165 
166  ConditionRegistry ConditionHandler::getPastConditions(const Ice::Current& c)
167  {
168  std::unique_lock lock(conditionHistoryMutex);
169 
170  return conditionHistory;
171  }
172 
173  ConditionRootBasePtr ConditionHandler::getCondition(Ice::Int id, const Ice::Current& c)
174  {
175  {
176  std::unique_lock lock(conditionRegistryMutex);
177  auto it = conditionRegistry.find(id);
178 
179  if (it != conditionRegistry.end())
180  {
181  return it->second;
182  }
183  }
184  {
185  std::unique_lock lock(conditionHistoryMutex);
186  auto it = conditionHistory.find(id);
187 
188  if (it != conditionHistory.end())
189  {
190  return it->second;
191  }
192  }
193  return nullptr;
194  }
195 
197  {
198  return "ConditionHandler";
199  }
200 
201  Ice::StringSeq ConditionHandler::getObserverNames(const Ice::Current& c)
202  {
203  return observerNames;
204  }
205 
206  // *******************************************************
207  // Component hooks
208  // *******************************************************
210  {
211  currentId = 0;
212 
213  // read observer dependencies. These observers will be precaches
214  std::string observers = getProperty<std::string>("Observers");
215  simox::alg::trim(observers);
216 
217  if (!observers.empty())
218  {
219  observerNames = simox::alg::split(observers, ",");
220  }
221 
222  useObservers(observerNames);
223 
224  // read max length of history
225  historyLength = getProperty<int>("HistoryLength").getValue();
226 
228  }
229 
231  {
232 
233  // precach
234  preCacheObservers(observerNames);
235 
236  // subclass init
238  }
239 
240  // *******************************************************
241  // private methods
242  // *******************************************************
243  int ConditionHandler::generateId()
244  {
245  std::unique_lock lock(idMutex);
246  return currentId++;
247  }
248 
249  void ConditionHandler::useObservers(std::vector<std::string>& names)
250  {
251  std::unique_lock lock(iceManagerMutex);
252 
253  std::vector<std::string>::iterator iter = names.begin();
254 
255  while (iter != names.end())
256  {
257  usingProxy(*iter);
258  iter++;
259  }
260  }
261 
262  void ConditionHandler::preCacheObservers(std::vector<std::string>& names)
263  {
264  std::unique_lock lock(iceManagerMutex);
265 
266  std::vector<std::string>::iterator iter = names.begin();
267 
268  while (iter != names.end())
269  {
270  getProxy<ObserverInterfacePrx>(*iter);
271  iter++;
272  }
273  }
274 
275  ObserverInterfacePrx ConditionHandler::getObserver(std::string observerName)
276  {
277  std::unique_lock lock(iceManagerMutex);
278  ObserverInterfacePrx proxy;
279 
280  try
281  {
282  proxy = getProxy<ObserverInterfacePrx>(observerName);
283  ObserverInterfacePrx::checkedCast(proxy);
284  }
285  catch (...)
286  {
287  throw InvalidConditionException("Observer \"" + observerName + "\" does not exist.");
288  }
289 
290  return proxy;
291  }
292 
293 
294 
295  void ConditionHandler::installChecks(std::vector<LiteralImplPtr>& literals, const Ice::Current& c)
296  {
297  std::vector<LiteralImplPtr>::iterator iter = literals.begin();
298 
299  while (iter != literals.end())
300  {
301  ObserverInterfacePrx proxy = getObserver((*iter)->checkConfig.dataFieldIdentifier->observerName);
302  (*iter)->installCheck(c.adapter, proxy);
303 
304  iter++;
305  }
306  }
307 
308  void ConditionHandler::removeChecks(std::vector<LiteralImplPtr>& literals, const Ice::Current& c)
309  {
310  std::vector<LiteralImplPtr>::iterator iter = literals.begin();
311 
312  while (iter != literals.end())
313  {
314  ObserverInterfacePrx proxy = getObserver((*iter)->checkConfig.dataFieldIdentifier->observerName);
315  (*iter)->removeCheck(c.adapter, proxy);
316 
317  iter++;
318  }
319  }
320 
322  {
325  }
326 }
armarx::ConditionHandler::getPastConditions
ConditionRegistry getPastConditions(const Ice::Current &c=Ice::emptyCurrent) override
Retrieve the list of conditions that have been registered in the past.
Definition: ConditionHandler.cpp:166
armarx::Variant
The Variant class is described here: Variants.
Definition: Variant.h:224
armarx::ConditionHandler::onConnectComponent
void onConnectComponent() override
Pure virtual hook for the subclass.
Definition: ConditionHandler.cpp:230
armarx::ManagedIceObject::setMetaInfo
void setMetaInfo(const std::string &id, const VariantBasePtr &value)
Allows to set meta information that can be queried live via Ice interface on the ArmarXManager.
Definition: ManagedIceObject.cpp:749
armarx::ConditionHandler::removeAllConditions
void removeAllConditions(const Ice::Current &c=Ice::emptyCurrent) override
Removes all conditions.
Definition: ConditionHandler.cpp:127
armarx::ConditionHandler::installConditionWithDescription
ConditionIdentifier installConditionWithDescription(const EventListenerInterfacePrx &listener, const TermImplBasePtr &expression, const EventBasePtr &e, const std::string &desc, bool onlyFireOnce, bool reportDatafields, const DatafieldRefList &refs, const Ice::Current &c=Ice::emptyCurrent) override
Installs a condition.
Definition: ConditionHandler.cpp:52
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
IceInternal::Handle
Definition: forward_declarations.h:8
armarx::ConditionHandler::getDefaultName
std::string getDefaultName() const override
Retrieve default name of component.
Definition: ConditionHandler.cpp:196
ObserverObjectFactories.h
armarx::ConditionHandler::getActiveConditions
ConditionRegistry getActiveConditions(const Ice::Current &c=Ice::emptyCurrent) override
Retrieve the list of active conditions.
Definition: ConditionHandler.cpp:159
armarx::ConditionHandler::createPropertyDefinitions
PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: ConditionHandler.cpp:321
ConditionHandler.h
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:177
armarx::ConditionHandler::onStartConditionHandler
virtual void onStartConditionHandler()
Framework hook.
Definition: ConditionHandler.h:145
armarx::ConditionHandlerPropertyDefinitions::ConditionHandlerPropertyDefinitions
ConditionHandlerPropertyDefinitions(std::string prefix)
Definition: ConditionHandler.cpp:36
ConditionRoot.h
armarx::Component::getConfigIdentifier
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition: Component.cpp:74
armarx::ComponentPropertyDefinitions
Default component property definition container.
Definition: Component.h:70
armarx::viz::data::ElementFlags::names
const simox::meta::IntEnumNames names
Definition: json_elements.cpp:14
armarx::ConditionHandler::installCondition
ConditionIdentifier installCondition(const EventListenerInterfacePrx &listener, const TermImplBasePtr &expression, const EventBasePtr &e, bool onlyFireOnce, bool reportDatafields, const DatafieldRefList &refs, const Ice::Current &c=Ice::emptyCurrent) override
Installs a condition.
Definition: ConditionHandler.cpp:47
IceUtil::Handle< class PropertyDefinitionContainer >
armarx::VariantType::Int
const VariantTypeId Int
Definition: Variant.h:916
armarx::ConditionRoot
ConditionRoot Condition roots are top-level nodes of the expression tree.
Definition: ConditionRoot.h:60
ConditionCheck.h
armarx::ConditionRoot::ExtractLiterals
static std::vector< LiteralImplPtr > ExtractLiterals(const TermImplBasePtr &expression)
Definition: ConditionRoot.cpp:146
armarx::ConditionHandler::getCondition
ConditionRootBasePtr getCondition(Ice::Int id, const Ice::Current &c=Ice::emptyCurrent) override
Definition: ConditionHandler.cpp:173
armarx::ConditionHandler::onInitConditionHandler
virtual void onInitConditionHandler()
Framework hook.
Definition: ConditionHandler.h:140
armarx::PropertyDefinitionsPtr
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
Definition: forward_declarations.h:34
armarx::ConditionHandler::removeCondition
void removeCondition(const ConditionIdentifier &id, const Ice::Current &c=Ice::emptyCurrent) override
Removes a condition.
Definition: ConditionHandler.cpp:90
armarx::ManagedIceObject::usingProxy
bool usingProxy(const std::string &name, const std::string &endpoints="")
Registers a proxy for retrieval after initialization and adds it to the dependency list.
Definition: ManagedIceObject.cpp:151
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::ConditionHandler::onInitComponent
void onInitComponent() override
Pure virtual hook for the subclass.
Definition: ConditionHandler.cpp:209
armarx::ConditionHandlerPropertyDefinitions
Definition: ConditionHandler.h:46
armarx::ConditionHandler::getObserverNames
Ice::StringSeq getObserverNames(const Ice::Current &c=Ice::emptyCurrent) override
Retrieve the list of known observers as provided in the config file.
Definition: ConditionHandler.cpp:201
armarx::split
std::vector< std::string > split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
Definition: StringHelpers.cpp:36