LotSizingDataModel.Solver 2.0.1
Solver-independent modeling, execution, monitoring and adapter infrastructure.
Loading...
Searching...
No Matches
NativeSolverAdapterUtilities.cs
Go to the documentation of this file.
1using System;
2using System.IO;
4
6
7/// <summary>
8/// Provides small reusable helpers for native solver plugins.
9/// </summary>
11{
12 /// <summary>
13 /// Creates an unavailable solver description with one
14 /// diagnostic message.
15 /// </summary>
16 /// <param name="solverKind">
17 /// Solver kind.
18 /// </param>
19 /// <param name="solverName">
20 /// Solver display name.
21 /// </param>
22 /// <param name="status">
23 /// Availability status.
24 /// </param>
25 /// <param name="diagnostic">
26 /// Diagnostic message.
27 /// </param>
28 /// <returns>
29 /// Availability information.
30 /// </returns>
32 SolverKind solverKind,
33 string solverName,
35 string diagnostic)
36 {
37 if (string.IsNullOrWhiteSpace(
38 solverName))
39 {
40 throw new ArgumentException(
41 "A solver name is required.",
42 nameof(solverName));
43 }
44
45 if (string.IsNullOrWhiteSpace(
46 diagnostic))
47 {
48 throw new ArgumentException(
49 "A diagnostic message is required.",
50 nameof(diagnostic));
51 }
52
53 var result =
55 solverKind,
56 status)
57 {
58 SolverName =
59 solverName
60 };
61
62 result.AddDiagnostic(
63 diagnostic);
64
65 return result;
66 }
67
68 /// <summary>
69 /// Returns the first existing full path among candidate
70 /// paths.
71 /// </summary>
72 /// <param name="candidatePaths">
73 /// Candidate file or directory paths.
74 /// </param>
75 /// <returns>
76 /// First existing path, or an empty string when none exists.
77 /// </returns>
78 public static string FindFirstExistingPath(
79 params string?[] candidatePaths)
80 {
81 ArgumentNullException.ThrowIfNull(
82 candidatePaths);
83
84 foreach (
85 string? candidate
86 in candidatePaths)
87 {
88 if (string.IsNullOrWhiteSpace(
89 candidate))
90 {
91 continue;
92 }
93
94 string expanded =
95 Environment.ExpandEnvironmentVariables(
96 candidate.Trim());
97
98 string fullPath;
99
100 try
101 {
102 fullPath =
103 Path.GetFullPath(
104 expanded);
105 }
106 catch (
107 Exception exception)
108 when (
109 exception is
110 ArgumentException or
111 NotSupportedException or
112 PathTooLongException)
113 {
114 continue;
115 }
116
117 if (File.Exists(
118 fullPath) ||
119 Directory.Exists(
120 fullPath))
121 {
122 return fullPath;
123 }
124 }
125
126 return string.Empty;
127 }
128
129 /// <summary>
130 /// Reads and trims an environment variable.
131 /// </summary>
132 /// <param name="name">
133 /// Environment-variable name.
134 /// </param>
135 /// <returns>
136 /// Trimmed value, or an empty string when undefined.
137 /// </returns>
138 public static string ReadEnvironmentVariable(
139 string name)
140 {
141 if (string.IsNullOrWhiteSpace(
142 name))
143 {
144 throw new ArgumentException(
145 "An environment-variable name is required.",
146 nameof(name));
147 }
148
149 return
150 Environment.GetEnvironmentVariable(
151 name.Trim())?.Trim() ??
152 string.Empty;
153 }
154}
Provides small reusable helpers for native solver plugins.
static string FindFirstExistingPath(params string?[] candidatePaths)
Returns the first existing full path among candidate paths.
static SolverAvailabilityInfo CreateAvailability(SolverKind solverKind, string solverName, SolverAvailabilityStatus status, string diagnostic)
Creates an unavailable solver description with one diagnostic message.
static string ReadEnvironmentVariable(string name)
Reads and trims an environment variable.
Describes the availability and detected installation details of a mathematical optimization solver.
void AddDiagnostic(string message)
Adds a diagnostic message.
SolverKind
Identifies a supported mathematical optimization solver.
Definition SolverKind.cs:12
SolverAvailabilityStatus
Describes the availability state of a solver adapter on the current computer.