20#include "absl/algorithm/container.h"
21#include "absl/log/check.h"
22#include "absl/strings/escaping.h"
23#include "absl/strings/str_cat.h"
24#include "absl/strings/str_join.h"
25#include "absl/strings/string_view.h"
26#include "absl/types/span.h"
40template <
typename AttrType>
41std::string GetAttrKeyNames(
const Elemental& elemental,
const AttrType attr,
43 std::vector<std::string> element_names;
44 for (
int i = 0; i < GetAttrKeySize<AttrType>(); ++
i) {
45 auto n = elemental.GetElementNameUntyped(
GetElementTypes(attr)[i], key[i]);
47 element_names.push_back(absl::StrCat(
"\"", absl::CEscape(*n),
"\""));
49 return absl::StrCat(
"(", absl::StrJoin(element_names,
", "),
")");
52std::vector<std::string> ElementalModelDebugString(
const Elemental& elemental) {
53 std::vector<std::string> lines;
54 lines.push_back(
"Model:");
55 if (!elemental.model_name().empty()) {
56 lines.push_back(absl::StrCat(
"model_name: \"",
57 absl::CEscape(elemental.model_name()),
"\""));
59 if (!elemental.primary_objective_name().empty()) {
61 absl::StrCat(
"primary_objective_name: \"",
62 absl::CEscape(elemental.primary_objective_name()),
"\""));
66 if (elemental.NextElementId(e) == 0) {
69 lines.push_back(absl::StrCat(
"ElementType: ", e,
70 " num_elements: ", elemental.NumElements(e),
71 " next_id: ", elemental.NextElementId(e)));
72 std::vector<int64_t> elements = elemental.AllElementsUntyped(e);
73 absl::c_sort(elements);
74 for (
const int64_t element_id : elements) {
75 absl::StatusOr<absl::string_view> element_name =
76 elemental.GetElementNameUntyped(e, element_id);
77 CHECK_OK(element_name);
78 lines.push_back(absl::StrCat(
" id: ", element_id,
" name: \"",
79 absl::CEscape(*element_name),
"\""));
83 const int64_t num_non_defaults = elemental.AttrNumNonDefaults(a);
84 if (num_non_defaults == 0) {
88 absl::StrCat(
"Attribute: ", a,
" non-defaults: ", num_non_defaults));
89 auto keys = elemental.AttrNonDefaults(a);
91 for (
const auto key : keys) {
92 lines.push_back(absl::StrCat(
95 " (key names: ", GetAttrKeyNames(elemental, a, key),
")"));
101std::vector<std::string> DiffDebugString(
const Elemental& elemental,
103 std::vector<std::string> lines;
105 if (diff.checkpoint(e) == elemental.NextElementId(e) &&
106 diff.deleted_elements(e).empty()) {
109 lines.push_back(absl::StrCat(
"ElementType: ", e,
110 " next_id: ", elemental.NextElementId(e),
111 " checkpoint: ", diff.checkpoint(e)));
112 if (diff.deleted_elements(e).empty()) {
115 std::vector<int64_t> deleted_elements;
116 for (
const int64_t element_id : diff.deleted_elements(e)) {
117 deleted_elements.push_back(element_id);
119 absl::c_sort(deleted_elements);
120 lines.push_back(absl::StrCat(
" deleted: [",
121 absl::StrJoin(deleted_elements,
", "),
"]"));
125 if (diff.modified_keys(attr).empty()) {
128 std::vector<AttrKeyFor<Attr>> sorted_keys;
130 sorted_keys.push_back(key);
132 absl::c_sort(sorted_keys);
133 lines.push_back(absl::StrCat(
"Attribute: ", attr));
134 for (
const auto key : sorted_keys) {
135 lines.push_back(absl::StrCat(
136 " ", key,
" (names: ", GetAttrKeyNames(elemental, attr, key),
")"));
142std::string ElementalDebugString(
144 absl::Span<
const std::pair<int64_t, const Diff*>> diffs) {
145 std::vector<std::string> lines = ElementalModelDebugString(elemental);
146 for (
const auto& [
id, diff] : diffs) {
147 lines.push_back(absl::StrCat(
"Diff: ",
id));
148 absl::c_move(DiffDebugString(elemental, *diff), std::back_inserter(lines));
150 return absl::StrJoin(lines,
"\n");
156 std::vector<std::pair<int64_t, const Diff*>> diffs;
158 for (
auto& [
id, diff] : diffs_->GetAll()) {
159 diffs.push_back({id, diff});
165 return ElementalDebugString(*
this, absl::MakeConstSpan(diffs));
std::string DebugString(bool print_diffs=true) const
An object oriented wrapper for quadratic constraints in ModelStorage.
AttrKey< AttrTypeDescriptorT< AttrType >::kNumKeyElements, typename AttrTypeDescriptorT< AttrType >::Symmetry > AttrKeyFor
The type of the AttrKey for attribute type AttrType.
constexpr std::array< ElementType, GetAttrKeySize< attr >()> GetElementTypes()
std::string FormatAttrValue(const ValueType v)
static void ForEachAttr(Fn &&fn)