|
| 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 | +} |
0 commit comments