Skip to content

Commit 91095d4

Browse files
* continue avoid empty list
* polish Operation
1 parent 07c7fa1 commit 91095d4

23 files changed

+521
-121
lines changed

src/AasxCsharpLibrary/AdminShellUtil.cs

+19-7
Original file line numberDiff line numberDiff line change
@@ -336,8 +336,19 @@ public static IEnumerable<AasSubmodelElements> GetAdequateEnums(AasSubmodelEleme
336336
return null;
337337
}
338338

339-
public static ISubmodelElement CreateSubmodelElementFromEnum(AasSubmodelElements smeEnum, ISubmodelElement sourceSme = null)
339+
public class CreateSubmodelElementDefaultHelper
340340
{
341+
public Func<IReference> CreateDefaultReference = null;
342+
}
343+
344+
public static ISubmodelElement CreateSubmodelElementFromEnum(
345+
AasSubmodelElements smeEnum, ISubmodelElement sourceSme = null,
346+
CreateSubmodelElementDefaultHelper defaultHelper = null)
347+
{
348+
Func<IReference> crDefRef = () => { return (defaultHelper?.CreateDefaultReference?.Invoke()) ??
349+
new Reference(ReferenceTypes.ExternalReference, new List<IKey>(
350+
new[] { new Key(KeyTypes.GlobalReference, "") })); };
351+
341352
switch (smeEnum)
342353
{
343354
case AasSubmodelElements.Property:
@@ -364,21 +375,21 @@ public static ISubmodelElement CreateSubmodelElementFromEnum(AasSubmodelElements
364375
{
365376
// TODO (??, 0000-00-00): AAS core crashes without this
366377
return new ReferenceElement(
367-
value: new Reference(ReferenceTypes.ExternalReference, new List<IKey>())
378+
value: crDefRef()
368379
).UpdateFrom(sourceSme);
369380
}
370381
case AasSubmodelElements.RelationshipElement:
371382
{
372383
return new RelationshipElement(
373-
new Reference(ReferenceTypes.ExternalReference, new List<IKey>()),
374-
new Reference(ReferenceTypes.ExternalReference, new List<IKey>()))
384+
crDefRef(),
385+
crDefRef())
375386
.UpdateFrom(sourceSme);
376387
}
377388
case AasSubmodelElements.AnnotatedRelationshipElement:
378389
{
379390
return new AnnotatedRelationshipElement(
380-
new Reference(ReferenceTypes.ExternalReference, new List<IKey>()),
381-
new Reference(ReferenceTypes.ExternalReference, new List<IKey>()))
391+
crDefRef(),
392+
crDefRef())
382393
.UpdateFrom(sourceSme);
383394
}
384395
case AasSubmodelElements.Capability:
@@ -399,7 +410,8 @@ public static ISubmodelElement CreateSubmodelElementFromEnum(AasSubmodelElements
399410
}
400411
case AasSubmodelElements.BasicEventElement:
401412
{
402-
return new BasicEventElement(null, Direction.Input, StateOfEvent.Off).UpdateFrom(sourceSme);
413+
return new BasicEventElement(crDefRef(),
414+
Direction.Input, StateOfEvent.Off).UpdateFrom(sourceSme);
403415
}
404416
case AasSubmodelElements.Entity:
405417
{

src/AasxCsharpLibrary/Extensions/ExtendAnnotatedRelationshipElement.cs

+9
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ public static void Remove(this AnnotatedRelationshipElement annotatedRelationshi
4141
}
4242
}
4343

44+
public static int Replace(
45+
this AnnotatedRelationshipElement annotatedRelationshipElement,
46+
IDataElement oldElem, IDataElement newElem)
47+
{
48+
if (annotatedRelationshipElement?.Annotations == null)
49+
return -1;
50+
return annotatedRelationshipElement.Annotations.Replace(oldElem, newElem);
51+
}
52+
4453
public static object AddChild(
4554
this AnnotatedRelationshipElement annotatedRelationshipElement,
4655
ISubmodelElement childSubmodelElement,

src/AasxCsharpLibrary/Extensions/ExtendEmbeddedDataSpecification.cs

+79
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,24 @@ This source code is licensed under the Apache License 2.0 (see LICENSE.txt).
66
77
This source code may use other Open Source software components (see LICENSE.txt).
88
*/
9+
using System;
910
using System.Collections.Generic;
1011

1112
namespace Extensions
1213
{
1314
// TODO (Jui, 2022-12-21): I do not know, if to put the List<> extension here or in a separate file
1415
public static class ExtendListOfEmbeddedDataSpecification
1516
{
17+
public static bool IsOneBlank(this List<IEmbeddedDataSpecification> list)
18+
{
19+
return
20+
list != null
21+
&& list.Count == 1
22+
&& list[0]?.DataSpecification?.IsOneBlank() == true
23+
&& list[0].DataSpecificationContent is DataSpecificationBlank;
24+
}
25+
26+
1627
public static IEmbeddedDataSpecification FindFirstIEC61360Spec(this List<IEmbeddedDataSpecification> list)
1728
{
1829
foreach (var eds in list)
@@ -119,5 +130,73 @@ public static bool FixReferenceWrtContent(this IEmbeddedDataSpecification eds)
119130
new List<IKey> { ExtendIDataSpecificationContent.GetKeyFor(ctc) });
120131
return true;
121132
}
133+
134+
}
135+
136+
/// <summary>
137+
/// This class is intended to provide a "blank" DataSpecificationContent
138+
/// in order to satisfy the cardinality [1] of EmbeddedDataSpecification.
139+
/// It has no specific semantics or sense. It is purely existing.
140+
/// </summary>
141+
public class DataSpecificationBlank : IDataSpecificationContent
142+
{
143+
/// <summary>
144+
/// Iterate over all the class instances referenced from this instance
145+
/// without further recursion.
146+
/// </summary>
147+
public IEnumerable<IClass> DescendOnce()
148+
{
149+
yield break;
150+
}
151+
152+
/// <summary>
153+
/// Iterate recursively over all the class instances referenced from this instance.
154+
/// </summary>
155+
public IEnumerable<IClass> Descend()
156+
{
157+
yield break;
158+
}
159+
160+
/// <summary>
161+
/// Accept the <paramref name="visitor" /> to visit this instance
162+
/// for double dispatch.
163+
/// </summary>
164+
public void Accept(Visitation.IVisitor visitor)
165+
{
166+
}
167+
168+
/// <summary>
169+
/// Accept the visitor to visit this instance for double dispatch
170+
/// with the <paramref name="context" />.
171+
/// </summary>
172+
public void Accept<TContext>(
173+
Visitation.IVisitorWithContext<TContext> visitor,
174+
TContext context)
175+
{
176+
}
177+
178+
/// <summary>
179+
/// Accept the <paramref name="transformer" /> to transform this instance
180+
/// for double dispatch.
181+
/// </summary>
182+
public T Transform<T>(Visitation.ITransformer<T> transformer)
183+
{
184+
return default(T);
185+
}
186+
187+
/// <summary>
188+
/// Accept the <paramref name="transformer" /> to visit this instance
189+
/// for double dispatch with the <paramref name="context" />.
190+
/// </summary>
191+
public T Transform<TContext, T>(
192+
Visitation.ITransformerWithContext<TContext, T> transformer,
193+
TContext context)
194+
{
195+
return default(T);
196+
}
197+
198+
public DataSpecificationBlank()
199+
{
200+
}
122201
}
123202
}

src/AasxCsharpLibrary/Extensions/ExtendEntity.cs

+9
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ public static void Remove(this Entity entity, ISubmodelElement submodelElement)
4141
}
4242
}
4343

