##// END OF EJS Templates
Basic serial variable creation and update...
jeandet -
r2:c7f53bda4647
parent child
Show More
@@ -0,0 +1,9
1 #ifndef DEBUG_H
2 #define DEBUG_H
3
4 #ifdef SCIQLOP_CRASH_ON_ERROR
5 #define SCIQLOP_ERROR(x) assert(false)
6 #else
7 #define SCIQLOP_ERROR(x)
8 #endif
9 #endif
@@ -50,6 +50,7 FILE (GLOB_RECURSE core_SRCS
50 ./include/Common/Numeric.h
50 ./include/Common/Numeric.h
51 ./include/Common/deprecate.h
51 ./include/Common/deprecate.h
52 ./include/Common/containers.h
52 ./include/Common/containers.h
53 ./include/Common/debug.h
53 ./include/Plugin/IPlugin.h
54 ./include/Plugin/IPlugin.h
54 ./include/Data/ArrayDataIterator.h
55 ./include/Data/ArrayDataIterator.h
55 ./include/Data/VariableRequest.h
56 ./include/Data/VariableRequest.h
@@ -46,7 +46,12 public:
46 = 0;
46 = 0;
47 )
47 )
48
48
49 virtual QUuid getData(const DataProviderParameters &parameters){return QUuid::createUuid();}
49 // Synchronous call -> asyncGetData may be written for asynchronous get
50 virtual IDataSeries* getData(const DataProviderParameters &parameters)
51 {
52 Q_UNUSED(parameters);
53 return nullptr;
54 }
50
55
51
56
52 DEPRECATE(
57 DEPRECATE(
@@ -56,7 +61,7 public:
56 virtual void requestDataAborting(QUuid acqIdentifier) = 0;
61 virtual void requestDataAborting(QUuid acqIdentifier) = 0;
57 )
62 )
58
63
59 virtual void abort(QUuid requestID){}
64 virtual void abort(QUuid requestID){Q_UNUSED(requestID);}
60
65
61 signals:
66 signals:
62 DEPRECATE(
67 DEPRECATE(
@@ -9,7 +9,9
9
9
10 #include <QLoggingCategory>
10 #include <QLoggingCategory>
11 #include <QObject>
11 #include <QObject>
12 #include <QUuid>
12
13
14 #include <Common/deprecate.h>
13 #include <Common/MetaTypes.h>
15 #include <Common/MetaTypes.h>
14 #include <Common/spimpl.h>
16 #include <Common/spimpl.h>
15
17
@@ -70,7 +72,10 public:
70
72
71 QVector<DateTimeRange> provideNotInCacheRangeList(const DateTimeRange &range) const noexcept;
73 QVector<DateTimeRange> provideNotInCacheRangeList(const DateTimeRange &range) const noexcept;
72 QVector<DateTimeRange> provideInCacheRangeList(const DateTimeRange &range) const noexcept;
74 QVector<DateTimeRange> provideInCacheRangeList(const DateTimeRange &range) const noexcept;
75 DEPRECATE(
73 void mergeDataSeries(std::shared_ptr<IDataSeries> dataSeries) noexcept;
76 void mergeDataSeries(std::shared_ptr<IDataSeries> dataSeries) noexcept;
77 )
78 void mergeDataSeries(IDataSeries* dataSeries) noexcept;
74
79
75 static QVector<DateTimeRange> provideNotInCacheRangeList(const DateTimeRange &oldRange,
80 static QVector<DateTimeRange> provideNotInCacheRangeList(const DateTimeRange &oldRange,
76 const DateTimeRange &nextRange);
81 const DateTimeRange &nextRange);
@@ -78,6 +83,7 public:
78 static QVector<DateTimeRange> provideInCacheRangeList(const DateTimeRange &oldRange,
83 static QVector<DateTimeRange> provideInCacheRangeList(const DateTimeRange &oldRange,
79 const DateTimeRange &nextRange);
84 const DateTimeRange &nextRange);
80
85
86 QUuid ID(){return _uuid;}
81 signals:
87 signals:
82 void updated();
88 void updated();
83 /// Signal emitted when when the data series of the variable is loaded for the first time
89 /// Signal emitted when when the data series of the variable is loaded for the first time
@@ -86,6 +92,7 signals:
86 private:
92 private:
87 class VariablePrivate;
93 class VariablePrivate;
88 spimpl::unique_impl_ptr<VariablePrivate> impl;
94 spimpl::unique_impl_ptr<VariablePrivate> impl;
95 QUuid _uuid;
89 };
96 };
90
97
91 // Required for using shared_ptr in signals/slots
98 // Required for using shared_ptr in signals/slots
@@ -1,26 +1,36
1 #include <Common/spimpl.h>
2 #include <memory>
1 #include <memory>
3 #include <vector>
2 #include <vector>
4 #include <QHash>
3 #include <QHash>
4 #include <QObject>
5 #include <QMutexLocker>
6 #include <QUuid>
7 #include <QItemSelectionModel>
8 #include <Common/spimpl.h>
5 #include <Variable/Variable.h>
9 #include <Variable/Variable.h>
6 #include <Variable/VariableSynchronizationGroup.h>
10 #include <Variable/VariableSynchronizationGroup.h>
7 #include <Variable/VariableModel.h>
11 #include <Variable/VariableModel.h>
8 #include <Data/IDataProvider.h>
12 #include <Data/IDataProvider.h>
9 #include "Data/DateTimeRange.h"
13 #include "Data/DateTimeRange.h"
10 #include <QMutexLocker>
11 #include <QUuid>
12 #include <QItemSelectionModel>
13
14
14 class VariableController2Private;
15 class VariableController2: public QObject
15 class VariableController2
16 {
16 {
17 class VariableController2Private;
18 Q_OBJECT
19
17 spimpl::unique_impl_ptr<VariableController2Private> impl;
20 spimpl::unique_impl_ptr<VariableController2Private> impl;
18
21
19 public:
22 public:
20 explicit VariableController2();
23 explicit VariableController2();
21 std::shared_ptr<Variable> createVariable(const QString &name, const QVariantHash &metadata,
24 std::shared_ptr<Variable> createVariable(const QString &name, const QVariantHash &metadata,
22 std::shared_ptr<IDataProvider> provider, const DateTimeRange &range);
25 std::shared_ptr<IDataProvider> provider, const DateTimeRange &range);
23 void changeRange(QUuid variable, DateTimeRange r);
26
24 void asyncChangeRange(QUuid variable, DateTimeRange r);
27 void deleteVariable(std::shared_ptr<Variable> variable);
28 void changeRange(std::shared_ptr<Variable> variable, DateTimeRange r);
29 void asyncChangeRange(std::shared_ptr<Variable> variable, DateTimeRange r);
30 const std::set<std::shared_ptr<Variable> > &variables();
31
32 signals:
33 void variableAdded(std::shared_ptr<Variable>);
34 void variableDeleted(std::shared_ptr<Variable>);
25
35
26 };
36 };
@@ -6,6 +6,7
6 #include <QMutex>
6 #include <QMutex>
7 #include <QReadWriteLock>
7 #include <QReadWriteLock>
8 #include <QThread>
8 #include <QThread>
9 #include <Common/debug.h>
9
10
10 Q_LOGGING_CATEGORY(LOG_Variable, "Variable")
11 Q_LOGGING_CATEGORY(LOG_Variable, "Variable")
11
12
@@ -106,12 +107,14 struct Variable::VariablePrivate {
106 };
107 };
107
108
108 Variable::Variable(const QString &name, const QVariantHash &metadata)
109 Variable::Variable(const QString &name, const QVariantHash &metadata)
109 : impl{spimpl::make_unique_impl<VariablePrivate>(name, metadata)}
110 : impl{spimpl::make_unique_impl<VariablePrivate>(name, metadata)},
111 _uuid{QUuid::createUuid()}
110 {
112 {
111 }
113 }
112
114
113 Variable::Variable(const Variable &other)
115 Variable::Variable(const Variable &other)
114 : impl{spimpl::make_unique_impl<VariablePrivate>(*other.impl)}
116 : impl{spimpl::make_unique_impl<VariablePrivate>(*other.impl)},
117 _uuid{QUuid::createUuid()} //is a clone but must have a != uuid
115 {
118 {
116 }
119 }
117
120
@@ -206,6 +209,34 void Variable::mergeDataSeries(std::shared_ptr<IDataSeries> dataSeries) noexcept
206 }
209 }
207 }
210 }
208
211
212 void Variable::mergeDataSeries(IDataSeries *dataSeries) noexcept
213 {
214 if (dataSeries==nullptr) {
215 SCIQLOP_ERROR("Given IDataSeries is nullptr");
216 return;
217 }
218
219 auto dataInit = false;
220 // @TODO move to impl to Pimpl this is what it stands for...
221 // Add or merge the data
222 impl->lockWrite();
223 if (!impl->m_DataSeries) {
224 //@TODO find a better way
225 impl->m_DataSeries = dataSeries->clone();
226 dataInit = true;
227 delete dataSeries;
228 }
229 else {
230 impl->m_DataSeries->merge(dataSeries);
231 }
232 impl->purgeDataSeries();
233 impl->unlock();
234
235 if (dataInit) {
236 emit dataInitialized();
237 }
238 }
239
209
240
210 std::shared_ptr<IDataSeries> Variable::dataSeries() const noexcept
241 std::shared_ptr<IDataSeries> Variable::dataSeries() const noexcept
211 {
242 {
@@ -1,17 +1,56
1 #include "Variable/VariableController2.h"
1 #include "Variable/VariableController2.h"
2 #include <Common/containers.h>
3 #include <Common/debug.h>
4 #include <Data/DataProviderParameters.h>
2
5
3
6 class VariableController2::VariableController2Private
4 class VariableController2Private
5 {
7 {
6
8 std::set<std::shared_ptr<Variable>> _variables;
9 QMap<QUuid,std::shared_ptr<IDataProvider>> _providers;
7 public:
10 public:
8 VariableController2Private(QObject* parent=Q_NULLPTR)
11 VariableController2Private(QObject* parent=Q_NULLPTR)
9 {}
12 {
13 Q_UNUSED(parent);
14 }
15
10 ~VariableController2Private() = default;
16 ~VariableController2Private() = default;
17
11 std::shared_ptr<Variable> createVariable(const QString &name, const QVariantHash &metadata, std::shared_ptr<IDataProvider> provider, const DateTimeRange &range)
18 std::shared_ptr<Variable> createVariable(const QString &name, const QVariantHash &metadata, std::shared_ptr<IDataProvider> provider, const DateTimeRange &range)
12 {
19 {
13 return std::make_shared<Variable>(name,metadata);
20 auto newVar = std::make_shared<Variable>(name,metadata);
21 this->_variables.insert(newVar);
22 this->_providers[newVar->ID()] = provider;
23 return newVar;
24 }
25
26 void deleteVariable(std::shared_ptr<Variable> variable)
27 {
28 if(this->_providers.contains(variable->ID()))
29 this->_providers.remove(variable->ID());
30 if(SciQLop::containers::contains(this->_variables, variable))
31 this->_variables.erase(variable);
32 }
33
34 void changeRange(std::shared_ptr<Variable> variable, DateTimeRange r)
35 {
36 if(_providers.contains(variable->ID()))
37 {
38 auto provider = _providers[variable->ID()];
39 auto data = provider->getData(DataProviderParameters{{r},variable->metadata()});
40 variable->mergeDataSeries(data);
41 }
42 else
43 {
44 SCIQLOP_ERROR("No provider found for given variable");
45 }
46 }
47
48
49 const std::set<std::shared_ptr<Variable>>& variables()
50 {
51 return _variables;
14 }
52 }
53
15 };
54 };
16
55
17 VariableController2::VariableController2()
56 VariableController2::VariableController2()
@@ -20,6 +59,25 VariableController2::VariableController2()
20
59
21 std::shared_ptr<Variable> VariableController2::createVariable(const QString &name, const QVariantHash &metadata, std::shared_ptr<IDataProvider> provider, const DateTimeRange &range)
60 std::shared_ptr<Variable> VariableController2::createVariable(const QString &name, const QVariantHash &metadata, std::shared_ptr<IDataProvider> provider, const DateTimeRange &range)
22 {
61 {
23 return impl->createVariable(name, metadata, provider, range);
62 auto var = impl->createVariable(name, metadata, provider, range);
63 emit variableAdded(var);
64 impl->changeRange(var,range);
65 return var;
66 }
67
68 void VariableController2::deleteVariable(std::shared_ptr<Variable> variable)
69 {
70 impl->deleteVariable(variable);
71 emit variableDeleted(variable);
72 }
73
74 void VariableController2::changeRange(std::shared_ptr<Variable> variable, DateTimeRange r)
75 {
76 impl->changeRange(variable, r);
77 }
78
79 const std::set<std::shared_ptr<Variable> > &VariableController2::variables()
80 {
81 return impl->variables();
24 }
82 }
25
83
@@ -1,9 +1,51
1 #include <cmath>
1 #include <QtTest>
2 #include <QtTest>
2 #include <QObject>
3 #include <QObject>
4 #include <Variable/VariableController2.h>
5 #include <Data/DateTimeRange.h>
6 #include <Data/IDataProvider.h>
7 #include <Data/ScalarSeries.h>
8 #include <Data/DataProviderParameters.h>
9 #include <Common/containers.h>
3
10
11 class FakeProvider: public IDataProvider
12 {
13 public:
14 FakeProvider() = default;
4
15
5 class TestVariableController2 : public QObject
16 std::shared_ptr<IDataProvider> clone() const override{ return std::make_shared<FakeProvider>(); }
17
18 IDataSeries* getData(const DataProviderParameters &parameters) override
19 {
20 auto tstart = parameters.m_Times[0].m_TStart;
21 auto tend = parameters.m_Times[0].m_TEnd;
22 std::vector<double> x;
23 std::vector<double> y;
24 for(auto i = tstart;i<tend;i+=1.) //1 seconde data resolution
25 {
26 x.push_back(i);
27 y.push_back(i);
28 }
29 auto serie = new ScalarSeries(std::move(x),std::move(y),Unit("Secondes",true),Unit("Volts",false));
30 return serie;
31 }
32
33
34
35 void requestDataLoading(QUuid acqIdentifier, const DataProviderParameters &parameters) override
36 {
37 Q_UNUSED(acqIdentifier)
38 Q_UNUSED(parameters)
39 }
40
41 void requestDataAborting(QUuid acqIdentifier) override
42 {
43 Q_UNUSED(acqIdentifier)
44 }
45
46 };
6
47
48 class TestVariableController2 : public QObject
7 {
49 {
8 Q_OBJECT
50 Q_OBJECT
9 public:
51 public:
@@ -14,9 +56,45 private slots:
14 void initTestCase(){}
56 void initTestCase(){}
15 void cleanupTestCase(){}
57 void cleanupTestCase(){}
16
58
17 void test1()
59 void testCreateVariable()
60 {
61 VariableController2 vc;
62 bool callbackCalled = false;
63 connect(&vc,&VariableController2::variableAdded, [&callbackCalled](std::shared_ptr<Variable>){callbackCalled=true;});
64 auto provider = std::make_shared<FakeProvider>();
65 QVERIFY(!callbackCalled);
66 auto var1 = vc.createVariable("var1",{},provider,DateTimeRange());
67 QVERIFY(SciQLop::containers::contains(vc.variables(), var1));
68 QVERIFY(callbackCalled);
69 }
70
71 void testDeleteVariable()
72 {
73 VariableController2 vc;
74 bool callbackCalled = false;
75 connect(&vc,&VariableController2::variableDeleted, [&callbackCalled](std::shared_ptr<Variable>){callbackCalled=true;});
76 auto provider = std::make_shared<FakeProvider>();
77 auto var1 = vc.createVariable("var1",{},provider,DateTimeRange());
78 QVERIFY(SciQLop::containers::contains(vc.variables(), var1));
79 QVERIFY(!callbackCalled);
80 vc.deleteVariable(var1);
81 QVERIFY(!SciQLop::containers::contains(vc.variables(), var1));
82 QVERIFY(callbackCalled);
83 }
84
85 void testGetData()
18 {
86 {
19 QCOMPARE(1+1, 2);
87 VariableController2 vc;
88 auto provider = std::make_shared<FakeProvider>();
89 auto range1 = DateTimeRange::fromDateTime(QDate(2018,8,7),QTime(14,00),
90 QDate(2018,8,7),QTime(16,00));
91 auto range2 = DateTimeRange::fromDateTime(QDate(2018,8,7),QTime(12,00),
92 QDate(2018,8,7),QTime(18,00));
93 auto var1 = vc.createVariable("var1", {}, provider, range1);
94 QCOMPARE(var1->nbPoints(), int(range1.delta()));
95 vc.changeRange(var1, range2);
96 QCOMPARE(var1->nbPoints(), int(range2.delta()));
97
20 }
98 }
21
99
22 private:
100 private:
General Comments 0
You need to be logged in to leave comments. Login now