-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathExtendIReferable.cs
More file actions
638 lines (582 loc) · 23.8 KB
/
Copy pathExtendIReferable.cs
File metadata and controls
638 lines (582 loc) · 23.8 KB
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
/*
Copyright (c) 2018-2023 Festo SE & Co. KG <https://www.festo.com/net/de_de/Forms/web/contact_international>
Author: Michael Hoffmeister
This source code is licensed under the Apache License 2.0 (see LICENSE.txt).
This source code may use other Open Source software components (see LICENSE.txt).
*/
using AdminShellNS;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Extensions
{
public static class ExtendIReferable
{
#region AasxPackageExplorer
/// <summary>
/// Recurses on all Submodel elements of a Submodel or SME, which allows children.
/// The <c>state</c> object will be passed to the lambda function in order to provide
/// stateful approaches. Include this element, as well.
/// </summary>
/// <param name="state">State object to be provided to lambda. Could be <c>null.</c></param>
/// <param name="lambda">The lambda function as <c>(state, parents, SME)</c>
/// The lambda shall return <c>TRUE</c> in order to deep into recursion.</param>
/// <param name="includeThis">Include this element as well. <c>parents</c> will then
/// include this element as well!</param>
public static void RecurseOnReferables(this IReferable referable,
object state, Func<object, List<IReferable>, IReferable, bool> lambda,
bool includeThis = false)
{
// TODO (MIHO, 2023-07-28): not all elements are covered
if (referable is Submodel submodel)
{
submodel.RecurseOnReferables(state, lambda, includeThis);
}
else if (referable is SubmodelElementCollection submodelElementCollection)
{
submodelElementCollection.RecurseOnReferables(state, lambda, includeThis);
}
else if (referable is SubmodelElementList submodelElementList)
{
submodelElementList.RecurseOnReferables(state, lambda, includeThis);
}
else if (includeThis)
lambda(state, null, referable);
}
public static int Replace(
this IReferable referable, ISubmodelElement oldElem, ISubmodelElement newElem)
{
if (referable is Submodel submodel)
{
return submodel.Replace(oldElem, newElem);
}
else if (referable is AnnotatedRelationshipElement annotatedRelationshipElement
&& oldElem is IDataElement oldDE && newElem is IDataElement newDE)
{
return annotatedRelationshipElement.Replace(oldDE, newDE);
}
else if (referable is SubmodelElementCollection submodelElementCollection)
{
return submodelElementCollection.Replace(oldElem, newElem);
}
else if (referable is SubmodelElementList submodelElementList)
{
return submodelElementList.Replace(oldElem, newElem);
}
else if (referable is Entity entity)
{
return entity.Replace(oldElem, newElem);
}
else if (referable is Operation operation)
{
return operation.Replace(oldElem, newElem);
}
return -1;
}
public static void Remove(this IReferable referable, ISubmodelElement submodelElement)
{
if (referable is Submodel submodel)
{
submodel.Remove(submodelElement);
}
else if (referable is AnnotatedRelationshipElement annotatedRelationshipElement)
{
annotatedRelationshipElement.Remove(submodelElement);
}
else if (referable is SubmodelElementCollection submodelElementCollection)
{
submodelElementCollection.Remove(submodelElement);
}
else if (referable is SubmodelElementList submodelElementList)
{
submodelElementList.Remove(submodelElement);
}
else if (referable is Entity entity)
{
entity.Remove(submodelElement);
}
else if (referable is Operation operation)
{
operation.Remove(submodelElement);
}
}
public static void Add(this IReferable referable, ISubmodelElement submodelElement,
OperationVariableDirection direction = OperationVariableDirection.In)
{
if (referable is Submodel submodel)
{
submodel.Add(submodelElement);
}
else if (referable is AnnotatedRelationshipElement annotatedRelationshipElement)
{
annotatedRelationshipElement.Add(submodelElement);
}
else if (referable is SubmodelElementCollection submodelElementCollection)
{
submodelElementCollection.Add(submodelElement);
}
else if (referable is SubmodelElementList submodelElementList)
{
submodelElementList.Add(submodelElement);
}
else if (referable is Entity entity)
{
entity.Add(submodelElement);
}
else if (referable is Operation operation)
{
operation.Add(submodelElement, direction);
}
}
#region Display
public static EnumerationPlacmentBase GetChildrenPlacement(this IReferable referable, ISubmodelElement submodelElement)
{
if (referable is Operation operation)
{
return operation.GetChildrenPlacement(submodelElement);
}
return null;
}
#endregion
public static IIdentifiable FindParentFirstIdentifiable(this IReferable referable)
{
IReferable curr = referable;
while (curr != null)
{
if (curr is IIdentifiable curri)
return curri;
curr = curr.Parent as IReferable;
}
return null;
}
#endregion
#region ListOfReferables
public static Reference GetReference(this List<IReferable> referables)
{
return new Reference(ReferenceTypes.ExternalReference, referables.ToKeyList());
}
public static List<IKey> ToKeyList(this List<IReferable> referables)
{
var res = new List<IKey>();
foreach (var rf in referables)
res.Add(new Key(rf.GetSelfDescription()?.KeyType ?? KeyTypes.GlobalReference, rf.IdShort));
return res;
}
#endregion
public static string ToIdShortString(this IReferable rf)
{
if (rf.IdShort == null || rf.IdShort.Trim().Length < 1)
return ("<no idShort!>");
return rf.IdShort.Trim();
}
public static IReference GetReference(this IReferable referable)
{
if (referable is IIdentifiable identifiable)
{
return identifiable.GetReference();
}
else if (referable is ISubmodelElement submodelElement)
{
return submodelElement.GetModelReference();
}
else
return null;
}
public static void Validate(this IReferable referable, AasValidationRecordList results)
{
referable.BaseValidation(results);
if (referable is ConceptDescription conceptDescription)
{
conceptDescription.Validate(results);
}
else if (referable is Submodel submodel)
{
submodel.Validate(results);
}
else if (referable is ISubmodelElement submodelElement)
{
// No further validation for SME
}
}
public static void BaseValidation(this IReferable referable, AasValidationRecordList results)
{
// access
if (results == null)
return;
// check
if (string.IsNullOrEmpty(referable.IdShort))
results.Add(new AasValidationRecord(
AasValidationSeverity.SpecViolation, referable,
"Referable: missing idShort",
() =>
{
referable.IdShort = "TO_FIX";
}));
if (referable.Description != null && (referable.Description.Count < 1))
results.Add(new AasValidationRecord(
AasValidationSeverity.SchemaViolation, referable,
"Referable: existing description with missing langString",
() =>
{
referable.Description = null;
}));
}
/// <summary>
/// Tells, if the IReferable can sub-structure more elements
/// </summary>
public static bool IsStructured(this IReferable rf)
{
return (rf is ISubmodel
|| rf is ISubmodelElementCollection
|| rf is ISubmodelElementList
|| rf is IOperation
|| rf is IEntity);
}
/// <summary>
/// Tells, if the IReferable is used with an index instead of <c>idShort</c>.
/// </summary>
public static bool IsIndexed(this IReferable rf)
{
return rf is SubmodelElementList;
}
public static AasElementSelfDescription GetSelfDescription(this IReferable referable)
{
if (referable is AssetAdministrationShell)
{
return new AasElementSelfDescription("AssetAdministrationShell", "AAS",
KeyTypes.AssetAdministrationShell, null);
}
else if (referable is ConceptDescription)
{
return new AasElementSelfDescription("ConceptDescription", "CD",
KeyTypes.ConceptDescription, null);
}
else if (referable is Submodel)
{
return new AasElementSelfDescription("Submodel", "SM",
KeyTypes.Submodel, null);
}
else if (referable is Property)
{
return new AasElementSelfDescription("Property", "Prop",
KeyTypes.Property, AasSubmodelElements.Property);
}
else if (referable is MultiLanguageProperty)
{
return new AasElementSelfDescription("MultiLanguageProperty", "MLP",
KeyTypes.MultiLanguageProperty, AasSubmodelElements.MultiLanguageProperty);
}
else if (referable is AasCore.Aas3_0.Range)
{
return new AasElementSelfDescription("Range", "Range",
KeyTypes.Range, AasSubmodelElements.Range);
}
else if (referable is Blob)
{
return new AasElementSelfDescription("Blob", "Blob",
KeyTypes.Blob, AasSubmodelElements.Blob);
}
else if (referable is AasCore.Aas3_0.File)
{
return new AasElementSelfDescription("File", "File",
KeyTypes.File, AasSubmodelElements.File);
}
else if (referable is ReferenceElement)
{
return new AasElementSelfDescription("ReferenceElement", "Ref",
KeyTypes.ReferenceElement, AasSubmodelElements.ReferenceElement);
}
else if (referable is RelationshipElement)
{
return new AasElementSelfDescription("RelationshipElement", "Rel",
KeyTypes.RelationshipElement, AasSubmodelElements.RelationshipElement);
}
else if (referable is AnnotatedRelationshipElement)
{
return new AasElementSelfDescription("AnnotatedRelationshipElement", "RelA",
KeyTypes.AnnotatedRelationshipElement, AasSubmodelElements.AnnotatedRelationshipElement);
}
else if (referable is Capability)
{
return new AasElementSelfDescription("Capability", "Cap",
KeyTypes.Capability, AasSubmodelElements.Capability);
}
else if (referable is SubmodelElementCollection)
{
return new AasElementSelfDescription("SubmodelElementCollection", "SMC",
KeyTypes.SubmodelElementCollection, AasSubmodelElements.SubmodelElementCollection);
}
else if (referable is SubmodelElementList)
{
return new AasElementSelfDescription("SubmodelElementList", "SML",
KeyTypes.SubmodelElementList, AasSubmodelElements.SubmodelElementList);
}
else if (referable is Operation)
{
return new AasElementSelfDescription("Operation", "Opr",
KeyTypes.Operation, AasSubmodelElements.Operation);
}
else if (referable is Entity)
{
return new AasElementSelfDescription("Entity", "Ent",
KeyTypes.Entity, AasSubmodelElements.Entity);
}
else if (referable is BasicEventElement)
{
return new AasElementSelfDescription("BasicEventElement", "Evt",
KeyTypes.BasicEventElement, AasSubmodelElements.BasicEventElement);
}
else if (referable is IDataElement)
{
return new AasElementSelfDescription("DataElement", "DE",
KeyTypes.DataElement, AasSubmodelElements.DataElement);
}
else if (referable is ISubmodelElement)
{
return new AasElementSelfDescription("SubmodelElement", "SME",
KeyTypes.SubmodelElement, AasSubmodelElements.SubmodelElement);
}
else
{
return new AasElementSelfDescription("Referable", "Ref",
KeyTypes.Referable, null);
}
}
public static void CollectReferencesByParent(this IReferable referable, List<IKey> refs)
{
// access
if (refs == null)
return;
// check, if this is identifiable
if (referable is IIdentifiable)
{
var idf = referable as IIdentifiable;
if (idf != null)
{
var key = new Key((KeyTypes)Stringification.KeyTypesFromString(idf.GetType().Name), idf.Id);
refs.Insert(0, key);
}
}
else
{
var key = new Key((KeyTypes)Stringification.KeyTypesFromString(referable.GetType().Name), referable.IdShort);
refs.Insert(0, key);
// recurse upwards!
if (referable.Parent is IReferable prf)
prf.CollectReferencesByParent(refs);
}
}
public static void SetTimeStamp(this IReferable referable, DateTime timeStamp)
{
IReferable newReferable = referable;
do
{
newReferable.TimeStamp = timeStamp;
if (newReferable != newReferable.Parent)
{
newReferable = (IReferable)newReferable.Parent;
}
else
newReferable = null;
}
while (newReferable != null);
}
public static bool EnumeratesChildren(this ISubmodelElement elem)
{
var num = elem.EnumerateChildren().Count();
return (num > 0);
}
public static IEnumerable<ISubmodelElement> EnumerateChildren(this IReferable rf)
{
// the code below was done by Jui
// MIHO: I think, we should now use the methods of AAS core
if (rf == null)
yield break;
foreach (var desc in rf.DescendOnce())
if (desc is ISubmodelElement sme)
yield return sme;
}
public static IEnumerable<ISubmodelElement> EnumerateChildrenFor(this IReferable rf,
bool SMC = false,
bool SML = false)
{
if (rf is ISubmodelElementCollection && SMC
|| rf is ISubmodelElementList && SML)
{
foreach (var x in rf.EnumerateChildren())
yield return x;
}
}
public static void SetAllParentsAndTimestamps(this IReferable referable, IReferable parent, DateTime timeStamp, DateTime timeStampCreate)
{
if (parent == null)
return;
referable.Parent = parent;
referable.TimeStamp = timeStamp;
referable.TimeStampCreate = timeStampCreate;
foreach (var submodelElement in referable.EnumerateChildren())
{
submodelElement.SetAllParentsAndTimestamps(referable, timeStamp, timeStampCreate);
}
}
public static Submodel GetParentSubmodel(this IReferable referable)
{
IReferable parent = referable;
while (parent is not Submodel && parent != null)
parent = (IReferable)parent.Parent;
return parent as Submodel;
}
public static string CollectIdShortPathByParent(
this IReferable referable,
char separatorChar = '/',
bool excludeIdentifiable = false)
{
// recurse first
var head = "";
if (referable is not IIdentifiable
&& referable.Parent is IReferable parentReferable
&& (!excludeIdentifiable || parentReferable is not IIdentifiable))
// can go up
head = parentReferable.CollectIdShortPathByParent(separatorChar, excludeIdentifiable)
+ separatorChar;
// add own
var myid = "<no id-Short!>";
if (!string.IsNullOrEmpty(referable.IdShort))
myid = referable.IdShort.Trim();
// together
return head + myid;
}
public static void AddDescription(this IReferable referable, string language, string Text)
{
if (referable.Description == null)
referable.Description = new List<ILangStringTextType>();
referable.Description.Add(new LangStringTextType(language, Text));
}
public static List<IReferable> ListOfIReferableFrom(
System.Text.Json.Nodes.JsonNode node)
{
var res = new List<IReferable>();
if (node == null)
return res;
var array = node.AsArray();
foreach (var it in array)
{
var ir = Jsonization.Deserialize.IReferableFrom(it);
res.Add(ir);
}
return res;
}
public static Key ToKey(this IReferable rf)
{
var sd = rf.GetSelfDescription();
if (sd == null || !sd.KeyType.HasValue)
return null;
if (rf is IIdentifiable rfi)
return new Key(sd.KeyType.Value, rfi.Id);
return new Key(sd.KeyType.Value, rf.IdShort);
}
public static System.Text.Json.Nodes.JsonNode ToJsonObject(List<IClass> classes)
{
var jar = new System.Text.Json.Nodes.JsonArray();
if (classes != null)
foreach (var c in classes)
jar.Add(Jsonization.Serialize.ToJsonObject(c));
return jar;
}
public static IEnumerable<IQualifier> FindAllQualifierType(this IReferable rf, string qualifierType)
{
if (!(rf is IQualifiable rfq) || rfq.Qualifiers == null || qualifierType == null)
yield break;
foreach (var q in rfq.Qualifiers)
if (q.Type.Trim().ToLower() == qualifierType.Trim().ToLower())
yield return q;
}
public static IQualifier HasQualifierOfType(this IReferable rf, string qualifierType)
{
if (!(rf is IQualifiable rfq) || rfq.Qualifiers == null)
return null;
foreach (var q in rfq.Qualifiers)
if (q.Type?.Trim().ToLower() == qualifierType?.Trim().ToLower())
return q;
return null;
}
public static Qualifier Add(this IReferable rf, Qualifier q)
{
if (!(rf is IQualifiable rfq))
return null;
if (rfq.Qualifiers == null)
rfq.Qualifiers = new List<IQualifier>();
rfq.Qualifiers.Add(q);
return q;
}
public static IEnumerable<IExtension> FindAllExtensionName(this IReferable rf, string extensionName)
{
if (!(rf is IHasExtensions rfe) || rfe.Extensions == null)
yield break;
foreach (var e in rfe.Extensions)
if (e.Name?.Trim().ToLower() == extensionName?.Trim().ToLower())
yield return e;
}
public static IExtension HasExtensionOfName(this IReferable rf, string extensionName)
{
if (!(rf is IHasExtensions rfe) || rfe.Extensions == null)
return null;
foreach (var e in rfe.Extensions)
if (e.Name?.Trim().ToLower() == extensionName?.Trim().ToLower())
return e;
return null;
}
public static Extension Add(this IReferable rf, Extension ext)
{
if (rf.Extensions == null)
rf.Extensions = new List<IExtension>();
rf.Extensions.Add(ext);
return ext;
}
public static void MigrateV20QualifiersToExtensions(this IReferable rf)
{
// access
if (!(rf is IQualifiable iq) || iq.Qualifiers == null || !(rf is IHasExtensions ihe))
return;
// Qualifiers to migrate
var toMigrate = new[] {
"Animate.Args", "Plotting.Args", "TimeSeries.Args", "BOM.Args", "ImageMap.Args"
};
List<IQualifier> toMove = new List<IQualifier>();
foreach (var q in iq.Qualifiers)
foreach (var tm in toMigrate)
if (q?.Type?.Equals(tm, StringComparison.InvariantCultureIgnoreCase) == true)
toMove.Add(q);
// now move these
for (int i = 0; i < toMove.Count; i++)
{
var q = toMove[i];
var ext = new Extension(
name: q.Type, semanticId: q.SemanticId,
valueType: q.ValueType, value: q.Value);
rf.Add(ext);
iq.Qualifiers.Remove(q);
}
}
public static void FixReferences(IReferable rf, IEnumerable<ISubmodel> smToCheck)
{
if (rf == null || smToCheck == null)
return;
foreach (var x in rf.DescendOnce())
if (x is IReference xrf
&& xrf.Count() > 0
&& xrf.Keys.First()?.Type == KeyTypes.Submodel
&& xrf.Keys.First().Value?.HasContent() == true)
{
foreach (var smtc in smToCheck)
if (smtc.IdShort?.HasContent() == true
&& smtc.Id?.HasContent() == true
&& xrf.Keys.First().Value.Trim() == smtc.IdShort.Trim())
{
// found place to fix
xrf.Keys.First().Value = smtc.Id;
}
}
}
}
}