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/SbVec3f.h>
32 #include <Inventor/SbRotation.h>
33 
34 // MemoryXInterface
35 #include <MemoryX/interface/components/WorkingMemoryInterface.h>
36 #include <MemoryX/interface/memorytypes/MemorySegments.h>
39 
40 // Core
42 
43 #include "MemoryXController.h"
44 
45 #include <mutex>
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  /**
64  * @brief Constructor.
65  *
66  * Creates a new instance of this class and registers this managed ice object.
67  *
68  * @param workingMemoryPrx the proxy to the used working memory
69  * @param workingMemoryUpdatesTopic the name of the topic this ice object will subscribe to recieve events from the working memory
70  * @param objectInstancesSegmentName the name of the segment in the data base where the object instances are stored
71  * @param memoryXController the MemoryXController
72  *
73  * @see memoryxcontroller::MemoryXController
74  */
75  WorkingMemoryController(const memoryx::WorkingMemoryInterfacePrx& workingMemoryPrx,
76  const std::string& workingMemoryUpdatesTopic,
77  const std::string& objectInstancesSegmentName,
78  const std::shared_ptr<MemoryXController>& memoryXController);
79  /**
80  * @brief Destructor.
81  *
82  * Destructs this instance of this class.
83  */
84  ~WorkingMemoryController() override;
85  /**
86  * @brief Adds a new object instance to the working memory.
87  *
88  * Returns the id of the new object instance.
89  *
90  * @param objectName the name of the added object instance
91  * @param objectClassPtr a pointer to the object class of the new object instance
92  * @return std::string the unique id of this object instance that is set by the working memory
93  */
94  std::string addObjectInstance(const std::string& objectName, 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> addObjectInstances(const std::vector<memoryx::ObjectInstancePtr>& instancePtrs);
104  /**
105  * @brief Removes the object instance with the given id.
106  *
107  * @throws Exception when there is no object instance with the given id
108  * @param id the id of the object instance that should be removed from the working memory
109  */
110  void removeObjectInstance(const std::string& id);
111  /**
112  * @brief Applies the given orientation and position to the object instance with the given id.
113  *
114  * Important: The values of the position vector will be multiplied by 1000 because the working memory uses another scale
115  *
116  * @param id the id of the object that gets the new orientation and position
117  * @param newOrientation the new orientation
118  * @param newPosition the new position
119  */
120  void rotateTranslateObject(const std::string& id, const SbRotation& newOrientation, const SbVec3f& newPosition);
121  /**
122  * @brief Returns all object ids that are currently in the working memory.
123  *
124  * @return std::vector<std::string> all object ids of the objects in the working memory
125  */
126  std::vector<std::string> getAllObjectInstances() const;
127 
128  /**
129  * @brief Adds all object instances of the working memory to the local scene.
130  *
131  */
132  void addAllInstancesToLocalScene() const;
133 
134  /**
135  * @brief Saves the whole scene in a snapshot with the given name.
136  *
137  * Retruns true if the saving was sucessful.
138  *
139  * @param snapshotName the name of the snapshot where all object instances of the working memory are saved
140  * @param longtermMemoryPrx a proxy for the longterm-memory where the snapshot will be stored
141  * @return bool returns true if the saving was succesful
142  */
143  bool saveSceneInSnapshot(const std::string& snapshotName, const memoryx::LongtermMemoryInterfacePrx& longtermMemoryPrx) const;
144  /**
145  * @brief Saves the object instances with the given ids in a snapshot with the given name.
146  *
147  * Retruns true if the saving was sucessful.
148  *
149  * @param snapshotName the name of the snapshot where the object instances are saved
150  * @param longtermMemoryPrx a proxy for the longterm-memory where the snapshot will be stored
151  * @param objectIds the list of ids
152  * @return bool returns true if the saving was succesful
153  */
154  bool saveObjectsInSnapshot(const std::string& snapshotName, const memoryx::LongtermMemoryInterfacePrx& longtermMemoryPrx, const std::vector<std::string>& objectIds) const;
155 
156  /**
157  * Returns the rotation of a object instance. The rotation is translated to the format used in the Scene.
158  *
159  * @return The rotation.
160  */
161  static SbRotation getSbRotationFromInstance(const memoryx::ObjectInstancePtr& objectInstance);
162 
163  /**
164  * Returns the position of a object instance. The position is translated to the format used in the Scene.
165  *
166  * @return The position.
167  */
168  static SbVec3f getSbVec3fFromInstance(const memoryx::ObjectInstancePtr& objectInstance);
169 
170  protected:
171  /**
172  * @see armarx::ManagedIceObject::onInitComponent()
173  *
174  */
175  void onInitComponent() override;
176 
177  /**
178  * @see armarx::ManagedIceObject::onConnectComponent()
179  *
180  */
181  void onConnectComponent() override;
182 
183  /**
184  * @brief Returns the default name of this managed ice object.
185  *
186  * @see armarx::ManagedIceObject::getDefaultName()
187  */
188  std::string getDefaultName() const override;
189 
190  /**
191  * @brief Recognises when an entity is created in the working memory
192  *
193  * @see armarx::ManagedIceObject::reportEntityCreated()
194  */
195  void reportEntityCreated(const std::string& segmentName, const ::memoryx::EntityBasePtr& entity, const ::Ice::Current& = Ice::emptyCurrent) override;
196  /**
197  * @brief Recognises when an entity is updated in the working memory
198  *
199  * @see armarx::ManagedIceObject::reportEntityUpdated()
200  */
201  void reportEntityUpdated(const std::string& segmentName, const ::memoryx::EntityBasePtr& entityOld, const ::memoryx::EntityBasePtr& entityNew, const ::Ice::Current& = Ice::emptyCurrent) override;
202  /**
203  * @brief Recognises when an entity is removed from the working memory
204  *
205  * @see armarx::ManagedIceObject::reportEntityRemoved()
206  */
207  void reportEntityRemoved(const std::string& segmentName, const ::memoryx::EntityBasePtr& entity, const ::Ice::Current& = Ice::emptyCurrent) override;
208  /**
209  * @brief Recognises when a segment was loaded from a snapshot
210  *
211  * @see armarx::ManagedIceObject::reportSnapshotLoaded()
212  */
213  void reportSnapshotLoaded(const std::string& segmentName, const ::Ice::Current& = Ice::emptyCurrent) override;
214  void reportSnapshotCompletelyLoaded(const Ice::Current& c = Ice::emptyCurrent) override { }
215 
216  /**
217  * @brief Recognises when an entity is created on the working memory
218  *
219  * @see armarx::ManagedIceObject::reportMemoryCleared()
220  */
221  void reportMemoryCleared(const std::string& segmentName, const ::Ice::Current& = Ice::emptyCurrent) override;
222 
223  private:
224  mutable std::recursive_mutex mutexEntities;
225 
226  bool findAndRemoveInstanceFromList(std::list<memoryx::ObjectInstancePtr>& list, const memoryx::ObjectInstancePtr& instance) const;
227 
228  memoryx::WorkingMemoryInterfacePrx workingMemoryPrx;
229  memoryx::ObjectInstanceMemorySegmentBasePrx objectInstancesPrx;
230  std::weak_ptr<memoryxcontroller::MemoryXController> memoryXController;
231 
232  std::string workingMemoryUpdatesTopic;
233  std::list<memoryx::ObjectInstancePtr> currentChangedInstances;
234  };
236 }
237 
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:157
memoryxcontroller::WorkingMemoryController::onInitComponent
void onInitComponent() override
Definition: WorkingMemoryController.cpp:384
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:43
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:389
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:432
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:83
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:407
memoryxcontroller::WorkingMemoryController::getSbRotationFromInstance
static SbRotation getSbRotationFromInstance(const memoryx::ObjectInstancePtr &objectInstance)
Returns the rotation of a object instance.
Definition: WorkingMemoryController.cpp:400
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:437
armarx::ManagedIceObject
The ManagedIceObject is the base class for all ArmarX objects.
Definition: ManagedIceObject.h:163
ObjectInstance.h
memoryxcontroller::WorkingMemoryController::addAllInstancesToLocalScene
void addAllInstancesToLocalScene() const
Adds all object instances of the working memory to the local scene.
Definition: WorkingMemoryController.cpp:190
memoryxcontroller::WorkingMemoryController::reportSnapshotCompletelyLoaded
void reportSnapshotCompletelyLoaded(const Ice::Current &c=Ice::emptyCurrent) override
Definition: WorkingMemoryController.h:214
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:394
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:62
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:58
memoryxcontroller::WorkingMemoryController::removeObjectInstance
void removeObjectInstance(const std::string &id)
Removes the object instance with the given id.
Definition: WorkingMemoryController.cpp:142