Skip to content

Commit

Permalink
Further implementation of the ContinousMeasurementDataProtocol
Browse files Browse the repository at this point in the history
Details:
	- Implementing the regex patterns
	- Adding the methods required by the interfaces
	- Starting to implement the statemachine of the ProcessData()
  • Loading branch information
KGergo88 committed Aug 30, 2020
1 parent dfa2fed commit b7d481b
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 59 deletions.
117 changes: 68 additions & 49 deletions application/sources/continous_measurement_data_protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

ContinousMeasurementDataProtocol::ContinousMeasurementDataProtocol()
{
state = Constants::States::WaitingForTransmission;
state = Constants::States::WaitingForHeaderMessageStart;
}

std::string ContinousMeasurementDataProtocol::GetProtocolName(void)
Expand All @@ -41,7 +41,6 @@ std::vector<DiagramSpecialized> ContinousMeasurementDataProtocol::ProcessData(st
std::string received_data;
std::string actual_line;

/*
while(std::getline(input_data, actual_line))
{
std::smatch match_results;
Expand All @@ -53,18 +52,17 @@ std::vector<DiagramSpecialized> ContinousMeasurementDataProtocol::ProcessData(st
{
switch(state)
{
case Constants::States::WaitingForStartLine:
// If a start line was found...
if(std::regex_match(actual_line, std::regex(Constants::Regex::start_line)))
case Constants::States::WaitingForHeaderMessageStart:
// If a header message start line was found
if(std::regex_match(actual_line, std::regex(Constants::Regex::header_start)))
{
state = Constants::States::ProcessingTitleLine;
state = Constants::States::ProcessingHeaderDiagramTitle;
}
break;
case Constants::States::ProcessingTitleLine:
// In any case, we will switch to the next state
state = Constants::States::ProcessingHeadline;
// If this is a diagram title line
if(std::regex_search(actual_line, match_results, std::regex(Constants::Regex::title_line)))
case Constants::States::ProcessingHeaderDiagramTitle:
state = Constants::States::ProcessingHeaderDataLines;
// If a header message diagram title line was found
if(std::regex_search(actual_line, match_results, std::regex(Constants::Regex::header_diagram_title)))
{
// Then we create a diagram object with the title
actual_diagram = DiagramSpecialized(match_results[1]);
Expand All @@ -81,50 +79,63 @@ std::vector<DiagramSpecialized> ContinousMeasurementDataProtocol::ProcessData(st
actual_diagram = DiagramSpecialized(current_date_and_time_string);
// Switching to the next state without a break --> a new line will NOT be fetched, because this line is the headline
}
// The falltrough is not an error in this case, this behaviour needed because there was no diagram title found, the actual_line contains the headline
[[fallthrough]];
case Constants::States::ProcessingHeadline:
// If this is a headline but not a dataline
// (this is needed because with regex it is difficult to define the differences between the data and headlines)
if((std::regex_match(actual_line, std::regex(Constants::Regex::headline))) &&
(!std::regex_match(actual_line, std::regex(Constants::Regex::data_line))))
[[fallthrough]];
case Constants::States::ProcessingHeaderDataLines:
if(std::regex_match(actual_line, std::regex(Constants::Regex::header_datalines)))
{
std::string headline = actual_line;
DataIndexType column_index = 0;
std::string header_data_lines = actual_line;

// Collecting the labels from the headline
while(std::regex_search(headline, match_results, std::regex(Constants::Regex::headline_analyzer)))
{
if(0 == column_index)
{
actual_diagram.SetAxisXTitle(match_results[1]);
}
else
{
actual_diagram.AddNewDataLine(match_results[1]);
}
// Extracting the title of the X axis
std::regex_search(header_data_lines, match_results, std::regex(Constants::Regex::header_dataline_x));
actual_diagram.SetAxisXTitle(match_results[1]);
header_data_lines = match_results.suffix().str();

++column_index;
headline = match_results.suffix().str();
// Extracting the title of the Y axis
while(std::regex_search(header_data_lines, match_results, std::regex(Constants::Regex::header_dataline_y)))
{
actual_diagram.AddNewDataLine(match_results[1], match_results[2]);
header_data_lines = match_results.suffix().str();
}
state = Constants::States::ProcessingDataLines;
state = Constants::States::WaitingForHeaderMessageEnd;
}
else
{
state = Constants::States::WaitingForStartLine;
state = Constants::States::WaitingForHeaderMessageStart;
}
break;
case Constants::States::ProcessingDataLines:
if(std::regex_match(actual_line, std::regex(Constants::Regex::data_line)))
case Constants::States::WaitingForHeaderMessageEnd:
// If a header message end line was found
if(std::regex_match(actual_line, std::regex(Constants::Regex::header_end)))
{
state = Constants::States::WaitingForDataMessageStart;
}
else
{
state = Constants::States::WaitingForHeaderMessageStart;
}
break;
case Constants::States::WaitingForDataMessageStart:
// If a header message end line was found
if(std::regex_match(actual_line, std::regex(Constants::Regex::data_start)))
{
state = Constants::States::ProcessingDataMessageContent;
}
else if(std::regex_match(actual_line, std::regex(Constants::Regex::tail)))
{
assembled_diagrams.push_back(actual_diagram);
state = Constants::States::WaitingForHeaderMessageStart;
}
break;
case Constants::States::ProcessingDataMessageContent:
if(std::regex_match(actual_line, std::regex(Constants::Regex::data_datalines)))
{
std::string data_line = actual_line;
DataIndexType column_index = 0;
DataPointType data_point_x_value = 0;

// Collecting the data from the dataline
while(std::regex_search(data_line, match_results, std::regex(Constants::Regex::data_line_analyzer)))
while(std::regex_search(data_line, match_results, std::regex(Constants::Regex::data_datalines)))
{
if(0 == column_index)
{
Expand All @@ -142,7 +153,7 @@ std::vector<DiagramSpecialized> ContinousMeasurementDataProtocol::ProcessData(st
}
else
{
state = Constants::States::WaitingForStartLine;
state = Constants::States::WaitingForHeaderMessageStart;
break;
}
}
Expand All @@ -152,20 +163,23 @@ std::vector<DiagramSpecialized> ContinousMeasurementDataProtocol::ProcessData(st
}
if((column_index - 1) != actual_diagram.GetTheNumberOfDataLines())
{
state = Constants::States::WaitingForStartLine;
state = Constants::States::WaitingForHeaderMessageStart;
}
}
else
{
if(std::regex_match(actual_line, std::regex(Constants::Regex::end_line)))
if(std::regex_match(actual_line, std::regex(Constants::Regex::data_end)))
{
state = Constants::States::WaitingForDataMessageStart;
}
else
{
assembled_diagrams.push_back(actual_diagram);
state = Constants::States::WaitingForHeaderMessageStart;
}
state = Constants::States::WaitingForStartLine;
}
break;
default:
state = Constants::States::WaitingForStartLine;
state = Constants::States::WaitingForHeaderMessageStart;
throw("The DataProcessor::ProcessData's statemachine switched to an unexpected state: " + std::to_string(static_cast<std::underlying_type<Constants::States>::type>(state)));
break;
}
Expand All @@ -175,15 +189,20 @@ std::vector<DiagramSpecialized> ContinousMeasurementDataProtocol::ProcessData(st
throw("A regex exception was caught: " + std::to_string(exception.code()) + ": " + exception.what());
}
}
*/

return assembled_diagrams;
}

