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 <vector>
36
37#include <boost/algorithm/string.hpp>
38#include <boost/core/ignore_unused.hpp>
39#include <boost/regex.hpp>
40
41#include <MetricFileExists.h>
42
43using namespace boost::filesystem;
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 = 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}
122
123void MetricFileExists::v_Generate(std::istream &pStdout, std::istream &pStderr)
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}
165} // namespace Nektar
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:215
std::string RegisterCreatorFunction(std::string key, CreatorFunction func)
Definition: Metric.h:138
static std::string type
std::map< std::string, int > m_fileCounts
static MetricSharedPtr create(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.
MetricFileExists(TiXmlElement *metric, bool generate)
virtual void v_Generate(std::istream &pStdout, std::istream &pStderr)
Virtual function to generate 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:70
TiXmlElement * m_metric
Pointer to XML structure containing metric definition.
Definition: Metric.h:106
bool m_generate
Determines whether to generate this metric or not.
Definition: Metric.h:101
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:2
MetricFactory & GetMetricFactory()
Definition: Metric.cpp:42