3using System.Threading.Tasks;
18 private readonly
object _syncRoot =
21 private CancellationTokenSource? _activeSolveCancellationSource;
23 private bool _isRunning;
84 public async ValueTask<MathematicalModelSolveResult>
SolveAsync(
86 CancellationToken cancellationToken =
default)
88 ArgumentNullException.ThrowIfNull(
93 CancellationTokenSource linkedCancellationSource =
102 linkedCancellationSource.Token);
104 ArgumentNullException.ThrowIfNull(
118 linkedCancellationSource);
128 CancellationTokenSource? cancellationSource;
133 _activeSolveCancellationSource;
136 if (cancellationSource is
null)
143 cancellationSource.Cancel();
145 catch (ObjectDisposedException)
165 protected abstract ValueTask<MathematicalModelSolveResult>
168 CancellationToken cancellationToken);
195 if (
string.IsNullOrWhiteSpace(
202 if (
string.IsNullOrWhiteSpace(
205 result.FormulationId =
216 if (
string.IsNullOrWhiteSpace(
223 if (
string.IsNullOrWhiteSpace(
226 result.SolverVersion =
231 private CancellationTokenSource BeginSolve(
232 CancellationToken cancellationToken)
238 throw new InvalidOperationException(
239 $
"Solver '{SolverName}' is already running.");
242 var linkedCancellationSource =
243 CancellationTokenSource.CreateLinkedTokenSource(
246 _activeSolveCancellationSource =
247 linkedCancellationSource;
252 return linkedCancellationSource;
256 private void EndSolve(
257 CancellationTokenSource cancellationSource)
262 _activeSolveCancellationSource,
265 _activeSolveCancellationSource =
273 cancellationSource.Dispose();
Provides common execution-state and cancellation handling for solver adapters that solve solver-indep...
bool IsRunning
Gets a value indicating whether a solve operation is currently running.
SolverKind SolverKind
Gets the solver kind.
string SolverName
Gets the solver name.
virtual void CompleteResultMetadata(MathematicalModelSolveRequest request, MathematicalModelSolveResult result)
Completes generic result metadata when it was not supplied by the native adapter.
void RequestStop()
Requests interruption of the currently running solve operation.
virtual void RequestNativeStop()
Requests interruption through the native solver API.
ValueTask< MathematicalModelSolveResult > SolveCoreAsync(MathematicalModelSolveRequest request, CancellationToken cancellationToken)
Performs the solver-specific translation and solve.
string SolverVersion
Gets the solver version when available.
async ValueTask< MathematicalModelSolveResult > SolveAsync(MathematicalModelSolveRequest request, CancellationToken cancellationToken=default)
Solves a solver-independent mathematical model.
Describes a request to solve an already built, solver-independent mathematical model.
string FormulationId
Gets or sets the identifier of the formulation that generated the mathematical model.
string RunName
Gets or sets an optional run name.
void EnsureValid()
Validates the mathematical-model solve request.
Stores the solver result for a solver-independent mathematical model.
void EnsureValid()
Validates the mathematical-model solve result.
string FormulationId
Gets or sets the identifier of the formulation that generated the mathematical model.
string SolverName
Gets or sets the solver name.
string RunName
Gets or sets the solve-run name.
SolverKind SolverKind
Gets or sets the solver kind.
string SolverVersion
Gets or sets the solver version.
Defines a solver adapter capable of solving an already built, solver-independent mathematical model.