prediction_helpers.h
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 * @author phesch ( ulila at student dot kit dot edu )
17 * @date 2022
18 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
19 * GNU General Public License
20 */
21
22#pragma once
23
24#include <functional>
25#include <vector>
26
28
32
33namespace armarx::armem
34{
35 /**
36 * Holds info on snapshot data extracted from a time range.
37 * Used for performing predictions.
38 */
39 template <typename DataType, typename LatestType>
41 {
42 bool success = false;
43 std::string errorMessage = "";
44
45 std::vector<double> timestampsSec = {};
46 std::vector<DataType> values = {};
47 LatestType latestValue;
48 };
49
50 /*!
51 * @brief Get data points for the snapshots of an entity in a given time range.
52 *
53 * The resulting `SnapshotRangeInfo` is useful for performing predictions.
54 * Data from the individual snapshots is given to the conversion functions
55 * before being stored in the result.
56 * Timestamps in the `timestampsSec` vector are relative to the endTime parameter
57 * to this function.
58 * Data from a snapshot is always retrieved from the instance with index 0.
59 * If some data cannot be retrieved, `success` is set to `false`
60 * and the `errorMessage` in the return value is set accordingly.
61 *
62 * @param segment the segment to get the snapshot data from
63 * @param entityID the entity to get the data for
64 * @param startTime the beginning of the time range
65 * @param endTime the end of the time range. Timestamps are relative to this value
66 * @param dictToData the conversion function from `DictPtr` to `DataType`
67 * @param dictToLatest the conversion function from `DictPtr` to `LatestType`
68 * @return the corresponding `LatestSnapshotInfo`
69 */
70 template <typename SegmentType, typename DataType, typename LatestType>
72 getSnapshotsInRange(const SegmentType* segment,
73 const MemoryID& entityID,
74 const DateTime& startTime,
75 const DateTime& endTime,
76 std::function<DataType(const aron::data::DictPtr&)> dictToData,
77 std::function<LatestType(const aron::data::DictPtr&)> dictToLatest)
78 {
80 result.success = false;
81
82 const server::wm::Entity* entity = segment->findEntity(entityID);
83 if (entity == nullptr)
84 {
85 std::stringstream sstream;
86 sstream << "Could not find entity with ID " << entityID << ".";
87 result.errorMessage = sstream.str();
88 return result;
89 }
90
91 const int instanceIndex = 0;
92 bool querySuccess = true;
94 startTime,
95 endTime,
96 [&](const wm::EntitySnapshot& snapshot)
97 {
98 const auto* instance = snapshot.findInstance(instanceIndex);
99 if (instance)
100 {
101 result.timestampsSec.push_back(
102 (instance->id().timestamp - endTime).toSecondsDouble());
103 result.values.emplace_back(dictToData(instance->data()));
104 }
105 else
106 {
107 std::stringstream sstream;
108 sstream << "Could not find instance with index " << instanceIndex
109 << " in snapshot " << snapshot.id() << ".";
110 result.errorMessage = sstream.str();
111 querySuccess = false;
112 }
113 });
114
115 if (querySuccess)
116 {
117 aron::data::DictPtr latest = entity->findLatestInstanceData(instanceIndex);
118 if (latest)
119 {
120 result.success = true;
121 result.latestValue = dictToLatest(latest);
122 }
123 else
124 {
125 std::stringstream sstream;
126 sstream << "Could not find instance with index " << instanceIndex << " for entity "
127 << entity->id() << ".";
128 result.errorMessage = sstream.str();
129 }
130 }
131
132 return result;
133 }
134
135} // namespace armarx::armem
void forEachSnapshotInTimeRange(const Time &min, const Time &max, FunctionT &&func) const
Return all snapshots between, including, min and max.
Definition EntityBase.h:519
EntityInstanceT * findInstance(int index)
Represents a point in time.
Definition DateTime.h:25
armem::wm::EntitySnapshot EntitySnapshot
SnapshotRangeInfo< DataType, LatestType > getSnapshotsInRange(const SegmentType *segment, const MemoryID &entityID, const DateTime &startTime, const DateTime &endTime, std::function< DataType(const aron::data::DictPtr &)> dictToData, std::function< LatestType(const aron::data::DictPtr &)> dictToLatest)
Get data points for the snapshots of an entity in a given time range.
std::shared_ptr< Dict > DictPtr
Definition Dict.h:42
Holds info on snapshot data extracted from a time range.
aron::data::DictPtr findLatestInstanceData(int instanceIndex=0) const