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

#include <MetricNoWarning.h>

Inheritance diagram for Nektar::MetricNoWarning:
[legend]

Public Member Functions

 ~MetricNoWarning () 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

 MetricNoWarning (TiXmlElement *metric, bool generate)
 Constructor. More...
 
bool v_Test (std::istream &pStdout, std::istream &pStderr) override
 Test if the output contains the warning message. If so, the test fails, otherwise, it passes. More...
 
void v_Generate (std::istream &pStdout, std::istream &pStderr) override
 Test if the output contains the warning message. If so, generate the .tst file. 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::regex m_regexWarning {".*WARNING.*"}
 
std::vector< std::vector< std::string > > m_matches
 
- 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 45 of file MetricNoWarning.h.

Constructor & Destructor Documentation

◆ ~MetricNoWarning()

Nektar::MetricNoWarning::~MetricNoWarning ( )
inlineoverride

Definition at line 48 of file MetricNoWarning.h.

48{};

◆ MetricNoWarning()

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

Constructor.

Definition at line 84 of file MetricNoWarning.cpp.

85 : Metric(metric, generate)
86{
87 // Check if user has provided custom warning regex, which can contain
88 // several matching groups to target specific Warning messages
89 TiXmlElement *regex = metric->FirstChildElement("regex");
90 if (regex)
91 {
92 // Check that user provided a regex for this xml tag
93 const char *tmp = regex->GetText();
94 ASSERTL0(tmp, "No text found in regex tag");
95 m_regexWarning = tmp;
96 }
97
98 // If we generate from output, we don't need to parse the xml file
99 if (m_generate)
100 {
101 return;
102 }
103
104 // Check if user has provided custom matching groups for regex
105 // Note that this is optional, a regex without matching groups is
106 // also permissible
107 TiXmlElement *matches = metric->FirstChildElement("matches");
108 if (matches)
109 {
110 // Go through all "match" tags
111 for (TiXmlElement *match = matches->FirstChildElement("match"); match;
112 match = match->NextSiblingElement("match"))
113 {
114 // Add vector with regex groups
115 m_matches.push_back(std::vector<std::string>());
116
117 // Go though all "field" tags
118 for (TiXmlElement *field = match->FirstChildElement("field"); field;
119 field = field->NextSiblingElement("field"))
120 {
121 // Extract field text (i.e. regex group)
122 const char *tmp = field->GetText();
123 // Check that user provided a text
124 ASSERTL0(tmp, "No text that specifies regex group "
125 "found in field tag");
126 m_matches.back().push_back(tmp);
127 }
128 }
129
130 // Check if user provided any match tags
131 ASSERTL0(m_matches.size(),
132 "No match tag that specifies "
133 "regex groups was found inside matches tag.");
134
135 // Check that all set of regex groups are the same size,
136 // i.e. contains the same number of "field" tags
137 int size_min = 1E3;
138 int size_max = 0;
139 for (const auto &match : m_matches)
140 {
141 size_min = std::min((int)match.size(), size_min);
142 size_max = std::max((int)match.size(), size_max);
143 }
144 ASSERTL0(size_min != 0, "No valid field tags found "
145 "for one of the match tags");
146 ASSERTL0(size_min == size_max, "Number of valid field tags "
147 "not the same for all match tags");
148 }
149}
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:208
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
std::vector< std::vector< std::string > > m_matches

References ASSERTL0, FilterPython_Function::field, Nektar::Metric::m_generate, m_matches, and m_regexWarning.

Referenced by create().

Member Function Documentation

◆ create()

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

Definition at line 50 of file MetricNoWarning.h.

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

References MetricNoWarning().

◆ v_Generate()

void Nektar::MetricNoWarning::v_Generate ( std::istream &  pStdout,
std::istream &  pStderr 
)
overrideprotectedvirtual

Test if the output contains the warning message. If so, generate the .tst file.

Implements Nektar::Metric.

Definition at line 217 of file MetricNoWarning.cpp.

