Files
ortools-clone/ortools/util/graph_export.cc

174 lines
5.1 KiB
C++
Raw Normal View History

// Copyright 2010-2021 Google LLC
// 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.
#include "ortools/util/graph_export.h"
#include <memory>
#include <string>
#include "absl/status/status.h"
dotnet: Remove reference to dotnet release command - Currently not implemented... Add abseil patch - Add patches/absl-config.cmake Makefile: Add abseil-cpp on unix - Force abseil-cpp SHA1 to 45221cc note: Just before the PR #136 which break all CMake Makefile: Add abseil-cpp on windows - Force abseil-cpp SHA1 to 45221cc note: Just before the PR #136 which break all CMake CMake: Add abseil-cpp - Force abseil-cpp SHA1 to 45221cc note: Just before the PR #136 which break all CMake port to absl: C++ Part - Fix warning with the use of ABSL_MUST_USE_RESULT > The macro must appear as the very first part of a function declaration or definition: ... Note: past advice was to place the macro after the argument list. src: dependencies/sources/abseil-cpp-master/absl/base/attributes.h:418 - Rename enum after windows clash - Remove non compact table constraints - Change index type from int64 to int in routing library - Fix file_nonport compilation on windows - Fix another naming conflict with windows (NO_ERROR is a macro) - Cleanup hash containers; work on sat internals - Add optional_boolean sub-proto Sync cpp examples with internal code - reenable issue173 after reducing number of loops port to absl: Python Part - Add back cp_model.INT32_MIN|MAX for examples Update Python examples - Add random_tsp.py - Run words_square example - Run magic_square in python tests port to absl: Java Part - Fix compilation of the new routing parameters in java - Protect some code from SWIG parsing Update Java Examples port to absl: .Net Part Update .Net examples work on sat internals; Add C++ CP-SAT CpModelBuilder API; update sample code and recipes to use the new API; sync with internal code Remove VS 2015 in Appveyor-CI - abseil-cpp does not support VS 2015... improve tables upgrade C++ sat examples to use the new API; work on sat internals update license dates rewrite jobshop_ft06_distance.py to use the CP-SAT solver rename last example revert last commit more work on SAT internals fix
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"
#include "ortools/base/logging.h"
#include "ortools/base/macros.h"
namespace operations_research {
GraphExporter::~GraphExporter() {}
namespace {
class GraphSyntax {
2020-10-22 23:36:58 +02:00
public:
virtual ~GraphSyntax() {}
// Node in the right syntax.
virtual std::string Node(const std::string& name, const std::string& label,
const std::string& shape,
const std::string& color) = 0;
// Adds one link in the generated graph.
virtual std::string Link(const std::string& source,
const std::string& destination,
const std::string& label) = 0;
// File header.
virtual std::string Header(const std::string& name) = 0;
// File footer.
virtual std::string Footer() = 0;
};
class DotSyntax : public GraphSyntax {
2020-10-22 23:36:58 +02:00
public:
~DotSyntax() override {}
std::string Node(const std::string& name, const std::string& label,
const std::string& shape,
const std::string& color) override {
dotnet: Remove reference to dotnet release command - Currently not implemented... Add abseil patch - Add patches/absl-config.cmake Makefile: Add abseil-cpp on unix - Force abseil-cpp SHA1 to 45221cc note: Just before the PR #136 which break all CMake Makefile: Add abseil-cpp on windows - Force abseil-cpp SHA1 to 45221cc note: Just before the PR #136 which break all CMake CMake: Add abseil-cpp - Force abseil-cpp SHA1 to 45221cc note: Just before the PR #136 which break all CMake port to absl: C++ Part - Fix warning with the use of ABSL_MUST_USE_RESULT > The macro must appear as the very first part of a function declaration or definition: ... Note: past advice was to place the macro after the argument list. src: dependencies/sources/abseil-cpp-master/absl/base/attributes.h:418 - Rename enum after windows clash - Remove non compact table constraints - Change index type from int64 to int in routing library - Fix file_nonport compilation on windows - Fix another naming conflict with windows (NO_ERROR is a macro) - Cleanup hash containers; work on sat internals - Add optional_boolean sub-proto Sync cpp examples with internal code - reenable issue173 after reducing number of loops port to absl: Python Part - Add back cp_model.INT32_MIN|MAX for examples Update Python examples - Add random_tsp.py - Run words_square example - Run magic_square in python tests port to absl: Java Part - Fix compilation of the new routing parameters in java - Protect some code from SWIG parsing Update Java Examples port to absl: .Net Part Update .Net examples work on sat internals; Add C++ CP-SAT CpModelBuilder API; update sample code and recipes to use the new API; sync with internal code Remove VS 2015 in Appveyor-CI - abseil-cpp does not support VS 2015... improve tables upgrade C++ sat examples to use the new API; work on sat internals update license dates rewrite jobshop_ft06_distance.py to use the CP-SAT solver rename last example revert last commit more work on SAT internals fix
2018-10-31 16:18:18 +01:00
return absl::StrFormat("%s [shape=%s label=\"%s\" color=%s]\n", name, shape,
label, color);
}
// Adds one link in the generated graph.
std::string Link(const std::string& source, const std::string& destination,
const std::string& label) override {
dotnet: Remove reference to dotnet release command - Currently not implemented... Add abseil patch - Add patches/absl-config.cmake Makefile: Add abseil-cpp on unix - Force abseil-cpp SHA1 to 45221cc note: Just before the PR #136 which break all CMake Makefile: Add abseil-cpp on windows - Force abseil-cpp SHA1 to 45221cc note: Just before the PR #136 which break all CMake CMake: Add abseil-cpp - Force abseil-cpp SHA1 to 45221cc note: Just before the PR #136 which break all CMake port to absl: C++ Part - Fix warning with the use of ABSL_MUST_USE_RESULT > The macro must appear as the very first part of a function declaration or definition: ... Note: past advice was to place the macro after the argument list. src: dependencies/sources/abseil-cpp-master/absl/base/attributes.h:418 - Rename enum after windows clash - Remove non compact table constraints - Change index type from int64 to int in routing library - Fix file_nonport compilation on windows - Fix another naming conflict with windows (NO_ERROR is a macro) - Cleanup hash containers; work on sat internals - Add optional_boolean sub-proto Sync cpp examples with internal code - reenable issue173 after reducing number of loops port to absl: Python Part - Add back cp_model.INT32_MIN|MAX for examples Update Python examples - Add random_tsp.py - Run words_square example - Run magic_square in python tests port to absl: Java Part - Fix compilation of the new routing parameters in java - Protect some code from SWIG parsing Update Java Examples port to absl: .Net Part Update .Net examples work on sat internals; Add C++ CP-SAT CpModelBuilder API; update sample code and recipes to use the new API; sync with internal code Remove VS 2015 in Appveyor-CI - abseil-cpp does not support VS 2015... improve tables upgrade C++ sat examples to use the new API; work on sat internals update license dates rewrite jobshop_ft06_distance.py to use the CP-SAT solver rename last example revert last commit more work on SAT internals fix
2018-10-31 16:18:18 +01:00
return absl::StrFormat("%s -> %s [label=%s]\n", source, destination, label);
}
// File header.
std::string Header(const std::string& name) override {
dotnet: Remove reference to dotnet release command - Currently not implemented... Add abseil patch - Add patches/absl-config.cmake Makefile: Add abseil-cpp on unix - Force abseil-cpp SHA1 to 45221cc note: Just before the PR #136 which break all CMake Makefile: Add abseil-cpp on windows - Force abseil-cpp SHA1 to 45221cc note: Just before the PR #136 which break all CMake CMake: Add abseil-cpp - Force abseil-cpp SHA1 to 45221cc note: Just before the PR #136 which break all CMake port to absl: C++ Part - Fix warning with the use of ABSL_MUST_USE_RESULT > The macro must appear as the very first part of a function declaration or definition: ... Note: past advice was to place the macro after the argument list. src: dependencies/sources/abseil-cpp-master/absl/base/attributes.h:418 - Rename enum after windows clash - Remove non compact table constraints - Change index type from int64 to int in routing library - Fix file_nonport compilation on windows - Fix another naming conflict with windows (NO_ERROR is a macro) - Cleanup hash containers; work on sat internals - Add optional_boolean sub-proto Sync cpp examples with internal code - reenable issue173 after reducing number of loops port to absl: Python Part - Add back cp_model.INT32_MIN|MAX for examples Update Python examples - Add random_tsp.py - Run words_square example - Run magic_square in python tests port to absl: Java Part - Fix compilation of the new routing parameters in java - Protect some code from SWIG parsing Update Java Examples port to absl: .Net Part Update .Net examples work on sat internals; Add C++ CP-SAT CpModelBuilder API; update sample code and recipes to use the new API; sync with internal code Remove VS 2015 in Appveyor-CI - abseil-cpp does not support VS 2015... improve tables upgrade C++ sat examples to use the new API; work on sat internals update license dates rewrite jobshop_ft06_distance.py to use the CP-SAT solver rename last example revert last commit more work on SAT internals fix
2018-10-31 16:18:18 +01:00
return absl::StrFormat("graph %s {\n", name);
}
// File footer.
std::string Footer() override { return "}\n"; }
};
class GmlSyntax : public GraphSyntax {
2020-10-22 23:36:58 +02:00
public:
~GmlSyntax() override {}
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);
}
// Adds one link in the generated graph.
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);
}
// File header.
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);
}
// File footer.
std::string Footer() override { return "]\n"; }
};
// 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.
class FileGraphExporter : public GraphExporter {
2020-10-22 23:36:58 +02:00
public:
FileGraphExporter(File* const file, GraphSyntax* const syntax)
: file_(file), syntax_(syntax) {}
~FileGraphExporter() override {}
// Write node in GML or DOT format.
void WriteNode(const std::string& name, const std::string& label,
const std::string& shape, const std::string& color) override {
Append(syntax_->Node(name, label, shape, color));
}
// Adds one link in the generated graph.
void WriteLink(const std::string& source, const std::string& destination,
const std::string& label) override {
Append(syntax_->Link(source, destination, label));
}
void WriteHeader(const std::string& name) override {
Append(syntax_->Header(name));
}
void WriteFooter() override { Append(syntax_->Footer()); }
2020-10-22 23:36:58 +02:00
private:
void Append(const std::string& str) {
file::WriteString(file_, str, file::Defaults()).IgnoreError();
}
File* const file_;
std::unique_ptr<GraphSyntax> syntax_;
};
2020-10-22 23:36:58 +02:00
} // namespace
GraphExporter* GraphExporter::MakeFileExporter(
File* const file, GraphExporter::GraphFormat format) {
GraphSyntax* syntax = nullptr;
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";
}
2017-04-19 16:20:56 +02:00
CHECK(syntax != nullptr);
return new FileGraphExporter(file, syntax);
}
2020-10-22 23:36:58 +02:00
} // namespace operations_research