CommonStorageExample.cpp
Go to the documentation of this file.
1 /*
2 * This file is part of ArmarX.
3 *
4 * ArmarX is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ArmarX is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * @package CommonStorageTest::
17 * @author ( at kit dot edu)
18 * @date
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22 
23 #include "CommonStorageExample.h"
24 
25 #include <ArmarXCore/interface/core/Log.h>
26 
27 namespace memoryx
28 {
29  void
31  {
32  std::cout << "onInitComponent" << std::endl;
33 
34  usingProxy("CommonStorage");
35 
36  // onInitComponent has to do the following tasks
37  // 1) Make sure the component is in a consistent state (initialize all members).
38  // After onInitComponent, the Ice object adapter is activated and the component
39  // is accessible via Ice. Therefore, the component needs to be in a sane state.
40  // 2) Make Ice dependencies explicit
41  // All proxies and topics used by this component need to be made explicit. Further
42  // all topics that are provided by the component need to be made explicit.
43  // Use the following calls:
44  // usingProxy("proxyName"): this component used the proxy "proxyName"
45  // usingTopic("topciName"): this component subscribes to the topic "topicName"
46  // offeringTopic("topciName"): this component provides the topic "topicName"
47  // The initialization procedure assures that all required proxies and topics are
48  // available before the component is started.
49  }
50 
51  void
53  {
54  std::cout << "onConnectComponent" << std::endl;
55 
56  ARMARX_INFO << "Starting CommonStorageTest";
57 
58  dataBasePrx = getProxy<CommonStorageInterfacePrx>("CommonStorage");
59 
60  CollectionInterfacePrx actionCollectionPrx =
61  dataBasePrx->requestCollection("workmem.actions");
62 
63  // INSERT
64  DBStorableData action1;
65  action1.JSON =
66  "{\"name\":\"put\", params: ['what', 'where'], prereq: [{pred: 'isIn', params: ['what', 'hand'] }], \
67  effects: [{pred: 'isOn', params: ['what', 'where'] }] }";
68  const std::string action1ID = actionCollectionPrx->insert(action1);
69 
70  ARMARX_INFO << "Inserted action1 with ID = " << action1ID;
71 
72  DBStorableData action2;
73  action2.JSON = "{name: 'take', params: ['what'], prereq: [], effects: [{pred: 'isIn', "
74  "params: ['what', 'hand'] }] }";
75  actionCollectionPrx->save(action2);
76 
77  // UPDATE
78  action2.JSON = "{\"name\": \"take\", params: [], prereq: [], effects: [] }";
79  actionCollectionPrx->saveWithUserKey(action2, "name");
80 
81  // FIND
82  try
83  {
84  // DBStorableData action1found = actionCollectionPrx->findByMongoId("11");
85  // ARMARX_INFO << "Found action by id: " << action1found.JSON;
86  }
87  catch (InvalidMongoIdException& e)
88  {
89  ARMARX_ERROR << "Failed to fetch action by id: " << e.reason;
90  }
91 
92  const std::string findQuery = "{name: 'take'}";
93  DBStorableDataList actions = actionCollectionPrx->findByQuery(findQuery);
94 
95  for (DBStorableDataList::const_iterator it = actions.begin(); it != actions.end(); ++it)
96  {
97  ARMARX_INFO << "Found action by name: " << it->JSON;
98  }
99 
100  // REMOVE
101  // ARMARX_INFO << "Removing action with ID = " << action1ID;
102  // actionCollectionPrx->removeByMongoId(action1ID);
103 
104  const std::string remQuery = "{name: 'put'}";
105  ARMARX_INFO << "Removing action by query: " << remQuery;
106  actionCollectionPrx->removeByQuery(remQuery);
107 
108  dataBasePrx->releaseCollection(actionCollectionPrx);
109 
110  GridFileInterfacePrx filePrx = dataBasePrx->getFileProxyByName("workmem", "Cup.iv");
111 
112  if (filePrx)
113  {
114  ARMARX_INFO << "Got file of size: " << filePrx->getFileSize();
115  dataBasePrx->releaseFileProxy(filePrx);
116  }
117  }
118 } // namespace memoryx
memoryx::CommonStorageExample::onInitComponent
void onInitComponent() override
Pure virtual hook for the subclass.
Definition: CommonStorageExample.cpp:30
memoryx
VirtualRobot headers.
Definition: CommonPlacesTester.cpp:48
CommonStorageExample.h
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:196
memoryx::CommonStorageExample::onConnectComponent
void onConnectComponent() override
Pure virtual hook for the subclass.
Definition: CommonStorageExample.cpp:52
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:181
armarx::ManagedIceObject::usingProxy
bool usingProxy(const std::string &name, const std::string &endpoints="")
Registers a proxy for retrieval after initialization and adds it to the dependency list.
Definition: ManagedIceObject.cpp:154