EtAlii.Generators
Introduction
Code generation is fun and when done right extremely helpful. This project aims to use a combination of ANTLR and Roslyn source generators to simplify usage of some of the more mainstream frameworks/libraries. The first attempt is to use PlantUML to visually design Stateless state machines.
Project status
Examples
Behavior trees
Tbd.
EtAlii.Generators.Stateless - PlantUML state diagrams to Stateless state machines
Usage:
- Add the package to the target project:
<ItemGroup> <PackageReference Update="EtAlii.Generators.Stateless" Version="1.0.0" PrivateAssets="all" /> </ItemGroup>
- Come up with a fancy PlantUML state machine diagram:
@startuml 'stateless namespace My.ExampleNamespace 'stateless class MyFancyStateMachine 'stateless generate partial [*] --> State1 : Start State1 --> [*] State1 : this is a string State1 : this is another string State1 -> State2 : Continue State2 --> [*] State2 -> State3 : DoState3 @enduml
- Put the PlantUML diagram in a file and reference it in the project as a
Stateless
entry:<ItemGroup> <Stateless Include="MyFancyStateMachine.puml" /> </ItemGroup>
- Compile the project - the Roslyn generator now uses the diagram to create C# state machine code.
- Add a class file to implement the needed state machine behavior.
namespace My.ExampleNamespace { using System; public partial class MyFancyStateMachine { public override void OnState1Entered() => Console.WriteLine("Entered State 1"); public override void OnState1Exited() => Console.WriteLine("Exited State 1"); public override void OnState2Entered() => Console.WriteLine("Entered State 2"); public override void OnState2Exited() => Console.WriteLine("Exited State 2"); } }
- Create and trigger the state machine from somewhere in your code.
namespace My.ExampleNamespace { using System; public static void Main() { var stateMachine = new MyFancyStateMachine(); stateMachine.Start(); stateMachine.Continue(); } }