2021-04-01 21:00:53 +02:00
|
|
|
// Copyright 2010-2021 Google LLC
|
2011-11-23 23:04:58 +00:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
//
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
2017-04-26 17:30:25 +02:00
|
|
|
#include "ortools/util/graph_export.h"
|
2011-11-23 23:04:58 +00:00
|
|
|
|
2015-08-13 16:00:54 +02:00
|
|
|
#include <memory>
|
2022-03-29 17:58:33 +02:00
|
|
|
#include <string>
|
2013-12-05 09:23:39 +00:00
|
|
|
|
2020-05-06 18:22:10 +02:00
|
|
|
#include "absl/status/status.h"
|
2018-10-31 16:18:18 +01:00
|
|
|
#include "absl/strings/str_format.h"
|
2018-06-08 16:40:43 +02:00
|
|
|
#include "ortools/base/file.h"
|
2017-04-26 17:30:25 +02:00
|
|
|
#include "ortools/base/logging.h"
|
|
|
|
|
#include "ortools/base/macros.h"
|
2011-11-23 23:04:58 +00:00
|
|
|
|
|
|
|
|
namespace operations_research {
|
|
|
|
|
|
|
|
|
|
GraphExporter::~GraphExporter() {}
|
|
|
|
|
|
|
|
|
|
namespace {
|
2011-11-25 11:53:00 +00:00
|
|
|
class GraphSyntax {
|
2020-10-22 23:36:58 +02:00
|
|
|
public:
|
2011-11-25 11:53:00 +00:00
|
|
|
virtual ~GraphSyntax() {}
|
|
|
|
|
|
|
|
|
|
// Node in the right syntax.
|
2020-10-28 13:42:36 +01:00
|
|
|
virtual std::string Node(const std::string& name, const std::string& label,
|
|
|
|
|
const std::string& shape,
|
|
|
|
|
const std::string& color) = 0;
|
2011-11-25 11:53:00 +00:00
|
|
|
// Adds one link in the generated graph.
|
2020-10-28 13:42:36 +01:00
|
|
|
virtual std::string Link(const std::string& source,
|
|
|
|
|
const std::string& destination,
|
|
|
|
|
const std::string& label) = 0;
|
2011-11-25 11:53:00 +00:00
|
|
|
// File header.
|
2020-10-28 13:42:36 +01:00
|
|
|
virtual std::string Header(const std::string& name) = 0;
|
2011-11-25 11:53:00 +00:00
|
|
|
|
|
|
|
|
// File footer.
|
2013-12-16 10:24:42 +00:00
|
|
|
virtual std::string Footer() = 0;
|
2011-11-25 11:53:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class DotSyntax : public GraphSyntax {
|
2020-10-22 23:36:58 +02:00
|
|
|
public:
|
2015-04-16 16:31:39 +02:00
|
|
|
~DotSyntax() override {}
|
2011-11-25 11:53:00 +00:00
|
|
|
|
2020-10-28 13:42:36 +01:00
|
|
|
std::string Node(const std::string& name, const std::string& label,
|
|
|
|
|
const std::string& shape,
|
|
|
|
|
const std::string& color) override {
|
2018-10-31 16:18:18 +01:00
|
|
|
return absl::StrFormat("%s [shape=%s label=\"%s\" color=%s]\n", name, shape,
|
|
|
|
|
label, color);
|
2011-11-25 11:53:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Adds one link in the generated graph.
|
2020-10-28 13:42:36 +01:00
|
|
|
std::string Link(const std::string& source, const std::string& destination,
|
|
|
|
|
const std::string& label) override {
|
2018-10-31 16:18:18 +01:00
|
|
|
return absl::StrFormat("%s -> %s [label=%s]\n", source, destination, label);
|
2011-11-25 11:53:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// File header.
|
2020-10-28 13:42:36 +01:00
|
|
|
std::string Header(const std::string& name) override {
|
2018-10-31 16:18:18 +01:00
|
|
|
return absl::StrFormat("graph %s {\n", name);
|
2011-11-25 11:53:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// File footer.
|
2015-04-16 16:31:39 +02:00
|
|
|
std::string Footer() override { return "}\n"; }
|
2011-11-25 11:53:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class GmlSyntax : public GraphSyntax {
|
2020-10-22 23:36:58 +02:00
|
|
|
public:
|
2015-04-16 16:31:39 +02:00
|
|
|
~GmlSyntax() override {}
|
2011-11-25 11:53:00 +00:00
|
|
|
|
2020-10-28 13:42:36 +01:00
|
|
|
std::string Node(const std::string& name, const std::string& label,
|
|
|
|
|
const std::string& shape,
|
|
|
|
|
const std::string& color) override {
|
2020-10-22 23:36:58 +02:00
|
|
|
return absl::StrFormat(
|
|
|
|
|
" node [\n"
|
|
|
|
|
" name \"%s\"\n"
|
|
|
|
|
" label \"%s\"\n"
|
|
|
|
|
" graphics [\n"
|
|
|
|
|
" type \"%s\"\n"
|
|
|
|
|
" fill \"%s\"\n"
|
|
|
|
|
" ]\n"
|
|
|
|
|
" ]\n",
|
|
|
|
|
name, label, shape, color);
|
2011-11-25 11:53:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Adds one link in the generated graph.
|
2020-10-28 13:42:36 +01:00
|
|
|
std::string Link(const std::string& source, const std::string& destination,
|
|
|
|
|
const std::string& label) override {
|
2020-10-22 23:36:58 +02:00
|
|
|
return absl::StrFormat(
|
|
|
|
|
" edge [\n"
|
|
|
|
|
" label \"%s\"\n"
|
|
|
|
|
" source \"%s\"\n"
|
|
|
|
|
" target \"%s\"\n"
|
|
|
|
|
" ]\n",
|
|
|
|
|
label, source, destination);
|
2011-11-25 11:53:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// File header.
|
2020-10-28 13:42:36 +01:00
|
|
|
std::string Header(const std::string& name) override {
|
2020-10-22 23:36:58 +02:00
|
|
|
return absl::StrFormat(
|
|
|
|
|
"graph [\n"
|
|
|
|
|
" name \"%s\"\n",
|
|
|
|
|
name);
|
2011-11-25 11:53:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// File footer.
|
2015-04-16 16:31:39 +02:00
|
|
|
std::string Footer() override { return "]\n"; }
|
2011-11-25 11:53:00 +00:00
|
|
|
};
|
|
|
|
|
|
2011-11-23 23:04:58 +00:00
|
|
|
// Graph exporter that will write to a file with a given format.
|
2011-11-25 12:07:52 +00:00
|
|
|
// Takes ownership of the GraphSyntax parameter.
|
2011-11-23 23:04:58 +00:00
|
|
|
class FileGraphExporter : public GraphExporter {
|
2020-10-22 23:36:58 +02:00
|
|
|
public:
|
2020-10-28 13:42:36 +01:00
|
|
|
FileGraphExporter(File* const file, GraphSyntax* const syntax)
|
2011-11-25 11:53:00 +00:00
|
|
|
: file_(file), syntax_(syntax) {}
|
2011-11-23 23:04:58 +00:00
|
|
|
|
2015-04-16 16:31:39 +02:00
|
|
|
~FileGraphExporter() override {}
|
2011-11-23 23:04:58 +00:00
|
|
|
|
2011-11-25 11:53:00 +00:00
|
|
|
// Write node in GML or DOT format.
|
2020-10-28 13:42:36 +01:00
|
|
|
void WriteNode(const std::string& name, const std::string& label,
|
|
|
|
|
const std::string& shape, const std::string& color) override {
|
2011-11-25 11:53:00 +00:00
|
|
|
Append(syntax_->Node(name, label, shape, color));
|
2011-11-23 23:04:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Adds one link in the generated graph.
|
2020-10-28 13:42:36 +01:00
|
|
|
void WriteLink(const std::string& source, const std::string& destination,
|
|
|
|
|
const std::string& label) override {
|
2011-11-25 11:53:00 +00:00
|
|
|
Append(syntax_->Link(source, destination, label));
|
2011-11-23 23:04:58 +00:00
|
|
|
}
|
|
|
|
|
|
2020-10-28 13:42:36 +01:00
|
|
|
void WriteHeader(const std::string& name) override {
|
2011-11-25 11:53:00 +00:00
|
|
|
Append(syntax_->Header(name));
|
2011-11-23 23:04:58 +00:00
|
|
|
}
|
|
|
|
|
|
2015-04-16 16:31:39 +02:00
|
|
|
void WriteFooter() override { Append(syntax_->Footer()); }
|
2011-11-23 23:04:58 +00:00
|
|
|
|
2020-10-22 23:36:58 +02:00
|
|
|
private:
|
2020-10-28 13:42:36 +01:00
|
|
|
void Append(const std::string& str) {
|
2014-05-13 12:56:44 +00:00
|
|
|
file::WriteString(file_, str, file::Defaults()).IgnoreError();
|
|
|
|
|
}
|
2011-11-25 11:53:00 +00:00
|
|
|
|
2020-10-28 13:42:36 +01:00
|
|
|
File* const file_;
|
2013-12-05 09:23:39 +00:00
|
|
|
std::unique_ptr<GraphSyntax> syntax_;
|
2011-11-23 23:04:58 +00:00
|
|
|
};
|
2020-10-22 23:36:58 +02:00
|
|
|
} // namespace
|
2011-11-23 23:04:58 +00:00
|
|
|
|
2020-10-28 13:42:36 +01:00
|
|
|
GraphExporter* GraphExporter::MakeFileExporter(
|
|
|
|
|
File* const file, GraphExporter::GraphFormat format) {
|
|
|
|
|
GraphSyntax* syntax = nullptr;
|
2011-11-25 11:53:00 +00:00
|
|
|
switch (format) {
|
2020-10-22 23:36:58 +02:00
|
|
|
case GraphExporter::DOT_FORMAT: {
|
|
|
|
|
syntax = new DotSyntax();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case GraphExporter::GML_FORMAT: {
|
|
|
|
|
syntax = new GmlSyntax();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
LOG(FATAL) << "Unknown graph format";
|
2011-11-25 11:53:00 +00:00
|
|
|
}
|
2017-04-19 16:20:56 +02:00
|
|
|
CHECK(syntax != nullptr);
|
2011-11-25 11:53:00 +00:00
|
|
|
return new FileGraphExporter(file, syntax);
|
2011-11-23 23:04:58 +00:00
|
|
|
}
|
2020-10-22 23:36:58 +02:00
|
|
|
} // namespace operations_research
|