-
Notifications
You must be signed in to change notification settings - Fork 0
/
AsrBase.cs
337 lines (321 loc) · 13.5 KB
/
AsrBase.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
using Autosar;
namespace AutosarClass
{
public delegate void PropertyChangedEventHandler(object? sender, string name, object oldVaule, object newVaule);
/// <summary>
/// Asr Class represent a single arxml file
/// </summary>
public class Asr
{
/// <summary>
/// File name of arxml file.
/// </summary>
private string FileName { get; } = "";
/// <summary>
/// Whether arxml model is modified or not.
/// </summary>
private bool isDirty = false;
/// <summary>
/// Converted data model of arxml file.
/// </summary>
public AUTOSAR? Model { get; } = null;
/// <summary>
/// Packages of Autosar in arxml file.
/// </summary>
public List<ARPACKAGE> ArPackages { get; } = new List<ARPACKAGE>();
/// <summary>
/// Autosar path and reference manager.
/// </summary>
public AsrPathReferenceManager PathReferenceManager { get; } = new AsrPathReferenceManager();
/// <summary>
/// Whether arxml model is modified or not.
/// </summary>
public bool IsDirty
{
get
{
return isDirty;
}
set
{
if (value != IsDirty)
{
isDirty = value;
}
}
}
/// <summary>
/// Initialze Asr from filename.
/// File will be converted to data model. All Autosar packages will be extracted.
/// Autosar path dictionary will be generated.
/// </summary>
/// <param name="filename">Filename of file want to be converted.</param>
public Asr(string filename)
{
FileName = filename;
Model = AUTOSAR.Load(filename);
if (Model != null)
{
// get all autosar packages
FindAllArPackage(ArPackages);
// iterate all elements to generate Autosar path dictionary
CreateAsrPath(PathReferenceManager);
CreateAsrReference(PathReferenceManager);
}
}
/// <summary>
/// Iterate all ARPACKAGE and ARPACKAGES tag to get all Autosar package from Model.
/// ARPACKAGES tag can have ARPACKAGE member and ARPACKAGE tag can have ARPACKAGES member.
/// All ARPACKAGES and ARPACKAGE tag shall be checked to find all Autosar packages.
/// </summary>
private void FindAllArPackage(List<ARPACKAGE> result)
{
if (Model == null)
{
return;
}
var query = from package in Model.ARPACKAGES.ARPACKAGE
select package;
query.ToList().ForEach(x =>
{
if (x.ARPACKAGES != null)
{
// Tag ARPACKAGE has ARPACKAGES member, step deeper.
FindAllArPackage(x, result);
}
// Tag ARPACKAGE has no ARPACKAGES member, get it.
result.Add(x);
});
}
/// <summary>
/// Iterate all ARPACKAGE and ARPACKAGES tag to get all Autosar package from ARPACKAGE.
/// </summary>
/// <param name="arPackage">Autosar package to be checked</param>
public void FindAllArPackage(ARPACKAGE arPackage, List<ARPACKAGE> result)
{
var query = from package in arPackage.ARPACKAGES.ARPACKAGE
select package;
query.ToList().ForEach(x =>
{
if (x.ARPACKAGES != null)
{
// Tag ARPACKAGE has ARPACKAGES member, step deeper.
FindAllArPackage(x, result);
}
// Tag ARPACKAGE has no ARPACKAGES member, get it.
result.Add(x);
});
}
/// <summary>
/// Iterate all tag in Model to genereate Autosar path dictionary.
/// Any tag has SHORTNAME member can be treated as a dictionary.
/// SHORTNAME with seperator "/" from top can idenfify tags in Model.
/// as directory path in file system.
/// </summary>
public void CreateAsrPath(AsrPathReferenceManager manager)
{
if (Model != null)
{
// start from top with ARPACKAGES tag
CreateAsrPath(Model.ARPACKAGES, "", "", manager);
}
}
/// <summary>
/// Iterate all tag in model to genereate Autosar path dictionary.
/// Any tag has SHORTNAME member can be treated as a dictionary.
/// SHORTNAME with seperator "/" from top can idenfify tags in Model.
/// </summary>
/// <param name="model">model to be checked</param>
/// <param name="asrPath">current Autosar path</param>
/// <param name="asrPathDest">current Autosar dest path</param>
private void CreateAsrPath(object model, string asrPath, string asrPathDest, AsrPathReferenceManager manager)
{
// Reflect all properties to get SHORTNAME in model
foreach (var prop in model.GetType().GetProperties())
{
if (prop.Name == "SHORTNAME")
{
// Property with member "SHORTNAME"
if (prop.GetValue(model) is IDENTIFIER identifier)
{
// It is an IDENTIFIER instance, generate new path and put it into dictionary
asrPath += $"/{identifier.TypedValue}";
asrPathDest += $"/{model.GetType()}";
manager.AddPath(model, asrPath, asrPathDest);
}
}
}
// Reflect all properties to decide it is needed to check deeper
foreach (var prop in model.GetType().GetProperties())
{
if ((prop.PropertyType.Namespace == "Autosar") || (prop.PropertyType.Namespace == "System.Collections.Generic"))
{
// Property with namespace "Autosar" or it is a Generic Collection instance
var v = prop.GetValue(model);
if ((v != null) && (prop.Name != "SHORTNAME"))
{
// Property is not null and not "SHORTNAME"
if (v is IEnumerable<object> valueList)
{
// Property is a Generic Collection instance, check each member deeper
foreach (var v2 in valueList)
{
CreateAsrPath(v2, asrPath, asrPathDest, manager);
}
}
else
{
// Property is not Generic Collection instance, check it deeper
CreateAsrPath(v, asrPath, asrPathDest, manager);
}
}
}
}
}
/// <summary>
/// Iterate all tag in Model to genereate Autosar path dictionary.
/// Any tag has SHORTNAME member can be treated as a dictionary.
/// SHORTNAME with seperator "/" from top can idenfify tags in Model.
/// as directory path in file system.
/// </summary>
public void CreateAsrReference(AsrPathReferenceManager manager)
{
if (Model != null)
{
// start from top with ARPACKAGES tag
foreach (var package in ArPackages)
{
CreateAsrReference(package, manager);
}
}
}
/// <summary>
/// Iterate all tag in model to genereate Autosar path dictionary.
/// Any tag has SHORTNAME member can be treated as a dictionary.
/// SHORTNAME with seperator "/" from top can idenfify tags in Model.
/// </summary>
/// <param name="model">model to be checked</param>
/// <param name="asrPathLiteral">current Autosar path</param>
private void CreateAsrReference(object model, AsrPathReferenceManager manager)
{
// Reflect all properties to get SHORTNAME in model
foreach (var prop in model.GetType().GetProperties())
{
try
{
var v = prop.GetValue(model);
if (v == null)
{
continue;
}
if (prop.Name.EndsWith("REF"))
{
if (prop.PropertyType.Namespace == "System.Collections.Generic")
{
// Property is not null and not "SHORTNAME"
if (v is IEnumerable<REF> valueList)
{
// Property is a Generic Collection instance, check each member deeper
foreach (var v1 in valueList)
{
var isRef = false;
var asrReference = v1.TypedValue;
var asrReferenceTag = v1.GetType().Name;
foreach (var prop2 in v1.GetType().GetProperties())
{
if (prop2.Name == "DEST")
{
var v2 = prop2.GetValue(v1);
if (v2 != null)
{
var asrReferenceDest = v2.ToString();
if (asrReferenceDest != null)
{
manager.AddReference(v1, asrReference, asrReferenceTag, asrReferenceDest);
isRef = true;
break;
}
}
}
}
if (isRef == false)
{
CreateAsrReference(v1, manager);
}
}
}
}
else if (prop.GetValue(model) is REF reference)
{
var isRef = false;
var asrReference = reference.TypedValue;
var asrReferenceTag = reference.GetType().Name;
foreach (var prop2 in reference.GetType().GetProperties())
{
if (prop2.Name == "DEST")
{
var v1 = prop2.GetValue(reference);
if (v1 != null)
{
var asrReferenceDest = v1.ToString();
if (asrReferenceDest != null)
{
manager.AddReference(reference, asrReference, asrReferenceTag, asrReferenceDest);
isRef = true;
break;
}
}
}
}
if (isRef == false)
{
CreateAsrReference(v, manager);
}
}
else
{
CreateAsrReference(v, manager);
}
}
else
{
if (prop.PropertyType.Namespace == "System.Collections.Generic")
{
if (v is IEnumerable<object> valueList)
{
// Property is a Generic Collection instance, check each member deeper
foreach (var v1 in valueList)
{
if (v1.GetType().Namespace == "Autosar")
{
CreateAsrReference(v1, manager);
}
}
}
}
else if (prop.PropertyType.Namespace == "Autosar")
{
CreateAsrReference(v, manager);
}
}
}
catch
{
continue;
}
}
}
/// <summary>
/// Save model to file.
/// After save, all module shall make IsDirty to false.
/// </summary>
public void Save()
{
if (FileName != "" && Model != null)
{
Model.Save(FileName);
IsDirty = false;
}
}
}
}