-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathMetadataFeatureExtensions.cs
More file actions
249 lines (227 loc) · 9.97 KB
/
MetadataFeatureExtensions.cs
File metadata and controls
249 lines (227 loc) · 9.97 KB
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
// -------------------------------------------------------------------------------------------------
// <copyright file="MetadataFeatureExtensions.cs" company="Starion Group S.A.">
//
// Copyright (C) 2022-2026 Starion Group S.A.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// </copyright>
// ------------------------------------------------------------------------------------------------
namespace SysML2.NET.Core.POCO.Kernel.Metadata
{
using System;
using System.Collections.Generic;
using System.Linq;
using SysML2.NET.Core.POCO.Core.Features;
using SysML2.NET.Core.POCO.Kernel.FeatureValues;
using SysML2.NET.Core.POCO.Root.Elements;
/// <summary>
/// The <see cref="MetadataFeatureExtensions"/> class provides extensions methods for
/// the <see cref="IMetadataFeature"/> interface
/// </summary>
internal static class MetadataFeatureExtensions
{
/// <summary>
/// Computes the derived property.
/// </summary>
/// <remarks>
/// OCL2.0:
/// <code>
/// metaclass =
/// let metaclassTypes : Sequence(Type) = type->selectByKind(Metaclass) in
/// if metaclassTypes->isEmpty() then null
/// else metaClassTypes->first()
/// endif
/// </code>
/// </remarks>
/// <param name="metadataFeatureSubject">
/// The subject <see cref="IMetadataFeature"/>
/// </param>
/// <returns>
/// the computed result
/// </returns>
internal static IMetaclass ComputeMetaclass(this IMetadataFeature metadataFeatureSubject)
{
if (metadataFeatureSubject == null)
{
throw new ArgumentNullException(nameof(metadataFeatureSubject));
}
var metaclassTypes = metadataFeatureSubject.type.OfType<IMetaclass>().ToList();
return metaclassTypes.Count == 0 ? null : metaclassTypes[0];
}
/// <summary>
/// If the given baseFeature is a feature of this MetadataFeature, or is directly or indirectly
/// redefined by a feature, then return the result of evaluating the appropriate (model-level evaluable)
/// value Expression for it (if any), with the MetadataFeature as the target.
/// </summary>
/// <remarks>
/// OCL2.0:
/// <code>
/// let selectedFeatures : Sequence(Feature) = feature->
/// select(closure(ownedRedefinition.redefinedFeature)->
/// includes(baseFeature)) in
/// if selectedFeatures->isEmpty() then null
/// else
/// let selectedFeature : Feature = selectedFeatures->first() in
/// let featureValues : FeatureValue = selectedFeature->
/// closure(ownedRedefinition.redefinedFeature).ownedMember->
/// selectAsKind(FeatureValue) in
/// if featureValues->isEmpty() then null
/// else featureValues->first().value.evaluate(self)
/// endif
/// </code>
/// </remarks>
/// <param name="metadataFeatureSubject">
/// The subject <see cref="IMetadataFeature"/>
/// </param>
/// <param name="baseFeature">
/// The base <see cref="IFeature"/> to look up in the redefinition closure of each feature
/// owned by the subject.
/// </param>
/// <returns>
/// The expected collection of <see cref="IElement" />
/// </returns>
internal static List<IElement> ComputeEvaluateFeatureOperation(this IMetadataFeature metadataFeatureSubject, IFeature baseFeature)
{
if (metadataFeatureSubject == null)
{
throw new ArgumentNullException(nameof(metadataFeatureSubject));
}
if (baseFeature == null)
{
throw new ArgumentNullException(nameof(baseFeature));
}
var selectedFeatures = metadataFeatureSubject.feature
.Where(feature => ComputeRedefinitionClosure(feature).Contains(baseFeature))
.ToList();
if (selectedFeatures.Count == 0)
{
return [];
}
var selectedFeature = selectedFeatures[0];
var featureValues = ComputeRedefinitionClosure(selectedFeature)
.SelectMany(feature => feature.ownedMember)
.OfType<IFeatureValue>()
.ToList();
if (featureValues.Count == 0)
{
return [];
}
var valueExpression = featureValues[0].value;
return valueExpression == null ? [] : valueExpression.Evaluate(metadataFeatureSubject);
}
/// <summary>
/// Check if this MetadataFeature has a metaclass which is a kind of SemanticMetadata.
/// </summary>
/// <remarks>
/// OCL2.0:
/// <code>
/// specializesFromLibrary('Metaobjects::SemanticMetadata')
/// </code>
/// </remarks>
/// <param name="metadataFeatureSubject">
/// The subject <see cref="IMetadataFeature"/>
/// </param>
/// <returns>
/// The expected <see cref="bool" />
/// </returns>
internal static bool ComputeIsSemanticOperation(this IMetadataFeature metadataFeatureSubject)
{
return metadataFeatureSubject == null
? throw new ArgumentNullException(nameof(metadataFeatureSubject))
: metadataFeatureSubject.SpecializesFromLibrary("Metaobjects::SemanticMetadata");
}
/// <summary>
/// Check if this MetadataFeature has a metaclass that is a kind of KerML::Element (that is, it is from
/// the reflective abstract syntax model).
/// </summary>
/// <remarks>
/// OCL2.0:
/// <code>
/// specializesFromLibrary('KerML::Element')
/// </code>
/// </remarks>
/// <param name="metadataFeatureSubject">
/// The subject <see cref="IMetadataFeature"/>
/// </param>
/// <returns>
/// The expected <see cref="bool" />
/// </returns>
internal static bool ComputeIsSyntacticOperation(this IMetadataFeature metadataFeatureSubject)
{
return metadataFeatureSubject == null
? throw new ArgumentNullException(nameof(metadataFeatureSubject))
: metadataFeatureSubject.SpecializesFromLibrary("KerML::Element");
}
/// <summary>
/// If this MetadataFeature reflectively represents a model element, then return the corresponding
/// Element instance from the MOF abstract syntax representation of the model.
/// </summary>
/// <remarks>
/// English:
/// <code>
/// No OCL
/// </code>
/// OCL2.0:
/// <code>
/// isSyntactic()
/// </code>
/// </remarks>
/// <param name="metadataFeatureSubject">
/// The subject <see cref="IMetadataFeature"/>
/// </param>
/// <returns>
/// The expected <see cref="IElement" />
/// </returns>
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
internal static IElement ComputeSyntaxElementOperation(this IMetadataFeature metadataFeatureSubject)
{
// Implementation deferred: requires a MOF reflective metaclass registry
// (runtime MetadataFeature -> reflected IElement) that is not present in this SDK.
throw new NotSupportedException("Create a GitHub issue when this method is required");
}
/// <summary>
/// Computes the reflexive-transitive closure of <paramref name="start"/> over
/// <c>ownedRedefinition.RedefinedFeature</c>, using a HashSet visited-set for cycle protection.
/// </summary>
/// <param name="start">
/// The seed <see cref="IFeature"/> to start the closure from. The seed itself is included in
/// the result when non-null.
/// </param>
/// <returns>
/// A fresh <see cref="List{T}"/> containing the seed and all transitively redefined features,
/// in BFS order. Returns an empty list when <paramref name="start"/> is null.
/// </returns>
private static List<IFeature> ComputeRedefinitionClosure(IFeature start)
{
var visited = new HashSet<IFeature>();
var result = new List<IFeature>();
var queue = new Queue<IFeature>();
if (start != null && visited.Add(start))
{
queue.Enqueue(start);
result.Add(start);
}
while (queue.Count > 0)
{
var current = queue.Dequeue();
foreach (var redefinedFeature in current.ownedRedefinition.Select(x => x.RedefinedFeature).Where(x => x != null && visited.Add(x)))
{
queue.Enqueue(redefinedFeature);
result.Add(redefinedFeature);
}
}
return result;
}
}
}