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 <ArmarXCore/interface/core/Log.h>
24 
25 #include "CommonStorageExample.h"
26 
27 namespace memoryx
28 {
30  {
31  std::cout << "onInitComponent" << std::endl;
32 
33  usingProxy("CommonStorage");
34 
35  // onInitComponent has to do the following tasks
36  // 1) Make sure the component is in a consistent state (initialize all members).
37  // After onInitComponent, the Ice object adapter is activated and the component
38  // is accessible via Ice. Therefore, the component needs to be in a sane state.
39  // 2) Make Ice dependencies explicit
40  // All proxies and topics used by this component need to be made explicit. Further
41  // all topics that are provided by the component need to be made explicit.
42  // Use the following calls:
43  // usingProxy("proxyName"): this component used the proxy "proxyName"
44  // usingTopic("topciName"): this component subscribes to the topic "topicName"
45  // offeringTopic("topciName"): this component provides the topic "topicName"
46  // The initialization procedure assures that all required proxies and topics are
47  // available before the component is started.
48 
49  }
50 
51 
53  {
54  std::cout << "onConnectComponent" << std::endl;
55 
56  ARMARX_INFO << "Starting CommonStorageTest";
57 
58  dataBasePrx = getProxy<CommonStorageInterfacePrx>("CommonStorage");
59 
60  CollectionInterfacePrx actionCollectionPrx = dataBasePrx->requestCollection("workmem.actions");
61 
62  // INSERT
63  DBStorableData action1;
64  action1.JSON = "{\"name\":\"put\", params: ['what', 'where'], prereq: [{pred: 'isIn', params: ['what', 'hand'] }], \
65  effects: [{pred: 'isOn', params: ['what', 'where'] }] }";
66  const std::string action1ID = actionCollectionPrx->insert(action1);
67 
68  ARMARX_INFO << "Inserted action1 with ID = " << action1ID;
69 
70  DBStorableData action2;
71  action2.JSON = "{name: 'take', params: ['what'], prereq: [], effects: [{pred: 'isIn', params: ['what', 'hand'] }] }";
72  actionCollectionPrx->save(action2);
73 
74  // UPDATE
75  action2.JSON = "{\"name\": \"take\", params: [], prereq: [], effects: [] }";
76  actionCollectionPrx->saveWithUserKey(action2, "name");
77 
78  // FIND
79  try
80  {
81  // DBStorableData action1found = actionCollectionPrx->findByMongoId("11");
82  // ARMARX_INFO << "Found action by id: " << action1found.JSON;
83  }
84  catch (InvalidMongoIdException& e)
85  {
86  ARMARX_ERROR << "Failed to fetch action by id: " << e.reason;
87  }
88 
89  const std::string findQuery = "{name: 'take'}";
90  DBStorableDataList actions = actionCollectionPrx->findByQuery(findQuery);
91 
92  for (DBStorableDataList::const_iterator it = actions.begin(); it != actions.end(); ++it)
93  {
94  ARMARX_INFO << "Found action by name: " << it->JSON;
95  }
96 
97  // REMOVE
98  // ARMARX_INFO << "Removing action with ID = " << action1ID;
99  // actionCollectionPrx->removeByMongoId(action1ID);
100 
101  const std::string remQuery = "{name: 'put'}";
102  ARMARX_INFO << "Removing action by query: " << remQuery;
103  actionCollectionPrx->removeByQuery(remQuery);
104 
105  dataBasePrx->releaseCollection(actionCollectionPrx);
106 
107  GridFileInterfacePrx filePrx = dataBasePrx->getFileProxyByName("workmem", "Cup.iv");
108 
109  if (filePrx)
110  {
111  ARMARX_INFO << "Got file of size: " << filePrx->getFileSize();
112  dataBasePrx->releaseFileProxy(filePrx);
113  }
114 
115  }
116 }
memoryx::CommonStorageExample::onInitComponent
void onInitComponent() override
Pure virtual hook for the subclass.
Definition: CommonStorageExample.cpp:29
memoryx
VirtualRobot headers.
Definition: CommonPlacesTester.cpp:48
CommonStorageExample.h
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:189
memoryx::CommonStorageExample::onConnectComponent
void onConnectComponent() override
Pure virtual hook for the subclass.
Definition: CommonStorageExample.cpp:52
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
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:151