44+
public static int Replace(
45+
this Entity entity,
46+
ISubmodelElement oldElem, ISubmodelElement newElem)
47+
{
48+
if (entity?.Statements == null)
49+
return -1;
50+
return entity.Statements.Replace(oldElem, newElem);
51+
}
52+
4453
public static object AddChild(this Entity entity, ISubmodelElement childSubmodelElement, EnumerationPlacmentBase placement = null)
4554
{
4655
if (childSubmodelElement == null)

src/AasxCsharpLibrary/Extensions/ExtendIReferable.cs

+42-1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,38 @@ public static void RecurseOnReferables(this IReferable referable,
4848
lambda(state, null, referable);
4949
}
5050

51+
public static int Replace(
52+
this IReferable referable, ISubmodelElement oldElem, ISubmodelElement newElem)
53+
{
54+
if (referable is Submodel submodel)
55+
{
56+
return submodel.Replace(oldElem, newElem);
57+
}
58+
else if (referable is AnnotatedRelationshipElement annotatedRelationshipElement
59+
&& oldElem is IDataElement oldDE && newElem is IDataElement newDE)
60+
{
61+
return annotatedRelationshipElement.Replace(oldDE, newDE);
62+
}
63+
else if (referable is SubmodelElementCollection submodelElementCollection)
64+
{
65+
return submodelElementCollection.Replace(oldElem, newElem);
66+
}
67+
else if (referable is SubmodelElementList submodelElementList)
68+
{
69+
return submodelElementList.Replace(oldElem, newElem);
70+
}
71+
else if (referable is Entity entity)
72+
{
73+
return entity.Replace(oldElem, newElem);
74+
}
75+
else if (referable is Operation operation)
76+
{
77+
return operation.Replace(oldElem, newElem);
78+
}
79+
80+
return -1;
81+
}
82+
5183
public static void Remove(this IReferable referable, ISubmodelElement submodelElement)
5284
{
5385
if (referable is Submodel submodel)
@@ -70,9 +102,14 @@ public static void Remove(this IReferable referable, ISubmodelElement submodelEl
70102
{
71103
entity.Remove(submodelElement);
72104
}
105+
else if (referable is Operation operation)
106+
{
107+
operation.Remove(submodelElement);
108+
}
73109
}
74110

75-
public static void Add(this IReferable referable, ISubmodelElement submodelElement)
111+
public static void Add(this IReferable referable, ISubmodelElement submodelElement,
112+
OperationVariableDirection direction = OperationVariableDirection.In)
76113
{
77114
if (referable is Submodel submodel)
78115
{
@@ -94,6 +131,10 @@ public static void Add(this IReferable referable, ISubmodelElement submodelEleme
94131
{
95132
entity.Add(submodelElement);
96133
}
134+
else if (referable is Operation operation)
135+
{
136+
operation.Add(submodelElement, direction);
137+
}
97138
}
98139

99140
#region Display

src/AasxCsharpLibrary/Extensions/ExtendISubmodelElement.cs

+22
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,28 @@ private static void BasicConversionFromV20(this ISubmodelElement submodelElement
605605

606606
#region List<ISubmodelElement>
607607

608+
public static int Replace(
609+
this List<ISubmodelElement> submodelElements,
610+
ISubmodelElement oldElem, ISubmodelElement newElem)
611+
{
612+
var ndx = submodelElements.IndexOf(oldElem);
613+
if (ndx < 0)
614+
return -1;
615+
submodelElements[ndx] = newElem;
616+
return ndx;
617+
}
618+
619+
public static int Replace(
620+
this List<IDataElement> submodelElements,
621+
IDataElement oldElem, IDataElement newElem)
622+
{
623+
var ndx = submodelElements.IndexOf(oldElem);
624+
if (ndx < 0)
625+
return -1;
626+
submodelElements[ndx] = newElem;
627+
return ndx;
628+
}
629+
608630
public static IReferable FindReferableByReference(
609631
this List<ISubmodelElement> submodelElements, Reference rf, int keyIndex)
610632
{

src/AasxCsharpLibrary/Extensions/ExtendKeyList.cs

+6
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ public static bool IsEmpty(this List<IKey> keys)
2121
return keys.Count < 1;
2222
}
2323

24+
public static bool IsOneBlank(this List<IKey> keys)
25+
{
26+
return keys.Count == 1
27+
&& keys[0].Value?.HasContent() != true;
28+
}
29+
2430
public static bool Matches(this List<IKey> keys, List<IKey> other, MatchMode matchMode = MatchMode.Strict)
2531
{
2632
if (other == null || other.Count != keys.Count)

src/AasxCsharpLibrary/Extensions/ExtendOperation.cs

+48
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ This source code may use other Open Source software components (see LICENSE.txt)
99
using AdminShellNS;
1010
using System.Collections.Generic;
1111
using System.Linq;
12+
using System.Security.Cryptography;
1213

1314
namespace Extensions
1415
{
@@ -120,6 +121,53 @@ public static List<IOperationVariable> SetVars(
120121
return op.InoutputVariables;
121122
}
122123

124+
public static void Add(this Operation operation, ISubmodelElement submodelElement,
125+
OperationVariableDirection direction = OperationVariableDirection.In)
126+
{
127+
var ovl = GetVars(operation, direction);
128+
if (ovl == null)
129+
{
130+
ovl = new List<IOperationVariable>();
131+
SetVars(operation, direction, ovl);
132+
}
133+
ovl.Add(new OperationVariable(submodelElement));
134+
}
135+
136+
public static void Remove(this Operation operation, ISubmodelElement submodelElement)
137+
{
138+
foreach (var ovd in AdminShellUtil.GetEnumValues<OperationVariableDirection>())
139+
{
140+
var ovl = GetVars(operation, ovd);
141+
if (ovl == null)
142+
continue;
143+
foreach (var ov in ovl)
144+
if (submodelElement != null
145+
&& ov.Value == submodelElement)
146+
{
147+
ovl.Remove(ov);
148+
break;
149+
}
150+
}
151+
}
152+
153+
public static int Replace(
154+
this Operation operation,
155+
ISubmodelElement oldElem, ISubmodelElement newElem)
156+
{
157+
foreach (var ovd in AdminShellUtil.GetEnumValues<OperationVariableDirection>())
158+
{
159+
var ovl = GetVars(operation, ovd);
160+
foreach (var ov in ovl)
161+
if (oldElem != null
162+
&& ov.Value == oldElem)
163+
{
164+
ov.Value = newElem;
165+
return 1;
166+
}
167+
}
168+
return -1;
169+
}
170+
123171
#endregion
124172

125173
public static IOperation UpdateFrom(

src/AasxCsharpLibrary/Extensions/ExtendReference.cs

+7
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ public static bool IsValid(this IReference reference)
3131
return true;
3232
}
3333

34+
public static bool IsOneBlank(this IReference reference)
35+
{
36+
return reference != null
37+
&& reference.Type == ReferenceTypes.ExternalReference
38+
&& reference.Keys?.IsOneBlank() == true;
39+
}
40+
3441
public static bool IsValid(this List<IReference> references)
3542
{
3643
bool isValid = false;

src/AasxCsharpLibrary/Extensions/ExtendSubmodel.cs

+9
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ public static void Remove(this Submodel submodel, ISubmodelElement submodelEleme
5252
}
5353
}
5454

55+
public static int Replace(
56+
this Submodel submodel,
57+
ISubmodelElement oldElem, ISubmodelElement newElem)
58+
{
59+
if (submodel?.SubmodelElements == null)
60+
return -1;
61+
return submodel.SubmodelElements.Replace(oldElem, newElem);
62+
}
63+
5564
public static object AddChild(
5665
this ISubmodel submodel, ISubmodelElement childSubmodelElement,
5766
EnumerationPlacmentBase placement = null)

0 commit comments

Comments
 (0)