MemoryXController.cpp
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 #include <exception>
25 #include <string>
26 #include <vector>
27 
28 // MemoryXInterface
29 #include <SimoxUtility/algorithm/string/string_tools.h>
30 
32 
35 
36 #include "MemoryXController.h"
39 #include <MemoryX/interface/components/LongtermMemoryInterface.h>
40 #include <MemoryX/interface/components/WorkingMemoryInterface.h>
41 #include <MemoryX/interface/core/EntityBase.h>
43 
44 memoryxcontroller::MemoryXController::MemoryXController() : LONGTERM_SNAPSHOT_PREFIX("Snapshot_")
45 {
46 }
47 
49 {
50  if (this->workingMemoryController && this->workingMemoryController->getArmarXManager())
51  {
52  this->workingMemoryController->getArmarXManager()->removeObjectBlocking(
53  this->workingMemoryController);
54  }
55 }
56 
57 void
59  const memoryx::PriorKnowledgeInterfacePrx& priorKnowledgePrx,
60  const memoryx::WorkingMemoryInterfacePrx& workingMemoryPrx,
61  const std::string& workingMemoryUpdatesTopic,
62  const std::string& objectInstancesSegmentName,
63  const memoryx::LongtermMemoryInterfacePrx& longtermMemoryPrx,
64  const controller::ControllerPtr& mainController)
65 {
66  this->mainController = mainController;
67  priorKnowledgeController.reset(new PriorKnowledgeController(priorKnowledgePrx));
68  workingMemoryController = new WorkingMemoryController(workingMemoryPrx,
69  workingMemoryUpdatesTopic,
70  objectInstancesSegmentName,
71  mainController->getMemoryXController());
72  this->longtermMemoryPrx = longtermMemoryPrx;
73 }
74 
75 bool
77 {
78  if (snapshotName.empty())
79  {
80  return false;
81  }
82 
83  if (!simox::alg::starts_with(snapshotName, LONGTERM_SNAPSHOT_PREFIX))
84  {
85  snapshotName = std::string(LONGTERM_SNAPSHOT_PREFIX) + snapshotName;
86  }
87 
88  bool worked =
89  this->workingMemoryController->saveSceneInSnapshot(snapshotName, this->longtermMemoryPrx);
90 
91  return worked;
92 }
93 
94 bool
96  std::string& snapshotName,
97  const std::vector<std::string>& objectIds)
98 {
99  if (snapshotName.empty())
100  {
101  return false;
102  }
103 
104  if (!simox::alg::starts_with(snapshotName, LONGTERM_SNAPSHOT_PREFIX))
105  {
106  snapshotName = std::string(LONGTERM_SNAPSHOT_PREFIX) + snapshotName;
107  }
108 
109  bool worked = this->workingMemoryController->saveObjectsInSnapshot(
110  snapshotName, this->longtermMemoryPrx, objectIds);
111 
112  return worked;
113 }
114 
115 std::vector<std::string>
117 {
118  std::vector<std::string> addedIds;
119 
120  if (!snapshotName.empty())
121  {
122  try
123  {
124  memoryx::WorkingMemorySnapshotInterfacePrx snapshot =
125  longtermMemoryPrx->getWorkingMemorySnapshotListSegment()->openSnapshot(
126  snapshotName);
127  memoryx::PersistentEntitySegmentBasePrx segObjects =
128  snapshot->getSegment("objectInstances");
129 
130  std::vector<memoryx::ObjectInstancePtr> objectInstances;
131 
132  if (segObjects)
133  {
134  memoryx::EntityIdList ids = segObjects->getAllEntityIds();
135 
136  for (memoryx::EntityIdList::const_iterator it = ids.begin(); it != ids.end(); ++it)
137  {
138  memoryx::ObjectInstancePtr objectInstance =
139  memoryx::ObjectInstancePtr::dynamicCast(segObjects->getEntityById(*it));
140  objectInstances.push_back(objectInstance);
141  }
142  }
143 
144  addedIds = this->workingMemoryController->addObjectInstances(objectInstances);
145  }
146  catch (const armarx::LocalException& e)
147  {
148  std::cout << "SnapshotName could not be loaded: " << snapshotName << std::endl;
149  }
150  }
151 
152  return addedIds;
153 }
154 
157 {
158  return workingMemoryController;
159 }
160 
161 std::shared_ptr<memoryxcontroller::PriorKnowledgeController>
163 {
164  return priorKnowledgeController;
165 }
166 
169 {
170  return mainController.lock();
171 }
172 
173 std::vector<std::string>
175 {
176  return longtermMemoryPrx->getSnapshotNames();
177 }
WorkingMemoryController.h
memoryxcontroller::MemoryXController::getController
controller::ControllerPtr getController() const
Returns the main controller of this plugin.
Definition: MemoryXController.cpp:168
memoryxcontroller::MemoryXController::init
void init(const memoryx::PriorKnowledgeInterfacePrx &priorKnowledgePrx, const memoryx::WorkingMemoryInterfacePrx &workingMemoryPrx, const std::string &workingMemoryUpdatesTopic, const std::string &objectInstancesSegmentName, const memoryx::LongtermMemoryInterfacePrx &longtermMemoryPrx, const controller::ControllerPtr &mainController)
Initialisates the MemoryXController.
Definition: MemoryXController.cpp:58
ArmarXManager.h
memoryxcontroller::MemoryXController::getWorkingMemoryController
IceInternal::Handle< WorkingMemoryController > getWorkingMemoryController() const
Returns a shared pointer to the WorkingMemoryController.
Definition: MemoryXController.cpp:156
memoryxcontroller::MemoryXController::MemoryXController
MemoryXController()
Constructor.
Definition: MemoryXController.cpp:44
ObjectClass.h
armarx::starts_with
bool starts_with(const std::string &haystack, const std::string &needle)
Definition: StringHelpers.cpp:47
IceInternal::Handle< ObjectInstance >
memoryxcontroller::MemoryXController::getAllSnapshots
std::vector< std::string > getAllSnapshots()
Returns a list with the names of all existing snapshots.
Definition: MemoryXController.cpp:174
ArmarXGuiPlugin.h
ArmarXComponentWidgetController.h
memoryxcontroller::WorkingMemoryController
The controller for the communication with the working memory.
Definition: WorkingMemoryController.h:57
controller::ControllerPtr
std::shared_ptr< Controller > ControllerPtr
Definition: ClassDefinitions.h:41
memoryxcontroller::MemoryXController::getPriorKnowlegdeController
std::shared_ptr< PriorKnowledgeController > getPriorKnowlegdeController() const
Returns a shared pointer to the PriorKnowledgeController.
Definition: MemoryXController.cpp:162
memoryxcontroller::MemoryXController::saveSceneInSnapshot
bool saveSceneInSnapshot(std::string &snapshotName)
Saves the whole content of the working memory in a snapshot with the given name.
Definition: MemoryXController.cpp:76
memoryxcontroller::MemoryXController::~MemoryXController
~MemoryXController()
Destructor.
Definition: MemoryXController.cpp:48
memoryxcontroller::PriorKnowledgeController
The controller for the communication with the priorknowledge.
Definition: PriorKnowledgeController.h:54
memoryxcontroller::MemoryXController::saveObjectsInSnapshot
bool saveObjectsInSnapshot(std::string &snapshotName, const std::vector< std::string > &objectIds)
Saves all object instances whose ids are given in a snapshot with the given name.
Definition: MemoryXController.cpp:95
PriorKnowledgeController.h
memoryxcontroller::MemoryXController::loadSnapshot
std::vector< std::string > loadSnapshot(const std::string &snapshotName)
Loads the content of the snapshot with the given name and add it to the current working memory.
Definition: MemoryXController.cpp:116
MemoryXController.h