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