30 #include <SimoxUtility/algorithm/string/string_tools.h>
39 defineOptionalProperty<std::string>(
"Observers",
"",
"Comma seperated observer list");
40 defineOptionalProperty<int>(
"HistoryLength", 1000,
"Length of condition history kept by the conditionhandler");
47 ConditionIdentifier
ConditionHandler::installCondition(
const EventListenerInterfacePrx& listener,
const TermImplBasePtr& expression,
const EventBasePtr& e,
bool onlyFireOnce,
bool reportDatafields,
const DatafieldRefList& refs,
const Ice::Current&
c)
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)
56 ARMARX_DEBUG <<
"Installing condition: " << root << std::endl;
57 root->addChild(expression);
65 literal->checkConfig.reportAlways = !onlyFireOnce;
66 literal->checkConfig.reportDatafieldValues = reportDatafields;
69 std::unique_lock lock(conditionRegistryMutex);
72 installChecks(literals,
c);
75 std::pair<int, ConditionRootBasePtr> entry;
76 entry.first = generateId();
79 conditionRegistry.insert(entry);
81 ConditionIdentifier id;
82 id.uniqueId = entry.first;
84 ARMARX_DEBUG <<
"Installed condition: " << root << std::endl;
92 ARMARX_DEBUG <<
"Removing condition " <<
id.uniqueId << std::endl;
94 std::unique_lock lock(conditionRegistryMutex);
95 ConditionRegistry::iterator iter = conditionRegistry.find(
id.uniqueId);
97 if (iter == conditionRegistry.end())
108 removeChecks(literals,
c);
111 std::pair<int, ConditionRootBasePtr> tmp = *iter;
112 conditionRegistry.erase(iter);
115 std::unique_lock lockHistory(conditionHistoryMutex);
116 conditionHistory.insert(tmp);
118 if (
int(conditionHistory.size()) > historyLength)
120 conditionHistory.erase(conditionHistory.begin());
129 ConditionRegistry::iterator iter;
130 std::vector<int> conditionIds;
133 std::unique_lock lock(conditionRegistryMutex);
135 if (!conditionRegistry.empty())
137 for (iter = conditionRegistry.begin(); iter != conditionRegistry.end(); iter++)
139 conditionIds.push_back(iter->first);
144 if (!conditionIds.empty())
146 for (
int id : conditionIds)
152 std::unique_lock lockHistory(conditionHistoryMutex);
153 conditionHistory.clear();
161 std::unique_lock lock(conditionRegistryMutex);
163 return conditionRegistry;
168 std::unique_lock lock(conditionHistoryMutex);
170 return conditionHistory;
176 std::unique_lock lock(conditionRegistryMutex);
177 auto it = conditionRegistry.find(
id);
179 if (it != conditionRegistry.end())
185 std::unique_lock lock(conditionHistoryMutex);
186 auto it = conditionHistory.find(
id);
188 if (it != conditionHistory.end())
198 return "ConditionHandler";
203 return observerNames;
214 std::string observers = getProperty<std::string>(
"Observers");
215 simox::alg::trim(observers);
217 if (!observers.empty())
222 useObservers(observerNames);
225 historyLength = getProperty<int>(
"HistoryLength").getValue();
234 preCacheObservers(observerNames);
243 int ConditionHandler::generateId()
245 std::unique_lock lock(idMutex);
249 void ConditionHandler::useObservers(std::vector<std::string>&
names)
251 std::unique_lock lock(iceManagerMutex);
253 std::vector<std::string>::iterator iter =
names.begin();
255 while (iter !=
names.end())
262 void ConditionHandler::preCacheObservers(std::vector<std::string>&
names)
264 std::unique_lock lock(iceManagerMutex);
266 std::vector<std::string>::iterator iter =
names.begin();
268 while (iter !=
names.end())
270 getProxy<ObserverInterfacePrx>(*iter);
275 ObserverInterfacePrx ConditionHandler::getObserver(std::string observerName)
277 std::unique_lock lock(iceManagerMutex);
278 ObserverInterfacePrx proxy;
282 proxy = getProxy<ObserverInterfacePrx>(observerName);
283 ObserverInterfacePrx::checkedCast(proxy);
287 throw InvalidConditionException(
"Observer \"" + observerName +
"\" does not exist.");
295 void ConditionHandler::installChecks(std::vector<LiteralImplPtr>& literals,
const Ice::Current&
c)
297 std::vector<LiteralImplPtr>::iterator iter = literals.begin();
299 while (iter != literals.end())
301 ObserverInterfacePrx proxy = getObserver((*iter)->checkConfig.dataFieldIdentifier->observerName);
302 (*iter)->installCheck(
c.adapter, proxy);
308 void ConditionHandler::removeChecks(std::vector<LiteralImplPtr>& literals,
const Ice::Current&
c)
310 std::vector<LiteralImplPtr>::iterator iter = literals.begin();
312 while (iter != literals.end())
314 ObserverInterfacePrx proxy = getObserver((*iter)->checkConfig.dataFieldIdentifier->observerName);
315 (*iter)->removeCheck(
c.adapter, proxy);