Nektar++
Public Member Functions | Static Public Member Functions | Static Public Attributes | Protected Member Functions | Protected Attributes | List of all members
Nektar::MetricFileExists Class Reference

#include <MetricFileExists.h>

Inheritance diagram for Nektar::MetricFileExists:
[legend]

Public Member Functions

 ~MetricFileExists () override
 
- Public Member Functions inherited from Nektar::Metric
 Metric (TiXmlElement *metric, bool generate)
 Constructor. More...
 
virtual ~Metric ()=default
 
bool Test (std::istream &pStdout, std::istream &pStderr)
 Calls a metric's v_Test function (or v_Generate if m_generate). More...
 
void Generate (std::istream &pStdout, std::istream &pStderr)
 
std::string GetType ()
 Return metric type. More...
 
int GetID ()
 Return metric ID. More...
 
bool SupportsAverage () const
 Return whether this metric supports averaging results from multiple runs. More...
 

Static Public Member Functions

static MetricSharedPtr create (TiXmlElement *metric, bool generate)
 

Static Public Attributes

static std::string type
 

Protected Member Functions

 MetricFileExists (TiXmlElement *metric, bool generate)
 
bool v_Test (std::istream &pStdout, std::istream &pStderr) override
 Virtual function to test the metric. Should be redefined in derived classes. More...
 
void v_Generate (std::istream &pStdout, std::istream &pStderr) override
 Virtual function to generate the metric. Should be redefined in derived classes. More...
 
virtual bool v_Test (std::istream &pStdout, std::istream &pStderr)=0
 Virtual function to test the metric. Should be redefined in derived classes. More...
 
virtual void v_Generate (std::istream &pStdout, std::istream &pSrderr)=0
 Virtual function to generate the metric. Should be redefined in derived classes. More...
 

Protected Attributes

std::map< std::string, int > m_fileCounts
 
- Protected Attributes inherited from Nektar::Metric
int m_id
 Stores the ID of this metric. More...
 
std::string m_type
 Stores the type of this metric (uppercase). More...
 
bool m_generate
 Determines whether to generate this metric or not. More...
 
bool m_average = false
 Indicates whether a metric supports averaging results from multiple runs. More...
 
TiXmlElement * m_metric
 Pointer to XML structure containing metric definition. More...
 

Detailed Description

Definition at line 43 of file MetricFileExists.h.

Constructor & Destructor Documentation

◆ ~MetricFileExists()

Nektar::MetricFileExists::~MetricFileExists ( )
inlineoverride

Definition at line 46 of file MetricFileExists.h.

47 {
48 }

◆ MetricFileExists()

Nektar::MetricFileExists::MetricFileExists ( TiXmlElement *  metric,
bool  generate 
)
protected

Definition at line 50 of file MetricFileExists.cpp.

51 : Metric(metric, generate)
52{
53 TiXmlElement *file = metric->FirstChildElement("file");
54 ASSERTL0(file, "Missing file tag for FileExists metric!");
55
56 // Read metric and populate list of patterns to search for.
57 while (file)
58 {
59 std::string pattern, count;
60
61 // Check the pattern has been defined as an attribute and store.
62 ASSERTL0(file->Attribute("pattern"), "Missing filename for file tag!");
63 pattern = file->Attribute("pattern");
64
65 // If we are testing, extract and store the expected file count
66 // from the content portion of the tag.
67 if (!m_generate)
68 {
69 count = file->GetText();
70 m_fileCounts[pattern] = std::stoi(count);
71 }
72 // If we are generating, put a default value of zero so as to still
73 // have the pattern in the map.
74 else
75 {
76 m_fileCounts[pattern] = 0;
77 }
78
79 file = file->NextSiblingElement("file");
80 }
81}
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:208
std::map< std::string, int > m_fileCounts
Metric(TiXmlElement *metric, bool generate)
Constructor.
Definition: Metric.cpp:51
bool m_generate
Determines whether to generate this metric or not.
Definition: Metric.h:96

References ASSERTL0, m_fileCounts, and Nektar::Metric::m_generate.

Referenced by create().

Member Function Documentation

◆ create()

static MetricSharedPtr Nektar::MetricFileExists::create ( TiXmlElement *  metric,
bool  generate 
)
inlinestatic

Definition at line 50 of file MetricFileExists.h.

51 {
52 return MetricSharedPtr(new MetricFileExists(metric, generate));
53 }
MetricFileExists(TiXmlElement *metric, bool generate)
std::shared_ptr< Metric > MetricSharedPtr
A shared pointer to an EquationSystem object.
Definition: Metric.h:124

References MetricFileExists().

◆ v_Generate()

void Nektar::MetricFileExists::v_Generate ( std::istream &  pStdout,
std::istream &  pSrderr 
)
overrideprotectedvirtual

Virtual function to generate the metric. Should be redefined in derived classes.

Parameters
pStdoutReference to test output stream.
pSrderrReference to test error stream.

Implements Nektar::Metric.

Definition at line 124 of file MetricFileExists.cpp.

125{
126 boost::ignore_unused(pStdout, pStderr);
127
128 // Update File counts.
129 auto pwd = fs::current_path();
130
131 for (auto it = m_fileCounts.begin(); it != m_fileCounts.end(); ++it)
132 {
133 int cnt = 0;
134 std::regex r(it->first.c_str());
135 for (auto &e : fs::directory_iterator(pwd))
136 {
137 std::smatch matches;
138 std::string filename = e.path().string();
139 if (std::regex_match(filename, matches, r))
140 {
141 if (matches.size() == 1)
142 {
143 cnt++;
144 }
145 }
146 }
147 m_fileCounts[it->first] = cnt;
148 }
149
150 // Write new XML structure.
151 TiXmlElement *file = m_metric->FirstChildElement("file");
152 while (file)
153 {
154 std::string pattern = file->Attribute("pattern");
155 file->Clear();
156
157 ASSERTL0(m_fileCounts.count(pattern) != 0, "Couldn't find pattern " +
158 pattern +
159 " in list of calculated"
160 "hashes");
161
162 file->LinkEndChild(
163 new TiXmlText(std::to_string(m_fileCounts[pattern]).c_str()));
164 file = file->NextSiblingElement("file");
165 }
166}
TiXmlElement * m_metric
Pointer to XML structure containing metric definition.
Definition: Metric.h:101

References ASSERTL0, m_fileCounts, and Nektar::Metric::m_metric.

◆ v_Test()

bool Nektar::MetricFileExists::v_Test ( std::istream &  pStdout,
std::istream &  pStderr 
)
overrideprotectedvirtual

Virtual function to test the metric. Should be redefined in derived classes.

Parameters
pStdoutReference to test output stream.
pStderrReference to test error stream.
Returns
true if the test passes, false otherwise.

Implements Nektar::Metric.

Definition at line 83 of file MetricFileExists.cpp.

84{
85 boost::ignore_unused(pStdout, pStderr);
86
87 bool success = true;
88 auto pwd = fs::current_path();
89
90 // Check each pattern in turn
91 for (auto it = m_fileCounts.begin(); it != m_fileCounts.end(); ++it)
92 {
93 int cnt = 0;
94 std::regex r(it->first.c_str());
95
96 // Examine each file in the current path and check if it matches the
97 // pattern provided. Count the number of files which match.
98 for (auto &e : fs::directory_iterator(pwd))
99 {
100 std::smatch matches;
101 std::string filename = e.path().string();
102 if (std::regex_match(filename, matches, r))
103 {
104 if (matches.size() == 1)
105 {
106 cnt++;
107 }
108 }
109 }
110
111 // Check if the count matches what we expect.
112 if (it->second != cnt)
113 {
114 std::cerr << "Failed test." << std::endl;
115 std::cerr << " Expected file matches: " << it->second << std::endl;
116 std::cerr << " Found file matches: " << cnt << std::endl;
117 success = false;
118 }
119 }
120
121 return success;
122}

References m_fileCounts.

Member Data Documentation

◆ m_fileCounts

std::map<std::string, int> Nektar::MetricFileExists::m_fileCounts
protected

Definition at line 58 of file MetricFileExists.h.

Referenced by MetricFileExists(), v_Generate(), and v_Test().

◆ type

std::string Nektar::MetricFileExists::type
static
Initial value:
"FILEEXISTS", MetricFileExists::create)
std::string RegisterCreatorFunction(std::string key, CreatorFunction func)
Definition: Metric.h:133
static MetricSharedPtr create(TiXmlElement *metric, bool generate)
MetricFactory & GetMetricFactory()
Definition: Metric.cpp:42

Definition at line 55 of file MetricFileExists.h.