OR-Tools  8.2
graph_export.cc
Go to the documentation of this file.
1// Copyright 2010-2018 Google LLC
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
15
16#include <memory>
17
18#include "absl/status/status.h"
19#include "absl/strings/str_format.h"
20#include "ortools/base/file.h"
22#include "ortools/base/macros.h"
23
24namespace operations_research {
25
27
28namespace {
29class GraphSyntax {
30 public:
31 virtual ~GraphSyntax() {}
32
33 // Node in the right syntax.
34 virtual std::string Node(const std::string& name, const std::string& label,
35 const std::string& shape,
36 const std::string& color) = 0;
37 // Adds one link in the generated graph.
38 virtual std::string Link(const std::string& source,
39 const std::string& destination,
40 const std::string& label) = 0;
41 // File header.
42 virtual std::string Header(const std::string& name) = 0;
43
44 // File footer.
45 virtual std::string Footer() = 0;
46};
47
48class DotSyntax : public GraphSyntax {
49 public:
50 ~DotSyntax() override {}
51
52 std::string Node(const std::string& name, const std::string& label,
53 const std::string& shape,
54 const std::string& color) override {
55 return absl::StrFormat("%s [shape=%s label=\"%s\" color=%s]\n", name, shape,
56 label, color);
57 }
58
59 // Adds one link in the generated graph.
60 std::string Link(const std::string& source, const std::string& destination,
61 const std::string& label) override {
62 return absl::StrFormat("%s -> %s [label=%s]\n", source, destination, label);
63 }
64
65 // File header.
66 std::string Header(const std::string& name) override {
67 return absl::StrFormat("graph %s {\n", name);
68 }
69
70 // File footer.
71 std::string Footer() override { return "}\n"; }
72};
73
74class GmlSyntax : public GraphSyntax {
75 public:
76 ~GmlSyntax() override {}
77
78 std::string Node(const std::string& name, const std::string& label,
79 const std::string& shape,
80 const std::string& color) override {
81 return absl::StrFormat(
82 " node [\n"
83 " name \"%s\"\n"
84 " label \"%s\"\n"
85 " graphics [\n"
86 " type \"%s\"\n"
87 " fill \"%s\"\n"
88 " ]\n"
89 " ]\n",
90 name, label, shape, color);
91 }
92
93 // Adds one link in the generated graph.
94 std::string Link(const std::string& source, const std::string& destination,
95 const std::string& label) override {
96 return absl::StrFormat(
97 " edge [\n"
98 " label \"%s\"\n"
99 " source \"%s\"\n"
100 " target \"%s\"\n"
101 " ]\n",
102 label, source, destination);
103 }
104
105 // File header.
106 std::string Header(const std::string& name) override {
107 return absl::StrFormat(
108 "graph [\n"
109 " name \"%s\"\n",
110 name);
111 }
112
113 // File footer.
114 std::string Footer() override { return "]\n"; }
115};
116
117// Graph exporter that will write to a file with a given format.
118// Takes ownership of the GraphSyntax parameter.
119class FileGraphExporter : public GraphExporter {
120 public:
121 FileGraphExporter(File* const file, GraphSyntax* const syntax)
122 : file_(file), syntax_(syntax) {}
123
124 ~FileGraphExporter() override {}
125
126 // Write node in GML or DOT format.
127 void WriteNode(const std::string& name, const std::string& label,
128 const std::string& shape, const std::string& color) override {
129 Append(syntax_->Node(name, label, shape, color));
130 }
131
132 // Adds one link in the generated graph.
133 void WriteLink(const std::string& source, const std::string& destination,
134 const std::string& label) override {
135 Append(syntax_->Link(source, destination, label));
136 }
137
138 void WriteHeader(const std::string& name) override {
139 Append(syntax_->Header(name));
140 }
141
142 void WriteFooter() override { Append(syntax_->Footer()); }
143
144 private:
145 void Append(const std::string& str) {
146 file::WriteString(file_, str, file::Defaults()).IgnoreError();
147 }
148
149 File* const file_;
150 std::unique_ptr<GraphSyntax> syntax_;
151};
152} // namespace
153
155 File* const file, GraphExporter::GraphFormat format) {
156 GraphSyntax* syntax = nullptr;
157 switch (format) {
159 syntax = new DotSyntax();
160 break;
161 }
163 syntax = new GmlSyntax();
164 break;
165 }
166 default:
167 LOG(FATAL) << "Unknown graph format";
168 }
169 CHECK(syntax != nullptr);
170 return new FileGraphExporter(file, syntax);
171}
172} // namespace operations_research
#define CHECK(condition)
Definition: base/logging.h:495
#define LOG(severity)
Definition: base/logging.h:420
Definition: base/file.h:32
static GraphExporter * MakeFileExporter(File *const file, GraphExporter::GraphFormat format)
const std::string name
const int FATAL
Definition: log_severity.h:32
Definition: file.cc:141
absl::Status WriteString(File *file, const absl::string_view &contents, int flags)
Definition: file.cc:184
int Defaults()
Definition: base/file.h:119
The vehicle routing library lets one model and solve generic vehicle routing problems ranging from th...