using Elsa; using Elsa.Activities.Console.Activities; using Elsa.Activities.ControlFlow; using Elsa.Activities.Primitives; using Elsa.Expressions; using Elsa.Services; using Elsa.Services.Models; using Sample04.Activities; namespace Sample04 { public class CalculatorWorkflow : IWorkflow { public void Build(IWorkflowBuilder builder) { builder .StartWith(x => x.TextExpression = new PlainTextExpression("Welcome to Calculator Workflow!")) .Then(x => x.TextExpression = new PlainTextExpression("Enter number 1:"), id: "start") .Then(x => x.VariableName = "number1") .Then(x => x.TextExpression = new PlainTextExpression("Enter number 2:")) .Then(x => x.VariableName = "number2") .Then(x => x.TextExpression = new PlainTextExpression("Now enter the operation you wish to apply. Options are: add, subtract, multiply or divide:")) .Then(x => x.VariableName = "operation") .Then(@switch => { @switch.Expression = new JavaScriptExpression("operation"); @switch.Cases = new[] { "add", "subtract", "multiply", "divide" }; }, @switch => { @switch .When("add") .Then(SetupOperation) .Then("showResult"); @switch .When("subtract") .Then(SetupOperation) .Then("showResult"); @switch .When("multiply") .Then(SetupOperation) .Then("showResult"); @switch .When("divide") .Then(SetupOperation) .Then("showResult"); } ) .Add(x => x.TextExpression = new JavaScriptExpression("`Result: ${result}`"), "showResult") .Then(x => x.TextExpression = new PlainTextExpression("Try again? (y/n)")) .Then(x => x.VariableName = "retry") .Then( x => x.Expression = new JavaScriptExpression("retry.toLowerCase() === 'y'"), ifElse => { ifElse .When(OutcomeNames.True) .Then("start"); ifElse .When(OutcomeNames.False) .Then(x => x.TextExpression = new PlainTextExpression("Bye!")); });; } private void SetupOperation(ArithmeticOperation operation) { operation.Values = new JavaScriptExpression("[number1, number2]"); operation.ResultVariableName = "result"; } } }