Skip to content

Code Generation

samatrhea edited this page Apr 29, 2024 · 10 revisions

Code Generation

The SysML2.NET libraries are generated from the SysML.ecore ECore metamodel. We make use of convention and template based code generation to convert the information that is stored in the ECore model to various C# classes such as DTO's, POCO's, JSON serializers and much more. The Starion Group EcoreNetto library is used to read the Ecore model. Handlebars templates are used to generate code using the Handlebars.Net library.

The code-generators are executed from the Unit test project called SysML2.NET.CodeGenerator.Tests. The generated code is verified (compared) against so-called expected classes. These are hand-coded and are used to implement a pattern that needs to be generated. An analysis of the SysML2 Ecore model has been performed to check that all the variations in data-types, multiplicity, inheritance etc. is covered. A number of classes have been selected for which expected classes need to be in place such that the generated code is properly covered. The following lists provides an overview of the various kinds of features which classes cover these features, hence which class needs to be included in the expected classes:

The following Value Type Feature can be found in the ECore model:

  • Boolean [1..1] : translates to C# bool
  • Integer [1..1] : translates to C# int
  • Real [1..1] : translates to C# double
  • String [1..1] : translates to C# string
  • String [0..1] : translates to C# string? (nullable)
  • String [0..*] : translates to C# List
  • Enum [0..1] : translates to C# Enum? (nullable)
  • Enum [1..1] : translates to C# Enum

The following Reference Type Feature multiplicities can be found in the ECore model:

  • [0..1] : POCO translates to reference of specified type | DTO translates to a Guid? (nullable)
  • [1..1] : POCO translates to reference of specified type | DTO translates to a Guid
  • [0..*] : POCO translates to List<reference> of specified type | DTO translates to a List<Guid>
  • [1..*] : POCO translates to List<reference> of specified type | DTO translates to a List<Guid>

The following containment (composite aggregation) Reference Type Feature multiplicities can be found in the ECore model:

  • [0..*] : POCO translates to List<reference> of specified type | DTO translates to a List<Guid>

These variations are covered by the following classes:

  • AnnotatingElement
  • Annotation
  • Comment
  • Connector
  • Dependency
  • Element (abstract)
  • Feature
  • Import (abstract) / NamespaceImport (concrete)
  • LiteralInteger
  • LiteralRational

NOTE: The ModelInspector class is used to perform the beforementioned analysis of pattern variation.

Code Generators

The following code-generators are present:

NOTE: the structure of a code-generator is described here.