@@ -1,65 +1,66 | |||
|
1 | 1 | #ifndef SCIQLOP_VARIABLE_H |
|
2 | 2 | #define SCIQLOP_VARIABLE_H |
|
3 | 3 | |
|
4 | 4 | #include "CoreGlobal.h" |
|
5 | 5 | |
|
6 | 6 | #include <Data/SqpRange.h> |
|
7 | 7 | |
|
8 | 8 | #include <QLoggingCategory> |
|
9 | 9 | #include <QObject> |
|
10 | 10 | |
|
11 | 11 | #include <Common/MetaTypes.h> |
|
12 | 12 | #include <Common/spimpl.h> |
|
13 | 13 | |
|
14 | 14 | Q_DECLARE_LOGGING_CATEGORY(LOG_Variable) |
|
15 | 15 | |
|
16 | 16 | class IDataSeries; |
|
17 | 17 | class QString; |
|
18 | 18 | |
|
19 | 19 | /** |
|
20 | 20 | * @brief The Variable class represents a variable in SciQlop. |
|
21 | 21 | */ |
|
22 | 22 | class SCIQLOP_CORE_EXPORT Variable : public QObject { |
|
23 | 23 | |
|
24 | 24 | Q_OBJECT |
|
25 | 25 | |
|
26 | 26 | public: |
|
27 | 27 | explicit Variable(const QString &name, const SqpRange &dateTime, |
|
28 | 28 | const QVariantHash &metadata = {}); |
|
29 | 29 | |
|
30 | 30 | QString name() const noexcept; |
|
31 | 31 | SqpRange range() const noexcept; |
|
32 | 32 | void setRange(const SqpRange &range) noexcept; |
|
33 | 33 | SqpRange cacheRange() const noexcept; |
|
34 | 34 | void setCacheRange(const SqpRange &cacheRange) noexcept; |
|
35 | 35 | |
|
36 | 36 | /// @return the data of the variable, nullptr if there is no data |
|
37 | 37 | std::shared_ptr<IDataSeries> dataSeries() const noexcept; |
|
38 | 38 | |
|
39 | 39 | QVariantHash metadata() const noexcept; |
|
40 | 40 | |
|
41 | 41 | bool contains(const SqpRange &range) const noexcept; |
|
42 | 42 | bool intersect(const SqpRange &range) const noexcept; |
|
43 | 43 | bool isInside(const SqpRange &range) const noexcept; |
|
44 | 44 | |
|
45 | 45 | bool cacheContains(const SqpRange &range) const noexcept; |
|
46 | 46 | bool cacheIntersect(const SqpRange &range) const noexcept; |
|
47 | 47 | bool cacheIsInside(const SqpRange &range) const noexcept; |
|
48 | 48 | |
|
49 | 49 | QVector<SqpRange> provideNotInCacheRangeList(const SqpRange &range) const noexcept; |
|
50 | QVector<SqpRange> provideInCacheRangeList(const SqpRange &range) const noexcept; | |
|
50 | 51 | void setDataSeries(std::shared_ptr<IDataSeries> dataSeries) noexcept; |
|
51 | 52 | void mergeDataSeries(std::shared_ptr<IDataSeries> dataSeries) noexcept; |
|
52 | 53 | |
|
53 | 54 | signals: |
|
54 | 55 | void updated(); |
|
55 | 56 | |
|
56 | 57 | private: |
|
57 | 58 | class VariablePrivate; |
|
58 | 59 | spimpl::unique_impl_ptr<VariablePrivate> impl; |
|
59 | 60 | }; |
|
60 | 61 | |
|
61 | 62 | // Required for using shared_ptr in signals/slots |
|
62 | 63 | SCIQLOP_REGISTER_META_TYPE(VARIABLE_PTR_REGISTRY, std::shared_ptr<Variable>) |
|
63 | 64 | SCIQLOP_REGISTER_META_TYPE(VARIABLE_PTR_VECTOR_REGISTRY, QVector<std::shared_ptr<Variable> >) |
|
64 | 65 | |
|
65 | 66 | #endif // SCIQLOP_VARIABLE_H |
@@ -1,211 +1,243 | |||
|
1 | 1 | #include "Variable/Variable.h" |
|
2 | 2 | |
|
3 | 3 | #include <Data/IDataSeries.h> |
|
4 | 4 | #include <Data/SqpRange.h> |
|
5 | 5 | |
|
6 | 6 | #include <QMutex> |
|
7 | 7 | #include <QReadWriteLock> |
|
8 | 8 | #include <QThread> |
|
9 | 9 | |
|
10 | 10 | Q_LOGGING_CATEGORY(LOG_Variable, "Variable") |
|
11 | 11 | |
|
12 | 12 | struct Variable::VariablePrivate { |
|
13 | 13 | explicit VariablePrivate(const QString &name, const SqpRange &dateTime, |
|
14 | 14 | const QVariantHash &metadata) |
|
15 | 15 | : m_Name{name}, m_Range{dateTime}, m_Metadata{metadata}, m_DataSeries{nullptr} |
|
16 | 16 | { |
|
17 | 17 | } |
|
18 | 18 | |
|
19 | 19 | void lockRead() { m_Lock.lockForRead(); } |
|
20 | 20 | void lockWrite() { m_Lock.lockForWrite(); } |
|
21 | 21 | void unlock() { m_Lock.unlock(); } |
|
22 | 22 | |
|
23 | 23 | QString m_Name; |
|
24 | 24 | |
|
25 | 25 | SqpRange m_Range; |
|
26 | 26 | SqpRange m_CacheRange; |
|
27 | 27 | QVariantHash m_Metadata; |
|
28 | 28 | std::shared_ptr<IDataSeries> m_DataSeries; |
|
29 | 29 | |
|
30 | 30 | QReadWriteLock m_Lock; |
|
31 | 31 | }; |
|
32 | 32 | |
|
33 | 33 | Variable::Variable(const QString &name, const SqpRange &dateTime, const QVariantHash &metadata) |
|
34 | 34 | : impl{spimpl::make_unique_impl<VariablePrivate>(name, dateTime, metadata)} |
|
35 | 35 | { |
|
36 | 36 | } |
|
37 | 37 | |
|
38 | 38 | QString Variable::name() const noexcept |
|
39 | 39 | { |
|
40 | 40 | impl->lockRead(); |
|
41 | 41 | auto name = impl->m_Name; |
|
42 | 42 | impl->unlock(); |
|
43 | 43 | return name; |
|
44 | 44 | } |
|
45 | 45 | |
|
46 | 46 | SqpRange Variable::range() const noexcept |
|
47 | 47 | { |
|
48 | 48 | impl->lockRead(); |
|
49 | 49 | auto range = impl->m_Range; |
|
50 | 50 | impl->unlock(); |
|
51 | 51 | return range; |
|
52 | 52 | } |
|
53 | 53 | |
|
54 | 54 | void Variable::setRange(const SqpRange &range) noexcept |
|
55 | 55 | { |
|
56 | 56 | impl->lockWrite(); |
|
57 | 57 | impl->m_Range = range; |
|
58 | 58 | impl->unlock(); |
|
59 | 59 | } |
|
60 | 60 | |
|
61 | 61 | SqpRange Variable::cacheRange() const noexcept |
|
62 | 62 | { |
|
63 | 63 | impl->lockRead(); |
|
64 | 64 | auto cacheRange = impl->m_CacheRange; |
|
65 | 65 | impl->unlock(); |
|
66 | 66 | return cacheRange; |
|
67 | 67 | } |
|
68 | 68 | |
|
69 | 69 | void Variable::setCacheRange(const SqpRange &cacheRange) noexcept |
|
70 | 70 | { |
|
71 | 71 | impl->lockWrite(); |
|
72 | 72 | impl->m_CacheRange = cacheRange; |
|
73 | 73 | impl->unlock(); |
|
74 | 74 | } |
|
75 | 75 | |
|
76 | 76 | void Variable::setDataSeries(std::shared_ptr<IDataSeries> dataSeries) noexcept |
|
77 | 77 | { |
|
78 | 78 | qCDebug(LOG_Variable()) << "TORM Variable::setDataSeries" |
|
79 | 79 | << QThread::currentThread()->objectName(); |
|
80 | 80 | if (!dataSeries) { |
|
81 | 81 | /// @todo ALX : log |
|
82 | 82 | return; |
|
83 | 83 | } |
|
84 | 84 | impl->lockWrite(); |
|
85 | 85 | impl->m_DataSeries = dataSeries->clone(); |
|
86 | 86 | impl->unlock(); |
|
87 | 87 | } |
|
88 | 88 | |
|
89 | 89 | void Variable::mergeDataSeries(std::shared_ptr<IDataSeries> dataSeries) noexcept |
|
90 | 90 | { |
|
91 | 91 | qCDebug(LOG_Variable()) << "TORM Variable::mergeDataSeries" |
|
92 | 92 | << QThread::currentThread()->objectName(); |
|
93 | 93 | if (!dataSeries) { |
|
94 | 94 | /// @todo ALX : log |
|
95 | 95 | return; |
|
96 | 96 | } |
|
97 | 97 | |
|
98 | 98 | // Add or merge the data |
|
99 | 99 | // Inits the data series of the variable |
|
100 | 100 | impl->lockWrite(); |
|
101 | 101 | if (!impl->m_DataSeries) { |
|
102 | 102 | impl->m_DataSeries = dataSeries->clone(); |
|
103 | 103 | } |
|
104 | 104 | else { |
|
105 | 105 | impl->m_DataSeries->merge(dataSeries.get()); |
|
106 | 106 | } |
|
107 | 107 | impl->unlock(); |
|
108 | 108 | |
|
109 | 109 | // sub the data |
|
110 | 110 | auto subData = this->dataSeries()->subDataSeries(this->cacheRange()); |
|
111 | 111 | qCDebug(LOG_Variable()) << "TORM: Variable::mergeDataSeries sub" << subData->range(); |
|
112 | 112 | this->setDataSeries(subData); |
|
113 | 113 | qCDebug(LOG_Variable()) << "TORM: Variable::mergeDataSeries set" << this->dataSeries()->range(); |
|
114 | 114 | } |
|
115 | 115 | |
|
116 | 116 | std::shared_ptr<IDataSeries> Variable::dataSeries() const noexcept |
|
117 | 117 | { |
|
118 | 118 | impl->lockRead(); |
|
119 | 119 | auto dataSeries = impl->m_DataSeries; |
|
120 | 120 | impl->unlock(); |
|
121 | 121 | |
|
122 | 122 | return dataSeries; |
|
123 | 123 | } |
|
124 | 124 | |
|
125 | 125 | QVariantHash Variable::metadata() const noexcept |
|
126 | 126 | { |
|
127 | 127 | impl->lockRead(); |
|
128 | 128 | auto metadata = impl->m_Metadata; |
|
129 | 129 | impl->unlock(); |
|
130 | 130 | return metadata; |
|
131 | 131 | } |
|
132 | 132 | |
|
133 | 133 | bool Variable::contains(const SqpRange &range) const noexcept |
|
134 | 134 | { |
|
135 | 135 | impl->lockRead(); |
|
136 | 136 | auto res = impl->m_Range.contains(range); |
|
137 | 137 | impl->unlock(); |
|
138 | 138 | return res; |
|
139 | 139 | } |
|
140 | 140 | |
|
141 | 141 | bool Variable::intersect(const SqpRange &range) const noexcept |
|
142 | 142 | { |
|
143 | 143 | |
|
144 | 144 | impl->lockRead(); |
|
145 | 145 | auto res = impl->m_Range.intersect(range); |
|
146 | 146 | impl->unlock(); |
|
147 | 147 | return res; |
|
148 | 148 | } |
|
149 | 149 | |
|
150 | 150 | bool Variable::isInside(const SqpRange &range) const noexcept |
|
151 | 151 | { |
|
152 | 152 | impl->lockRead(); |
|
153 | 153 | auto res = range.contains(SqpRange{impl->m_Range.m_TStart, impl->m_Range.m_TEnd}); |
|
154 | 154 | impl->unlock(); |
|
155 | 155 | return res; |
|
156 | 156 | } |
|
157 | 157 | |
|
158 | 158 | bool Variable::cacheContains(const SqpRange &range) const noexcept |
|
159 | 159 | { |
|
160 | 160 | impl->lockRead(); |
|
161 | 161 | auto res = impl->m_CacheRange.contains(range); |
|
162 | 162 | impl->unlock(); |
|
163 | 163 | return res; |
|
164 | 164 | } |
|
165 | 165 | |
|
166 | 166 | bool Variable::cacheIntersect(const SqpRange &range) const noexcept |
|
167 | 167 | { |
|
168 | 168 | impl->lockRead(); |
|
169 | 169 | auto res = impl->m_CacheRange.intersect(range); |
|
170 | 170 | impl->unlock(); |
|
171 | 171 | return res; |
|
172 | 172 | } |
|
173 | 173 | |
|
174 | 174 | bool Variable::cacheIsInside(const SqpRange &range) const noexcept |
|
175 | 175 | { |
|
176 | 176 | impl->lockRead(); |
|
177 | 177 | auto res = range.contains(SqpRange{impl->m_CacheRange.m_TStart, impl->m_CacheRange.m_TEnd}); |
|
178 | 178 | impl->unlock(); |
|
179 | 179 | return res; |
|
180 | 180 | } |
|
181 | 181 | |
|
182 | 182 | |
|
183 | 183 | QVector<SqpRange> Variable::provideNotInCacheRangeList(const SqpRange &range) const noexcept |
|
184 | 184 | { |
|
185 | // This code assume that cach in contigue. Can return 0, 1 or 2 SqpRange | |
|
186 | ||
|
185 | 187 | auto notInCache = QVector<SqpRange>{}; |
|
186 | 188 | |
|
187 | 189 | if (!this->cacheContains(range)) { |
|
188 | 190 | if (range.m_TEnd <= impl->m_CacheRange.m_TStart |
|
189 | 191 | || range.m_TStart >= impl->m_CacheRange.m_TEnd) { |
|
190 | 192 | notInCache << range; |
|
191 | 193 | } |
|
192 | 194 | else if (range.m_TStart < impl->m_CacheRange.m_TStart |
|
193 | 195 | && range.m_TEnd <= impl->m_CacheRange.m_TEnd) { |
|
194 | 196 | notInCache << SqpRange{range.m_TStart, impl->m_CacheRange.m_TStart}; |
|
195 | 197 | } |
|
196 | 198 | else if (range.m_TStart < impl->m_CacheRange.m_TStart |
|
197 | 199 | && range.m_TEnd > impl->m_CacheRange.m_TEnd) { |
|
198 | 200 | notInCache << SqpRange{range.m_TStart, impl->m_CacheRange.m_TStart} |
|
199 | 201 | << SqpRange{impl->m_CacheRange.m_TEnd, range.m_TEnd}; |
|
200 | 202 | } |
|
201 | 203 | else if (range.m_TStart < impl->m_CacheRange.m_TEnd) { |
|
202 | 204 | notInCache << SqpRange{impl->m_CacheRange.m_TEnd, range.m_TEnd}; |
|
203 | 205 | } |
|
204 | 206 | else { |
|
205 | 207 | qCCritical(LOG_Variable()) << tr("Detection of unknown case.") |
|
206 | 208 | << QThread::currentThread(); |
|
207 | 209 | } |
|
208 | 210 | } |
|
209 | 211 | |
|
210 | 212 | return notInCache; |
|
211 | 213 | } |
|
214 | ||
|
215 | QVector<SqpRange> Variable::provideInCacheRangeList(const SqpRange &range) const noexcept | |
|
216 | { | |
|
217 | // This code assume that cach in contigue. Can return 0 or 1 SqpRange | |
|
218 | ||
|
219 | auto inCache = QVector<SqpRange>{}; | |
|
220 | ||
|
221 | ||
|
222 | if (this->cacheContains(range)) { | |
|
223 | if (range.m_TStart <= impl->m_CacheRange.m_TEnd | |
|
224 | && range.m_TEnd >= impl->m_CacheRange.m_TEnd) { | |
|
225 | inCache << SqpRange{range.m_TStart, impl->m_CacheRange.m_TEnd}; | |
|
226 | } | |
|
227 | ||
|
228 | else if (range.m_TStart >= impl->m_CacheRange.m_TStart | |
|
229 | && range.m_TEnd < impl->m_CacheRange.m_TEnd) { | |
|
230 | inCache << range; | |
|
231 | } | |
|
232 | else if (range.m_TStart < impl->m_CacheRange.m_TStart | |
|
233 | && range.m_TEnd >= impl->m_CacheRange.m_TStart) { | |
|
234 | inCache << SqpRange{impl->m_CacheRange.m_TStart, range.m_TEnd}; | |
|
235 | } | |
|
236 | else { | |
|
237 | qCCritical(LOG_Variable()) << tr("Detection of unknown case.") | |
|
238 | << QThread::currentThread(); | |
|
239 | } | |
|
240 | } | |
|
241 | ||
|
242 | return inCache; | |
|
243 | } |
General Comments 0
You need to be logged in to leave comments.
Login now