WorkingMemoryController.h
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2012-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 MemoryX::gui-plugins::SceneEditor
19  * @date 2015
20  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
21  * GNU General Public License
22  */
23 
24 #pragma once
25 
26 #include <exception>
27 #include <string>
28 #include <vector>
29 
30 // Coin3D
31 #include <Inventor/SbRotation.h>
32 #include <Inventor/SbVec3f.h>
33 
34 // MemoryXInterface
35 #include <MemoryX/interface/components/WorkingMemoryInterface.h>
36 #include <MemoryX/interface/memorytypes/MemorySegments.h>
39 
40 // Core
41 #include <mutex>
42 
44 
45 #include "MemoryXController.h"
46 
47 namespace memoryxcontroller
48 {
49  class MemoryXController;
50 
51  /**
52  * @brief The controller for the communication with the working memory.
53  *
54  * This class provides methods to add, change and remove object instances in the working memory.
55  * Furthermore it is possible to get the content of the memory and save its content in snapshots.
56  */
59  virtual public memoryx::WorkingMemoryListenerInterface
60  {
61  public:
62  /**
63  * @brief Constructor.
64  *
65  * Creates a new instance of this class and registers this managed ice object.
66  *
67  * @param workingMemoryPrx the proxy to the used working memory
68  * @param workingMemoryUpdatesTopic the name of the topic this ice object will subscribe to recieve events from the working memory
69  * @param objectInstancesSegmentName the name of the segment in the data base where the object instances are stored
70  * @param memoryXController the MemoryXController
71  *
72  * @see memoryxcontroller::MemoryXController
73  */
74  WorkingMemoryController(const memoryx::WorkingMemoryInterfacePrx& workingMemoryPrx,
75  const std::string& workingMemoryUpdatesTopic,
76  const std::string& objectInstancesSegmentName,
77  const std::shared_ptr<MemoryXController>& memoryXController);
78  /**
79  * @brief Destructor.
80  *
81  * Destructs this instance of this class.
82  */
83  ~WorkingMemoryController() override;
84  /**
85  * @brief Adds a new object instance to the working memory.
86  *
87  * Returns the id of the new object instance.
88  *
89  * @param objectName the name of the added object instance
90  * @param objectClassPtr a pointer to the object class of the new object instance
91  * @return std::string the unique id of this object instance that is set by the working memory
92  */
93  std::string addObjectInstance(const std::string& objectName,
94  const memoryx::ObjectClassPtr& objectClassPtr);
95  /**
96  * @brief Adds all given object instances to the working memory.
97  *
98  * This method will also add the given object instances to the local scene
99  *
100  * @param instancePtrs object instances to add to the working memory
101  * @return std::vector<std::string>
102  */
103  std::vector<std::string>
104  addObjectInstances(const std::vector<memoryx::ObjectInstancePtr>& instancePtrs);
105  /**
106  * @brief Removes the object instance with the given id.
107  *
108  * @throws Exception when there is no object instance with the given id
109  * @param id the id of the object instance that should be removed from the working memory
110  */
111  void removeObjectInstance(const std::string& id);
112  /**
113  * @brief Applies the given orientation and position to the object instance with the given id.
114  *
115  * Important: The values of the position vector will be multiplied by 1000 because the working memory uses another scale
116  *
117  * @param id the id of the object that gets the new orientation and position
118  * @param newOrientation the new orientation
119  * @param newPosition the new position
120  */
121  void rotateTranslateObject(const std::string& id,
122  const SbRotation& newOrientation,
123  const SbVec3f& newPosition);
124  /**
125  * @brief Returns all object ids that are currently in the working memory.
126  *
127  * @return std::vector<std::string> all object ids of the objects in the working memory
128  */
129  std::vector<std::string> getAllObjectInstances() const;
130 
131  /**
132  * @brief Adds all object instances of the working memory to the local scene.
133  *
134  */
135  void addAllInstancesToLocalScene() const;
136 
137  /**
138  * @brief Saves the whole scene in a snapshot with the given name.
139  *
140  * Retruns true if the saving was sucessful.
141  *
142  * @param snapshotName the name of the snapshot where all object instances of the working memory are saved
143  * @param longtermMemoryPrx a proxy for the longterm-memory where the snapshot will be stored
144  * @return bool returns true if the saving was succesful
145  */
146  bool
147  saveSceneInSnapshot(const std::string& snapshotName,
148  const memoryx::LongtermMemoryInterfacePrx& longtermMemoryPrx) const;
149  /**
150  * @brief Saves the object instances with the given ids in a snapshot with the given name.
151  *
152  * Retruns true if the saving was sucessful.
153  *
154  * @param snapshotName the name of the snapshot where the object instances are saved
155  * @param longtermMemoryPrx a proxy for the longterm-memory where the snapshot will be stored
156  * @param objectIds the list of ids
157  * @return bool returns true if the saving was succesful
158  */
159  bool saveObjectsInSnapshot(const std::string& snapshotName,
160  const memoryx::LongtermMemoryInterfacePrx& longtermMemoryPrx,
161  const std::vector<std::string>& objectIds) const;
162 
163  /**
164  * Returns the rotation of a object instance. The rotation is translated to the format used in the Scene.
165  *
166  * @return The rotation.
167  */
168  static SbRotation
170 
171  /**
172  * Returns the position of a object instance. The position is translated to the format used in the Scene.
173  *
174  * @return The position.
175  */
176  static SbVec3f getSbVec3fFromInstance(const memoryx::ObjectInstancePtr& objectInstance);
177 
178  protected:
179  /**
180  * @see armarx::ManagedIceObject::onInitComponent()
181  *
182  */
183  void onInitComponent() override;
184 
185  /**
186  * @see armarx::ManagedIceObject::onConnectComponent()
187  *
188  */
189  void onConnectComponent() override;
190 
191  /**
192  * @brief Returns the default name of this managed ice object.
193  *
194  * @see armarx::ManagedIceObject::getDefaultName()
195  */
196  std::string getDefaultName() const override;
197 
198  /**
199  * @brief Recognises when an entity is created in the working memory
200  *
201  * @see armarx::ManagedIceObject::reportEntityCreated()
202  */
203  void reportEntityCreated(const std::string& segmentName,
204  const ::memoryx::EntityBasePtr& entity,
205  const ::Ice::Current& = Ice::emptyCurrent) override;
206  /**
207  * @brief Recognises when an entity is updated in the working memory
208  *
209  * @see armarx::ManagedIceObject::reportEntityUpdated()
210  */
211  void reportEntityUpdated(const std::string& segmentName,
212  const ::memoryx::EntityBasePtr& entityOld,
213  const ::memoryx::EntityBasePtr& entityNew,
214  const ::Ice::Current& = Ice::emptyCurrent) override;
215  /**
216  * @brief Recognises when an entity is removed from the working memory
217  *
218  * @see armarx::ManagedIceObject::reportEntityRemoved()
219  */
220  void reportEntityRemoved(const std::string& segmentName,
221  const ::memoryx::EntityBasePtr& entity,
222  const ::Ice::Current& = Ice::emptyCurrent) override;
223  /**
224  * @brief Recognises when a segment was loaded from a snapshot
225  *
226  * @see armarx::ManagedIceObject::reportSnapshotLoaded()
227  */
228  void reportSnapshotLoaded(const std::string& segmentName,
229  const ::Ice::Current& = Ice::emptyCurrent) override;
230 
231  void
232  reportSnapshotCompletelyLoaded(const Ice::Current& c = Ice::emptyCurrent) override
233  {
234  }
235 
236  /**
237  * @brief Recognises when an entity is created on the working memory
238  *
239  * @see armarx::ManagedIceObject::reportMemoryCleared()
240  */
241  void reportMemoryCleared(const std::string& segmentName,
242  const ::Ice::Current& = Ice::emptyCurrent) override;
243 
244  private:
245  mutable std::recursive_mutex mutexEntities;
246 
247  bool findAndRemoveInstanceFromList(std::list<memoryx::ObjectInstancePtr>& list,
248  const memoryx::ObjectInstancePtr& instance) const;
249 
250  memoryx::WorkingMemoryInterfacePrx workingMemoryPrx;
251  memoryx::ObjectInstanceMemorySegmentBasePrx objectInstancesPrx;
252  std::weak_ptr<memoryxcontroller::MemoryXController> memoryXController;
253 
254  std::string workingMemoryUpdatesTopic;
255  std::list<memoryx::ObjectInstancePtr> currentChangedInstances;
256  };
257 
259 } // namespace memoryxcontroller
memoryxcontroller::WorkingMemoryController::rotateTranslateObject
void rotateTranslateObject(const std::string &id, const SbRotation &newOrientation, const SbVec3f &newPosition)
Applies the given orientation and position to the object instance with the given id.
Definition: WorkingMemoryController.cpp:181
memoryxcontroller::WorkingMemoryController::onInitComponent
void onInitComponent() override
Definition: WorkingMemoryController.cpp:446
list
list(APPEND SOURCES ${QT_RESOURCES}) set(COMPONENT_LIBS ArmarXGui ArmarXCoreObservers ArmarXCoreEigen3Variants PlotterController $
Definition: CMakeLists.txt:49
memoryxcontroller::WorkingMemoryController::reportEntityUpdated
void reportEntityUpdated(const std::string &segmentName, const ::memoryx::EntityBasePtr &entityOld, const ::memoryx::EntityBasePtr &entityNew, const ::Ice::Current &=Ice::emptyCurrent) override
Recognises when an entity is updated in the working memory.
memoryxcontroller::WorkingMemoryController::getAllObjectInstances
std::vector< std::string > getAllObjectInstances() const
Returns all object ids that are currently in the working memory.
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:46
ObjectClass.h
IceInternal::Handle< ObjectClass >
memoryxcontroller::WorkingMemoryController::WorkingMemoryController
WorkingMemoryController(const memoryx::WorkingMemoryInterfacePrx &workingMemoryPrx, const std::string &workingMemoryUpdatesTopic, const std::string &objectInstancesSegmentName, const std::shared_ptr< MemoryXController > &memoryXController)
Constructor.
memoryxcontroller::WorkingMemoryController::onConnectComponent
void onConnectComponent() override
Definition: WorkingMemoryController.cpp:452
memoryxcontroller::WorkingMemoryController::saveSceneInSnapshot
bool saveSceneInSnapshot(const std::string &snapshotName, const memoryx::LongtermMemoryInterfacePrx &longtermMemoryPrx) const
Saves the whole scene in a snapshot with the given name.
Definition: WorkingMemoryController.cpp:505
memoryxcontroller::WorkingMemoryController
The controller for the communication with the working memory.
Definition: WorkingMemoryController.h:57
ManagedIceObject.h
memoryxcontroller
Definition: MemoryXController.h:43
memoryxcontroller::WorkingMemoryController::addObjectInstances
std::vector< std::string > addObjectInstances(const std::vector< memoryx::ObjectInstancePtr > &instancePtrs)
Adds all given object instances to the working memory.
Definition: WorkingMemoryController.cpp:91
memoryxcontroller::WorkingMemoryController::reportMemoryCleared
void reportMemoryCleared(const std::string &segmentName, const ::Ice::Current &=Ice::emptyCurrent) override
Recognises when an entity is created on the working memory.
memoryxcontroller::WorkingMemoryController::getSbVec3fFromInstance
static SbVec3f getSbVec3fFromInstance(const memoryx::ObjectInstancePtr &objectInstance)
Returns the position of a object instance.
Definition: WorkingMemoryController.cpp:472
memoryxcontroller::WorkingMemoryController::getSbRotationFromInstance
static SbRotation getSbRotationFromInstance(const memoryx::ObjectInstancePtr &objectInstance)
Returns the rotation of a object instance.
Definition: WorkingMemoryController.cpp:463
memoryxcontroller::WorkingMemoryController::reportSnapshotLoaded
void reportSnapshotLoaded(const std::string &segmentName, const ::Ice::Current &=Ice::emptyCurrent) override
Recognises when a segment was loaded from a snapshot.
memoryxcontroller::WorkingMemoryController::saveObjectsInSnapshot
bool saveObjectsInSnapshot(const std::string &snapshotName, const memoryx::LongtermMemoryInterfacePrx &longtermMemoryPrx, const std::vector< std::string > &objectIds) const
Saves the object instances with the given ids in a snapshot with the given name.
Definition: WorkingMemoryController.cpp:513
armarx::ManagedIceObject
The ManagedIceObject is the base class for all ArmarX objects.
Definition: ManagedIceObject.h:162
ObjectInstance.h
memoryxcontroller::WorkingMemoryController::addAllInstancesToLocalScene
void addAllInstancesToLocalScene() const
Adds all object instances of the working memory to the local scene.
Definition: WorkingMemoryController.cpp:218
memoryxcontroller::WorkingMemoryController::reportSnapshotCompletelyLoaded
void reportSnapshotCompletelyLoaded(const Ice::Current &c=Ice::emptyCurrent) override
Definition: WorkingMemoryController.h:232
memoryxcontroller::WorkingMemoryController::reportEntityRemoved
void reportEntityRemoved(const std::string &segmentName, const ::memoryx::EntityBasePtr &entity, const ::Ice::Current &=Ice::emptyCurrent) override
Recognises when an entity is removed from the working memory.
memoryxcontroller::WorkingMemoryController::getDefaultName
std::string getDefaultName() const override
Returns the default name of this managed ice object.
Definition: WorkingMemoryController.cpp:457
memoryxcontroller::WorkingMemoryController::addObjectInstance
std::string addObjectInstance(const std::string &objectName, const memoryx::ObjectClassPtr &objectClassPtr)
Adds a new object instance to the working memory.
Definition: WorkingMemoryController.cpp:66
MemoryXController.h
memoryxcontroller::WorkingMemoryController::reportEntityCreated
void reportEntityCreated(const std::string &segmentName, const ::memoryx::EntityBasePtr &entity, const ::Ice::Current &=Ice::emptyCurrent) override
Recognises when an entity is created in the working memory.
memoryxcontroller::WorkingMemoryController::~WorkingMemoryController
~WorkingMemoryController() override
Destructor.
Definition: WorkingMemoryController.cpp:61
memoryxcontroller::WorkingMemoryController::removeObjectInstance
void removeObjectInstance(const std::string &id)
Removes the object instance with the given id.
Definition: WorkingMemoryController.cpp:164