Skip to content

mustaddon/FsmWorkflow

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Jun 23, 2022
690ad63 · Jun 23, 2022

History

11 Commits
Jun 23, 2022
Jun 23, 2022
Apr 15, 2019
Jun 23, 2022
Jun 23, 2022
Apr 15, 2019
Feb 7, 2020

Repository files navigation

FsmWorkflow NuGet version

Finite-state machine (FSM) based workflow

Example #1 - Code based workflow declaration

public enum State { S1, S2, S3 }
public enum Action { A1, A2, A3 }

public class SampleObject
{
    public string Title { get; set; }
    public State State { get; set; }
}

public class SampleWorkflow : IFsmWorkflowBuilder<SampleObject, State, Action>
{
    public IStateMachine<State, Action> Build(SampleObject obj)
    {
        return new FsmBuilder<State, Action>(obj.State)
            .OnJump(x =>
            {
                obj.State = x.Fsm.Current;
                Console.WriteLine($"State changed to {obj.State} from {x.PrevState}");
            })
            .State(State.S1)
                .On(Action.A1).Execute(x => { /* some operations */ return obj.Title; })
                .On(Action.A2).JumpTo(State.S2)
            .State(State.S2)
                .On(Action.A3).Enable(x => /* some conditions */ true).JumpTo(State.S3)
            .State(State.S3)
                .OnEnter(x => Console.WriteLine($"Enter to final state"))
            .Build();
    }
}
var sampleObject = new SampleObject { Title = "Test", State = State.S1  };
var workflowBuilder = new SampleWorkflow();
var workflow = workflowBuilder.Build(sampleObject);

workflow.Trigger(Action.A2);
workflow.Trigger(Action.A3);


// Console output:
// State change to S2 from S1
// State change to S3 from S2
// Enter to final state

Example #2 - JSON based workflow declaration

{
  "start": "_obj.State",
  "onJump": "_obj.State = _args.Fsm.Current; Console.WriteLine($\"State changed to {_obj.State} from {_args.PrevState}\")",

  "states": {
    "S1": {
      "events": {
        "A1": { "execute": "/* some operations */ return _obj.Title" },
        "A2": { "jumpTo": "ConsoleApp.State.S2" }
      }
    },
    "S2": {
      "events": {
        "A3": { "jumpTo": "ConsoleApp.State.S3" }
      }
    },
    "S3": {
      "onEnter": "Console.WriteLine(\"Enter to final state\")"
    }
  }
}
var sampleObject = new SampleObject { Title = "Test", State = State.S1  };
var workflowBuilder = new FsmWorkflowJson<SampleObject,State,Action>(File.ReadAllText(@"SampleWorkflow.json"));
var workflow = workflowBuilder.Build(sampleObject);

workflow.Trigger(Action.A2);
workflow.Trigger(Action.A3);


// Console output:
// State change to S2 from S1
// State change to S3 from S2
// Enter to final state

About

Finite-state machine (FSM) based workflow

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages