RSCG – SuperFluid

RSCG – SuperFluid
 
 

name SuperFluid
nuget https://www.nuget.org/packages/SuperFluid/
link https://github.com/hughesjs/SuperFluid
author James Hughes

Generate a state machine from a yaml file

 

This is how you can use SuperFluid .

The code that you start with is


<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFramework>net8.0</TargetFramework>
        <OutputType>Exe</OutputType>
        <Nullable>enable</Nullable>
        <ImplicitUsings>enable</ImplicitUsings>
    </PropertyGroup>

    <ItemGroup>
        <AdditionalFiles Include="Calculator.fluid.yml" />
    </ItemGroup>

    <ItemGroup>
      <PackageReference Include="SuperFluid" Version="1.0.1" OutputItemType="Analyzer" ReferenceOutputAssembly="true" >
        <PrivateAssets>all</PrivateAssets>
        <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      </PackageReference>
    </ItemGroup>
	<PropertyGroup>
		<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
		<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
	</PropertyGroup>
</Project>


The code that you will use is


Name: "ICalculator"
Namespace: "SimpleFluentExample"
InitialState:
  Name: "Create"
  CanTransitionTo: 
    - "Add"
    - "Subtract"
Methods:
  - Name: "Add"
    Arguments:
      - Name: "value"
        Type: "int"
    CanTransitionTo:
      - "Add"
      - "Subtract"
      - "Calculate"
  - Name: "Subtract"
    Arguments:
      - Name: "value"
        Type: "int"
    CanTransitionTo:
      - "Add"
      - "Subtract"
      - "Calculate"
  - Name: "Calculate"
    ReturnType: "int"
    CanTransitionTo: []



using SimpleFluentExample;

Console.WriteLine("Example Basic calculation state machine");
var result1 = CalculatorService.Create()
    .Add(10)
    .Subtract(3)
    .Add(5)
    .Calculate();

Console.WriteLine($"Result 1: {result1}"); // Output: 12
Console.WriteLine();

// Uncomment these lines to see compilation errors:
// CalculatorService.Create().Calculate();        // Can't calculate without operations  
// CalculatorService.Create().Add(5).Add(10);     // Missing Calculate() at the end



namespace SimpleFluentExample;

public class CalculatorService : ICalculator
{
    private int _currentValue = 0;

    // Static factory method as required by the generated interface
    public static ICanAddOrSubtract Create()
    {
        var calculator = new CalculatorService();
        Console.WriteLine("🧮 Calculator created");
        return calculator;
    }

    public ICanAddOrSubtractOrCalculate Add(int value)
    {
        _currentValue += value;
        Console.WriteLine($"➕ Added {value}, current value: {_currentValue}");
        return this;
    }

    public ICanAddOrSubtractOrCalculate Subtract(int value)
    {
        _currentValue -= value;
        Console.WriteLine($"➖ Subtracted {value}, current value: {_currentValue}");
        return this;
    }

    public int Calculate()
    {
        Console.WriteLine($"🎯 Final result: {_currentValue}");
        return _currentValue;
    }
}


 

The code that is generated is

namespace SimpleFluentExample;

public interface ICalculator: ICanAddOrSubtractOrCalculate,ICanAddOrSubtract
{
	public static abstract ICanAddOrSubtract Create();
}
namespace SimpleFluentExample;

public interface ICanAddOrSubtract
{
	public ICanAddOrSubtractOrCalculate Add(int value);
	public ICanAddOrSubtractOrCalculate Subtract(int value);
}
namespace SimpleFluentExample;

public interface ICanAddOrSubtractOrCalculate
{
	public ICanAddOrSubtractOrCalculate Add(int value);
	public ICanAddOrSubtractOrCalculate Subtract(int value);
	public int Calculate();
}

Code and pdf at

https://ignatandrei.github.io/RSCG_Examples/v2/docs/SuperFluid


Posted

in

, ,

by

Tags: