Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
message_callback_data.h
Go to the documentation of this file.
1// Copyright 2010-2024 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
14#ifndef OR_TOOLS_MATH_OPT_SOLVERS_MESSAGE_CALLBACK_DATA_H_
15#define OR_TOOLS_MATH_OPT_SOLVERS_MESSAGE_CALLBACK_DATA_H_
16
17#include <optional>
18#include <string>
19#include <vector>
20
21#include "absl/strings/string_view.h"
22#include "absl/synchronization/mutex.h"
24
26
27// Buffer for solvers messages that enforces the contract of MessageCallback.
28//
29// This contract mandates that each message is a full finished line. As a
30// consequence, if the solver calls the callback with a partial last line, this
31// one must not be passed immediately to MessageCallback but kept until the end
32// of the line is reached (or the solve is done).
33//
34// To implement that this class has two methods:
35//
36// - Parse() that is to be called for each received message from the solver.
37//
38// - Flush() that must be called at the end of the solve to generate the data
39// corresponding the last message sent by the solver if it was an unfinished -
40// line.
42 public:
44
47
48 // Parses the input message, returning a vector with all finished
49 // lines. Returns an empty vector if the input message did not contain any
50 // '\n'.
51 //
52 // It updates this object with the last unfinished line to use it to complete
53 // the next message.
54 std::vector<std::string> Parse(absl::string_view message);
55
56 // Returns a vector with the last unfinished line if it exists, else an empty
57 // vector.
58 std::vector<std::string> Flush();
59
60 private:
61 // The last message line not ending with '\n'.
62 std::string unfinished_line_;
63};
64
65// Buffers callback data into lines using MessageCallbackData and invokes the
66// callback as new lines are ready.
67//
68// In MathOpt, typically used for solvers that provide a callback with the
69// solver logs where the logs contain `\n` characters and messages may be both
70// less than a complete line or multiple lines. Recommended use:
71// * Register a callback with the underlying solver to get its logs. In the
72// callback, when given a log string, call OnMessage() on it.
73// * Run the solver's Solve() function.
74// * Unregister the callback with the underlying solver.
75// * Call Flush() to ensure any final incomplete lines are sent.
76//
77// If initialized with a nullptr for the user callback, all functions on this
78// class have no effect.
79//
80// This class is threadsafe if the input callback is also threadsafe.
82 public:
84 SolverInterface::MessageCallback user_message_callback);
85
86 // If false, incoming messages are ignored and OnMessage() and Flush() have no
87 // effect.
89 return user_message_callback_ != nullptr;
90 }
91
92 // Appends `message` to the buffer, then invokes the callback once on all
93 // newly complete lines and removes those lines from the buffer. In
94 // particular, the callback is not invoked if message does not contain any
95 // `\n`.
96 void OnMessage(absl::string_view message);
97
98 // If the buffer has any pending message, sends it to the callback. This
99 // function has no effect if called when the buffer is empty. Calling this
100 // function when the buffer is non-empty before the stream of logs is complete
101 // will result in the user getting extra line breaks.
102 void Flush();
103
104 private:
105 SolverInterface::MessageCallback user_message_callback_;
106 absl::Mutex mutex_;
107 MessageCallbackData message_callback_data_ ABSL_GUARDED_BY(mutex_);
108};
109
110} // namespace operations_research::math_opt
111
112#endif // OR_TOOLS_MATH_OPT_SOLVERS_MESSAGE_CALLBACK_DATA_H_
BufferedMessageCallback(SolverInterface::MessageCallback user_message_callback)
MessageCallbackData(const MessageCallbackData &)=delete
MessageCallbackData & operator=(const MessageCallbackData &)=delete
std::vector< std::string > Parse(absl::string_view message)
std::function< void(const std::vector< std::string > &)> MessageCallback
An object oriented wrapper for quadratic constraints in ModelStorage.
Definition gurobi_isv.cc:28
std::string message
Definition trace.cc:397