2019-07-14 10:07:58 +00:00
using Elsa ;
using Elsa.Activities.Console.Activities ;
2019-07-20 21:58:34 +00:00
using Elsa.Activities.ControlFlow ;
using Elsa.Activities.Primitives ;
using Elsa.Expressions ;
2019-07-14 10:07:58 +00:00
using Elsa.Services ;
using Elsa.Services.Models ;
using Sample04.Activities ;
namespace Sample04
{
public class CalculatorWorkflow : IWorkflow
{
public void Build ( IWorkflowBuilder builder )
{
builder
. StartWith < WriteLine > ( x = > x . TextExpression = new PlainTextExpression ( "Welcome to Calculator Workflow!" ) )
. Then < WriteLine > ( x = > x . TextExpression = new PlainTextExpression ( "Enter number 1:" ) , id : "start" )
2019-07-22 16:55:02 +00:00
. Then < ReadLine > ( x = > x . VariableName = "number1" )
2019-07-14 10:07:58 +00:00
. Then < WriteLine > ( x = > x . TextExpression = new PlainTextExpression ( "Enter number 2:" ) )
2019-07-22 16:55:02 +00:00
. Then < ReadLine > ( x = > x . VariableName = "number2" )
2019-07-14 10:07:58 +00:00
. Then < WriteLine > ( x = > x . TextExpression = new PlainTextExpression ( "Now enter the operation you wish to apply. Options are: add, subtract, multiply or divide:" ) )
2019-07-22 16:55:02 +00:00
. Then < ReadLine > ( x = > x . VariableName = "operation" )
2019-07-14 10:07:58 +00:00
. Then < Switch > ( @switch = >
{
@switch . Expression = new JavaScriptExpression < string > ( "operation" ) ;
@switch . Cases = new [ ] { "add" , "subtract" , "multiply" , "divide" } ;
} ,
@switch = >
{
@switch
. When ( "add" )
. Then < Sum > ( SetupOperation )
. Then ( "showResult" ) ;
@switch
. When ( "subtract" )
. Then < Subtract > ( SetupOperation )
. Then ( "showResult" ) ;
@switch
. When ( "multiply" )
. Then < Multiply > ( SetupOperation )
. Then ( "showResult" ) ;
@switch
. When ( "divide" )
. Then < Divide > ( SetupOperation )
. Then ( "showResult" ) ;
}
)
. Add < WriteLine > ( x = > x . TextExpression = new JavaScriptExpression < string > ( "`Result: ${result}`" ) , "showResult" )
. Then < WriteLine > ( x = > x . TextExpression = new PlainTextExpression ( "Try again? (y/n)" ) )
2019-07-22 16:55:02 +00:00
. Then < ReadLine > ( x = > x . VariableName = "retry" )
2019-07-14 10:07:58 +00:00
. Then < IfElse > (
2019-07-22 19:58:29 +00:00
x = > x . Expression = new JavaScriptExpression < bool > ( "retry.toLowerCase() === 'y'" ) ,
2019-07-14 10:07:58 +00:00
ifElse = >
{
ifElse
. When ( OutcomeNames . True )
. Then ( "start" ) ;
ifElse
. When ( OutcomeNames . False )
. Then < WriteLine > ( x = > x . TextExpression = new PlainTextExpression ( "Bye!" ) ) ;
} ) ; ;
}
private void SetupOperation ( ArithmeticOperation operation )
{
operation . Values = new JavaScriptExpression < double [ ] > ( "[number1, number2]" ) ;
operation . ResultVariableName = "result" ;
}
}
2019-06-27 20:55:55 +00:00
}