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

virtual ~MetricFileExists ()
 
- 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)
 
virtual bool v_Test (std::istream &pStdout, std::istream &pStderr)
 Virtual function to test the metric. Should be redefined in derived classes. More...
 
virtual void v_Generate (std::istream &pStdout, std::istream &pStderr)
 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()

virtual Nektar::MetricFileExists::~MetricFileExists ( )
inlinevirtual

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:215
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:101

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:129

References MetricFileExists().

◆ v_Generate()

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

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 123 of file MetricFileExists.cpp.

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

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

◆ v_Test()

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

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 = boost::filesystem::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 boost::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 : boost::make_iterator_range(directory_iterator(pwd), {}))
99 {
100 boost::cmatch matches;
101 if (boost::regex_match(e.path().string().c_str(), matches, r))
102 {
103 if (matches.size() == 1)
104 {
105 cnt++;
106 }
107 }
108 }
109
110 // Check if the count matches what we expect.
111 if (it->second != cnt)
112 {
113 std::cerr << "Failed test." << std::endl;
114 std::cerr << " Expected file matches: " << it->second << std::endl;
115 std::cerr << " Found file matches: " << cnt << std::endl;
116 success = false;
117 }
118 }
119
120 return success;
121}

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:138
static MetricSharedPtr create(TiXmlElement *metric, bool generate)
MetricFactory & GetMetricFactory()
Definition: Metric.cpp:42

Definition at line 55 of file MetricFileExists.h.