bool ContinousMeasurementDataProtocol::CanThisFileBeProcessed(const std::string path_to_file)
{
(void) path_to_file;
bool bResult = false;

std::string file_extension = QFileInfo(QString::fromStdString(path_to_file)).completeSuffix().toStdString();

if(std::string(Constants::native_file_extension) == file_extension)
{
bResult = true;
}

// The CMDP can not process files
return false;
return bResult;
}
38 changes: 28 additions & 10 deletions application/sources/continous_measurement_data_protocol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <sstream>
#include <algorithm>
#include <functional>
Expand All @@ -35,6 +36,7 @@
#include <QFileInfo>

#include "global.hpp"
#include "protocol_interface.hpp"
#include "data_processing_interface.hpp"
#include "diagram.hpp"

Expand All @@ -57,10 +59,10 @@ class ContinousMeasurementDataProtocol : public DataProcessingInterface
ContinousMeasurementDataProtocol& operator=(const ContinousMeasurementDataProtocol&) = delete;
ContinousMeasurementDataProtocol& operator=(ContinousMeasurementDataProtocol&&) = delete;

std::string GetProtocolName(void) override;
std::vector<DiagramSpecialized> ProcessData(std::istream& input_data) override;
bool CanThisFileBeProcessed(const std::string path_to_file) override;
std::string GetSupportedFileType(void) override {return Constants::native_file_extension;}
virtual std::string GetProtocolName(void) override;
virtual std::vector<DiagramSpecialized> ProcessData(std::istream& input_data) override;
virtual bool CanThisFileBeProcessed(const std::string path_to_file) override;
virtual std::string GetSupportedFileType(void) override {return Constants::native_file_extension;}

private:
struct Constants
Expand All @@ -71,12 +73,28 @@ class ContinousMeasurementDataProtocol : public DataProcessingInterface
enum class States : uint8_t
{
INVALID = 0,
WaitingForTransmission,
HeaderStartReceived,
HeaderContentReceived,
HeaderEndReceived,
DataStartReceived,
DataEndReceived
WaitingForHeaderMessageStart,
ProcessingHeaderDiagramTitle,
ProcessingHeaderDataLines,
WaitingForHeaderMessageEnd,
WaitingForDataMessageStart,
ProcessingDataMessageContent,
};

struct Regex
{
// REGEX strings to search the input data for valid measurement session
static constexpr char header_start[] = R"(^<CMDP_H>$)";
static constexpr char header_diagram_title[] = R"(^<.*>$)";
static constexpr char header_datalines[] = R"(^X:[^,]*,(Y\d+:[^,]*,)+$)";
static constexpr char header_dataline_x[] = R"(^X:([^,]*),)";
static constexpr char header_dataline_y[] = R"(^Y(\d+):([^,]*),)";
static constexpr char header_end[] = R"(^>CMDP_H<$)";
static constexpr char data_start[] = R"(^<CMDP_D>$)";
static constexpr char data_datalines[] = R"(^X:\"(\d+)\",(Y(\d):\"(\d+)\")+$)";
static constexpr char data_end[] = R"(^>CMDP_D<$)";
static constexpr char tail[] = R"(^<CMDP_T>$)";
static constexpr char reset[] = R"(^<CMDP_R>$)";
};
};

Expand Down

0 comments on commit b7d481b

Please sign in to comment.