SMT-RAT  24.02
Toolbox for Strategic and Parallel Satisfiability-Modulo-Theories Solving
XMLWriter.h
Go to the documentation of this file.
1 #pragma once
2 
3 
8 
9 #include <fstream>
10 #include <map>
11 #include <string>
12 
13 namespace benchmax {
14 
15 /**
16  * Writes results to a xml file.
17  */
18 class XMLWriter {
19 private:
20  std::ofstream mFile;
21 
22  void replace(std::string& s, const std::string& pattern, const std::string& replacement) const {
23  std::size_t pos = 0;
24  while ((pos = s.find(pattern, pos)) != std::string::npos) {
25  s.replace(pos, pattern.length(), replacement);
26  pos += replacement.length();
27  }
28  }
29  std::string sanitize(const std::string& s, bool eliminateSlashes = false) const {
30  std::string res(s);
31  replace(res, "<", "&lt;");
32  replace(res, ">", "&gt;");
33  if (eliminateSlashes) replace(res, "/", ".");
34  return res;
35  }
36  std::string sanitizeTool(const std::unique_ptr<Tool>& tool) const {
37  return sanitize(remove_prefix(tool->binary(), settings_tools().tools_common_prefix), false);
38  }
39  std::string sanitizeFile(const fs::path& file) const {
40  return sanitize(remove_prefix(file, settings_benchmarks().input_directories_common_prefix), false);
41  }
42  template<typename Results>
43  std::vector<std::string> collectStatistics(const Results& results) const {
44  std::vector<std::string> res;
45  for (const auto& run: results.data()) {
46  run.second.restore();
47  for (const auto& stat: run.second.additional) {
48  res.emplace_back(stat.first);
49  }
50  run.second.store();
51  }
52  std::sort(res.begin(), res.end());
53  res.erase(
54  std::unique(res.begin(), res.end()),
55  res.end()
56  );
57  return res;
58  }
59 public:
60  /// Open file for writing.
61  XMLWriter(const std::string& filename): mFile(filename) {
62  }
63 
64  /// Write results to file.
65  template<typename Results>
66  void write(const Jobs& jobs, const Results& results) {
67  mFile << "<?xml version=\"1.0\"?>" << std::endl;
68  mFile << "<results>" << std::endl;
69  mFile << "\t<information>" << std::endl;
70  mFile << "\t\t<info name=\"timeout\" type=\"seconds\" value=\"" << std::chrono::seconds(settings_benchmarks().limit_time).count() << "\" />" << std::endl;
71  mFile << "\t</information>" << std::endl;
72  mFile << "\t<solvers prefix=\"" << settings_tools().tools_common_prefix.native() << "\">" << std::endl;
73  for (const auto& tool: jobs.tools()) {
74  mFile << "\t\t<solver solver_id=\"" << sanitizeTool(tool) << "\" />" << std::endl;
75  }
76  mFile << "\t</solvers>" << std::endl;
77  mFile << "\t<statistics>" << std::endl;
78  for (const auto& s: collectStatistics(results)) {
79  mFile << "\t\t<stat name=\"" << sanitize(s) << "\" />" << std::endl;
80  }
81  mFile << "\t</statistics>" << std::endl;
82 
83  mFile << "\t<benchmarks prefix=\"" << settings_benchmarks().input_directories_common_prefix.native() << "\">" << std::endl;
84  for (const auto& filename: jobs.files()) {
85  mFile << "\t\t<file name=\"" << sanitizeFile(filename) << "\">" << std::endl;
86  for (const auto& tool: jobs.tools()) {
87  const auto& result = results.get(tool.get(), filename);
88  if (!result) continue;
89  const auto& res = result->get();
90  res.restore();
91  mFile << "\t\t\t<run solver_id=\"" << sanitizeTool(tool) << "\">" << std::endl;
92  if (!res.additional.empty()) {
93  mFile << "\t\t\t\t<statistics>" << std::endl;
94  for (const auto& stat: res.additional) {
95  mFile << "\t\t\t\t\t<stat name=\"" << sanitize(stat.first) << "\">" << stat.second << "</stat>" << std::endl;
96  }
97  mFile << "\t\t\t\t</statistics>" << std::endl;
98  }
99  mFile << "\t\t\t\t<results>" << std::endl;
100  mFile << "\t\t\t\t\t<result name=\"answer\" type=\"string\">" << res.answer << "</result>" << std::endl;
101  mFile << "\t\t\t\t\t<result name=\"exitcode\" type=\"int\">" << res.exitCode << "</result>" << std::endl;
102  mFile << "\t\t\t\t\t<result name=\"runtime\" type=\"milliseconds\">" << std::chrono::milliseconds(res.time).count() << "</result>" << std::endl;
103  mFile << "\t\t\t\t</results>" << std::endl;
104  mFile << "\t\t\t</run>" << std::endl;
105  res.forget();
106  }
107  mFile << "\t\t</file>" << std::endl;
108  }
109  mFile << "\t</benchmarks>" << std::endl;
110  mFile << "</results>" << std::endl;
111  }
112 };
113 
114 }
Represents a set of jobs, constructed as the cartesian product of a set of tools and a set of benchma...
Definition: Jobs.h:70
const auto & files() const
Returns the set of files.
Definition: Jobs.h:92
const auto & tools() const
Returns the set of tools.
Definition: Jobs.h:88
Stores results for for whole benchmax run.
Definition: Results.h:33
const auto & data() const
Definition: Results.h:50
std::optional< std::reference_wrapper< const BenchmarkResult > > get(const Tool *tool, const fs::path &file) const
Definition: Results.h:41
Writes results to a xml file.
Definition: XMLWriter.h:18
XMLWriter(const std::string &filename)
Open file for writing.
Definition: XMLWriter.h:61
std::string sanitize(const std::string &s, bool eliminateSlashes=false) const
Definition: XMLWriter.h:29
void replace(std::string &s, const std::string &pattern, const std::string &replacement) const
Definition: XMLWriter.h:22
std::string sanitizeTool(const std::unique_ptr< Tool > &tool) const
Definition: XMLWriter.h:36
void write(const Jobs &jobs, const Results &results)
Write results to file.
Definition: XMLWriter.h:66
std::ofstream mFile
Definition: XMLWriter.h:20
std::vector< std::string > collectStatistics(const Results &results) const
Definition: XMLWriter.h:43
std::string sanitizeFile(const fs::path &file) const
Definition: XMLWriter.h:39
void sort(T *array, int size, LessThan lt)
Definition: Sort.h:67
const auto & settings_benchmarks()
Return the benchmark settings.
Definition: benchmarks.h:41
std::filesystem::path remove_prefix(const std::filesystem::path &s, const std::filesystem::path &prefix)
Remove a prefix from a path.
Definition: filesystem.h:54
const auto & settings_tools()
Returns the tool settings.
Definition: Tools.h:59