##// END OF EJS Templates
Parser refactoring (3)...
Alexandre Leroux -
r987:49134789d5f3
parent child
Show More
@@ -0,0 +1,35
1 #ifndef SCIQLOP_AMDARESULTPARSERDEFS_H
2 #define SCIQLOP_AMDARESULTPARSERDEFS_H
3
4 #include <QtCore/QRegularExpression>
5 #include <QtCore/QString>
6 #include <QtCore/QVariantHash>
7
8 // ////////// //
9 // Properties //
10 // ////////// //
11
12 /// Alias to represent properties read in the header of AMDA file
13 using Properties = QVariantHash;
14
15 extern const QString X_AXIS_UNIT_PROPERTY;
16
17 // /////////////////// //
18 // Regular expressions //
19 // /////////////////// //
20
21 // AMDA V2
22 // /// ... PARAMETER_UNITS : nT ...
23 // /// ... PARAMETER_UNITS:nT ...
24 // /// ... PARAMETER_UNITS: mΒ² ...
25 // /// ... PARAMETER_UNITS : m/s ...
26 // const auto UNIT_REGEX = QRegularExpression{QStringLiteral("\\s*PARAMETER_UNITS\\s*:\\s*(.+)")};
27
28 /// Regex to find x-axis unit in a line. Examples of valid lines:
29 /// ... - Units : nT - ...
30 /// ... -Units:nT- ...
31 /// ... -Units: mΒ²- ...
32 /// ... - Units : m/s - ...
33 extern const QRegularExpression DEFAULT_X_AXIS_UNIT_REGEX;
34
35 #endif // SCIQLOP_AMDARESULTPARSERDEFS_H
@@ -0,0 +1,6
1 #include "AmdaResultParserDefs.h"
2
3 const QString X_AXIS_UNIT_PROPERTY = QStringLiteral("xAxisUnit");
4
5 const QRegularExpression DEFAULT_X_AXIS_UNIT_REGEX
6 = QRegularExpression{QStringLiteral("-\\s*Units\\s*:\\s*(.+?)\\s*-")};
@@ -1,6 +1,8
1 #ifndef SCIQLOP_UNIT_H
1 #ifndef SCIQLOP_UNIT_H
2 #define SCIQLOP_UNIT_H
2 #define SCIQLOP_UNIT_H
3
3
4 #include <Common/MetaTypes.h>
5
4 #include <QString>
6 #include <QString>
5 #include <tuple>
7 #include <tuple>
6
8
@@ -20,4 +22,6 struct Unit {
20 bool m_TimeUnit; ///< The unit is a unit of time (UTC)
22 bool m_TimeUnit; ///< The unit is a unit of time (UTC)
21 };
23 };
22
24
25 SCIQLOP_REGISTER_META_TYPE(UNIT_REGISTRY, Unit)
26
23 #endif // SCIQLOP_UNIT_H
27 #endif // SCIQLOP_UNIT_H
@@ -5,7 +5,6
5 #include <QtTest>
5 #include <QtTest>
6
6
7 Q_DECLARE_METATYPE(OptionalAxis)
7 Q_DECLARE_METATYPE(OptionalAxis)
8 Q_DECLARE_METATYPE(Unit)
9
8
10 class TestOptionalAxis : public QObject {
9 class TestOptionalAxis : public QObject {
11 Q_OBJECT
10 Q_OBJECT
@@ -1,6 +1,8
1 #ifndef SCIQLOP_AMDARESULTPARSERHELPER_H
1 #ifndef SCIQLOP_AMDARESULTPARSERHELPER_H
2 #define SCIQLOP_AMDARESULTPARSERHELPER_H
2 #define SCIQLOP_AMDARESULTPARSERHELPER_H
3
3
4 #include "AmdaResultParserDefs.h"
5
4 #include <QtCore/QLoggingCategory>
6 #include <QtCore/QLoggingCategory>
5 #include <QtCore/QString>
7 #include <QtCore/QString>
6
8
@@ -52,6 +54,9 public:
52 std::shared_ptr<IDataSeries> createSeries() override;
54 std::shared_ptr<IDataSeries> createSeries() override;
53 void readPropertyLine(const QString &line) override;
55 void readPropertyLine(const QString &line) override;
54 void readResultLine(const QString &line) override;
56 void readResultLine(const QString &line) override;
57
58 private:
59 Properties m_Properties{};
55 };
60 };
56
61
57 /**
62 /**
@@ -63,6 +68,9 public:
63 std::shared_ptr<IDataSeries> createSeries() override;
68 std::shared_ptr<IDataSeries> createSeries() override;
64 void readPropertyLine(const QString &line) override;
69 void readPropertyLine(const QString &line) override;
65 void readResultLine(const QString &line) override;
70 void readResultLine(const QString &line) override;
71
72 private:
73 Properties m_Properties{};
66 };
74 };
67
75
68 #endif // SCIQLOP_AMDARESULTPARSERHELPER_H
76 #endif // SCIQLOP_AMDARESULTPARSERHELPER_H
@@ -10,6 +10,7 amdaplugin_sources = [
10 'src/AmdaPlugin.cpp',
10 'src/AmdaPlugin.cpp',
11 'src/AmdaProvider.cpp',
11 'src/AmdaProvider.cpp',
12 'src/AmdaResultParser.cpp'
12 'src/AmdaResultParser.cpp'
13 'src/AmdaResultParserDefs.cpp'
13 'src/AmdaResultParserHelper.cpp'
14 'src/AmdaResultParserHelper.cpp'
14 ]
15 ]
15
16
@@ -1,14 +1,82
1 #include "AmdaResultParserHelper.h"
1 #include "AmdaResultParserHelper.h"
2
2
3 #include <Data/Unit.h>
4
3 Q_LOGGING_CATEGORY(LOG_AmdaResultParserHelper, "AmdaResultParserHelper")
5 Q_LOGGING_CATEGORY(LOG_AmdaResultParserHelper, "AmdaResultParserHelper")
4
6
7 namespace {
8
9 // /////// //
10 // Methods //
11 // /////// //
12
13 /**
14 * Checks that the properties contain a specific unit and that this unit is valid
15 * @param properties the properties map in which to search unit
16 * @param key the key to search for the unit in the properties
17 * @param errorMessage the error message to log in case the unit is invalid
18 * @return true if the unit is valid, false it it's invalid or was not found in the properties
19 */
20 bool checkUnit(const Properties &properties, const QString &key, const QString &errorMessage)
21 {
22 auto unit = properties.value(key).value<Unit>();
23 if (unit.m_Name.isEmpty()) {
24 qCWarning(LOG_AmdaResultParserHelper()) << errorMessage;
25 return false;
26 }
27
28 return true;
29 }
30
31 /**
32 * Reads a line from the AMDA file and tries to extract a property from it
33 * @param properties the properties map in which to put the property extracted from the line
34 * @param key the key to which the property is added in the properties map
35 * @param line the line to read to extract the property
36 * @param regex the expected regex to extract the property. If the line matches this regex, the
37 * property is generated
38 * @param fun the function used to generate the property
39 * @return true if the property could be generated, false if the line does not match the regex, or
40 * if a property has already been generated for the key
41 */
42 template <typename GeneratePropertyFun>
43 bool tryReadProperty(Properties &properties, const QString &key, const QString &line,
44 const QRegularExpression &regex, GeneratePropertyFun fun)
45 {
46 if (properties.contains(key)) {
47 return false;
48 }
49
50 auto match = regex.match(line);
51 if (match.hasMatch()) {
52 properties.insert(key, fun(match));
53 }
54
55 return match.hasMatch();
56 }
57
58 /**
59 * Reads a line from the AMDA file and tries to extract a unit from it
60 * @sa tryReadProperty()
61 */
62 bool tryReadUnit(Properties &properties, const QString &key, const QString &line,
63 const QRegularExpression &regex, bool timeUnit = false)
64 {
65 return tryReadProperty(properties, key, line, regex, [timeUnit](const auto &match) {
66 return QVariant::fromValue(Unit{match.captured(1), timeUnit});
67 });
68 }
69
70 } // namespace
71
5 // ////////////////// //
72 // ////////////////// //
6 // ScalarParserHelper //
73 // ScalarParserHelper //
7 // ////////////////// //
74 // ////////////////// //
8
75
9 bool ScalarParserHelper::checkProperties()
76 bool ScalarParserHelper::checkProperties()
10 {
77 {
11 /// @todo ALX
78 return checkUnit(m_Properties, X_AXIS_UNIT_PROPERTY,
79 QObject::tr("The x-axis unit could not be found in the file"));
12 }
80 }
13
81
14 std::shared_ptr<IDataSeries> ScalarParserHelper::createSeries()
82 std::shared_ptr<IDataSeries> ScalarParserHelper::createSeries()
@@ -18,7 +86,7 std::shared_ptr<IDataSeries> ScalarParserHelper::createSeries()
18
86
19 void ScalarParserHelper::readPropertyLine(const QString &line)
87 void ScalarParserHelper::readPropertyLine(const QString &line)
20 {
88 {
21 /// @todo ALX
89 tryReadUnit(m_Properties, X_AXIS_UNIT_PROPERTY, line, DEFAULT_X_AXIS_UNIT_REGEX, true);
22 }
90 }
23
91
24 void ScalarParserHelper::readResultLine(const QString &line)
92 void ScalarParserHelper::readResultLine(const QString &line)
@@ -32,7 +100,8 void ScalarParserHelper::readResultLine(const QString &line)
32
100
33 bool VectorParserHelper::checkProperties()
101 bool VectorParserHelper::checkProperties()
34 {
102 {
35 /// @todo ALX
103 return checkUnit(m_Properties, X_AXIS_UNIT_PROPERTY,
104 QObject::tr("The x-axis unit could not be found in the file"));
36 }
105 }
37
106
38 std::shared_ptr<IDataSeries> VectorParserHelper::createSeries()
107 std::shared_ptr<IDataSeries> VectorParserHelper::createSeries()
@@ -42,7 +111,7 std::shared_ptr<IDataSeries> VectorParserHelper::createSeries()
42
111
43 void VectorParserHelper::readPropertyLine(const QString &line)
112 void VectorParserHelper::readPropertyLine(const QString &line)
44 {
113 {
45 /// @todo ALX
114 tryReadUnit(m_Properties, X_AXIS_UNIT_PROPERTY, line, DEFAULT_X_AXIS_UNIT_REGEX, true);
46 }
115 }
47
116
48 void VectorParserHelper::readResultLine(const QString &line)
117 void VectorParserHelper::readResultLine(const QString &line)
General Comments 0
You need to be logged in to leave comments. Login now