Main Page · Modules · All Classes · Class Hierarchy
MACollectedData.cpp
1  /*
2  * This file is part of the AiBO+ project
3  *
4  * Copyright (C) 2005-2016 Csaba Kertész (csaba.kertesz@gmail.com)
5  *
6  * AiBO+ is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * AiBO+ is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
19  *
20  */
21 
22 #include "MACollectedData.hpp"
23 
24 #include <MCBinaryData.hpp>
25 #include <MCDataContainer.hpp>
26 #include <MCLog.hpp>
27 
28 MACollectedData::MACollectedData(const int session_id, const std::string& name, const int id,
29  const std::string& variable_map) :
30  DataStorage(new MCDataStorage(name, true))
31 {
32  MC::DataContainerSPtr Container = DataStorage->CreateContainer("UserData");
33 
34  Container->AddData(*new MCDataItem<std::string>(variable_map, "RobotStateVariableMap"));
35  Container->AddData(*new MCDataItem<int>(id, "CollectionID"));
36  Container->AddData(*new MCDataItem<int>(session_id, "SessionID"));
37 }
38 
39 
40 std::string MACollectedData::GetName() const
41 {
42  return DataStorage->GetName();
43 }
44 
45 
47 {
48  MC::DataContainerSPtr Container = DataStorage->GetContainer("UserData");
49 
50  return *reinterpret_cast<int*>(Container->GetData("CollectionID")->DataPtr);
51 }
52 
53 
55 {
56  MC::DataContainerSPtr Container = DataStorage->GetContainer("UserData");
57 
58  return *reinterpret_cast<int*>(Container->GetData("SessionID")->DataPtr);
59 }
60 
61 
62 void MACollectedData::SetCustomData(const std::string& data_name, const MCBinaryData& data_value)
63 {
64  if (data_name.empty())
65  return;
66 
67  MC::DataContainerSPtr Container = DataStorage->GetContainer("UserData");
68 
69  Container->AddData(*new MCDataItem<MCBinaryData>(data_value, data_name));
70 }
71 
72 
73 MCBinaryData* MACollectedData::GetCustomData(const std::string& data_name)
74 {
75  MC::DataContainerSPtr Container = DataStorage->GetContainer("UserData");
76  MCDataItemBase* DataItem = Container->GetData(data_name);
77 
78  if (DataItem && MCGetClassName<MCBinaryData>() == DataItem->TypeName)
79  {
80  MCBinaryData* BinaryData = new MCBinaryData;
81 
82  *BinaryData = *reinterpret_cast<MCBinaryData*>(DataItem->DataPtr);
83  return BinaryData;
84  }
85  return nullptr;
86 }
87 
88 
90 {
91  return DataStorage->IsCompact();
92 }
93 
94 
96 {
97  DataStorage->OptimizeCachedContainers();
98 }
99 
100 
102 {
103  MC::StringList ContainerNames = DataStorage->GetContainerNames();
104 
105  for (unsigned int i = 0; i < ContainerNames.size(); ++i)
106  {
107  MC_LOG("%s: %d items", ContainerNames[i].c_str(),
108  DataStorage->GetContainer(ContainerNames[i])->GetAllData().size());
109  }
110 }
111 
112 
114 {
115  MC::DataContainerSPtr Container = DataStorage->GetContainer("UserData");
116 
117  return *reinterpret_cast<std::string*>(Container->GetData("RobotStateVariableMap")->DataPtr);
118 }
119 
120 
121 void MACollectedData::AddNewData(const std::string& name, int cycle_id, const MCBinaryData& data)
122 {
123  if (name == "UserData")
124  {
125  MC_WARNING("UserData container cannot store collected data.");
126  return;
127  }
128  MC::DataContainerSPtr DataContainer = DataStorage->GetContainer(name);
129 
130  if (!DataContainer.get())
131  {
132  DataContainer = DataStorage->CreateContainer(name);
133  }
134  // The data is cloned to avoid any resource handling problem
135  DataContainer->AddData(*new MCDataItem<MCBinaryData>(data, MCToStr(cycle_id)));
136 }
137 
138 
139 MA::BinaryDataPtrList MACollectedData::GetAllDataByTypeName(const std::string& type_name)
140 {
141  MC::DataContainerSPtr DataContainer = DataStorage->GetContainer(type_name);
142 
143  if (!DataContainer.get())
144  {
145  MC_WARNING("Data type is not collected (%s)\n", type_name.c_str());
146  return MA::BinaryDataPtrList();
147  }
148  MA::BinaryDataPtrList CollectedData;
149  const MC::DataItemBasePtrList& DataItems = DataContainer->GetAllData();
150 
151  for (auto& item : DataItems)
152  {
153  if (item->TypeName == "MCBinaryData")
154  {
155  CollectedData.push_back(reinterpret_cast<MCBinaryData*>(item->DataPtr));
156  }
157  }
158  return CollectedData;
159 }
160 
161 
163 {
164  return DataStorage->Encode();
165 }
166 
167 
169 {
171 
172  if (DataStorage)
173  {
174  MACollectedData* CollectedData = new MACollectedData(0, "", 0, "");
175 
176  CollectedData->DataStorage.reset(DataStorage);
177  return CollectedData;
178  }
179  return nullptr;
180 }
MA::BinaryDataPtrList GetAllDataByTypeName(const std::string &type_name)
Get all data by type name.
Collected data with portable storage functions.
A helper class to fill the type info to the base structure.
std::string GetName() const
Get name.
Binary data class.
void MakeCompact()
Compress the collected data if it has not been done.
#define MC_WARNING(...)
Warning macro.
Definition: MCLog.hpp:43
Data container item base structure to store basic type info.
int GetCollectionID()
Get collection ID.
std::string MCToStr(const T value, bool hex_manipulator=false)
Convert an other type to string with std::stringstream.
Definition: MCDefs.hpp:360
static MCDataStorage * Decode(const MCBinaryData &data, bool portable=false)
Load the data storage from binary form.
void PrintSummary()
Print summary of the collected data.
MCBinaryData * GetCustomData(const std::string &data_name)
Get custom data.
static MACollectedData * Decode(const MCBinaryData &data)
Decode from binary data.
bool IsCompact() const
Check if the collected data is compressed.
MCBinaryData * Encode() const
Encode into binary data.
const std::string & GetRobotStateVariableMap() const
Get robot state variable map.
Data storage with file support.
void AddNewData(const std::string &name, int cycle_id, const MCBinaryData &data)
Add new data to the current cycle container.
boost::scoped_ptr< MCDataStorage > DataStorage
Data storage of all collected data.
MACollectedData(const int session_id, const std::string &name, const int id, const std::string &variable_map)
Class constructor.
int GetSessionID()
Get session ID.
#define MC_LOG(...)
Debug macro.
Definition: MCLog.hpp:41
void SetCustomData(const std::string &data_name, const MCBinaryData &data_value)
Add custom data.