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

#include <MetricFile.h>

Inheritance diagram for Nektar::MetricFile:
[legend]

Public Member Functions

virtual ~MetricFile ()
 
- 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

 MetricFile (TiXmlElement *metric, bool generate)
 
std::string CalculateHash (std::string filename)
 
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, std::string > m_filehash
 Stores filenames to perform hash on. More...
 
- 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 MetricFile.h.

Constructor & Destructor Documentation

◆ ~MetricFile()

virtual Nektar::MetricFile::~MetricFile ( )
inlinevirtual

Definition at line 46 of file MetricFile.h.

47 {
48 }

◆ MetricFile()

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

Definition at line 50 of file MetricFile.cpp.

51 : Metric(metric, generate)
52{
53 TiXmlElement *file = metric->FirstChildElement("file");
54 ASSERTL0(file, "Missing file tag for file metric!");
55
56 while (file)
57 {
58 std::string filename, sha1hash;
59
60 if (file->Attribute("filename"))
61 {
62 filename = file->Attribute("filename");
63 }
64 else
65 {
66 ASSERTL0(false, "Missing filename for file tag!");
67 }
68
69 if (!m_generate)
70 {
71 TiXmlElement *sha1 = file->FirstChildElement("sha1");
72 ASSERTL0(sha1, "Missing SHA1 hash for file " + filename);
73 sha1hash = sha1->GetText();
74 ASSERTL0(sha1hash.size() == 40, "Incorrect length for SHA1 hash");
75 }
76
77 m_filehash[filename] = sha1hash;
78
79 file = file->NextSiblingElement("file");
80 }
81}
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:215
std::map< std::string, std::string > m_filehash
Stores filenames to perform hash on.
Definition: MetricFile.h:66
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
Definition: sha1.cpp:72

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

Referenced by create().

Member Function Documentation

◆ CalculateHash()

std::string Nektar::MetricFile::CalculateHash ( std::string  filename)
protected

Definition at line 83 of file MetricFile.cpp.

84{
85 int fdot = pfilename.find_last_of('.');
86 string ending = pfilename.substr(fdot);
87 string filename;
88
89 if (ending == ".fld" || ending == ".chk" || ending == ".rst")
90 {
91 TiXmlDocument *xmlFldFile;
92 fs::path pathfilename(pfilename);
93
94 if (fs::is_directory(pathfilename))
95 {
96 std::vector<fs::path> dirfiles;
97
98 // make list of all files in directoryj
99 copy(fs::directory_iterator(pathfilename), fs::directory_iterator(),
100 back_inserter(dirfiles));
101
102 xmlFldFile = new TiXmlDocument;
103
104 // load all them into the
105 for (int i = 0; i < dirfiles.size(); ++i)
106 {
107 std::string infile = PortablePath(dirfiles[i]);
108 std::ifstream file(infile.c_str());
109 ASSERTL0(file.good(), "Unable to open file: " + infile);
110 file >> (*xmlFldFile);
111 }
112 }
113 else
114 {
115 xmlFldFile = new TiXmlDocument(pfilename);
116 xmlFldFile->LoadFile(pfilename);
117 }
118
119 // strip out meta data before check
120 TiXmlElement *vNektar = xmlFldFile->FirstChildElement("NEKTAR");
121 while (vNektar)
122 {
123
124 TiXmlNode *vMetaData = vNektar->FirstChild("Metadata");
125
126 // delete MetaData section
127 if (vMetaData)
128 {
129 vNektar->RemoveChild(vMetaData);
130 }
131 vNektar = vNektar->NextSiblingElement("NEKTAR");
132 }
133
134 filename = pfilename + ".tmp";
135 xmlFldFile->SaveFile(filename);
136 }
137 else
138 {
139 filename = pfilename;
140 }
141
142 // Open file.
143 std::ifstream testFile(filename.c_str(), std::ios::binary);
144 ASSERTL0(testFile.is_open(), "Error opening file " + filename);
145
146 // Read in file contents.
147 std::vector<char> fileContents((std::istreambuf_iterator<char>(testFile)),
148 std::istreambuf_iterator<char>());
149
150 // Calculate SHA1 hash.
151 unsigned char hash[20];
152 char strhash[41];
153
154 sha1::calc((void *)&fileContents[0], fileContents.size(), hash);
155 sha1::toHexString(hash, strhash);
156
157 // Close file and return string containing SHA1 hash.
158 testFile.close();
159
160 return std::string(strhash);
161}
def copy(self)
Definition: pycml.py:2663
std::string PortablePath(const boost::filesystem::path &path)
create portable path on different platforms for boost::filesystem path
Definition: FileSystem.cpp:45
void toHexString(const unsigned char *hash, char *hexstring)
Calculate a string which represents the SHA1 hash as a hexadecimal number.
Definition: sha1.cpp:213
void calc(const void *src, const int bytelength, unsigned char *hash)
Calculate the SHA1 hash of some data set.
Definition: sha1.cpp:150

