Background
Cephei.QL is a large project, consisting of over two thousand classes and twenty thousand closures. It has been implemented in F# because parallel executing needs to be functionally immutable to avoid co-mutation of data on separate threads, but is also a simpler problem when type inference can be implied in many places.
Whilst there is a huge amount of code that needs to be produced; with some exceptions, the pattern of mapping an underlying library to a higher level abstraction is common
Overview
This blog is about how model-driven architecture can be applied to the problem of creating a library that integrates a number of underlying sources. Traditionally Architecture modelling tools approach this problem by translating classes to a metadata rich source where the use of templates allows the implementation to be derived at runtime.
For Cephei.QL, code generation has been used because:
- The target language (F#) is not supported by the current generation of software engineering tools.
- The permutations are more complex than is supported by templates and would require a runtime code generator to be used.
- Runtime generation is not appropriate for high-performance production environments.
- The target library is intended to provide recipes for specific implementation, that can tailored to specific problems.
While this example is concerned with generation of abstractions over a large financial library (library -> cell models -> serialisation -> Excel integration), the problem is the same as implementing data-structures that need to be mapped from Javascript though application tiers to data persistence
Components
Sparx Enterprise Architect is the leading tool for software engineering because of the wide range languages supported and tools for Model-Driven-Architecture.. In this scenario it is also extremely strong because the underlying repository supports SQL queries and therefore rich database integration.
EA.Gen.Model is a .NET class library (that we developed years ago) that uses the Entity Data Model to provide a high-level object-graph view of the underlying Sparx Repository, with Linq Queries There are many templating technologies based on the principles for web-server pages that generate HTML for browsers, but the one we use is T4 Text Templates because it can be used within a Build Server (TFS, Teamcity, Jenkins, etc) without having to configure open access.
Implementation
The full Visual Studio 2019 code for Cephei.Gen is available from GitHub, but includes the C++ code generator that was previously used to wrap QuantLib before switching to the C# port QLNet. The NetModel/Class.cs provides the data model for classes, and NetQL/Class.tt provides template for the bulk of the Cephei.QL code.
After reverse engineering the source code into Sparx, the code generator can be run to generate F# code.
Conclusion
For a wide variety of problems, model driven architecture can be used to accrue greater value from the models we create during systems design, increasing productivity, and reducing errors introduced when coded by han