Dynamic Control Flow Library (Finite State Machine?)

36 views
Skip to first unread message

Phil Heltewig

unread,
Jul 22, 2016, 12:16:51 AM7/22/16
to nodejs
Hi all,

I've searched the forums, yet can't find anything that describes an answer to what I am looking for.

In a nutshell, I would like to control flow of my application be defined by an external flow input (file) in which the individual steps (functions) to call, their order and conditions are described.

Example code with hardcoded control flow:

// Code

function A(input){
return input * 2;
}

function B(input){
return input + 2;
}

function C(input){
return input / 2;
}

functionZ
(input){
if(input==3) lowResult();
else highResult();;
}

function lowResult(){
console
.log('Result smaller than 4');
}

function highResult(){
console
.log('Result equal or larger than 4')
}

// flow 1
Z
(C(B(A(2)))); // (((2 * 2) + 2) / 2) = 3 returns 'Result smaller than 4'
// flow 2
Z
(B(C(A(2)))); // (((2 * 2) / 2) + 2) = 4 returns 'Result equal or larger 4'


Now what I am looking for is a library or approach that allows me to define flow 1 and flow 2 in a file and then have the code execute according to the defined flow. Best would be format that allows for conditions, etc as well.

<!-- FLOW 1 -->
<STEP function="A" />
<STEP function="A" />

<STEP function="A" />

<IF condition="==3">
   
<STEP function="lowResult" />>
<ELSE>
   
<STEP function="highResult" />
</IF>



That way different control flows could be easily composed by connecting a number of components.

Thanks for reading,
Phil


Phil Heltewig

unread,
Jul 22, 2016, 12:22:36 AM7/22/16
to nodejs
In the meantime I found this code: https://gist.github.com/robotlolita/ccecb9b6781ba085d142040a943e1a96

It does what I want to do.

As a follow up question: Does anyone know of a tool that allows users to visually compose flows, similar to the way it is done in Azure ML Studio (http://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-02-52/Azure-ML-Operationalization.png)?

Thanks a lot,
Phil

Phil Heltewig

unread,
Jul 23, 2016, 10:26:44 PM7/23/16
to nodejs
For anyone reading this post, the best solution I found was using Promises instead.

Promise.resolve(1).then(a).then(a).then(a).then(x => {if(x==3) { console.log('low Result'); } else { console.log('high result'); });

Still leaves the question of a visual tool.
Reply all
Reply to author
Forward
0 new messages