19 #include "absl/status/status.h"
20 #include "absl/strings/str_format.h"
33 virtual ~GraphSyntax() {}
36 virtual std::string Node(
const std::string&
name,
const std::string& label,
37 const std::string& shape,
38 const std::string& color) = 0;
40 virtual std::string Link(
const std::string& source,
41 const std::string& destination,
42 const std::string& label) = 0;
44 virtual std::string Header(
const std::string&
name) = 0;
47 virtual std::string Footer() = 0;
50 class DotSyntax :
public GraphSyntax {
52 ~DotSyntax()
override {}
54 std::string Node(
const std::string&
name,
const std::string& label,
55 const std::string& shape,
56 const std::string& color)
override {
57 return absl::StrFormat(
"%s [shape=%s label=\"%s\" color=%s]\n",
name, shape,
62 std::string Link(
const std::string& source,
const std::string& destination,
63 const std::string& label)
override {
64 return absl::StrFormat(
"%s -> %s [label=%s]\n", source, destination, label);
68 std::string Header(
const std::string&
name)
override {
69 return absl::StrFormat(
"graph %s {\n",
name);
73 std::string Footer()
override {
return "}\n"; }
76 class GmlSyntax :
public GraphSyntax {
78 ~GmlSyntax()
override {}
80 std::string Node(
const std::string&
name,
const std::string& label,
81 const std::string& shape,
82 const std::string& color)
override {
83 return absl::StrFormat(
92 name, label, shape, color);
96 std::string Link(
const std::string& source,
const std::string& destination,
97 const std::string& label)
override {
98 return absl::StrFormat(
104 label, source, destination);
108 std::string Header(
const std::string&
name)
override {
109 return absl::StrFormat(
116 std::string Footer()
override {
return "]\n"; }
121 class FileGraphExporter :
public GraphExporter {
123 FileGraphExporter(
File*
const file, GraphSyntax*
const syntax)
124 : file_(
file), syntax_(syntax) {}
126 ~FileGraphExporter()
override {}
129 void WriteNode(
const std::string&
name,
const std::string& label,
130 const std::string& shape,
const std::string& color)
override {
131 Append(syntax_->Node(
name, label, shape, color));
135 void WriteLink(
const std::string& source,
const std::string& destination,
136 const std::string& label)
override {
137 Append(syntax_->Link(source, destination, label));
140 void WriteHeader(
const std::string&
name)
override {
141 Append(syntax_->Header(
name));
144 void WriteFooter()
override { Append(syntax_->Footer()); }
147 void Append(
const std::string& str) {
152 std::unique_ptr<GraphSyntax> syntax_;
158 GraphSyntax* syntax =
nullptr;
161 syntax =
new DotSyntax();
165 syntax =
new GmlSyntax();
169 LOG(FATAL) <<
"Unknown graph format";
171 CHECK(syntax !=
nullptr);
172 return new FileGraphExporter(
file, syntax);
static GraphExporter * MakeFileExporter(File *const file, GraphExporter::GraphFormat format)
absl::Status WriteString(File *file, const absl::string_view &contents, int flags)
Collection of objects used to extend the Constraint Solver library.