Skip to content

Commit 7931118

Browse files
committed
* refacture Env.Submodels, AAS, CDs,
AAS.Submodels to have accessor-methods in super-ordinate entities to provide empty-list / null - behaviour.
1 parent 8809886 commit 7931118

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+629
-593
lines changed

src/AasxAmlImExport/AmlExport.cs

+5-4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ This source code may use other Open Source software components (see LICENSE.txt)
1515
using System.Collections.Generic;
1616
using System.Reflection;
1717
using System.Text.RegularExpressions;
18+
using Aas = AasCore.Aas3_0;
1819

1920
namespace AasxAmlImExport
2021
{
@@ -816,7 +817,7 @@ private static void ExportAAS(
816817
//
817818
// Submodels can be of kind Type/ Instance
818819
//
819-
foreach (var smref in aas.Submodels)
820+
foreach (var smref in aas.AllSubmodels())
820821
{
821822
// ref -> Submodel
822823
var sm = env.FindSubmodel(smref);
@@ -1026,14 +1027,14 @@ private static void SetAttributesForConceptDescription(
10261027

10271028

10281029
private static void ExportConceptDescriptionsWithExtraContentToIHT(
1029-
InstanceHierarchyType lib, AasCore.Aas3_0.Environment env)
1030+
InstanceHierarchyType lib, Aas.Environment env)
10301031
{
10311032
// acceess
10321033
if (lib == null || env == null)
10331034
return;
10341035

10351036
// over CDs
1036-
foreach (var cd in env.ConceptDescriptions)
1037+
foreach (var cd in env.AllConceptDescriptions())
10371038
{
10381039
// make IE for CD itself (outer IE)
10391040
string name = "TODO-CD";
@@ -1164,7 +1165,7 @@ public static bool ExportTo(AdminShellPackageEnv package, string amlfn, bool try
11641165
var matcher = new AasAmlMatcher();
11651166

11661167
// over all AAS
1167-
foreach (var aas in package.AasEnv.AssetAdministrationShells)
1168+
foreach (var aas in package.AasEnv.AllAssetAdministrationShells())
11681169
{
11691170
ExportAAS(matcher, insthier, suchier, package.AasEnv, aas, tryUseCompactProperties);
11701171
}

src/AasxAmlImExport/AmlImport.cs

+9-21
Original file line numberDiff line numberDiff line change
@@ -1096,7 +1096,7 @@ public void ParseInternalElementsForAasEntities(
10961096
Debug(indentation, " AAS with required attributes recognised. Starting new AAS..");
10971097

10981098
// make temporary object official
1099-
this.package.AasEnv.AssetAdministrationShells.Add(aas);
1099+
this.package.AasEnv.Add(aas);
11001100
currentAas = aas;
11011101
matcher.AddMatch(aas, ie);
11021102
}
@@ -1173,12 +1173,8 @@ public void ParseInternalElementsForAasEntities(
11731173
// Remark: add only, if not a SM with the same ID is existing. This could have the
11741174
// consequences that additional properties in the 2nd SM with the same SM get lost!
11751175
if (null == this.package.AasEnv.FindSubmodelById(sm.Id))
1176-
this.package.AasEnv.Submodels.Add(sm);
1177-
if (currentAas.Submodels == null)
1178-
{
1179-
currentAas.Submodels = new List<IReference>();
1180-
}
1181-
currentAas.Submodels.Add(sm.GetReference());
1176+
this.package.AasEnv.Add(sm);
1177+
currentAas.Add(sm.GetReference());
11821178
currentSmeCollection = sm;
11831179
}
11841180
else
@@ -1195,13 +1191,9 @@ public void ParseInternalElementsForAasEntities(
11951191
{
11961192
// try use Identification to find existing Submodel
11971193
var existSm = package.AasEnv.FindSubmodelById(targetSm.Id);
1198-
if (currentAas.Submodels == null)
1199-
{
1200-
currentAas.Submodels = new List<IReference>();
1201-
}
12021194

12031195
// if so, add a SubmodelRef
1204-
currentAas.Submodels.Add(existSm.GetReference());
1196+
currentAas.Add(existSm.GetReference());
12051197
}
12061198
}
12071199

@@ -1448,7 +1440,7 @@ public void ParseSystemUnits(
14481440
aas = existAas;
14491441
else
14501442
// add
1451-
this.package.AasEnv.AssetAdministrationShells.Add(aas);
1443+
this.package.AasEnv.Add(aas);
14521444

14531445
// remember
14541446
currentAas = aas;
@@ -1481,12 +1473,8 @@ public void ParseSystemUnits(
14811473
currentSubmodel = sm;
14821474

14831475
// this will be the parent for child elements
1484-
this.package.AasEnv.Submodels.Add(sm);
1485-
if (currentAas.Submodels == null)
1486-
{
1487-
currentAas.Submodels = new List<IReference>();
1488-
}
1489-
currentAas.Submodels.Add(sm.GetReference());
1476+
this.package.AasEnv.Add(sm);
1477+
currentAas.Add(sm.GetReference());
14901478
currentSmeCollection = sm;
14911479
}
14921480
else
@@ -1530,7 +1518,7 @@ public void ParseIEsForConceptDescriptions(
15301518
{
15311519
// add
15321520
Debug(indentation, " .. added as {0}", cd.Id);
1533-
this.package.AasEnv.ConceptDescriptions.Add(cd);
1521+
this.package.AasEnv.Add(cd);
15341522

15351523
// look for direct descendants = Data Specifcations
15361524
if (ie.InternalElement != null)
@@ -1671,7 +1659,7 @@ public static void ImportInto(AdminShellPackageEnv package, string amlfn)
16711659
parser.ParseSystemUnits(x.SystemUnitClass);
16721660

16731661
// the following steps will require valid parent information
1674-
foreach (var sm in package.AasEnv.Submodels)
1662+
foreach (var sm in package.AasEnv.AllSubmodels())
16751663
sm.SetAllParents();
16761664

16771665
// do the late population of views

src/AasxBammRdfImExport/RDFimport.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public static void ImportInto(
7575

7676
var msemanticID = ExtendReference.CreateFromKey(Aas.KeyTypes.GlobalReference, t.Subject.ToString());
7777
sm.SemanticId = msemanticID;
78-
thePackageEnv.AasEnv.Submodels.Add(sm);
78+
thePackageEnv.AasEnv.Add(sm);
7979
}
8080
}
8181

@@ -373,7 +373,7 @@ public static void ImportInto(
373373

374374
{
375375
var cd = new Aas.ConceptDescription(idShort: property_Name, id: semantic);
376-
env.ConceptDescriptions.Add(cd);
376+
env.Add(cd);
377377
cd.SetIEC61360Spec(
378378
preferredNames: new[] { "EN", property_PreferredName },
379379
shortName: null,
@@ -445,7 +445,7 @@ public static void ImportInto(
445445

446446
{
447447
var cd = new Aas.ConceptDescription(idShort: "" + name, id: semantic);
448-
env.ConceptDescriptions.Add(cd);
448+
env.Add(cd);
449449
cd.SetIEC61360Spec(
450450
preferredNames: new[] { "EN", preferred_name },
451451
shortName: null,

src/AasxCsharpLibrary/AdminShellPackageEnv.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -912,8 +912,9 @@ public bool SaveAs(string fn, bool writeFreshly = false, SerializationFormat pre
912912
{
913913
// create, as not existing
914914
var frn = "aasenv-with-no-id";
915-
if (_aasEnv.AssetAdministrationShells.Count > 0)
916-
frn = _aasEnv.AssetAdministrationShells[0].GetFriendlyName() ?? frn;
915+
if (_aasEnv.AssetAdministrationShellCount() > 0)
916+
frn = _aasEnv.AllAssetAdministrationShells().FirstOrDefault()
917+
.GetFriendlyName() ?? frn;
917918
var aas_spec_fn = "/aasx/#/#.aas";
918919
if (prefFmt == SerializationFormat.Json)
919920
aas_spec_fn += ".json";

src/AasxCsharpLibrary/Extensions/ExtendAssetAdministrationShell.cs

+40-10
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,8 @@ public static IEnumerable<LocatedReference> FindAllReferences(this IAssetAdminis
3939
// yield return new LocatedReference(assetAdministrationShell, assetAdministrationShell.AssetInformation);
4040
// dead-csharp on
4141
// Submodel references
42-
if (assetAdministrationShell.Submodels != null)
43-
foreach (var r in assetAdministrationShell.Submodels)
44-
yield return new LocatedReference(assetAdministrationShell, r);
45-
42+
foreach (var r in assetAdministrationShell.AllSubmodels())
43+
yield return new LocatedReference(assetAdministrationShell, r);
4644
}
4745

4846
#endregion
@@ -52,7 +50,7 @@ public static IReference FindSubmodelReference(this IAssetAdministrationShell aa
5250
if (aas?.Submodels == null || smRef == null)
5351
return null;
5452

55-
foreach (var smr in aas.Submodels)
53+
foreach (var smr in aas.AllSubmodels())
5654
if (smr.Matches(smRef))
5755
return smr;
5856

@@ -64,6 +62,40 @@ public static bool HasSubmodelReference(this IAssetAdministrationShell aas, Refe
6462
return aas.FindSubmodelReference(smRef) != null;
6563
}
6664

65+
/// <summary>
66+
/// Enumerates any references to Submodels in the AAS. Will not return <c>null</c>.
67+
/// Is tolerant, if the list is <c>null</c>.
68+
/// </summary>
69+
public static IEnumerable<IReference> AllSubmodels(this IAssetAdministrationShell aas)
70+
{
71+
if (aas?.Submodels != null)
72+
foreach (var sm in aas.Submodels)
73+
if (sm != null)
74+
yield return sm;
75+
}
76+
77+
/// <summary>
78+
/// Returns the number of references to Submodels.
79+
/// Is tolerant, if the list is <c>null</c>.
80+
/// </summary>
81+
public static int SubmodelCount(this IAssetAdministrationShell aas)
82+
{
83+
if (aas?.Submodels != null)
84+
return aas.Submodels.Count;
85+
return 0;
86+
}
87+
88+
89+
/// <summary>
90+
/// Returns the <c>index</c>-th Submodel, if exists. Returns <c>null</c> in any other case.
91+
/// </summary>
92+
public static IReference SubmodelByIndex(this IAssetAdministrationShell aas, int index)
93+
{
94+
if (aas?.Submodels == null || index < 0 || index >= aas.Submodels.Count)
95+
return null;
96+
return aas.Submodels[index];
97+
}
98+
6799
/// <summary>
68100
/// Adds. Might create the list.
69101
/// </summary>
@@ -78,7 +110,7 @@ public static void Add(this IAssetAdministrationShell aas, IReference newSmRef)
78110
}
79111

80112
/// <summary>
81-
/// Removes. Might set the list to <c>null</c> !!
113+
/// Removes the reference, if contained in list. Might set the list to <c>null</c> !!
82114
/// Note: <c>smRef</c> must be the exact object, not only match it!
83115
/// </summary>
84116
public static void Remove(this IAssetAdministrationShell aas, IReference smRef)
@@ -166,8 +198,7 @@ public static AssetAdministrationShell ConvertFromV10(this AssetAdministrationSh
166198
Console.WriteLine($"KeyType value {refKey.type} not found.");
167199
}
168200
}
169-
assetAdministrationShell.Submodels ??= new List<IReference>();
170-
assetAdministrationShell.Submodels.Add(new Reference(ReferenceTypes.ModelReference, keyList));
201+
assetAdministrationShell.Add(new Reference(ReferenceTypes.ModelReference, keyList));
171202
}
172203
}
173204
}
@@ -254,8 +285,7 @@ public static AssetAdministrationShell ConvertFromV20(this AssetAdministrationSh
254285
Console.WriteLine($"KeyType value {refKey.type} not found.");
255286
}
256287
}
257-
assetAdministrationShell.Submodels ??= new List<IReference>();
258-
assetAdministrationShell.Submodels.Add(new Reference(ReferenceTypes.ModelReference, keyList));
288+
assetAdministrationShell.Add(new Reference(ReferenceTypes.ModelReference, keyList));
259289
}
260290
}
261291
}

src/AasxCsharpLibrary/Extensions/ExtendConceptDescription.cs

-23
Original file line numberDiff line numberDiff line change
@@ -111,29 +111,6 @@ public static IEnumerable<Reference> FindAllReferences(this IConceptDescription
111111
yield break;
112112
}
113113

114-
#endregion
115-
#region ListOfConceptDescription
116-
public static IConceptDescription AddConceptDescriptionOrReturnExisting(this List<IConceptDescription> conceptDescriptions, ConceptDescription newConceptDescription)
117-
{
118-
if (newConceptDescription == null)
119-
{
120-
return null;
121-
}
122-
if (conceptDescriptions != null)
123-
{
124-
var existingCd = conceptDescriptions.Where(c => c.Id == newConceptDescription.Id).FirstOrDefault();
125-
if (existingCd != null)
126-
{
127-
return existingCd;
128-
}
129-
else
130-
{
131-
conceptDescriptions.Add(newConceptDescription);
132-
}
133-
}
134-
135-
return newConceptDescription;
136-
}
137114
#endregion
138115

139116
public static void Validate(

0 commit comments

Comments
 (0)