-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathAmlExport.cs
1185 lines (1016 loc) · 51 KB
/
AmlExport.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 AasxIntegrationBase;
using AdminShellNS;
using Aml.Engine.CAEX;
using Extensions;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text.RegularExpressions;
using Aas = AasCore.Aas3_0;
namespace AasxAmlImExport
{
public static class AmlExport
{
private class AmlInternalLinkEntity
{
public string Name = "InternalLink";
public ExternalInterfaceType extIf = null;
public IReferable sideA = null;
public IReferable sideB = null;
public string ifClassA = null;
public string ifClassB = null;
public Action<InternalElementType> lambdaForSideB = null;
public AmlInternalLinkEntity(
ExternalInterfaceType extIf, IReferable sideA, IReferable sideB,
string ifClassA, string ifClassB, Action<InternalElementType> lambdaForSideB = null)
{
this.extIf = extIf;
this.sideA = sideA;
this.sideB = sideB;
this.ifClassA = ifClassA;
this.ifClassB = ifClassB;
this.lambdaForSideB = lambdaForSideB;
}
}
private static InternalElementType AppendIeNameAndRole(
InternalElementSequence ieseq, string name, string altName = "Unknown", string role = null)
{
if (ieseq == null)
return null;
var ie = ieseq.Append(
name != null && name.Trim() != "" ? name : altName + " " + Guid.NewGuid().ToString());
if (role != null)
{
var rr = ie.RoleRequirements.Append();
rr.RefBaseRoleClassPath = role;
}
return ie;
}
private static AttributeType AppendAttributeNameAndRole(
AttributeSequence aseq, string name, string role = null, string val = null,
string attributeDataType = null)
{
if (aseq == null)
return null;
var a = aseq.Append(name);
if (role != null)
{
var rf = a.RefSemantic.Append();
rf.CorrespondingAttributePath = role;
}
if (val != null)
{
a.Value = val;
}
if (attributeDataType != null)
{
a.AttributeDataType = attributeDataType;
}
return a;
}
private static void SetIdentification(AttributeSequence aseq, string id)
{
if (id == null)
return;
var a0 = AppendAttributeNameAndRole(aseq, "identification", AmlConst.Attributes.Identification);
AppendAttributeNameAndRole(a0.Attribute, "id", AmlConst.Attributes.Identification_id, id);
}
private static void SetAdministration(AttributeSequence aseq, IAdministrativeInformation adm)
{
if (adm == null)
return;
var a0 = AppendAttributeNameAndRole(aseq, "administration", AmlConst.Attributes.Administration);
AppendAttributeNameAndRole(
a0.Attribute, "version", AmlConst.Attributes.Administration_Version, adm.Version);
AppendAttributeNameAndRole(
a0.Attribute, "revision", AmlConst.Attributes.Administration_Revision, adm.Revision);
}
private static void SetLangStr(
AttributeSequence aseq, List<ILangStringTextType> langString, string aname, string role)
{
if (aseq == null || langString == null || langString.Count < 1)
return;
var a0 = AppendAttributeNameAndRole(aseq, aname, role, langString[0].Text);
foreach (var ls in langString)
{
if (ls.Language.Trim().ToLower() == "default")
continue;
AppendAttributeNameAndRole(
a0.Attribute, AmlConst.Names.AmlLanguageHeader + ls.Language, role: null, val: ls.Text);
}
}
private static void SetLangStr(
AttributeSequence aseq, List<ILangStringDefinitionTypeIec61360> langString, string aname, string role)
{
if (aseq == null || langString == null || langString.Count < 1)
return;
var a0 = AppendAttributeNameAndRole(aseq, aname, role, langString[0].Text);
foreach (var ls in langString)
{
if (ls.Language.Trim().ToLower() == "default")
continue;
AppendAttributeNameAndRole(
a0.Attribute, AmlConst.Names.AmlLanguageHeader + ls.Language, role: null, val: ls.Text);
}
}
private static void SetLangStr(
AttributeSequence aseq, List<ILangStringShortNameTypeIec61360> langString, string aname, string role)
{
if (aseq == null || langString == null || langString.Count < 1)
return;
var a0 = AppendAttributeNameAndRole(aseq, aname, role, langString[0].Text);
foreach (var ls in langString)
{
if (ls.Language.Trim().ToLower() == "default")
continue;
AppendAttributeNameAndRole(
a0.Attribute, AmlConst.Names.AmlLanguageHeader + ls.Language, role: null, val: ls.Text);
}
}
private static void SetLangStr(
AttributeSequence aseq, List<ILangStringPreferredNameTypeIec61360> langString, string aname, string role)
{
if (aseq == null || langString == null || langString.Count < 1)
return;
var a0 = AppendAttributeNameAndRole(aseq, aname, role, langString[0].Text);
foreach (var ls in langString)
{
if (ls.Language.Trim().ToLower() == "default")
continue;
AppendAttributeNameAndRole(
a0.Attribute, AmlConst.Names.AmlLanguageHeader + ls.Language, role: null, val: ls.Text);
}
}
private static void SetReferable(AttributeSequence aseq, IReferable rf)
{
if (aseq == null || rf == null)
return;
if (rf.IdShort != null)
AppendAttributeNameAndRole(aseq, "idShort", AmlConst.Attributes.Referable_IdShort, rf.IdShort);
if (rf.Category != null)
AppendAttributeNameAndRole(aseq, "category", AmlConst.Attributes.Referable_Category, rf.Category);
SetLangStr(aseq, rf.Description, "description", AmlConst.Attributes.Referable_Description);
}
private static void SetAssetKind(
AttributeSequence aseq, AssetKind kind, string attributeRole = null)
{
if (aseq == null)
return;
if (attributeRole == null)
attributeRole = AmlConst.Attributes.HasKind_Kind;
AppendAttributeNameAndRole(aseq, "kind", attributeRole, Stringification.ToString(kind));
}
private static void SetModelingKind(
AttributeSequence aseq, ModellingKind? kind, string attributeRole = null)
{
if (aseq == null || !kind.HasValue)
return;
if (attributeRole == null)
attributeRole = AmlConst.Attributes.HasKind_Kind;
AppendAttributeNameAndRole(aseq, "kind", attributeRole, Stringification.ToString(kind));
}
private static string ToAmlName(string input)
{
var clean = Regex.Replace(input, @"[^a-zA-Z0-9\-_]", "_");
while (true)
{
var len0 = clean.Length;
clean = clean.Replace("__", "_");
if (len0 == clean.Length)
break;
}
return clean;
}
private static string ToAmlSemanticId(IReference semid)
{
if (semid == null || semid.IsEmpty())
return null;
var semstr = "";
foreach (var k in semid.Keys)
semstr += String.Format(
"({0})({1})", k.Type, k.Value);
return semstr;
}
private static string ToAmlReference(IReference refid)
{
if (refid == null || refid.IsEmpty())
return null;
var semstr = "";
foreach (var k in refid.Keys)
{
if (semstr != "")
semstr += ",";
semstr += String.Format(
"({0})({1})", k.Type, k.Value);
}
return semstr;
}
private static void SetSemanticId(AttributeSequence aseq, IReference semid)
{
if (aseq == null || semid == null || semid.IsEmpty())
return;
AppendAttributeNameAndRole(aseq, "semanticId", AmlConst.Attributes.SemanticId, ToAmlSemanticId(semid));
}
private static void SetHasDataSpecification(AttributeSequence aseq, List<IEmbeddedDataSpecification> ds)
{
if (aseq == null || ds == null || ds.Count < 1)
return;
foreach (var r in ds)
AppendAttributeNameAndRole(
aseq, "dataSpecification", AmlConst.Attributes.DataSpecificationRef,
ToAmlReference(r.DataSpecification));
}
private static void SetQualifiers(
InternalElementSequence parentIeSeq, AttributeSequence parentAttrSeq,
List<IQualifier> qualifiers, bool parentAsInternalElements = false)
{
if ((parentIeSeq == null && parentAttrSeq == null) || qualifiers == null || qualifiers.Count < 1)
return;
foreach (var q in qualifiers)
{
// aml-stlye name
var qid = AmlConst.Names.AmlQualifierHeader + (q.Type?.Trim() ?? "qualifier");
if (q.Value != null)
qid += "=" + q.Value.Trim();
else if (q.ValueId != null)
qid += "=" + ToAmlReference(q.ValueId);
AttributeSequence qas = null;
if (parentAsInternalElements && parentIeSeq != null)
{
// choose IE as well
var qie = AppendIeNameAndRole(
parentIeSeq, name: q.Type, altName: "Qualifier", role: AmlConst.Roles.Qualifer);
qas = qie.Attribute;
}
else
{
var a = AppendAttributeNameAndRole(parentAttrSeq, qid, AmlConst.Attributes.Qualifer);
qas = a.Attribute;
}
if (q.SemanticId != null)
AppendAttributeNameAndRole(
qas, "semanticId", AmlConst.Attributes.SemanticId, ToAmlSemanticId(q.SemanticId));
if (q.Type != null)
AppendAttributeNameAndRole(qas, "type", AmlConst.Attributes.Qualifer_Type, q.Type);
if (q.Value != null)
AppendAttributeNameAndRole(qas, "value", AmlConst.Attributes.Qualifer_Value, q.Value);
if (q.ValueId != null)
AppendAttributeNameAndRole(
qas, "valueId", AmlConst.Attributes.Qualifer_ValueId, ToAmlReference(q.ValueId));
}
}
private static void ExportReferenceWithSme(
Aas.IEnvironment env,
List<AmlInternalLinkEntity> internalLinksToCreate,
InternalElementType ie,
IReferable referable,
IReference refInReferable,
string attrName,
string roleName,
string outgoingLinkName,
bool aasStyleAttributes = false, bool amlStyleAttributes = true)
{
// access
if (env == null || ie == null || !attrName.HasContent() || !roleName.HasContent()
|| !outgoingLinkName.HasContent())
return;
// working mode(s)
if (aasStyleAttributes)
{
if (refInReferable != null)
{
AppendAttributeNameAndRole(ie.Attribute, attrName, roleName, ToAmlReference(refInReferable));
}
}
if (amlStyleAttributes)
{
var extIf = ie.ExternalInterface.Append(outgoingLinkName);
if (extIf != null)
{
// set the internal interface by class
extIf.RefBaseClassPath = AmlConst.Interfaces.ReferableReference;
// serialize Reference as string in AAS style
if (refInReferable != null)
{
AppendAttributeNameAndRole(ie.Attribute, attrName, roleName, ToAmlReference(refInReferable));
}
// try find the referenced element as IReferable in the AAS environment
var targetReferable = env.FindReferableByReference(refInReferable);
if (targetReferable != null && internalLinksToCreate != null)
{
internalLinksToCreate.Add(
new AmlInternalLinkEntity(extIf, referable, targetReferable,
outgoingLinkName, "ReferableReference",
(x) =>
{
if (x != null)
{
var x2 = x.ExternalInterface.Append("ReferableReference");
if (x2 != null)
{
x2.RefBaseClassPath = AmlConst.Interfaces.ReferableReference;
}
}
}));
}
}
}
}
private static void ExportListOfSme(
AasAmlMatcher matcher, List<AmlInternalLinkEntity> internalLinksToCreate,
SystemUnitClassType parent, Aas.IEnvironment env,
List<ISubmodelElement> wrappers, bool tryUseCompactProperties = false,
bool aasStyleAttributes = false, bool amlStyleAttributes = true)
{
if (parent == null || env == null || wrappers == null)
return;
foreach (var smw in wrappers)
{
// access
var sme = smw;
var smep = sme as Property;
if (sme == null)
continue;
// device if compact or not ..
if (tryUseCompactProperties && smep != null && smep.Value != null && smep.ValueId == null)
{
//
// Property as compact attribute
//
// value itself as Property with idShort
var a = AppendAttributeNameAndRole(
parent.Attribute, smep.IdShort, AmlConst.Attributes.SME_Property, smep.Value);
// here is no equivalence to set a match!! (MIHO deleted the **to**do** here)
// add some data underneath
SetReferable(a.Attribute, sme);
SetSemanticId(a.Attribute, sme.SemanticId);
SetHasDataSpecification(a.Attribute, sme.EmbeddedDataSpecifications);
// Property specific
a.AttributeDataType = "xs:" + Stringification.ToString(smep.ValueType).Trim();
// Qualifiers
SetQualifiers(null, a.Attribute, sme.Qualifiers, parentAsInternalElements: false);
}
else
{
//
// SubmodelElement as self-standing internal element
//
// make an InternalElement
var ie = AppendIeNameAndRole(
parent.InternalElement, name: sme.IdShort, altName: sme.GetType().Name,
role: AmlConst.Roles.SubmodelElement_Header + sme.GetType().Name);
matcher.AddMatch(sme, ie);
// set some data
SetReferable(ie.Attribute, sme);
SetSemanticId(ie.Attribute, sme.SemanticId);
SetHasDataSpecification(ie.Attribute, sme.EmbeddedDataSpecifications);
// depends on type
if (smep != null)
{
if (smep.Value != null)
{
var a = AppendAttributeNameAndRole(
ie.Attribute, "value", AmlConst.Attributes.Property_Value, smep.Value);
a.AttributeDataType = "xs:" + Stringification.ToString(smep.ValueType).Trim();
}
if (smep.ValueId != null)
AppendAttributeNameAndRole(
ie.Attribute, "valueId", AmlConst.Attributes.Property_ValueId,
ToAmlReference(smep.ValueId));
}
switch (sme)
{
case MultiLanguageProperty mlp:
// value
if (mlp.Value != null)
{
SetLangStr(ie.Attribute, mlp.Value, "value",
AmlConst.Attributes.MultiLanguageProperty_Value);
}
// value id
if (mlp.ValueId != null)
AppendAttributeNameAndRole(
ie.Attribute, "valueId", AmlConst.Attributes.MultiLanguageProperty_ValueId,
ToAmlReference(mlp.ValueId));
break;
case Blob smeb:
// mime type
if (smeb.ContentType != null)
AppendAttributeNameAndRole(
ie.Attribute, "mimeType", AmlConst.Attributes.Blob_MimeType, smeb.ContentType);
// value
if (smeb.Value != null)
{
var a = AppendAttributeNameAndRole(
ie.Attribute, "value", AmlConst.Attributes.Blob_Value, System.Text.Encoding.Default.GetString(smeb.Value));
a.AttributeDataType = "xs:string";
}
break;
case File smef:
if (aasStyleAttributes)
{
if (smef.ContentType != null)
AppendAttributeNameAndRole(
ie.Attribute, "mimeType", AmlConst.Attributes.File_MimeType, smef.ContentType);
if (smef.Value != null)
{
AppendAttributeNameAndRole(
ie.Attribute, "value", AmlConst.Attributes.File_Value, smef.Value);
}
}
if (amlStyleAttributes)
{
var extIf = ie.ExternalInterface.Append("FileDataReference");
if (extIf != null)
{
extIf.RefBaseClassPath = AmlConst.Interfaces.FileDataReference;
if (smef.ContentType != null)
AppendAttributeNameAndRole(
extIf.Attribute, "MIMEType", role: null, val: smef.ContentType,
attributeDataType: "xs:string");
if (smef.Value != null)
{
AppendAttributeNameAndRole(
extIf.Attribute, "refURI", role: null, val: smef.Value,
attributeDataType: "xs:anyURI");
}
}
}
break;
case ReferenceElement smer:
// value == a Reference
ExportReferenceWithSme(env, internalLinksToCreate, ie, smer,
smer.Value, "value", AmlConst.Attributes.ReferenceElement_Value, "value",
aasStyleAttributes, amlStyleAttributes);
break;
case RelationshipElement smer:
// first & second
ExportReferenceWithSme(env, internalLinksToCreate, ie, smer,
smer.First, "first", AmlConst.Attributes.RelationshipElement_First, "first",
aasStyleAttributes, amlStyleAttributes);
ExportReferenceWithSme(env, internalLinksToCreate, ie, smer,
smer.Second, "second", AmlConst.Attributes.RelationshipElement_Second, "second",
aasStyleAttributes, amlStyleAttributes);
// Recurse
if (sme is AnnotatedRelationshipElement anno)
{
var annotations = new List<ISubmodelElement>(anno.Annotations);
ExportListOfSme(matcher, internalLinksToCreate, ie, env, annotations);
}
break;
case SubmodelElementCollection smec:
// dead-csharp off
// recurse
//ordered and allowDuplicates removed from SMEColl in V3
//AppendAttributeNameAndRole(
// ie.Attribute, "ordered", AmlConst.Attributes.SMEC_ordered,
// smec.Ordered ? "true" : "false", attributeDataType: "xs:boolean");
//AppendAttributeNameAndRole(
// ie.Attribute, "allowDuplicates", AmlConst.Attributes.SMEC_allowDuplicates,
// smec.allowDuplicates ? "true" : "false", attributeDataType: "xs:boolean");
// dead-csharp on
ExportListOfSme(matcher, internalLinksToCreate, ie, env, smec.Value);
break;
case SubmodelElementList smel:
// recurse
AppendAttributeNameAndRole(
ie.Attribute, "ordered", AmlConst.Attributes.SMEC_ordered,
(bool)smel.OrderRelevant ? "true" : "false", attributeDataType: "xs:boolean");
ExportListOfSme(matcher, internalLinksToCreate, ie, env, smel.Value);
break;
case Operation op:
// over in, out
for (int i = 0; i < 2; i++)
{
// internal element
var oie = AppendIeNameAndRole(
ie.InternalElement,
(new[] { "InputVariables", "OutputVariables" })[i],
"",
role: (
new[] {
AmlConst.Roles.OperationVariableIn,
AmlConst.Roles.OperationVariableOut
})[i]);
// just a list of SMEs
var lop = new List<ISubmodelElement>();
foreach (var opVar in op.InputVariables)
{
lop.Add(opVar.Value);
}
foreach (var opVar in op.OutputVariables)
{
lop.Add(opVar.Value);
}
foreach (var opVar in op.InoutputVariables)
{
lop.Add(opVar.Value);
}
ExportListOfSme(matcher, internalLinksToCreate, oie, env, lop);
}
break;
case Entity ent:
// entityType
AppendAttributeNameAndRole(
ie.Attribute, "entityType", AmlConst.Attributes.Entity_entityType,
Stringification.ToString(ent.EntityType), attributeDataType: "xs:string");
// assetRef
//TODO (jtikekar, 0000-00-00): SpecficAssetId
if (!string.IsNullOrEmpty(ent.GlobalAssetId))
{
// dead-csharp off
//TODO (jtikekar, 0000-00-00): uncomment and support
//ExportReferenceWithSme(env, internalLinksToCreate, ie, ent,
// ent.GlobalAssetId, "asset", AmlConst.Attributes.Entity_asset, "asset",
// aasStyleAttributes, amlStyleAttributes);
// dead-csharp on
}
// Recurse
ExportListOfSme(matcher, internalLinksToCreate, ie, env, ent.Statements);
break;
case AasCore.Aas3_0.Range rng:
// min
if (rng.Min != null)
{
var a = AppendAttributeNameAndRole(
ie.Attribute, "min", AmlConst.Attributes.Range_Min, rng.Min);
a.AttributeDataType = "xs:" + Stringification.ToString(rng.ValueType).Trim();
}
// max
if (rng.Max != null)
{
var a = AppendAttributeNameAndRole(
ie.Attribute, "max", AmlConst.Attributes.Range_Max, rng.Max);
a.AttributeDataType = "xs:" + Stringification.ToString(rng.ValueType).Trim();
}
break;
}
// Qualifiers
SetQualifiers(ie.InternalElement, ie.Attribute, sme.Qualifiers, parentAsInternalElements: false);
}
}
}
private static void ExportSubmodelIntoElement(
AasAmlMatcher matcher, List<AmlInternalLinkEntity> internalLinksToCreate,
SystemUnitClassType parent,
IEnvironment env,
ISubmodel sm,
bool tryUseCompactProperties = false,
bool exportShallow = false)
{
if (parent == null || env == null || sm == null)
return;
// set some data
SetIdentification(parent.Attribute, sm.Id);
SetAdministration(parent.Attribute, sm.Administration);
SetReferable(parent.Attribute, sm);
SetModelingKind(parent.Attribute, sm.Kind);
SetSemanticId(parent.Attribute, sm.SemanticId);
SetHasDataSpecification(parent.Attribute, sm.EmbeddedDataSpecifications);
SetQualifiers(null, parent.Attribute, sm.Qualifiers, parentAsInternalElements: false);
// properties
if (!exportShallow)
ExportListOfSme(
matcher, internalLinksToCreate, parent, env, sm.SubmodelElements, tryUseCompactProperties);
else
{
// add a small information
AppendIeNameAndRole(parent.InternalElement, name: "Remark: duplicate AAS:Submodel");
}
}
private static InternalElementType ExportSubmodel(
AasAmlMatcher matcher,
List<AmlInternalLinkEntity> internalLinksToCreate, InternalElementSequence ieseq,
Aas.IEnvironment env,
ISubmodel sm,
bool tryUseCompactProperties = false,
bool exportShallow = false)
{
if (ieseq == null || env == null || sm == null)
return null;
// directly add internal element
var ie = AppendIeNameAndRole(ieseq, name: sm.IdShort, altName: "Submodel", role: AmlConst.Roles.Submodel);
ExportSubmodelIntoElement(
matcher, internalLinksToCreate, ie, env, sm, tryUseCompactProperties, exportShallow);
// return IE
return ie;
}
private static void ExportAsset(
InternalElementSequence ieseq, Aas.IEnvironment env, Aas.IAssetInformation asset)
{
if (ieseq == null || env == null || asset == null)
return;
// directly add internal element
var ie = AppendIeNameAndRole(ieseq, name: asset.GlobalAssetId, altName: "AssetInformation", role: AmlConst.Roles.AssetInformation);
// set some data
//TODO (jtikekar, 0000-00-00): what about specific asset Ids
if (!string.IsNullOrEmpty(asset.GlobalAssetId))
{
SetIdentification(ie.Attribute, asset.GlobalAssetId);
}
SetAssetKind(ie.Attribute, asset.AssetKind, attributeRole: AmlConst.Attributes.Asset_Kind);
// dead-csharp off
// do some data directly
//No More assetIdentificationModelRef and BOM a part of Asset
//if (asset.assetIdentificationModelRef != null)
// AppendAttributeNameAndRole(
// ie.Attribute, "assetIdentificationModelRef", AmlConst.Attributes.Asset_IdentificationModelRef,
// ToAmlReference(asset.assetIdentificationModelRef));
//if (asset.billOfMaterialRef != null)
// AppendAttributeNameAndRole(ie.Attribute, "billOfMaterialRef",
// AmlConst.Attributes.Asset_BillOfMaterialRef, ToAmlReference(asset.billOfMaterialRef));
}
//private static void ExportView(
// AasAmlMatcher matcher, InternalElementSequence ieseq, AasCore.Aas3_0_RC02.Environment env, View view)
//{
// if (ieseq == null || env == null || view == null)
// return;
// // directly add internal element
// var ie = AppendIeNameAndRole(ieseq, name: view.IdShort, altName: "View", role: AmlConst.Roles.View);
// // set some data
// SetReferable(ie.Attribute, view);
// SetSemanticId(ie.Attribute, view.SemanticId);
// SetHasDataSpecification(ie.Attribute, view.hasDataSpecification);
// // view references
// // from the Meeting: Views sind Listen von "Mirror-Elementen",
// // die auf die Properties.. (IEs) der AAS verweisen.
// // Views hängen unter der jeweiligen AAS (IReferable)
// for (int i = 0; i < view.Count; i++)
// {
// // access contained element
// var ce = view[i];
// if (ce == null)
// continue;
// // find the referenced element
// var targetReferable = env.FindReferableByReference(ce);
// if (targetReferable == null)
// continue;
// // rely on the "hope", that there is a match
// var targetAml = matcher.GetAmlObject(targetReferable);
// if (targetAml == null)
// continue;
// // for the time being, make an IE
// // Note: it is "forbidden" to set Roles for mirror elements
// var iece = AppendIeNameAndRole(
// ie.InternalElement, name: ce.ListOfValues("/"), altName: "Reference",
// role: null);
// // just convert it to an mirror element
// iece.RefBaseSystemUnitPath = "" + targetAml.ID;
// }
//}
// dead-csharp on
private static void ExportAAS(
AasAmlMatcher matcher, InstanceHierarchyType insthier, SystemUnitClassLibType suchier,
IEnvironment env, IAssetAdministrationShell aas,
bool tryUseCompactProperties = false)
{
// access
if (insthier == null || suchier == null || env == null || aas == null || matcher == null)
return;
// for attaching internal links, we need an IE. Therefore, we do the linking for each AAS separately
// and ingnore intra-AAS linking
var internalLinksToCreate = new List<AmlInternalLinkEntity>();
//
// always create an AAS under InstanceHierarchy as being the conveyor of AAS information
//
// directly add internal element
var aasIE = AppendIeNameAndRole(
insthier.InternalElement, name: aas.IdShort, altName: "AAS", role: AmlConst.Roles.AAS);
// set some data
SetIdentification(aasIE.Attribute, aas.Id);
SetAdministration(aasIE.Attribute, aas.Administration);
SetReferable(aasIE.Attribute, aas);
SetHasDataSpecification(aasIE.Attribute, aas.EmbeddedDataSpecifications);
if (aas.DerivedFrom != null)
AppendAttributeNameAndRole(
aasIE.Attribute, "derivedFrom", AmlConst.Attributes.AAS_DerivedFrom,
ToAmlReference(aas.DerivedFrom));
// asset
var asset = aas.AssetInformation;
ExportAsset(aasIE.InternalElement, env, asset);
// the AAS for Submodels of kind = Type willbe created on demand
SystemUnitFamilyType aasSUC = null;
//
// Submodels can be of kind Type/ Instance
//
foreach (var smref in aas.AllSubmodels())
{
// ref -> Submodel
var sm = env.FindSubmodel(smref);
if (sm == null)
continue;
// SM types go to system unit classes, instances goe to instance hierarchy
if (sm.Kind != null && sm.Kind == ModellingKind.Template)
{
// create AAS for SUCs on demand
if (aasSUC == null)
{
// create parent
aasSUC = suchier.SystemUnitClass.Append(aas.IdShort ?? "AAS_" + Guid.NewGuid().ToString());
// role
var rr = aasSUC.SupportedRoleClass.Append();
rr.RefRoleClassPath = AmlConst.Roles.AAS;
}
// create a dedicated SUC for this
var smSUC = aasSUC.SystemUnitClass.Append(sm.IdShort ?? "Submodel_" + Guid.NewGuid().ToString());
// role
var rq = smSUC.SupportedRoleClass.Append();
rq.RefRoleClassPath = AmlConst.Roles.Submodel;
// set same data data, in order to correlate, but not asset
SetIdentification(aasSUC.Attribute, aas.Id);
SetAdministration(aasSUC.Attribute, aas.Administration);
SetReferable(aasSUC.Attribute, aas);
SetHasDataSpecification(aasSUC.Attribute, aas.EmbeddedDataSpecifications);
// use normal function to export Submodel data into the SUC element
ExportSubmodelIntoElement(matcher, internalLinksToCreate, smSUC, env, sm, tryUseCompactProperties);
// there could be the chance, that the semanticId of a Submodel of kind = Instance links
// to a local Submodel of kind = Type
// therefore: store in either way
matcher.AddMatch(sm, smSUC);
}
else
{
// check, if the Submodel is already exported
var smAlreadyExported = matcher.ContainsAasObject(sm);
if (!smAlreadyExported)
{
// export the submodel
var smIE = ExportSubmodel(
matcher, internalLinksToCreate, aasIE.InternalElement, env, sm,
tryUseCompactProperties: tryUseCompactProperties, exportShallow: false);
// there could be the chance, that the semanticId of a Submodel of kind = Instance links
// to a local Submodel of kind = Type
// therefore: store in either way
matcher.AddMatch(sm, smIE);
}
else
{
// try to utilze "Mirror Elements" for duplicate Submodel
// for the time being, make an IE
// Note: it is "forbidden" to set Roles for mirror elements
var ieSubDup = AppendIeNameAndRole(
aasIE.InternalElement, name: sm.IdShort, altName: "Submodel", role: null);
// get AML entity of existing Submodel
var targetAml = matcher.GetAmlObject(sm);
if (targetAml != null)
{
// just convert it to an mirror element
ieSubDup.RefBaseSystemUnitPath = "" + targetAml.ID;
}
}
}
}
//
// Internal Links
//
SetInternalLinks(aasIE, matcher, internalLinksToCreate);
}
/// <summary>
/// Sets attributes to outer and inner element (optional) of an ConceptDescription.
/// If aseqInnner == null, "embeddedDataSpec"-approach will be used
/// </summary>
private static void SetAttributesForConceptDescription(
AttributeSequence aseqOuter, AttributeSequence aseqInner, IConceptDescription cd,
ref string name)
{
// check
if (aseqOuter == null || cd == null)
return;
// start to figure out an speaking name for the AML entity (idSHort, shortName .. will be codified
// as original attributes, as well)
// the name will be set at the end
name = "CD_" + Guid.NewGuid().ToString();
if (cd.IdShort.HasContent())
name = cd.IdShort;
// set data for identifiable
SetIdentification(aseqOuter, cd.Id);
SetAdministration(aseqOuter, cd.Administration);
SetReferable(aseqOuter, cd);
// isCaseOf
if (cd.IsCaseOf != null)
foreach (var r in cd.IsCaseOf)
AppendAttributeNameAndRole(aseqOuter, "isCaseOf", AmlConst.Attributes.CD_IsCaseOf, ToAmlReference(r));
// which data spec as reference
if (cd.EmbeddedDataSpecifications != null)
foreach (var eds in cd.EmbeddedDataSpecifications)
if (eds.DataSpecification != null)
AppendAttributeNameAndRole(
aseqOuter, "dataSpecification", AmlConst.Attributes.CD_DataSpecificationRef,
ToAmlReference(eds.DataSpecification));
//jtikekar:Added as üet DotAAS-1
// TODO (MIHO, 2022-12-21): do not understand this duplication?!
if (cd.EmbeddedDataSpecifications != null)
foreach (var ds in cd.EmbeddedDataSpecifications)
if (ds != null)
AppendAttributeNameAndRole(
aseqOuter, "dataSpecification", AmlConst.Attributes.CD_DataSpecificationRef,
ToAmlReference(ds.DataSpecification));
// which data spec to take as source?
var source61360 = cd.EmbeddedDataSpecifications?.GetIEC61360Content();
// TODO (Michael Hoffmeister, 2020-08-01): If further data specifications exist (in future), add here
// decide which approach to take (1 or 2 IE)
AttributeSequence dest61360 = null;
if (aseqInner == null)
{
// we will pack the attribute under an embedded data spec attribute branch
// now, to the embedded data spec
if (cd.EmbeddedDataSpecifications != null)
{
var eds = AppendAttributeNameAndRole(
aseqOuter, "dataSpecification", AmlConst.Attributes.CD_EmbeddedDataSpecification);
if (source61360 != null)
{
var dsc = AppendAttributeNameAndRole(
eds.Attribute, "dataSpecificationContent",
AmlConst.Attributes.CD_DataSpecificationContent);
// create attribute branch for CD contents
var dsc61360 = AppendAttributeNameAndRole(
dsc.Attribute, "dataSpecificationIEC61360",
AmlConst.Attributes.CD_DataSpecification61360);
dest61360 = dsc61360.Attribute;
}
}
}
else
{
// we will use an 2nd IE to have the attributes of the data spcec content
dest61360 = aseqInner;
}
// set attributes of 61360
if (source61360 != null && dest61360 != null)
{
// better name?
if (source61360.ShortName != null && source61360.ShortName.Count > 0)
name = source61360.ShortName.GetDefaultString();
// specific data
SetLangStr(
dest61360, source61360.PreferredName, "preferredName",
AmlConst.Attributes.CD_DSC61360_PreferredName);
SetLangStr(
dest61360, source61360.ShortName, "shortName",
AmlConst.Attributes.CD_DSC61360_ShortName);
if (source61360.Unit != null)