From 98220c931c839f5f32ca674f88cf2d0b6ec152e9 2017-12-11 09:31:18 From: Alexandre Leroux Date: 2017-12-11 09:31:18 Subject: [PATCH] Adds read compatibility for local AMDA server The local AMDA server uses another regex than the default server to read the units in x. We manage the compatibility by adding in the parser the possibility of testing several regexes to read a property --- diff --git a/plugins/amda/include/AmdaResultParserDefs.h b/plugins/amda/include/AmdaResultParserDefs.h index 410201a..5e3d6d8 100644 --- a/plugins/amda/include/AmdaResultParserDefs.h +++ b/plugins/amda/include/AmdaResultParserDefs.h @@ -46,6 +46,9 @@ extern const QString VALUES_UNIT_PROPERTY; /// ... - Units : m/s - ... extern const QRegularExpression DEFAULT_X_AXIS_UNIT_REGEX; +/// Alternative regex to find x-axis unit in a line +extern const QRegularExpression ALTERNATIVE_X_AXIS_UNIT_REGEX; + /// Regex to find end time of data in a line for a spectrogram extern const QRegularExpression SPECTROGRAM_END_TIME_REGEX; diff --git a/plugins/amda/src/AmdaResultParserDefs.cpp b/plugins/amda/src/AmdaResultParserDefs.cpp index 37c2cf9..311ebe1 100644 --- a/plugins/amda/src/AmdaResultParserDefs.cpp +++ b/plugins/amda/src/AmdaResultParserDefs.cpp @@ -11,9 +11,17 @@ const QString X_AXIS_UNIT_PROPERTY = QStringLiteral("xAxisUnit"); const QString Y_AXIS_UNIT_PROPERTY = QStringLiteral("yAxisUnit"); const QString VALUES_UNIT_PROPERTY = QStringLiteral("valuesUnit"); +namespace { + +const auto PARAMETER_UNITS_REGEX + = QRegularExpression{QStringLiteral("\\s*PARAMETER_UNITS\\s*:\\s*(.*)")}; +} + const QRegularExpression DEFAULT_X_AXIS_UNIT_REGEX = QRegularExpression{QStringLiteral("-\\s*Units\\s*:\\s*(.+?)\\s*-")}; +const QRegularExpression ALTERNATIVE_X_AXIS_UNIT_REGEX = PARAMETER_UNITS_REGEX; + const QRegularExpression SPECTROGRAM_END_TIME_REGEX = QRegularExpression{QStringLiteral("\\s*INTERVAL_STOP\\s*:\\s*(.*)")}; @@ -38,5 +46,4 @@ const QRegularExpression SPECTROGRAM_START_TIME_REGEX const QRegularExpression SPECTROGRAM_Y_AXIS_UNIT_REGEX = QRegularExpression{QStringLiteral("\\s*PARAMETER_TABLE_UNITS\\[0\\]\\s*:\\s*(.*)")}; -const QRegularExpression SPECTROGRAM_VALUES_UNIT_REGEX - = QRegularExpression{QStringLiteral("\\s*PARAMETER_UNITS\\s*:\\s*(.*)")}; +const QRegularExpression SPECTROGRAM_VALUES_UNIT_REGEX = PARAMETER_UNITS_REGEX; diff --git a/plugins/amda/src/AmdaResultParserHelper.cpp b/plugins/amda/src/AmdaResultParserHelper.cpp index a483800..64ab7de 100644 --- a/plugins/amda/src/AmdaResultParserHelper.cpp +++ b/plugins/amda/src/AmdaResultParserHelper.cpp @@ -136,7 +136,7 @@ void tryReadResult(std::vector &xAxisData, std::vector &valuesDa * @param properties the properties map in which to put the property extracted from the line * @param key the key to which the property is added in the properties map * @param line the line to read to extract the property - * @param regex the expected regex to extract the property. If the line matches this regex, the + * @param regexes the expected regexes to extract the property. If the line matches one regex, the * property is generated * @param fun the function used to generate the property * @return true if the property could be generated, false if the line does not match the regex, or @@ -144,18 +144,24 @@ void tryReadResult(std::vector &xAxisData, std::vector &valuesDa */ template bool tryReadProperty(Properties &properties, const QString &key, const QString &line, - const QRegularExpression ®ex, GeneratePropertyFun fun) + const std::vector ®exes, GeneratePropertyFun fun) { if (properties.contains(key)) { return false; } - auto match = regex.match(line); - if (match.hasMatch()) { - properties.insert(key, fun(match)); + // Searches for a match among all possible regexes + auto hasMatch = false; + for (auto regexIt = regexes.cbegin(), end = regexes.cend(); regexIt != end && !hasMatch; + ++regexIt) { + auto match = regexIt->match(line); + auto hasMatch = match.hasMatch(); + if (hasMatch) { + properties.insert(key, fun(match)); + } } - return match.hasMatch(); + return hasMatch; } /** @@ -163,9 +169,9 @@ bool tryReadProperty(Properties &properties, const QString &key, const QString & * @sa tryReadProperty() */ bool tryReadDate(Properties &properties, const QString &key, const QString &line, - const QRegularExpression ®ex, bool timeUnit = false) + const std::vector ®exes, bool timeUnit = false) { - return tryReadProperty(properties, key, line, regex, [timeUnit](const auto &match) { + return tryReadProperty(properties, key, line, regexes, [timeUnit](const auto &match) { return QVariant::fromValue(doubleDate(match.captured(1))); }); } @@ -175,9 +181,9 @@ bool tryReadDate(Properties &properties, const QString &key, const QString &line * @sa tryReadProperty() */ bool tryReadDouble(Properties &properties, const QString &key, const QString &line, - const QRegularExpression ®ex) + const std::vector ®exes) { - return tryReadProperty(properties, key, line, regex, [](const auto &match) { + return tryReadProperty(properties, key, line, regexes, [](const auto &match) { bool ok; // If the value can't be converted to double, it is set to NaN @@ -196,9 +202,10 @@ bool tryReadDouble(Properties &properties, const QString &key, const QString &li * @sa tryReadProperty() */ bool tryReadDoubles(Properties &properties, const QString &key, const QString &line, - const QRegularExpression ®ex, const QString &sep = QStringLiteral(",")) + const std::vector ®exes, + const QString &sep = QStringLiteral(",")) { - return tryReadProperty(properties, key, line, regex, [sep](const auto &match) { + return tryReadProperty(properties, key, line, regexes, [sep](const auto &match) { std::vector doubleValues{}; // If the value can't be converted to double, it is set to NaN @@ -223,9 +230,9 @@ bool tryReadDoubles(Properties &properties, const QString &key, const QString &l * @sa tryReadProperty() */ bool tryReadUnit(Properties &properties, const QString &key, const QString &line, - const QRegularExpression ®ex, bool timeUnit = false) + const std::vector ®exes, bool timeUnit = false) { - return tryReadProperty(properties, key, line, regex, [timeUnit](const auto &match) { + return tryReadProperty(properties, key, line, regexes, [timeUnit](const auto &match) { return QVariant::fromValue(Unit{match.captured(1), timeUnit}); }); } @@ -251,7 +258,8 @@ std::shared_ptr ScalarParserHelper::createSeries() void ScalarParserHelper::readPropertyLine(const QString &line) { - tryReadUnit(m_Properties, X_AXIS_UNIT_PROPERTY, line, DEFAULT_X_AXIS_UNIT_REGEX, true); + tryReadUnit(m_Properties, X_AXIS_UNIT_PROPERTY, line, + {DEFAULT_X_AXIS_UNIT_REGEX, ALTERNATIVE_X_AXIS_UNIT_REGEX}, true); } void ScalarParserHelper::readResultLine(const QString &line) @@ -321,46 +329,46 @@ void SpectrogramParserHelper::readPropertyLine(const QString &line) // values unit [&] { return tryReadUnit(m_Properties, VALUES_UNIT_PROPERTY, line, - SPECTROGRAM_VALUES_UNIT_REGEX); + {SPECTROGRAM_VALUES_UNIT_REGEX}); }, // y-axis unit [&] { return tryReadUnit(m_Properties, Y_AXIS_UNIT_PROPERTY, line, - SPECTROGRAM_Y_AXIS_UNIT_REGEX); + {SPECTROGRAM_Y_AXIS_UNIT_REGEX}); }, // min sampling [&] { return tryReadDouble(m_Properties, MIN_SAMPLING_PROPERTY, line, - SPECTROGRAM_MIN_SAMPLING_REGEX); + {SPECTROGRAM_MIN_SAMPLING_REGEX}); }, // max sampling [&] { return tryReadDouble(m_Properties, MAX_SAMPLING_PROPERTY, line, - SPECTROGRAM_MAX_SAMPLING_REGEX); + {SPECTROGRAM_MAX_SAMPLING_REGEX}); }, // fill value [&] { return tryReadDouble(m_Properties, FILL_VALUE_PROPERTY, line, - SPECTROGRAM_FILL_VALUE_REGEX); + {SPECTROGRAM_FILL_VALUE_REGEX}); }, // min bounds of each band [&] { return tryReadDoubles(m_Properties, MIN_BANDS_PROPERTY, line, - SPECTROGRAM_MIN_BANDS_REGEX); + {SPECTROGRAM_MIN_BANDS_REGEX}); }, // max bounds of each band [&] { return tryReadDoubles(m_Properties, MAX_BANDS_PROPERTY, line, - SPECTROGRAM_MAX_BANDS_REGEX); + {SPECTROGRAM_MAX_BANDS_REGEX}); }, // start time of data [&] { return tryReadDate(m_Properties, START_TIME_PROPERTY, line, - SPECTROGRAM_START_TIME_REGEX); + {SPECTROGRAM_START_TIME_REGEX}); }, // end time of data [&] { - return tryReadDate(m_Properties, END_TIME_PROPERTY, line, SPECTROGRAM_END_TIME_REGEX); + return tryReadDate(m_Properties, END_TIME_PROPERTY, line, {SPECTROGRAM_END_TIME_REGEX}); }}; for (auto function : functions) { @@ -407,7 +415,8 @@ std::shared_ptr VectorParserHelper::createSeries() void VectorParserHelper::readPropertyLine(const QString &line) { - tryReadUnit(m_Properties, X_AXIS_UNIT_PROPERTY, line, DEFAULT_X_AXIS_UNIT_REGEX, true); + tryReadUnit(m_Properties, X_AXIS_UNIT_PROPERTY, line, + {DEFAULT_X_AXIS_UNIT_REGEX, ALTERNATIVE_X_AXIS_UNIT_REGEX}, true); } void VectorParserHelper::readResultLine(const QString &line)