-
Notifications
You must be signed in to change notification settings - Fork 49
/
ExtendIReferable.cs
144 lines (134 loc) · 4.84 KB
/
ExtendIReferable.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
using AasCore.Aas3_0_RC02;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Extenstions
{
public static class ExtendIReferable
{
public static void SetTimeStamp(this IReferable referable, DateTime timeStamp)
{
IReferable newReferable = referable;
newReferable.TimeStamp = timeStamp;
do
{
newReferable.TimeStampTree = timeStamp;
if (newReferable != newReferable.Parent)
{
newReferable = (IReferable)newReferable.Parent;
}
else
newReferable = null;
}
while (newReferable != null);
}
public static IEnumerable<ISubmodelElement> EnumerateChildren(this IReferable referable)
{
if (referable is Submodel submodel && submodel.SubmodelElements != null)
{
if (submodel.SubmodelElements != null)
{
foreach (var submodelElement in submodel.SubmodelElements)
{
yield return submodelElement;
}
}
}
else if (referable is SubmodelElementCollection submodelElementCollection)
{
if (submodelElementCollection.Value != null)
{
foreach (var submodelElement in submodelElementCollection.Value)
{
yield return submodelElement;
}
}
}
else if (referable is SubmodelElementList submodelElementList)
{
if (submodelElementList.Value != null)
{
foreach (var submodelElement in submodelElementList.Value)
{
yield return submodelElement;
}
}
}
else if (referable is AnnotatedRelationshipElement annotatedRelationshipElement)
{
if (annotatedRelationshipElement.Annotations != null)
{
foreach (var submodelElement in annotatedRelationshipElement.Annotations)
{
yield return submodelElement;
}
}
}
else if (referable is Entity entity)
{
if (entity.Statements != null)
{
foreach (var submodelElement in entity.Statements)
{
yield return submodelElement;
}
}
}
else if (referable is Operation operation)
{
foreach (var inputVariable in operation.InputVariables)
{
yield return inputVariable.Value;
}
foreach (var outputVariable in operation.OutputVariables)
{
yield return outputVariable.Value;
}
foreach (var inOutVariable in operation.InoutputVariables)
{
yield return inOutVariable.Value;
}
}
else
{
yield break;
}
}
public static void SetAllParentsAndTimestamps(this IReferable referable, IReferable parent, DateTime timeStamp, DateTime timeStampCreate)
{
// if (parent == null)
// return;
referable.Parent = parent;
referable.TimeStamp = timeStamp;
referable.TimeStampCreate = timeStampCreate;
referable.TimeStampTree = timeStamp;
foreach (var submodelElement in referable.EnumerateChildren())
{
submodelElement.SetAllParentsAndTimestamps(referable, timeStamp, timeStampCreate);
}
}
public static Submodel GetParentSubmodel(this IReferable referable)
{
IReferable parent = referable;
while (parent is not Submodel && parent != null)
parent = (IReferable)parent.Parent;
return parent as Submodel;
}
public static string CollectIdShortByParent(this IReferable referable)
{
// recurse first
var head = "";
if (referable is not IIdentifiable && referable.Parent is IReferable parentReferable)
// can go up
head = parentReferable.CollectIdShortByParent() + "/";
// add own
var myid = "<no id-Short!>";
if (string.IsNullOrEmpty(referable.IdShort))
myid = referable.IdShort.Trim();
// together
return head + myid;
}
}
}