LotSizingDataModel.Instance 2.0.1
Lot-sizing instance representation, descriptors and problem characterization.
Loading...
Searching...
No Matches
HistoricalClassificationMappingRegistry.cs
Go to the documentation of this file.
2
4{
5 private readonly List<HistoricalMappingRule> _rules = new();
6
8 string registryVersion)
9 {
10 if (string.IsNullOrWhiteSpace(registryVersion))
11 {
12 throw new ArgumentException(
13 "A registry version is required.",
14 nameof(registryVersion));
15 }
16
17 RegistryVersion = registryVersion.Trim();
18 }
19
20 public string RegistryVersion { get; }
21 public IReadOnlyList<HistoricalMappingRule> Rules => _rules;
22
24 {
25 ArgumentNullException.ThrowIfNull(rule);
26 rule.EnsureValid();
27
28 if (_rules.Any(existing =>
29 string.Equals(
30 existing.RuleId,
31 rule.RuleId,
32 StringComparison.Ordinal)))
33 {
34 throw new InvalidOperationException(
35 $"Rule identifier '{rule.RuleId}' is already used.");
36 }
37
38 if (_rules.Any(existing =>
39 existing.Family == rule.Family &&
40 string.Equals(
41 existing.HistoricalToken,
42 rule.HistoricalToken,
43 StringComparison.Ordinal)))
44 {
45 throw new InvalidOperationException(
46 $"Token '{rule.HistoricalToken}' is already registered for '{rule.Family}'.");
47 }
48
49 _rules.Add(rule);
50 }
51
54 string historicalToken)
55 {
56 string normalized =
58
59 if (normalized.Length == 0)
60 {
61 return null;
62 }
63
64 return _rules.SingleOrDefault(rule =>
65 rule.Family == family &&
66 string.Equals(
67 rule.HistoricalToken,
68 normalized,
69 StringComparison.Ordinal));
70 }
71
74 IEnumerable<string> universalTokens)
75 {
76 ArgumentNullException.ThrowIfNull(universalTokens);
77
78 HistoricalMappingRule[] candidates = _rules
79 .Where(rule =>
80 rule.Family == family &&
81 rule.AllowsInverse &&
82 rule.Confidence == HistoricalMappingConfidence.Exact &&
83 rule.MatchesUniversalTokenSet(universalTokens))
84 .OrderBy(rule => rule.RuleId, StringComparer.Ordinal)
85 .ToArray();
86
87 if (candidates.Length == 0)
88 {
91 candidates);
92 }
93
94 if (candidates.Length == 1)
95 {
98 candidates);
99 }
100
103 candidates);
104 }
105
106 public IReadOnlyList<HistoricalMappingRule> GetRules(
108 {
109 return _rules
110 .Where(rule => rule.Family == family)
111 .OrderBy(rule => rule.RuleId, StringComparer.Ordinal)
112 .ToArray();
113 }
114
115 public void EnsureValid()
116 {
117 foreach (HistoricalMappingRule rule in _rules)
118 {
119 rule.EnsureValid();
120 }
121
122 if (_rules.GroupBy(rule => rule.RuleId, StringComparer.Ordinal)
123 .Any(group => group.Count() > 1))
124 {
125 throw new InvalidOperationException(
126 "Historical registry rule identifiers must be unique.");
127 }
128 }
129}
IReadOnlyList< HistoricalMappingRule > GetRules(HistoricalClassificationFamily family)
HistoricalInverseResolution ResolveInverse(HistoricalClassificationFamily family, IEnumerable< string > universalTokens)
HistoricalMappingRule? FindForward(HistoricalClassificationFamily family, string historicalToken)