Skip to content

Commit 1c96cc8

Browse files
[Generate] core classes based on UML model
1 parent 0602f9c commit 1c96cc8

17 files changed

Lines changed: 875 additions & 461566 deletions
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// -------------------------------------------------------------------------------------------------
2+
// <copyright file="ReservedCSharpNameMapper.cs" company="Starion Group S.A.">
3+
//
4+
// Copyright 2022-2025 Starion Group 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.Extensions
22+
{
23+
/// <summary>
24+
/// The purpose of the <see cref="ReservedCSharpNameMapper"/> is to map strings that are keywords used in C#
25+
/// </summary>
26+
public static class ReservedCSharpNameMapper
27+
{
28+
/// <summary>
29+
/// Maps a reserved keyword in c# to a string that can be used in code
30+
/// </summary>
31+
/// <param name="input">
32+
/// The input string
33+
/// </param>
34+
/// <returns></returns>
35+
public static string Map(string input)
36+
{
37+
switch (input)
38+
{
39+
case "in":
40+
return "@in";
41+
case "out":
42+
return "@out";
43+
case "ref":
44+
return "@ref";
45+
case "var":
46+
return "@var";
47+
case "<":
48+
return "LT";
49+
case "<=":
50+
return "LTEQ";
51+
case ">":
52+
return "GT";
53+
case ">=":
54+
return "GTEQ";
55+
case "=":
56+
return "EQ";
57+
case "true":
58+
return "True";
59+
case "false":
60+
return "False";
61+
default:
62+
return input;
63+
}
64+
}
65+
66+
/// <summary>
67+
/// Queries whether the input string is a reserved keyword in C#
68+
/// </summary>
69+
/// <param name="input">
70+
/// The string that is to be queried
71+
/// </param>
72+
/// <returns>
73+
/// true when reserved, false if not
74+
/// </returns>
75+
public static bool QueryIsReserved(string input)
76+
{
77+
switch (input)
78+
{
79+
case "in":
80+
case "out":
81+
case "ref":
82+
case "var":
83+
case "<":
84+
case "<=":
85+
case ">":
86+
case ">=":
87+
case "=":
88+
case "true":
89+
case "false":
90+
return true;
91+
default:
92+
return false;
93+
}
94+
}
95+
}
96+
}

SysML2.NET.CodeGenerator/Generators/Generator.cs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
namespace SysML2.NET.CodeGenerator.Generators
2222
{
23+
using System;
2324
using System.IO;
2425
using System.Reflection;
2526
using System.Text;
@@ -47,7 +48,7 @@ protected Generator()
4748
/// Gets the path where the template are stored
4849
/// </summary>
4950
public string TemplateFolderPath { get; private set; }
50-
51+
5152
/// <summary>
5253
/// perform code cleanup
5354
/// </summary>
@@ -90,5 +91,39 @@ protected static async Task Write(string generatedCode, DirectoryInfo outputDire
9091

9192
await File.WriteAllTextAsync(filePath, generatedCode, Encoding.UTF8);
9293
}
94+
95+
/// <summary>
96+
/// Writes the generated code to disk
97+
/// </summary>
98+
/// <param name="generatedCode">
99+
/// he generated code that needs to be written to disk
100+
/// </param>
101+
/// <param name="outputDirectory">
102+
/// The target <see cref="DirectoryInfo"/>
103+
/// </param>
104+
/// <param name="fileName">
105+
/// The name of the file
106+
/// </param>
107+
/// <returns>
108+
/// an awaitable <see cref="Task"/>
109+
/// </returns>
110+
protected static async Task WriteAsync(string generatedCode, DirectoryInfo outputDirectory, string fileName)
111+
{
112+
if (string.IsNullOrEmpty(generatedCode))
113+
{
114+
throw new ArgumentException(nameof(generatedCode));
115+
}
116+
117+
ArgumentNullException.ThrowIfNull(outputDirectory);
118+
119+
if (string.IsNullOrEmpty(fileName))
120+
{
121+
throw new ArgumentException(nameof(fileName));
122+
}
123+
124+
var filePath = Path.Combine(outputDirectory.FullName, fileName);
125+
126+
await File.WriteAllTextAsync(filePath, generatedCode, Encoding.UTF8);
127+
}
93128
}
94129
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// -------------------------------------------------------------------------------------------------
2+
// <copyright file="UmlHandleBarsGenerator.cs" company="Starion Group S.A.">
3+
//
4+
// Copyright 2022-2025 Starion Group 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.Generators.UmlHandleBarsGenerators
22+
{
23+
using System.IO;
24+
using System.Threading.Tasks;
25+
26+
using uml4net.xmi.Readers;
27+
28+
/// <summary>
29+
/// Abstract super class from which all uml based <see cref="HandlebarsDotNet"/> generators
30+
/// need to derive
31+
/// </summary>
32+
public abstract class UmlHandleBarsGenerator : HandleBarsGenerator
33+
{
34+
/// <summary>
35+
/// Generates code specific to the concrete implementation
36+
/// </summary>
37+
/// <param name="xmiReaderResult">
38+
/// the <see cref="XmiReaderResult"/> that contains the UML model to generate from
39+
/// </param>
40+
/// <param name="outputDirectory">
41+
/// The target <see cref="DirectoryInfo"/>
42+
/// </param>
43+
/// <returns>
44+
/// an awaitable <see cref="Task"/>
45+
/// </returns>
46+
public abstract Task GenerateAsync(XmiReaderResult xmiReaderResult, DirectoryInfo outputDirectory);
47+
}
48+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// -------------------------------------------------------------------------------------------------
2+
// <copyright file="EnumerationLiteralHelper.cs" company="Starion Group S.A.">
3+
//
4+
// Copyright 2022-2025 Starion Group 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.UmlHandleBarHelpers
22+
{
23+
using System;
24+
using System.Linq;
25+
26+
using HandlebarsDotNet;
27+
28+
using SysML2.NET.CodeGenerator.Extensions;
29+
30+
using uml4net;
31+
using uml4net.Extensions;
32+
using uml4net.SimpleClassifiers;
33+
34+
/// <summary>
35+
/// A block helper to support the generation of <see cref="Enumeration"/> and <see cref="EnumerationLiteral"/>
36+
/// </summary>
37+
public static class EnumerationLiteralHelper
38+
{
39+
/// <summary>
40+
/// Registers the <see cref="EnumerationLiteralHelper"/>
41+
/// </summary>
42+
/// <param name="handlebars">
43+
/// The <see cref="IHandlebars"/> context with which the helper needs to be registered
44+
/// </param>
45+
public static void RegisterTypeNameHelper(this IHandlebars handlebars)
46+
{
47+
handlebars.RegisterHelper("EnumerationLiteral.Write", (writer, context, arguments) =>
48+
{
49+
if (arguments.Length != 1)
50+
{
51+
throw new HandlebarsException("{{#EnumerationLiteral.Write}} helper must have exactly one argument");
52+
}
53+
54+
var enumerationLiteral = arguments.Single() as EnumerationLiteral;
55+
56+
var name = StringExtensions.CapitalizeFirstLetter(enumerationLiteral.Name);
57+
58+
if (ReservedCSharpNameMapper.QueryIsReserved(name))
59+
{
60+
name = ReservedCSharpNameMapper.Map(name);
61+
}
62+
63+
writer.WriteSafeString(name);
64+
});
65+
}
66+
}
67+
}

0 commit comments

Comments
 (0)