ConditionHandler.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 <mutex>
28#include <string>
29#include <vector>
30
34#include <ArmarXCore/interface/observers/ConditionHandlerInterface.h>
35#include <ArmarXCore/interface/observers/ObserverInterface.h>
37
38namespace armarx
39{
40 /**
41 * @class ConditionHandlerPropertyDefinitions
42 * @brief
43 */
49
50 /**
51 * @defgroup Component-ConditionHandler ConditionHandler
52 * @ingroup ArmarXCore-Components
53 * @brief This component allows installing distributed \ref armarx::Condition "conditions" on sensor data on multiple \ref armarx::Observer "Observers".
54 *
55 * The ConditionHandler manages the status of complex condition trees and distributes complex conditions \ref armarx::Literal "literals" to their designated observers.
56 * Upon fulfillment of conditions an associated event the send to the given EventListenerInterfacePrx.
57 * @see \ref Observers, armarx::ConditionCheck, armarx::ConditionRoot, armarx::Literal, armarx::Term
58 *
59 * @class ConditionHandler
60 * @ingroup Component-ConditionHandler
61 */
63 virtual public ConditionHandlerInterface,
64 virtual public Component
65 {
66 public:
67 /**
68 * Installs a condition
69 *
70 * @param listener Event listener
71 * @param expression Expression to test for
72 * @param e Event to generate when condition is fulfilled
73 *
74 * @throw InvalidConditionException
75 *
76 * @return Identifier of installed condition as required for removal (see removeCondition)
77 */
78 ConditionIdentifier installCondition(const EventListenerInterfacePrx& listener,
79 const TermImplBasePtr& expression,
80 const EventBasePtr& e,
81 bool onlyFireOnce,
82 bool reportDatafields,
83 const DatafieldRefList& refs,
84 const Ice::Current& c = Ice::emptyCurrent) override;
85
86 /**
87 * Installs a condition
88 *
89 * @param listener Event listener
90 * @param expression Expression to test for
91 * @param e Event to generate when condition is fulfilled
92 * @param desc Description of the condition for debugging and visualization purpose
93 *
94 * @throw InvalidConditionException
95 *
96 * @return Identifier of installed condition as required for removal (see removeCondition)
97 */
98 ConditionIdentifier
99 installConditionWithDescription(const EventListenerInterfacePrx& listener,
100 const TermImplBasePtr& expression,
101 const EventBasePtr& e,
102 const std::string& desc,
103 bool onlyFireOnce,
104 bool reportDatafields,
105 const DatafieldRefList& refs,
106 const Ice::Current& c = Ice::emptyCurrent) override;
107
108 /**
109 * Removes a condition. If the condition has already been removed, the function immediately returns.
110 *
111 * @param id Identifier of the condition to remove
112 */
113 void removeCondition(const ConditionIdentifier& id,
114 const Ice::Current& c = Ice::emptyCurrent) override;
115
116 /**
117 * Removes all conditions.
118 *
119 */
120 void removeAllConditions(const Ice::Current& c = Ice::emptyCurrent) override;
121
122 /**
123 * Retrieve the list of known observers as provided in the config file
124 *
125 * @return List of observer names
126 */
127 Ice::StringSeq getObserverNames(const Ice::Current& c = Ice::emptyCurrent) override;
128
129 /**
130 * Retrieve the list of active conditions.
131 *
132 * @return The condition registry
133 */
134 ConditionRegistry getActiveConditions(const Ice::Current& c = Ice::emptyCurrent) override;
135
136 /**
137 * Retrieve the list of conditions that have been registered in the past.
138 * The length limit of the list can be controlled with the config file parameter HistoryLength.
139 *
140 * @return The condition registry for past conditions
141 */
142 ConditionRegistry getPastConditions(const Ice::Current& c = Ice::emptyCurrent) override;
143
144 ConditionRootBasePtr getCondition(Ice::Int id,
145 const Ice::Current& c = Ice::emptyCurrent) override;
146
147 std::string getDefaultName() const override;
148
149 protected:
150 /**
151 * Framework hook. Called once on initialization of the ConditionInstaller.
152 */
153 virtual void
157
158 /**
159 * Framework hook. Called on first run, after ice setup.
160 */
161 virtual void
165
166 /**
167 * @see PropertyUser::createPropertyDefinitions()
168 */
169 PropertyDefinitionsPtr createPropertyDefinitions() override;
170
171 // inherited from Component
172 void onInitComponent() override;
173 void onConnectComponent() override;
174
175 private:
176 // active checks and associated events
177 std::mutex idMutex;
178 std::mutex iceManagerMutex;
179 std::mutex conditionRegistryMutex;
180 ConditionRegistry conditionRegistry;
181 std::mutex conditionHistoryMutex;
182 ConditionRegistry conditionHistory;
183 int historyLength;
184
185 // utility methods for observers
186 void useObservers(std::vector<std::string>& names);
187 void preCacheObservers(std::vector<std::string>& names);
188 ObserverInterfacePrx getObserver(std::string observerName);
189
190 // utility methods for check handling
191 void installChecks(std::vector<LiteralImplPtr>& literals, const Ice::Current& c);
192 void removeChecks(std::vector<LiteralImplPtr>& literals, const Ice::Current& c);
193
194 Ice::StringSeq observerNames;
195
196 int generateId();
197 int currentId;
198 };
199} // namespace armarx
#define ARMARXCORE_IMPORT_EXPORT
constexpr T c
ComponentPropertyDefinitions(std::string prefix, bool hasObjectNameParameter=true)
Definition Component.cpp:46
Component()
Protected default constructor. Used for virtual inheritance. Use createManagedIceObject() instead.
Definition Component.cpp:66
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.
virtual void onInitConditionHandler()
Framework hook.
ConditionRegistry getActiveConditions(const Ice::Current &c=Ice::emptyCurrent) override
Retrieve the list of active conditions.
void removeCondition(const ConditionIdentifier &id, const Ice::Current &c=Ice::emptyCurrent) override
Removes a condition.
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.
ConditionRegistry getPastConditions(const Ice::Current &c=Ice::emptyCurrent) override
Retrieve the list of conditions that have been registered in the past.
Ice::StringSeq getObserverNames(const Ice::Current &c=Ice::emptyCurrent) override
Retrieve the list of known observers as provided in the config file.
virtual void onStartConditionHandler()
Framework hook.
ConditionRootBasePtr getCondition(Ice::Int id, const Ice::Current &c=Ice::emptyCurrent) override
void removeAllConditions(const Ice::Current &c=Ice::emptyCurrent) override
Removes all conditions.
std::string getDefaultName() const override
Retrieve default name of component.
std::string prefix
Prefix of the properties such as namespace, domain, component name, etc.
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.