References ASSERTL0, sha1::calc(), CellMLToNektar.pycml::copy(), Nektar::LibUtilities::PortablePath(), and sha1::toHexString().

Referenced by v_Generate(), and v_Test().

◆ create()

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

Definition at line 50 of file MetricFile.h.

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

References MetricFile().

◆ v_Generate()

void Nektar::MetricFile::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 185 of file MetricFile.cpp.

186{
187 boost::ignore_unused(pStdout, pStderr);
188
189 std::map<std::string, std::string>::iterator it;
190
191 // Update SHA1 hashes.
192 for (it = m_filehash.begin(); it != m_filehash.end(); ++it)
193 {
194 std::string filehash = CalculateHash(it->first);
195 m_filehash[it->first] = filehash;
196 }
197
198 // Write new XML structure.
199 TiXmlElement *file = m_metric->FirstChildElement("file");
200 while (file)
201 {
202 std::string filename = file->Attribute("filename");
203 file->Clear();
204
205 TiXmlElement *sha1 = new TiXmlElement("sha1");
206
207 ASSERTL0(m_filehash.count(filename) != 0, "Couldn't find file " +
208 filename +
209 " in list of calculated"
210 "hashes");
211
212 sha1->LinkEndChild(new TiXmlText(m_filehash[filename]));
213 file->LinkEndChild(sha1);
214 file = file->NextSiblingElement("file");
215 }
216}
std::string CalculateHash(std::string filename)
Definition: MetricFile.cpp:83
TiXmlElement * m_metric
Pointer to XML structure containing metric definition.
Definition: Metric.h:106

References ASSERTL0, CalculateHash(), m_filehash, and Nektar::Metric::m_metric.

◆ v_Test()

bool Nektar::MetricFile::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 163 of file MetricFile.cpp.

164{
165 boost::ignore_unused(pStdout, pStderr);
166
167 std::map<std::string, std::string>::iterator it;
168 bool success = true;
169
170 for (it = m_filehash.begin(); it != m_filehash.end(); ++it)
171 {
172 std::string filehash = CalculateHash(it->first);
173 if (!boost::iequals(filehash, it->second))
174 {
175 std::cerr << "Failed SHA1 hash test." << std::endl;
176 std::cerr << " Expected: " << it->second << std::endl;
177 std::cerr << " Result: " << filehash << std::endl;
178 success = false;
179 }
180 }
181
182 return success;
183}

References CalculateHash(), and m_filehash.

Member Data Documentation

◆ m_filehash

std::map<std::string, std::string> Nektar::MetricFile::m_filehash
protected

Stores filenames to perform hash on.

Definition at line 66 of file MetricFile.h.

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

◆ type

std::string Nektar::MetricFile::type
static
Initial value:
=
std::string RegisterCreatorFunction(std::string key, CreatorFunction func)
Definition: Metric.h:138
static MetricSharedPtr create(TiXmlElement *metric, bool generate)
Definition: MetricFile.h:50
MetricFactory & GetMetricFactory()
Definition: Metric.cpp:42

Definition at line 55 of file MetricFile.h.