ULSAlgorithms 1.1.0-g3e5595996d
High-performance exact and heuristic algorithms for uncapacitated lot sizing
Loading...
Searching...
No Matches
UlsSolverConfiguration.cs
Go to the documentation of this file.
1using System.Text;
2using System.Text.Json;
3using System.Text.Json.Serialization;
5
7
8/// <summary>
9/// Versioned, serializable definition of one ULS strategy and its
10/// constructor-level options.
11/// </summary>
12/// <remarks>
13/// <para>
14/// The JSON schema is intentionally independent from the NuGet/package version.
15/// Schema version 1 is the first public reproducibility format.
16/// </para>
17/// <para>
18/// Parsing is strict: unknown JSON properties, integer enum values, unsupported
19/// schema versions, unknown solver identifiers and incompatible strategy
20/// options are rejected.
21/// </para>
22/// </remarks>
23public sealed class UlsSolverConfiguration
24{
25 /// <summary>The first and current public JSON schema version.</summary>
26 public const int CurrentSchemaVersion = 1;
27
28 private static readonly JsonSerializerOptions SerializerOptions =
29 CreateSerializerOptions();
30
31 /// <summary>Gets or sets the serialized schema version.</summary>
32 [JsonRequired]
33 public int SchemaVersion { get; set; } =
35
36 /// <summary>Gets or sets the stable solver identifier.</summary>
37 [JsonRequired]
38 public string SolverId { get; set; } =
39 string.Empty;
40
41 /// <summary>
42 /// Gets or sets the constructor-level strategy options.
43 /// </summary>
44 public UlsSolverCreationOptions Options { get; set; } =
45 new();
46
47 /// <summary>
48 /// Validates the schema, stable solver ID and all strategy-specific
49 /// options without constructing or solving a problem.
50 /// </summary>
51 public void Validate()
52 {
54 {
55 throw new NotSupportedException(
56 $"Unsupported ULS solver configuration schema version " +
57 $"'{SchemaVersion}'. Supported version: " +
58 $"{CurrentSchemaVersion}.");
59 }
60
61 ArgumentException.ThrowIfNullOrWhiteSpace(SolverId);
62 ArgumentNullException.ThrowIfNull(Options);
63
64 var descriptor =
66
67 Options.EnsureValidFor(descriptor);
68 }
69
70 /// <summary>
71 /// Creates a fresh solver from this validated configuration.
72 /// </summary>
73 /// <returns>The configured solver.</returns>
76
77 /// <summary>
78 /// Serializes this configuration to canonical, indented JSON.
79 /// </summary>
80 /// <returns>UTF-16 managed text containing UTF-8-compatible JSON.</returns>
81 public string ToJson()
82 {
83 Validate();
84
85 return JsonSerializer.Serialize(
86 this,
87 SerializerOptions);
88 }
89
90 /// <summary>
91 /// Writes this configuration as UTF-8 without a byte-order mark.
92 /// </summary>
93 /// <param name="path">Destination JSON path.</param>
94 public void SaveJson(string path)
95 {
96 ArgumentException.ThrowIfNullOrWhiteSpace(path);
97
98 var fullPath =
99 Path.GetFullPath(path);
100
101 var directory =
102 Path.GetDirectoryName(fullPath);
103
104 if (!string.IsNullOrWhiteSpace(directory))
105 {
106 Directory.CreateDirectory(directory);
107 }
108
109 File.WriteAllText(
110 fullPath,
111 ToJson() + Environment.NewLine,
112 new UTF8Encoding(
113 encoderShouldEmitUTF8Identifier: false));
114 }
115
116 /// <summary>
117 /// Parses and validates one JSON configuration.
118 /// </summary>
119 /// <param name="json">JSON text.</param>
120 /// <returns>The validated configuration.</returns>
122 string json)
123 {
124 ArgumentException.ThrowIfNullOrWhiteSpace(json);
125
126 var configuration =
127 JsonSerializer.Deserialize<UlsSolverConfiguration>(
128 json,
129 SerializerOptions)
130 ?? throw new JsonException(
131 "The JSON document did not contain a ULS solver configuration.");
132
133 configuration.Validate();
134 return configuration;
135 }
136
137 /// <summary>
138 /// Loads and validates one UTF-8 JSON configuration file.
139 /// </summary>
140 /// <param name="path">Configuration file path.</param>
141 /// <returns>The validated configuration.</returns>
143 string path)
144 {
145 ArgumentException.ThrowIfNullOrWhiteSpace(path);
146
147 return ParseJson(
148 File.ReadAllText(
149 Path.GetFullPath(path),
150 Encoding.UTF8));
151 }
152
153 private static JsonSerializerOptions CreateSerializerOptions()
154 {
155 var options =
156 new JsonSerializerOptions
157 {
158 PropertyNamingPolicy =
159 JsonNamingPolicy.CamelCase,
160 WriteIndented = true,
161 DefaultIgnoreCondition =
162 JsonIgnoreCondition.WhenWritingNull,
163 UnmappedMemberHandling =
164 JsonUnmappedMemberHandling.Disallow,
165 PropertyNameCaseInsensitive = false
166 };
167
168 options.Converters.Add(
169 new JsonStringEnumConverter(
170 JsonNamingPolicy.CamelCase,
171 allowIntegerValues: false));
172
173 return options;
174 }
175}
Canonical runtime inventory of every public IUlsSolver strategy.
static UlsSolverDescriptor Get(string id)
Gets one descriptor by stable identifier.
Versioned, serializable definition of one ULS strategy and its constructor-level options.
string SolverId
Gets or sets the stable solver identifier.
const int CurrentSchemaVersion
The first and current public JSON schema version.
IUlsSolver CreateSolver()
Creates a fresh solver from this validated configuration.
static UlsSolverConfiguration LoadJson(string path)
Loads and validates one UTF-8 JSON configuration file.
string ToJson()
Serializes this configuration to canonical, indented JSON.
void SaveJson(string path)
Writes this configuration as UTF-8 without a byte-order mark.
void Validate()
Validates the schema, stable solver ID and all strategy-specific options without constructing or solv...
UlsSolverCreationOptions Options
Gets or sets the constructor-level strategy options.
int SchemaVersion
Gets or sets the serialized schema version.
static UlsSolverConfiguration ParseJson(string json)
Parses and validates one JSON configuration.
Composes the existing strategy-specific constructor options used by UlsSolverFactory.
Creates public ULS strategies from stable catalog identifiers.
static IUlsSolver Create(string id)
Creates a new solver using its stable catalog identifier and default constructor policy.
Defines the common strategy contract implemented by every ULS solver.
Definition IUlsSolver.cs:14