Nektar++
MetricFileExists.cpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: MetricFileExists.cpp
4//
5// For more information, please see: http://www.nektar.info
6//
7// The MIT License
8//
9// Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10// Department of Aeronautics, Imperial College London (UK), and Scientific
11// Computing and Imaging Institute, University of Utah (USA).
12//
13// Permission is hereby granted, free of charge, to any person obtaining a
14// copy of this software and associated documentation files (the "Software"),
15// to deal in the Software without restriction, including without limitation
16// the rights to use, copy, modify, merge, publish, distribute, sublicense,
17// and/or sell copies of the Software, and to permit persons to whom the
18// Software is furnished to do so, subject to the following conditions:
19//
20// The above copyright notice and this permission notice shall be included
21// in all copies or substantial portions of the Software.
22//
23// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29// DEALINGS IN THE SOFTWARE.
30//
31// Description: Implementation of the FileExists metric.
32//
33///////////////////////////////////////////////////////////////////////////////
34
35#include <regex>
36#include <vector>
37
38#include <boost/algorithm/string.hpp>
39#include <boost/core/ignore_unused.hpp>
40
41#include <MetricFileExists.h>
42
44
45namespace Nektar
46{
48 "FILEEXISTS", MetricFileExists::create);
49
50MetricFileExists::MetricFileExists(TiXmlElement *metric, bool generate)
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}
82
83bool MetricFileExists::v_Test(std::istream &pStdout, std::istream &pStderr)
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}
123
124void MetricFileExists::v_Generate(std::istream &pStdout, std::istream &pStderr)
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}
167} // namespace Nektar
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:208
std::string RegisterCreatorFunction(std::string key, CreatorFunction func)
Definition: Metric.h:133
static std::string type
std::map< std::string, int > m_fileCounts
void v_Generate(std::istream &pStdout, std::istream &pStderr) override
Virtual function to generate the metric. Should be redefined in derived classes.
static MetricSharedPtr create(TiXmlElement *metric, bool generate)
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.
Base class for all metrics. Metric represents a test metric that can be used to evaluate the function...
Definition: Metric.h:65
TiXmlElement * m_metric
Pointer to XML structure containing metric definition.
Definition: Metric.h:101
bool m_generate
Determines whether to generate this metric or not.
Definition: Metric.h:96
MetricFactory & GetMetricFactory()
Definition: Metric.cpp:42