/
Dtmi.cs
260 lines (231 loc) · 9.03 KB
/
Dtmi.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
namespace DTDLParser
{
using System;
using System.Text.RegularExpressions;
/// <summary>
/// Digital Twin Modeling Identifier.
/// </summary>
public class Dtmi : Uri, IComparable<Dtmi>
{
private static readonly Regex DtmiRegex;
private static readonly Uri InvalidDtmiValidationId;
private static readonly char[] Separators = { ':', ';' };
private bool versionInfoExtracted;
private int majorVersion;
private int minorVersion;
private double completeVersion;
private string versionless;
private string[] labels;
private bool? isReserved;
private string tail;
private Dtmi fragmentless;
static Dtmi()
{
DtmiRegex = new Regex(@"^dtmi:(?:_+[A-Za-z0-9]|[A-Za-z])(?:[A-Za-z0-9_]*[A-Za-z0-9])?(?::(?:_+[A-Za-z0-9]|[A-Za-z])(?:[A-Za-z0-9_]*[A-Za-z0-9])?)*(?:;[1-9][0-9]{0,8}(?:\.[1-9][0-9]{0,5})?)?(?:#(?:(?:_+[A-Za-z0-9]|[A-Za-z])(?:[A-Za-z0-9_]*[A-Za-z0-9])?)?)?$", RegexOptions.Compiled);
InvalidDtmiValidationId = new Uri("dtmi:dtdl:parsingError:invalidDtmi");
}
/// <summary>
/// Initializes a new instance of the <see cref="Dtmi"/> class from a Uri instance.
/// </summary>
/// <param name="uri">The URI to copy from.</param>
/// <param name="skipValidation">Skips the validation check.</param>
public Dtmi(Uri uri, bool skipValidation = false)
: this(uri.OriginalString, skipValidation || (uri is Dtmi))
{
}
/// <summary>
/// Initializes a new instance of the <see cref="Dtmi"/> class from a string.
/// </summary>
/// <param name="value">String value of the DTMI.</param>
/// <param name="skipValidation">Skips the validation check.</param>
public Dtmi(string value, bool skipValidation = false)
: base(value)
{
if (!skipValidation && !DtmiRegex.IsMatch(value))
{
throw this.GetInvalidIdException(value);
}
this.versionInfoExtracted = false;
this.labels = null;
this.isReserved = null;
this.tail = null;
this.fragmentless = null;
}
/// <summary>
/// Gets the major version of the DTMI as an integer.
/// </summary>
public int MajorVersion
{
get
{
this.ExtractVersionInfoIfNeeded();
return this.majorVersion;
}
}
/// <summary>
/// Gets the minor version of the DTMI as an integer.
/// </summary>
public int MinorVersion
{
get
{
this.ExtractVersionInfoIfNeeded();
return this.minorVersion;
}
}
/// <summary>
/// Gets the major and minor version of the DTMI as a double.
/// The major version is in the integral portion and the minor version is in the fractional portion.
/// The minor version is left-zero-padded so that sort order is maintained.
/// Ex: version 2.22 is returned as 2.000022.
/// </summary>
public double CompleteVersion
{
get
{
this.ExtractVersionInfoIfNeeded();
return this.completeVersion;
}
}
/// <summary>
/// Gets the portion of the DTMI that preceeds the version number.
/// </summary>
public string Versionless
{
get
{
this.ExtractVersionInfoIfNeeded();
return this.versionless;
}
}
/// <summary>
/// Gets the sequence of labels in the path portion of the DTMI.
/// </summary>
public string[] Labels
{
get
{
if (this.labels == null)
{
this.labels = this.AbsolutePath.Split(Separators);
if (this.AbsolutePath.Contains(";"))
{
Array.Resize(ref this.labels, this.labels.Length - 1);
}
}
return this.labels;
}
}
/// <summary>
/// Gets a value indicating whether the DTMI is reserved for auto-generation.
/// </summary>
public bool IsReserved
{
get
{
if (this.isReserved == null)
{
this.isReserved = this.AbsoluteUri.Contains(":_") || this.Fragment.StartsWith("#_");
}
return (bool)this.isReserved;
}
}
/// <summary>
/// Gets the name of the identifier's tail, which is the empty string if there is no fragment.
/// </summary>
/// <remarks>
/// The tail is the same as the fragment without the leading #.
/// The case of no fragment and the case of an empty fragment both yield an empty string for the tail.
/// </remarks>
public string Tail
{
get
{
if (this.tail == null)
{
this.tail = this.Fragment == string.Empty ? string.Empty : this.Fragment.Substring(1);
}
return this.tail;
}
}
/// <summary>
/// Gets the identifier with any fragment removed.
/// </summary>
public Dtmi Fragmentless
{
get
{
if (this.fragmentless == null)
{
int hashIx = this.AbsoluteUri.IndexOf('#');
this.fragmentless = hashIx >= 0 ? new Dtmi(this.AbsoluteUri.Substring(0, hashIx), skipValidation: true) : this;
}
return this.fragmentless;
}
}
/// <summary>
/// Try to create a new instance of the <see cref="Dtmi"/> class from a string.
/// </summary>
/// <param name="value">String value of the DTMI.</param>
/// <param name="dtmi">Out parameter to receive the newly created <see cref="Dtmi"/>.</param>
/// <returns>True if the string represents a valid DTMI; false if it does not.</returns>
public static bool TryCreateDtmi(string value, out Dtmi dtmi)
{
dtmi = DtmiRegex.IsMatch(value) ? new Dtmi(value, skipValidation: true) : null;
return dtmi != null;
}
/// <summary>
/// Compares the current instance with another <c>Dtmi</c> and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.
/// </summary>
/// <param name="other">The <c>Dtmi</c> to compare against.</param>
/// <returns>Negative if this instance precedes <paramref name="other"/>, positive if this instance follows <paramref name="other"/>, zero if position is the same.</returns>
public int CompareTo(Dtmi other)
{
return this.AbsoluteUri.CompareTo(other.AbsoluteUri);
}
private void ExtractVersionInfoIfNeeded()
{
if (this.versionInfoExtracted)
{
return;
}
if (this.Scheme != ParserValues.DtmiScheme)
{
throw this.GetInvalidIdException(this.AbsoluteUri.ToString());
}
this.versionInfoExtracted = true;
int sepIx = this.AbsoluteUri.IndexOf(';');
if (sepIx < 0)
{
this.majorVersion = 0;
this.minorVersion = 0;
this.completeVersion = 0.0;
this.versionless = this.AbsoluteUri;
}
else
{
int dotIx = this.AbsoluteUri.IndexOf('.');
int hashIx = this.AbsoluteUri.IndexOf('#');
int verEndIx = hashIx < 0 ? this.AbsoluteUri.Length : hashIx;
int majorEndIx = dotIx < 0 ? verEndIx : dotIx;
this.majorVersion = int.Parse(this.AbsoluteUri.Substring(sepIx + 1, majorEndIx - sepIx - 1));
this.minorVersion = dotIx < 0 ? 0 : int.Parse(this.AbsoluteUri.Substring(dotIx + 1, verEndIx - dotIx - 1));
this.completeVersion = (double)this.majorVersion + ((double)this.minorVersion * 0.000001);
this.versionless = this.AbsoluteUri.Substring(0, sepIx) + this.Fragment;
}
}
private ParsingException GetInvalidIdException(string value)
{
return new ParsingException(new ParsingError[]
{
new ParsingError()
{
ValidationID = InvalidDtmiValidationId,
Cause = $"{value} is not a legal DTMI.",
Action = "Replace the identifier with a string that conforms to the DTMI syntax -- see https://github.com/Azure/digital-twin-model-identifier.",
PrimaryID = this,
},
});
}
}
}