-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathAmlImport.cs
1675 lines (1472 loc) · 78.2 KB
/
AmlImport.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
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
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
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 Aml.Engine.CAEX;
using Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace AasxAmlImExport
{
public static class AmlImport
{
public static string PrintSemantic(CAEXSequence<RefSemanticType> sem)
{
var res = "";
if (sem != null)
foreach (var rf in sem)
if (rf.CorrespondingAttributePath != null && rf.CorrespondingAttributePath.Trim() != "")
{
if (res != "")
res += ", ";
res += rf.CorrespondingAttributePath.Trim();
}
return res;
}
public class AmlParser
{
public AdminShellPackageEnv package = null;
private class TargetIdAction
{
public int TargetId;
public Action<InternalLinkType, int> Action;
public TargetIdAction(int targetId, Action<InternalLinkType, int> action)
{
TargetId = targetId;
Action = action;
}
}
/// <summary>
/// During parsing of internal elements, AAS entities can register themselves to be source or target
/// of an AML internal link.
/// Key is the value of il.RefPartnerSide(A|B).
/// Lambda will be called, checking if link is meaningful needs to be done inside.
/// </summary>
private MultiValueDictionary<string, TargetIdAction> registerForInternalLinks =
new MultiValueDictionary<string, TargetIdAction>();
private class IeViewAmlTarget
{
public InternalElementType Ie;
public CAEXObject AmlTarget;
public IeViewAmlTarget(InternalElementType ie, CAEXObject amlTarget)
{
Ie = ie;
AmlTarget = amlTarget;
}
}
/// <summary>
/// Remember contained element refs for Views, to be assiciated later with AAS entities
/// </summary>
private List<IeViewAmlTarget> latePopoulationViews = new List<IeViewAmlTarget>();
/// <summary>
/// Hold available all IDs of input AML
/// </summary>
private Dictionary<string, InternalElementType> idDict = new Dictionary<string, InternalElementType>();
private AasAmlMatcher matcher = new AasAmlMatcher();
public AmlParser() { }
public AmlParser(AdminShellPackageEnv package)
{
this.package = package;
}
public void Debug(int indentation, string msg, params object[] args)
{
var st = String.Format(msg, args);
Console.WriteLine("{0}{1}", new String(' ', 2 * indentation), st);
}
public Reference ParseAmlReference(string refstr)
{
// trivial
if (refstr == null)
return null;
// a reference could carry multiple Keys, delimited by ","
var refstrs = refstr.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
if (refstr.Length < 1)
return null;
// build a Reference
var keyList = new List<IKey>();
// over all entries
foreach (var rs in refstrs)
{
var m = Regex.Match(rs.Trim(), @"^\(([^)]+)\)(.*)$");
if (!m.Success)
// immediate fail or next try?
return null;
// get string data
var ke = m.Groups[1].ToString();
var id = m.Groups[2].ToString();
// verify: ke has to be in allowed range
var keyType = Stringification.KeyTypesFromString(ke);
if (keyType.HasValue)
{
// create key and make on refece
var k = new Key(keyType.Value, id);
keyList.Add(k);
}
else
return null;
}
var res = new Reference(ReferenceTypes.ModelReference, keyList);
return res;
}
public bool CheckForRole(CAEXSequence<SupportedRoleClassType> seq, string refRoleClassPath)
{
if (seq == null)
return false;
foreach (var src in seq)
if (src.RefRoleClassPath != null && src.RefRoleClassPath.Trim() != "")
if (src.RefRoleClassPath.Trim().ToLower() == refRoleClassPath.Trim().ToLower())
return true;
return false;
}
public bool CheckForRole(CAEXSequence<RoleRequirementsType> seq, string refBaseRoleClassPath)
{
if (seq == null)
return false;
foreach (var src in seq)
if (src.RefBaseRoleClassPath != null && src.RefBaseRoleClassPath.Trim() != "")
if (src.RefBaseRoleClassPath.Trim().ToLower() == refBaseRoleClassPath.Trim().ToLower())
return true;
return false;
}
public bool CheckForRoleClassOrRoleRequirements(SystemUnitClassType ie, string classPath)
{
/*
HACK (MIHO, 2020-08-01): The check for role class or requirements is still questionable
but seems to be correct (see below)
Question MIHO: I dont understand the determinism behind that!
WIEGAND: me, neither ;-)
Wiegand: ich hab mir von Prof.Drath nochmal erklären lassen, wie SupportedRoleClass und
RoleRequirement verwendet werden:
In CAEX2.15(aktuelle AML Version und unsere AAS Mapping Version):
1.Eine SystemUnitClass hat eine oder mehrere SupportedRoleClasses, die ihre „mögliche Rolle
beschreiben(Drucker / Fax / kopierer)
2.Wird die SystemUnitClass als InternalElement instanziiert entscheidet man sich für eine
Hauptrolle, die dann zum RoleRequirement wird und evtl. Nebenklassen die dann
SupportedRoleClasses sind(ist ein Workaround weil CAEX2.15 in der Norm nur
ein RoleReuqirement erlaubt)
InCAEX3.0(nächste AMl Version):
1.Wie bei CAEX2.15
2.Wird die SystemUnitClass als Internal Elementinstanziiert werden die verwendeten Rollen
jeweils als RoleRequirement zugewiesen (in CAEX3 sind mehrere RoleReuqirements nun erlaubt)
*/
// Remark: SystemUnitClassType is suitable for SysUnitClasses and InternalElements
if (ie is InternalElementType iet)
if (CheckForRole(iet.RoleRequirements, classPath))
return true;
return
CheckForRole(ie.SupportedRoleClass, classPath);
}
public bool CheckAttributeFoRefSemantic(AttributeType a, string correspondingAttributePath)
{
if (a.RefSemantic != null)
foreach (var rf in a.RefSemantic)
if (rf.CorrespondingAttributePath != null &&
rf.CorrespondingAttributePath.Trim() != "" &&
rf.CorrespondingAttributePath.Trim().ToLower() == correspondingAttributePath.Trim().ToLower())
// found!
return true;
return false;
}
public AttributeType FindAttributeByRefSemantic(AttributeSequence aseq, string correspondingAttributePath)
{
foreach (var a in aseq)
{
// check attribute itself
if (CheckAttributeFoRefSemantic(a, correspondingAttributePath))
// found!
return a;
// could be childs
var x = FindAttributeByRefSemantic(a.Attribute, correspondingAttributePath);
if (x != null)
return x;
}
return null;
}
public string FindAttributeValueByRefSemantic(AttributeSequence aseq, string correspondingAttributePath)
{
var a = FindAttributeByRefSemantic(aseq, correspondingAttributePath);
return a?.Value;
}
public ExternalInterfaceType FindExternalInterfaceByNameAndBaseClassPath(
ExternalInterfaceSequence eiseq, string name, string classpath)
{
ExternalInterfaceType res = null;
if (eiseq != null)
foreach (var ei in eiseq)
if (
(name == null || (ei.Name != null &&
ei.Name.Trim().ToLower() == name.Trim().ToLower())) &&
(classpath == null ||
(ei.RefBaseClassPath != null &&
ei.RefBaseClassPath.Trim().ToLower() == classpath.Trim().ToLower())))
res = ei;
return res;
}
public List<ILangStringTextType> TryParseListOfLangStrFromAttributes(
AttributeSequence aseq, string correspondingAttributePath)
{
if (aseq == null || correspondingAttributePath == null)
return null;
var aroot = FindAttributeByRefSemantic(aseq, correspondingAttributePath);
if (aroot == null)
return null;
// primary stuff
var res = new List<ILangStringTextType> { new LangStringTextType("Default", aroot.Value) };
// assume the language-specific attributes being directly sub-ordinated
if (aroot.Attribute != null)
foreach (var a in aroot.Attribute)
{
var m = Regex.Match(a.Name.Trim(), @"([^=]+)\w*=(.*)$");
if (m.Success && m.Groups[1].ToString().ToLower() == "aml-lang")
res.Add(new LangStringTextType(m.Groups[2].ToString(), a.Value));
}
// end
return res;
}
public List<ILangStringDefinitionTypeIec61360> TryParseListOfDefinitionFromAttributes(
AttributeSequence aseq, string correspondingAttributePath)
{
if (aseq == null || correspondingAttributePath == null)
return null;
var aroot = FindAttributeByRefSemantic(aseq, correspondingAttributePath);
if (aroot == null)
return null;
// primary stuff
var res = new List<ILangStringDefinitionTypeIec61360> { new LangStringDefinitionTypeIec61360("Default", aroot.Value) };
// assume the language-specific attributes being directly sub-ordinated
if (aroot.Attribute != null)
foreach (var a in aroot.Attribute)
{
var m = Regex.Match(a.Name.Trim(), @"([^=]+)\w*=(.*)$");
if (m.Success && m.Groups[1].ToString().ToLower() == "aml-lang")
res.Add(new LangStringDefinitionTypeIec61360(m.Groups[2].ToString(), a.Value));
}
// end
return res;
}
public List<ILangStringShortNameTypeIec61360> TryParseListOfShortNamesFromAttributes(
AttributeSequence aseq, string correspondingAttributePath)
{
if (aseq == null || correspondingAttributePath == null)
return null;
var aroot = FindAttributeByRefSemantic(aseq, correspondingAttributePath);
if (aroot == null)
return null;
// primary stuff
var res = new List<ILangStringShortNameTypeIec61360> { new LangStringShortNameTypeIec61360("Default", aroot.Value) };
// assume the language-specific attributes being directly sub-ordinated
if (aroot.Attribute != null)
foreach (var a in aroot.Attribute)
{
var m = Regex.Match(a.Name.Trim(), @"([^=]+)\w*=(.*)$");
if (m.Success && m.Groups[1].ToString().ToLower() == "aml-lang")
res.Add(new LangStringShortNameTypeIec61360(m.Groups[2].ToString(), a.Value));
}
// end
return res;
}
public List<ILangStringPreferredNameTypeIec61360> TryParseListOfPreferredNamesFromAttributes(
AttributeSequence aseq, string correspondingAttributePath)
{
if (aseq == null || correspondingAttributePath == null)
return null;
var aroot = FindAttributeByRefSemantic(aseq, correspondingAttributePath);
if (aroot == null)
return null;
// primary stuff
var res = new List<ILangStringPreferredNameTypeIec61360> { new LangStringPreferredNameTypeIec61360("Default", aroot.Value) };
// assume the language-specific attributes being directly sub-ordinated
if (aroot.Attribute != null)
foreach (var a in aroot.Attribute)
{
var m = Regex.Match(a.Name.Trim(), @"([^=]+)\w*=(.*)$");
if (m.Success && m.Groups[1].ToString().ToLower() == "aml-lang")
res.Add(new LangStringPreferredNameTypeIec61360(m.Groups[2].ToString(), a.Value));
}
// end
return res;
}
public List<ILangStringTextType> TryParseDescriptionFromAttributes(
AttributeSequence aseq, string correspondingAttributePath)
{
var ls = TryParseListOfLangStrFromAttributes(aseq, correspondingAttributePath);
if (ls == null)
return null;
var res = new List<ILangStringTextType>(ls);
return res;
}
public List<IQualifier> TryParseQualifiersFromAttributes(AttributeSequence aseq)
{
if (aseq == null)
return null;
List<IQualifier> res = null;
foreach (var a in aseq)
if (CheckAttributeFoRefSemantic(a, AmlConst.Attributes.Qualifer))
{
// gather
var qt = FindAttributeValueByRefSemantic(a.Attribute, AmlConst.Attributes.Qualifer_Type);
var qv = FindAttributeValueByRefSemantic(a.Attribute, AmlConst.Attributes.Qualifer_Value);
var sid = FindAttributeValueByRefSemantic(a.Attribute, AmlConst.Attributes.SemanticId);
var qvid = FindAttributeValueByRefSemantic(a.Attribute, AmlConst.Attributes.Qualifer_ValueId);
// check
if ((qt != null || sid != null) && (qv != null || qvid != null))
{
// create
var q = new Qualifier(qt, DataTypeDefXsd.String)
{
Value = qv,
SemanticId = new Reference(ReferenceTypes.ModelReference, ParseAmlReference(sid)?.Keys),
ValueId = ParseAmlReference(qvid)
};
// add
if (res == null)
res = new List<IQualifier>();
res.Add(q);
}
}
return res;
}
public List<Reference> TryParseDataSpecificationFromAttributes(AttributeSequence aseq)
{
if (aseq == null)
return null;
List<Reference> res = null;
foreach (var a in aseq)
if (CheckAttributeFoRefSemantic(a, AmlConst.Attributes.DataSpecificationRef))
{
var r = ParseAmlReference(a.Value);
if (r != null)
{
if (res == null)
res = new List<Reference>(); //default initilization
//TODO (jtikekar, 0000-00-00): Temporarily removed, cannot be added, as it may reflect in the other places, like AssetAdministrationShell does not contain EmbeddedDS
// dead-csharp off
//res.Add(new EmbeddedDataSpecification(r));
// dead-csharp on
res.Add(r);
}
}
return res;
}
public List<T> TryParseListItemsFromAttributes<T>(
AttributeSequence aseq, string correspondingAttributePath, Func<string, T> lambda)
{
var list = new List<T>();
foreach (var a in aseq)
if (CheckAttributeFoRefSemantic(a, correspondingAttributePath))
{
var item = lambda(a.Value);
list.Add(item);
}
return list;
}
private void AddToSubmodelOrSmec(IReferable parent, ISubmodelElement se)
{
if (parent is Submodel submodel)
{
submodel.Add(se);
}
else if (parent is SubmodelElementCollection collection)
{
collection.Add(se);
}
else
{
Console.WriteLine("Unsupported parent");
}
}
private IAssetAdministrationShell TryParseAasFromIe(SystemUnitClassType ie)
{
// begin new (temporary) object
var aas = new AssetAdministrationShell("", new AssetInformation(AssetKind.Instance));
// gather important attributes
var idShort = FindAttributeValueByRefSemantic(ie.Attribute, AmlConst.Attributes.Referable_IdShort);
var id = FindAttributeValueByRefSemantic(ie.Attribute, AmlConst.Attributes.Identification_id);
var version = FindAttributeValueByRefSemantic(
ie.Attribute, AmlConst.Attributes.Administration_Version);
var revision = FindAttributeValueByRefSemantic(
ie.Attribute, AmlConst.Attributes.Administration_Revision);
var cat = FindAttributeValueByRefSemantic(ie.Attribute, AmlConst.Attributes.Referable_Category);
var desc = TryParseDescriptionFromAttributes(ie.Attribute, AmlConst.Attributes.Referable_Description);
var ds = TryParseDataSpecificationFromAttributes(ie.Attribute);
var derivedfrom = FindAttributeValueByRefSemantic(ie.Attribute, AmlConst.Attributes.AAS_DerivedFrom);
// we need to have some important information
if (id != null)
{
// set data
aas.IdShort = ie.Name;
if (idShort != null)
aas.IdShort = idShort;
aas.Id = id;
if (version != null && revision != null)
aas.Administration = new AdministrativeInformation(version: version, revision: revision);
aas.Category = cat;
if (desc != null)
aas.Description = desc;
if (ds != null)
{
var list = ds.Select((dsi) => new EmbeddedDataSpecification(dsi, null)).ToList();
aas.EmbeddedDataSpecifications = list.ConvertAll(eds => (IEmbeddedDataSpecification)eds);
}
if (derivedfrom != null)
{
var derivedFromRef = ParseAmlReference(derivedfrom);
aas.DerivedFrom = new Reference(derivedFromRef.Type, derivedFromRef.Keys);
}
// result
return aas;
}
else
// uups!
return null;
}
private AssetInformation TryParseAssetFromIe(InternalElementType ie)
{
// begin new (temporary) object
var asset = new AssetInformation(AssetKind.Instance);
// gather important attributes
var idShort = FindAttributeValueByRefSemantic(ie.Attribute, AmlConst.Attributes.Referable_IdShort);
var id = FindAttributeValueByRefSemantic(ie.Attribute, AmlConst.Attributes.Identification_id);
var version = FindAttributeValueByRefSemantic(
ie.Attribute, AmlConst.Attributes.Administration_Version);
var revision = FindAttributeValueByRefSemantic(
ie.Attribute, AmlConst.Attributes.Administration_Revision);
var cat = FindAttributeValueByRefSemantic(ie.Attribute, AmlConst.Attributes.Referable_Category);
var desc = TryParseDescriptionFromAttributes(ie.Attribute, AmlConst.Attributes.Referable_Description);
var kind = FindAttributeValueByRefSemantic(ie.Attribute, AmlConst.Attributes.Asset_Kind);
var ds = TryParseDataSpecificationFromAttributes(ie.Attribute);
// we need to have some important information
if (id != null)
{
// dead-csharp off
// set data
//TODO (jtikekar, 0000-00-00): Uncomment and Support
//asset.identification = new Identification(idType, id);
//NO administrativeInformation, catagory or description in V3 AssetInformation
//if (version != null && revision != null)
// asset.administration = new Administration(version, revision);
//asset.Category = cat;
//if (desc != null)
// asset.Description = desc;
asset.GlobalAssetId = id;
if (kind != null)
asset.AssetKind = (AssetKind)Stringification.AssetKindFromString(kind);
//No DataSpecification asset
//if (ds != null)
// asset.hasDataSpecification = ds;
// dead-csharp on
// result
return asset;
}
else
// uups!
return null;
}
private void FillDictWithInternalElementsIds(
Dictionary<string, InternalElementType> dict, InternalElementSequence ieseq)
{
if (dict == null || ieseq == null)
return;
foreach (var ie in ieseq)
{
if (ie.ID != null)
dict.Add(ie.ID, ie);
FillDictWithInternalElementsIds(dict, ie.InternalElement);
}
}
private InternalElementType FindInternalElementByID(string ID)
{
if (ID == null)
return null;
if (!idDict.ContainsKey(ID))
return null;
return idDict[ID];
}
// dead-csharp off
//private View TryParseViewFromIe(InstanceHierarchyType insthier, InternalElementType ie)
//{
// // access
// if (insthier == null || ie == null)
// return null;
// //
// // make up local data management
// //
// // begin new (temporary) objects
// var view = new View();
// // gather important attributes
// var idShort = FindAttributeValueByRefSemantic(ie.Attribute, AmlConst.Attributes.Referable_IdShort);
// var cat = FindAttributeValueByRefSemantic(ie.Attribute, AmlConst.Attributes.Referable_Category);
// var desc = TryParseDescriptionFromAttributes(ie.Attribute, AmlConst.Attributes.Referable_Description);
// var ds = TryParseDataSpecificationFromAttributes(ie.Attribute);
// var semid = FindAttributeValueByRefSemantic(ie.Attribute, AmlConst.Attributes.SemanticId);
// // we need to have some important information
// if (ie.Name != null)
// {
// // set data
// view.IdShort = ie.Name;
// if (idShort != null)
// view.IdShort = idShort;
// view.Category = cat;
// if (desc != null)
// view.Description = desc;
// if (semid != null)
// view.SemanticId = SemanticId.CreateFromKeys(ParseAmlReference(semid)?.Keys);
// if (ds != null)
// view.hasDataSpecification = ds;
// // check for direct descendents to be "Mirror-Elements"
// if (ie.InternalElement != null)
// foreach (var mie in ie.InternalElement)
// if (mie.RefBaseSystemUnitPath.HasContent())
// {
// // candidate .. try identify target
// var el = FindInternalElementByID(mie.RefBaseSystemUnitPath);
// if (el != null)
// {
// // for the View's contain element references, all targets of the references
// // shall exists.
// // This is not already the case, therefore store the AML IE / View Information
// // for later parsing
// this.latePopoulationViews.Add(new IeViewAmlTarget(ie, view, el));
// }
// }
// // result
// return view;
// }
// else
// // uups!
// return null;
//}
// dead-csharp on
private ISubmodel TryParseSubmodelFromIe(SystemUnitClassType ie)
{
// begin new (temporary) object
var sm = new Submodel("");
// gather important attributes
var idShort = FindAttributeValueByRefSemantic(ie.Attribute, AmlConst.Attributes.Referable_IdShort);
var id = FindAttributeValueByRefSemantic(ie.Attribute, AmlConst.Attributes.Identification_id);
var version = FindAttributeValueByRefSemantic(
ie.Attribute, AmlConst.Attributes.Administration_Version);
var revision = FindAttributeValueByRefSemantic(
ie.Attribute, AmlConst.Attributes.Administration_Revision);
var cat = FindAttributeValueByRefSemantic(ie.Attribute, AmlConst.Attributes.Referable_Category);
var desc = TryParseDescriptionFromAttributes(ie.Attribute, AmlConst.Attributes.Referable_Description);
var semid = FindAttributeValueByRefSemantic(ie.Attribute, AmlConst.Attributes.SemanticId);
var kind = FindAttributeValueByRefSemantic(ie.Attribute, AmlConst.Attributes.HasKind_Kind);
var qualifiers = TryParseQualifiersFromAttributes(ie.Attribute);
var ds = TryParseDataSpecificationFromAttributes(ie.Attribute);
// we need to have some important information
if (id != null)
{
// set data
sm.IdShort = ie.Name;
if (idShort != null)
sm.IdShort = idShort;
sm.Id = id;
if (version != null && revision != null)
sm.Administration = new AdministrativeInformation(version: version, revision: revision);
sm.Category = cat;
if (desc != null)
sm.Description = desc;
if (semid != null)
sm.SemanticId = new Reference(ReferenceTypes.ModelReference, ParseAmlReference(semid)?.Keys);
if (kind != null)
sm.Kind = Stringification.ModellingKindFromString(kind);
if (qualifiers != null)
sm.Qualifiers = qualifiers;
if (ds != null)
{
var list = ds.Select((dsi) => new EmbeddedDataSpecification(dsi, null)).ToList();
sm.EmbeddedDataSpecifications = list.ConvertAll(eds => (IEmbeddedDataSpecification)eds);
}
// result
return sm;
}
else
// uups!
return null;
}
private SubmodelElementCollection TryParseSubmodelElementCollection(SystemUnitClassType ie)
{
// begin new (temporary) object
var smec = new SubmodelElementCollection();
// gather important attributes
var idShort = FindAttributeValueByRefSemantic(ie.Attribute, AmlConst.Attributes.Referable_IdShort);
var semid = FindAttributeValueByRefSemantic(ie.Attribute, AmlConst.Attributes.SemanticId);
var kind = FindAttributeValueByRefSemantic(ie.Attribute, AmlConst.Attributes.HasKind_Kind);
var cat = FindAttributeValueByRefSemantic(ie.Attribute, AmlConst.Attributes.Referable_Category);
var desc = TryParseDescriptionFromAttributes(ie.Attribute, AmlConst.Attributes.Referable_Description);
var qualifiers = TryParseQualifiersFromAttributes(ie.Attribute);
var ds = TryParseDataSpecificationFromAttributes(ie.Attribute);
// we need to have some important information (only IReferable name, shoud be always there..)
if (ie.Name != null)
{
// set data
smec.IdShort = ie.Name;
if (idShort != null)
smec.IdShort = idShort;
if (semid != null)
smec.SemanticId = new Reference(ReferenceTypes.ModelReference, ParseAmlReference(semid)?.Keys);
if (desc != null)
smec.Description = desc;
if (cat != null)
smec.Category = cat;
if (qualifiers != null)
smec.Qualifiers = qualifiers;
if (ds != null)
{
var list = ds.Select((dsi) => new EmbeddedDataSpecification(dsi, null)).ToList();
smec.EmbeddedDataSpecifications = list.ConvertAll(eds => (IEmbeddedDataSpecification)eds);
}
// result
return smec;
}
else
// uups!
return null;
}
private void TryPopulateReferenceAttribute(
SystemUnitClassType ie, string ifName, string ifClassPath, ISubmodelElement target,
int targetId = 0)
{
// now used
var ei = FindExternalInterfaceByNameAndBaseClassPath(ie.ExternalInterface, ifName, ifClassPath);
if (ei != null)
{
// 1st, try parse internal AML relationship
// by this, AML can easily setup a reference
// to do so, register a link and attach the appropriate lambda
this.registerForInternalLinks.Add(
"" + ie.ID + ":" + ifName,
new TargetIdAction(
targetId,
(il, ti) =>
{
// trivial
if (il == null || ti != targetId)
return;
// assume to be side A
if (il.RelatedObjects.ASystemUnitClass == null ||
il.RelatedObjects.ASystemUnitClass != ie)
return;
// extract side B
if (il.RelatedObjects.BSystemUnitClass == null)
return;
// need to find the AASX entity of it!
// in a good world, the match can this do for us!
var aasref = matcher.GetAasObject(il.RelatedObjects.BSystemUnitClass);
if (aasref == null)
return;
// get a "real" reference of this
var theref = new Reference(ReferenceTypes.ModelReference, new List<IKey>());
aasref.CollectReferencesByParent(theref.Keys);
// nooooooooooow, set this
if (targetId == 1 && target is ReferenceElement tre)
tre.Value = theref;
if (targetId == 2 && target is RelationshipElement trse)
trse.First = theref;
if (targetId == 3 && target is RelationshipElement tre2)
tre2.Second = theref;
})
);
// 2nd (but earlier in evaluation sequence), we can try to access the AAS Reference
// via value directly
var value = FindAttributeValueByRefSemantic(
ei.Attribute, AmlConst.Attributes.ReferenceElement_Value);
if (value != null)
{
if (targetId == 1 && target is ReferenceElement tre)
tre.Value = ParseAmlReference(value);
if (targetId == 2 && target is RelationshipElement trse)
trse.First = ParseAmlReference(value);
if (targetId == 3 && target is RelationshipElement tre2)
tre2.Second = ParseAmlReference(value);
}
}
}
private ISubmodelElement TryPopulateSubmodelElement(
SystemUnitClassType ie, ISubmodelElement sme, bool aasStyleAttributes = false,
bool amlStyleAttributes = true)
{
// access?
if (sme == null)
return null;
// gather important attributes
var idShort = FindAttributeValueByRefSemantic(ie.Attribute, AmlConst.Attributes.Referable_IdShort);
var semid = FindAttributeValueByRefSemantic(ie.Attribute, AmlConst.Attributes.SemanticId);
var kind = FindAttributeValueByRefSemantic(ie.Attribute, AmlConst.Attributes.HasKind_Kind);
var cat = FindAttributeValueByRefSemantic(ie.Attribute, AmlConst.Attributes.Referable_Category);
var desc = TryParseDescriptionFromAttributes(ie.Attribute, AmlConst.Attributes.Referable_Description);
var qualifiers = TryParseQualifiersFromAttributes(ie.Attribute);
var ds = TryParseDataSpecificationFromAttributes(ie.Attribute);
if (ie.Name != null)
{
// set information
sme.IdShort = ie.Name;
if (idShort != null)
sme.IdShort = idShort;
if (semid != null)
sme.SemanticId = new Reference(ReferenceTypes.ModelReference, ParseAmlReference(semid)?.Keys);
if (desc != null)
sme.Description = desc;
if (cat != null)
sme.Category = cat;
if (qualifiers != null)
sme.Qualifiers = qualifiers;
if (ds != null)
{
var list = ds.Select((dsi) => new EmbeddedDataSpecification(dsi, null)).ToList();
sme.EmbeddedDataSpecifications = list.ConvertAll(eds => (IEmbeddedDataSpecification)eds);
}
// and also special attributes for each adequate type
if (sme is Property p)
{
var value = FindAttributeValueByRefSemantic(ie.Attribute, AmlConst.Attributes.Property_Value);
var valueAttr = FindAttributeByRefSemantic(ie.Attribute, AmlConst.Attributes.Property_Value);
var valueId = FindAttributeValueByRefSemantic(
ie.Attribute, AmlConst.Attributes.Property_ValueId);
p.Value = value;
if (valueId != null)
p.ValueId = ParseAmlReference(valueId);
if (valueAttr != null)
p.ValueType = Stringification.DataTypeDefXsdFromString(ParseAmlDataType(
valueAttr.AttributeDataType)) ?? DataTypeDefXsd.String;
}
if (sme is AasCore.Aas3_0.Range rng)
{
var min = FindAttributeValueByRefSemantic(ie.Attribute, AmlConst.Attributes.Range_Min);
var minAttr = FindAttributeByRefSemantic(ie.Attribute, AmlConst.Attributes.Range_Min);
var max = FindAttributeValueByRefSemantic(ie.Attribute, AmlConst.Attributes.Range_Max);
var maxAttr = FindAttributeByRefSemantic(ie.Attribute, AmlConst.Attributes.Range_Max);
if (min != null)
{
rng.Min = min;
if (minAttr != null)
rng.ValueType = Stringification.DataTypeDefXsdFromString(ParseAmlDataType(minAttr.AttributeDataType))
?? DataTypeDefXsd.String;
}
if (max != null)
{
rng.Max = max;
if (maxAttr != null)
rng.ValueType = Stringification.DataTypeDefXsdFromString(ParseAmlDataType(maxAttr.AttributeDataType))
?? DataTypeDefXsd.String;
}
}
if (sme is MultiLanguageProperty mlp)
{
var value = TryParseDescriptionFromAttributes(
ie.Attribute, AmlConst.Attributes.MultiLanguageProperty_Value);
var valueId = FindAttributeValueByRefSemantic(
ie.Attribute, AmlConst.Attributes.MultiLanguageProperty_ValueId);
if (value != null)
mlp.Value = value.Copy();
if (valueId != null)
mlp.ValueId = ParseAmlReference(valueId);
}
if (sme is Blob smeb)
{
var mimeType = FindAttributeValueByRefSemantic(
ie.Attribute, AmlConst.Attributes.Blob_MimeType);
var value = FindAttributeValueByRefSemantic(ie.Attribute, AmlConst.Attributes.Blob_Value);
if (mimeType != null)
smeb.ContentType = mimeType;
if (value != null)
smeb.Value = Encoding.Default.GetBytes(value);
}
if (sme is File smef)
{
var mimeType = FindAttributeValueByRefSemantic(
ie.Attribute, AmlConst.Attributes.File_MimeType);
var value = FindAttributeValueByRefSemantic(ie.Attribute, AmlConst.Attributes.File_Value);
if (mimeType != null)
smef.ContentType = mimeType;
if (value != null)
smef.Value = value;
}
if (sme is ReferenceElement smer)
{
if (aasStyleAttributes)
{
// not used anymore!
var value = FindAttributeValueByRefSemantic(
ie.Attribute, AmlConst.Attributes.ReferenceElement_Value);
if (value != null)
smer.Value = ParseAmlReference(value);
}
if (amlStyleAttributes)
{
// now the default
TryPopulateReferenceAttribute(
ie, "ReferableReference", AmlConst.Interfaces.ReferableReference, smer, 1);
}
}
// will also include AnnotatedRelationship !!
if (sme is RelationshipElement smere)
{
if (aasStyleAttributes)
{
// not used anymore!
var first = FindAttributeValueByRefSemantic(
ie.Attribute, AmlConst.Attributes.RelationshipElement_First);
var second = FindAttributeValueByRefSemantic(
ie.Attribute, AmlConst.Attributes.RelationshipElement_Second);
if (first != null && second != null)
{
smere.First = ParseAmlReference(first);
smere.Second = ParseAmlReference(second);
}
}
if (amlStyleAttributes)
{
// now the default
TryPopulateReferenceAttribute(
ie, "first", AmlConst.Interfaces.ReferableReference, smere, 2);
TryPopulateReferenceAttribute(
ie, "second", AmlConst.Interfaces.ReferableReference, smere, 3);
}
}
if (sme is Entity ent)
{
var entityType = FindAttributeValueByRefSemantic(
ie.Attribute, AmlConst.Attributes.Entity_entityType);
if (entityType != null)
ent.EntityType = (EntityType)Stringification.EntityTypeFromString(entityType);
var assetRef = FindAttributeValueByRefSemantic(
ie.Attribute, AmlConst.Attributes.Entity_asset);
if (assetRef != null)
{
var reference = ParseAmlReference(assetRef);
ent.GlobalAssetId = reference.GetAsIdentifier();
}
}
// ok
return sme;
}
else
// uups!
return null;
}
private ConceptDescription TryParseConceptDescription(AttributeSequence aseq)
{
// begin new (temporary) object
var cd = new ConceptDescription("");
// gather important attributes
var idShort = FindAttributeValueByRefSemantic(aseq, AmlConst.Attributes.Referable_IdShort);
var id = FindAttributeValueByRefSemantic(aseq, AmlConst.Attributes.Identification_id);
var version = FindAttributeValueByRefSemantic(aseq, AmlConst.Attributes.Administration_Version);
var revision = FindAttributeValueByRefSemantic(aseq, AmlConst.Attributes.Administration_Revision);
var cat = FindAttributeValueByRefSemantic(aseq, AmlConst.Attributes.Referable_Category);
var desc = TryParseDescriptionFromAttributes(aseq, AmlConst.Attributes.Referable_Description);
// we need to have some important information (only IReferable name, shoud be always there..)
if (id != null)
{
// set normal data
cd.IdShort = idShort;
cd.Id = id;
if (version != null && revision != null)
cd.Administration = new AdministrativeInformation(version: version, revision: revision);
if (desc != null)
cd.Description = desc;
if (cat != null)
cd.Category = cat;
// special data
cd.IsCaseOf = TryParseListItemsFromAttributes<IReference>(
aseq, AmlConst.Attributes.CD_IsCaseOf, (s) => { return ParseAmlReference(s); });
// result
return cd;
}