218{
219 boost::ignore_unused(pStderr);
220
221 // Check both standard output and error output
222 for (std::string line; getline(pStdout, line) || getline(pStderr, line);)
223 {
224 std::smatch matches;
225
226 // Check if regex match against given output
227 if (std::regex_search(line, matches, m_regexWarning))
228 {
229 // If regex groups were not found, continue
230 if (matches.size() == 1)
231 {
232 continue;
233 }
234
235 // Add vector with regex groups
236 m_matches.push_back(std::vector<std::string>());
237
238 // Save all regex groups
239 for (int i = 1; i < matches.size(); ++i)
240 {
241 // Construct string object that contains submatch
242 std::string submatch(matches[i].first, matches[i].second);
243
244 m_matches.back().push_back(submatch);
245 }
246 }
247 }
248
249 // Remove matches if they already exist.
250 TiXmlElement *matches = m_metric->FirstChildElement("matches");
251 if (matches)
252 {
253 ASSERTL0(m_metric->RemoveChild(matches), "Couldn't remove matches "
254 "from metric")
255 }
256
257 // Add new "matches" tag
258 matches = new TiXmlElement("matches");
259 m_metric->LinkEndChild(matches);
260
261 // Add all "match" tags under "matches" tag
262 for (const auto &match_it : m_matches)
263 {
264 TiXmlElement *match = new TiXmlElement("match");
265 matches->LinkEndChild(match);
266
267 int j = 0;
268 for (const auto &field_it : match_it)
269 {
270 TiXmlElement *field = new TiXmlElement("field");
271 match->LinkEndChild(field);
272
273 field->SetAttribute("id", boost::lexical_cast<std::string>(j++));
274
275 field->LinkEndChild(new TiXmlText(field_it));
276 }
277 }
278}
TiXmlElement * m_metric
Pointer to XML structure containing metric definition.
Definition: Metric.h:101

References ASSERTL0, FilterPython_Function::field, m_matches, Nektar::Metric::m_metric, and m_regexWarning.

◆ v_Test()

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

Test if the output contains the warning message. If so, the test fails, otherwise, it passes.

Implements Nektar::Metric.

Definition at line 155 of file MetricNoWarning.cpp.

156{
157 boost::ignore_unused(pStdout, pStderr);
158
159 // Loop over both standard output and error output
160 for (std::string line; getline(pStdout, line) || getline(pStderr, line);)
161 {
162 std::smatch matches;
163
164 // Check if regex match against given output
165 if (std::regex_search(line, matches, m_regexWarning))
166 {
167 // Test fails if regex matches line and
168 // no matching groups have been specified
169 if (!m_matches.size())
170 {
171 return false;
172 }
173 // If regex groups are specified, the test fails if any of
174 // them matches the groups in "matches"
175 for (const auto &match : m_matches)
176 {
177 bool all_match = false;
178 for (int i = 1;
179 i < matches.size() && matches.size() == match.size() + 1;
180 ++i)
181 {
182 // Construct string object that contains submatch
183 std::string submatch(matches[i].first, matches[i].second);
184
185 // Compare to specified pattern
186 if (submatch == match[i - 1])
187 {
188 all_match = true;
189 continue;
190 }
191 else
192 {
193 all_match = false;
194 break;
195 }
196 }
197 if (all_match)
198 {
199 return false;
200 }
201 else
202 {
203 continue;
204 }
205 }
206 }
207 }
208
209 // If we arrived here, the test passed
210 return true;
211}

References m_matches, and m_regexWarning.

Member Data Documentation

◆ m_matches

std::vector<std::vector<std::string> > Nektar::MetricNoWarning::m_matches
protected

Definition at line 62 of file MetricNoWarning.h.

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

◆ m_regexWarning

std::regex Nektar::MetricNoWarning::m_regexWarning {".*WARNING.*"}
protected

Definition at line 59 of file MetricNoWarning.h.

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

◆ type

std::string Nektar::MetricNoWarning::type
static
Initial value:
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 MetricNoWarning.h.