LotSizingDataModel.Solver 2.0.1
Solver-independent modeling, execution, monitoring and adapter infrastructure.
Loading...
Searching...
No Matches
MathematicalDecisionMapperRegistry.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Linq;
4
6
7/// <summary>
8/// Stores and resolves mathematical decision mappers by
9/// business-domain category.
10/// </summary>
12{
13 private readonly Dictionary<string, IMathematicalDecisionMapper>
14 _mappers =
15 new(
16 StringComparer.OrdinalIgnoreCase);
17
18 /// <summary>
19 /// Gets the number of registered decision mappers.
20 /// </summary>
21 public int Count =>
22 _mappers.Count;
23
24 /// <summary>
25 /// Registers a mathematical decision mapper.
26 /// </summary>
27 /// <param name="mapper">
28 /// Decision mapper to register.
29 /// </param>
30 /// <exception cref="ArgumentNullException">
31 /// Thrown when <paramref name="mapper"/> is
32 /// <see langword="null"/>.
33 /// </exception>
34 /// <exception cref="InvalidOperationException">
35 /// Thrown when the mapper category is empty or already
36 /// registered.
37 /// </exception>
38 public void Register(
40 {
41 ArgumentNullException.ThrowIfNull(
42 mapper);
43
44 string category =
45 NormalizeCategory(
46 mapper.Category);
47
48 if (!_mappers.TryAdd(
49 category,
50 mapper))
51 {
52 throw new InvalidOperationException(
53 $"A mathematical decision mapper for category " +
54 $"'{category}' is already registered.");
55 }
56 }
57
58 /// <summary>
59 /// Registers or replaces a mathematical decision mapper.
60 /// </summary>
61 /// <param name="mapper">
62 /// Decision mapper to register.
63 /// </param>
64 /// <exception cref="ArgumentNullException">
65 /// Thrown when <paramref name="mapper"/> is
66 /// <see langword="null"/>.
67 /// </exception>
68 /// <exception cref="InvalidOperationException">
69 /// Thrown when the mapper category is empty.
70 /// </exception>
71 public void RegisterOrReplace(
73 {
74 ArgumentNullException.ThrowIfNull(
75 mapper);
76
77 string category =
78 NormalizeCategory(
79 mapper.Category);
80
81 _mappers[category] =
82 mapper;
83 }
84
85 /// <summary>
86 /// Determines whether a decision mapper is registered for a
87 /// category.
88 /// </summary>
89 /// <param name="category">
90 /// Business-domain category.
91 /// </param>
92 /// <returns>
93 /// <see langword="true"/> when a mapper is registered;
94 /// otherwise, <see langword="false"/>.
95 /// </returns>
96 public bool Contains(
97 string category)
98 {
99 return _mappers.ContainsKey(
100 NormalizeCategory(
101 category));
102 }
103
104 /// <summary>
105 /// Gets the required decision mapper for a category.
106 /// </summary>
107 /// <param name="category">
108 /// Business-domain category.
109 /// </param>
110 /// <returns>
111 /// Registered decision mapper.
112 /// </returns>
113 /// <exception cref="KeyNotFoundException">
114 /// Thrown when no mapper is registered for the supplied
115 /// category.
116 /// </exception>
118 string category)
119 {
120 string normalizedCategory =
121 NormalizeCategory(
122 category);
123
124 if (_mappers.TryGetValue(
125 normalizedCategory,
126 out IMathematicalDecisionMapper? mapper))
127 {
128 return mapper;
129 }
130
131 throw new KeyNotFoundException(
132 $"No mathematical decision mapper is registered for " +
133 $"category '{normalizedCategory}'.");
134 }
135
136 /// <summary>
137 /// Attempts to get a decision mapper for a category.
138 /// </summary>
139 /// <param name="category">
140 /// Business-domain category.
141 /// </param>
142 /// <param name="mapper">
143 /// Registered mapper when found.
144 /// </param>
145 /// <returns>
146 /// <see langword="true"/> when a mapper is found; otherwise,
147 /// <see langword="false"/>.
148 /// </returns>
149 public bool TryGet(
150 string category,
151 out IMathematicalDecisionMapper? mapper)
152 {
153 return _mappers.TryGetValue(
154 NormalizeCategory(
155 category),
156 out mapper);
157 }
158
159 /// <summary>
160 /// Returns all registered decision mappers ordered by
161 /// category.
162 /// </summary>
163 /// <returns>
164 /// Read-only list of registered decision mappers.
165 /// </returns>
166 public IReadOnlyList<IMathematicalDecisionMapper> GetAll()
167 {
168 return _mappers.Values
169 .OrderBy(
170 mapper =>
171 mapper.Category,
172 StringComparer.OrdinalIgnoreCase)
173 .ToArray();
174 }
175
176 /// <summary>
177 /// Removes a decision mapper.
178 /// </summary>
179 /// <param name="category">
180 /// Business-domain category.
181 /// </param>
182 /// <returns>
183 /// <see langword="true"/> when a mapper was removed;
184 /// otherwise, <see langword="false"/>.
185 /// </returns>
186 public bool Remove(
187 string category)
188 {
189 return _mappers.Remove(
190 NormalizeCategory(
191 category));
192 }
193
194 /// <summary>
195 /// Removes all registered decision mappers.
196 /// </summary>
197 public void Clear()
198 {
199 _mappers.Clear();
200 }
201
202 private static string NormalizeCategory(
203 string category)
204 {
205 if (string.IsNullOrWhiteSpace(
206 category))
207 {
208 throw new InvalidOperationException(
209 "A mathematical decision-mapper category is " +
210 "required.");
211 }
212
213 return category.Trim();
214 }
215}
Stores and resolves mathematical decision mappers by business-domain category.
bool TryGet(string category, out IMathematicalDecisionMapper? mapper)
Attempts to get a decision mapper for a category.
bool Contains(string category)
Determines whether a decision mapper is registered for a category.
void Register(IMathematicalDecisionMapper mapper)
Registers a mathematical decision mapper.
IReadOnlyList< IMathematicalDecisionMapper > GetAll()
Returns all registered decision mappers ordered by category.
IMathematicalDecisionMapper GetRequired(string category)
Gets the required decision mapper for a category.
void RegisterOrReplace(IMathematicalDecisionMapper mapper)
Registers or replaces a mathematical decision mapper.
Defines a mapper responsible for one family of lot-sizing decisions identified by a mathematical doma...
string Category
Gets the mathematical domain-key category handled by this mapper.