A little workflow engine

A little workflow engine

In one of my projects, i need to create a little workflow system that fits with UI needs. This workflow engine needs basic features (if, while, foreach, basic actions) and also need to “stop and wait” for a user input for example. I have created my own one because I did not found my happiness in existing ones.

I dont care, bring me to the download now !

 

Inside the beast

The workflow will be defined for a given state (with a generic definition). This state will be the reference for all methods that will be integrated in the workflow.

Workflow<SimpleState> workflow = new Workflow<SimpleState>()
    .Do(EasyAction);

Easy action is typed as Action and is defined as

public void EasyAction(SimpleState state)
{
    state.StateFullVariable = 1;
}

Each method call in the workflow class is for workflow definition except Start and Unblock.
 

Fluently speaking

I wanted to create a workflow engine that is easy to use and easy to understand. The fluent definition was the good definition. Workflow of a blackjack game:

_MainWorkflow = new Workflow<GameState>()
    .Do(PrepareGame)
    .DoAndBlock(UserSelection)
    .Do(GiveCards)
    .Foreach(EnumeratePlayerForSplit,
        new Workflow<Tuple<Tuple<int, BlackjackSimplePlayer>, GameState>>()
            .DoAndBlock(PlayerChooseForSplit))
    .Foreach(EnumeratePlayer,
        new Workflow<Tuple<BlackjackSimplePlayer, GameState>>()
            .Foreach(EnumerateHand,
                new Workflow<Tuple<int, Tuple<BlackjackSimplePlayer, GameState>>>()
                    .WhileAndBlock(PlayerChoose, PlayerChoiceNotFinished)))
     .Do(DealerPlay)
     .Do(Resolution);

What can we do ?

Chain action


We can chain actions by calling :

.Do(EasyAction)
.Do(EasyAction2)

Foreach


We can Execute action (or sub workflow) for each object provided. The state of the workflow will be converted in a Tuple containing the item of the current loop and the initial state.

.Foreach(DynamicEnumerateItem,ActionInForeach);

Or

.Foreach(DynamicEnumerateItem,
    new Workflow<Tuple<int, ListState>>()
        .Do(ActionInForeach)
    );

While


Here is a while definition with a condition and the action that must be run until the condition return false.
Here is the definition:

.While(WhileTest, WhileContent);

If


The If definition tests the condition before redirecting to the good action (or the good workflow):

.If(TestToTrue, ActionIfTrue, ActionIfFalse);

To block or not to block…

You can decide to block your workflow execution. The workflow will wait for the call of the Unblock method.

Unblock()

You can imagine this workflow engine plugged in an MVVM architecture , directly in the ViewModel. This workflow that directly manage UI and block for user input before going on.

Download

The download link is available here : https://github.com/alphamax/TinyWorkflow
You also have an “how to” here : https://github.com/alphamax/TinyWorkflow#tinyworkflow–

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *