Skip to content

Commit ea5f09d

Browse files
author
samatrhea
committed
[Add] ModelInspector
1 parent 617230c commit ea5f09d

File tree

4 files changed

+186
-1
lines changed

4 files changed

+186
-1
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// -------------------------------------------------------------------------------------------------
2+
// <copyright file="ModelInspectorTestFixture.cs" company="RHEA System S.A.">
3+
//
4+
// Copyright 2022-2023 RHEA System S.A.
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// </copyright>
19+
// ------------------------------------------------------------------------------------------------
20+
21+
namespace SysML2.NET.CodeGenerator.Tests.Inspector
22+
{
23+
using System;
24+
using System.Linq;
25+
26+
using ECoreNetto;
27+
28+
using NUnit.Framework;
29+
30+
using SysML2.NET.CodeGenerator;
31+
using SysML2.NET.CodeGenerator.Inspector;
32+
33+
/// <summary>
34+
/// Suite of tests for the <see cref="ModelInspector"/> class.
35+
/// </summary>
36+
[TestFixture]
37+
public class ModelInspectorTestFixture
38+
{
39+
private ModelInspector modelInspector;
40+
41+
[SetUp]
42+
public void SetUp()
43+
{
44+
this.modelInspector = new ModelInspector();
45+
}
46+
47+
[Test]
48+
public void Verify_that_inspects_executes_as_expected()
49+
{
50+
Assert.That(() => this.modelInspector.Inspect(), Throws.Nothing);
51+
}
52+
}
53+
}

SysML2.NET.CodeGenerator.Tests/SysML2.NET.CodeGenerator.Tests.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,8 @@
6666
<ProjectReference Include="..\SysML2.NET.CodeGenerator\SysML2.NET.CodeGenerator.csproj" />
6767
</ItemGroup>
6868

69+
<ItemGroup>
70+
<Folder Include="Inspector\" />
71+
</ItemGroup>
72+
6973
</Project>
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
// -------------------------------------------------------------------------------------------------
2+
// <copyright file="ModelInspector.cs" company="RHEA System S.A.">
3+
//
4+
// Copyright 2022-2023 RHEA System S.A.
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// </copyright>
19+
// ------------------------------------------------------------------------------------------------
20+
21+
namespace SysML2.NET.CodeGenerator.Inspector
22+
{
23+
using System;
24+
using System.Collections.Generic;
25+
using System.Linq;
26+
27+
using ECoreNetto;
28+
using ECoreNetto.Extensions;
29+
30+
/// <summary>
31+
/// The purpose of the <see cref="ModelInspector"/> is to iterate through the model and report on the various kinds of
32+
/// patters that exist in the ECore model that need to be taken into account for code-generation
33+
/// </summary>
34+
public class ModelInspector
35+
{
36+
private readonly HashSet<EClass> interestingClasses = new HashSet<EClass>();
37+
38+
private readonly HashSet<string> referenceTypes = new HashSet<string>();
39+
40+
private readonly HashSet<string> valueTypes = new HashSet<string>();
41+
42+
private readonly HashSet<string> enums = new HashSet<string>();
43+
44+
public void Inspect()
45+
{
46+
var rootPackage = DataModelLoader.Load();
47+
48+
foreach (var eClass in rootPackage.EClassifiers.OfType<EClass>())
49+
{
50+
foreach (var structuralFeature in eClass.EStructuralFeatures)
51+
{
52+
if (structuralFeature is EReference reference)
53+
{
54+
var referenceType = $"{reference.LowerBound}:{reference.UpperBound}";
55+
56+
if (!this.referenceTypes.Contains(referenceType))
57+
{
58+
if (this.referenceTypes.Add(referenceType))
59+
{
60+
this.interestingClasses.Add(eClass);
61+
}
62+
}
63+
}
64+
65+
if (structuralFeature is EAttribute attribute)
66+
{
67+
if (structuralFeature.QueryIsEnum())
68+
{
69+
var @enum = $"{attribute.LowerBound}:{attribute.UpperBound}";
70+
if (this.enums.Add(@enum))
71+
{
72+
this.interestingClasses.Add(eClass);
73+
}
74+
}
75+
else
76+
{
77+
var valueType = $"{attribute.EType.Name}:{attribute.LowerBound}:{attribute.UpperBound}";
78+
79+
if (this.valueTypes.Add(valueType))
80+
{
81+
this.interestingClasses.Add(eClass);
82+
}
83+
}
84+
}
85+
}
86+
}
87+
88+
89+
var orderedReferenceTypes = this.referenceTypes.Order();
90+
91+
foreach (var referenceType in orderedReferenceTypes)
92+
{
93+
Console.WriteLine($"reference type: {referenceType}");
94+
}
95+
96+
var orderedEnums = this.enums.Order();
97+
98+
foreach (var @enum in orderedEnums)
99+
{
100+
Console.WriteLine($"enum type: {@enum}");
101+
}
102+
103+
var orderedValueTypes = this.valueTypes.Order();
104+
105+
foreach (var valueType in orderedValueTypes)
106+
{
107+
Console.WriteLine($"value type: {valueType}");
108+
}
109+
110+
foreach (var @class in this.interestingClasses.OrderBy(x => x.Name))
111+
{
112+
Console.WriteLine($"class: {@class.Name}");
113+
}
114+
}
115+
}
116+
}

SysML2.NET.sln

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SySML2.NET.REST", "SySML2.N
3636
EndProject
3737
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SySML2.NET.REST.Tests", "SySML2.NET.REST.Tests\SySML2.NET.REST.Tests.csproj", "{B5E96BF8-C1B6-4C29-97FB-87557B94DE6C}"
3838
EndProject
39-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SysML2.NET.Viewer.Tests", "SysML2.NET.Viewer.Tests\SysML2.NET.Viewer.Tests.csproj", "{2E1877CB-255F-49C9-A0F0-5DCB34FCD06A}"
39+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SysML2.NET.Viewer.Tests", "SysML2.NET.Viewer.Tests\SysML2.NET.Viewer.Tests.csproj", "{2E1877CB-255F-49C9-A0F0-5DCB34FCD06A}"
40+
EndProject
41+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SysML2.NET.Serializer.Dictionary", "SysML2.NET.Serializer.Dictionary\SysML2.NET.Serializer.Dictionary.csproj", "{03D37FC3-CFEC-4B90-852D-CE1BAA504C84}"
42+
EndProject
43+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SysML2.NET.Serializer.Dictionary.Tests", "SysML2.NET.Serializer.Dictionary.Tests\SysML2.NET.Serializer.Dictionary.Tests.csproj", "{102AD589-6440-4D27-84BA-BB5DB9A43B21}"
4044
EndProject
4145
Global
4246
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -80,6 +84,14 @@ Global
8084
{2E1877CB-255F-49C9-A0F0-5DCB34FCD06A}.Debug|Any CPU.Build.0 = Debug|Any CPU
8185
{2E1877CB-255F-49C9-A0F0-5DCB34FCD06A}.Release|Any CPU.ActiveCfg = Release|Any CPU
8286
{2E1877CB-255F-49C9-A0F0-5DCB34FCD06A}.Release|Any CPU.Build.0 = Release|Any CPU
87+
{03D37FC3-CFEC-4B90-852D-CE1BAA504C84}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
88+
{03D37FC3-CFEC-4B90-852D-CE1BAA504C84}.Debug|Any CPU.Build.0 = Debug|Any CPU
89+
{03D37FC3-CFEC-4B90-852D-CE1BAA504C84}.Release|Any CPU.ActiveCfg = Release|Any CPU
90+
{03D37FC3-CFEC-4B90-852D-CE1BAA504C84}.Release|Any CPU.Build.0 = Release|Any CPU
91+
{102AD589-6440-4D27-84BA-BB5DB9A43B21}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
92+
{102AD589-6440-4D27-84BA-BB5DB9A43B21}.Debug|Any CPU.Build.0 = Debug|Any CPU
93+
{102AD589-6440-4D27-84BA-BB5DB9A43B21}.Release|Any CPU.ActiveCfg = Release|Any CPU
94+
{102AD589-6440-4D27-84BA-BB5DB9A43B21}.Release|Any CPU.Build.0 = Release|Any CPU
8395
EndGlobalSection
8496
GlobalSection(SolutionProperties) = preSolution
8597
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)