using Elsa; using Elsa.Activities.Console.Activities; using Elsa.Activities.ControlFlow; 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 LiteralExpression("Welcome to Calculator Workflow!"), "welcome") .Then(x => x.TextExpression = new LiteralExpression("Enter number 1:"), id: "enter-first-number-prompt") .Then(x => x.VariableName = "number1", id: "read-first-number") .Then(x => x.TextExpression = new LiteralExpression("Enter number 2:"), id: "enter-second-number-prompt") .Then(x => x.VariableName = "number2", id: "read-second-number") .Then(x => x.TextExpression = new LiteralExpression("Now enter the operation you wish to apply. Options are: add, subtract, multiply or divide:"), id: "enter-operation-prompt") .Then(x => x.VariableName = "operation", id: "read-operation") .Then(@switch => { @switch.Expression = new JavaScriptExpression("operation"); @switch.Cases = new[] { "add", "subtract", "multiply", "divide" }; }, @switch => { @switch .When("add") .Then(SetupOperation, id: "perform-sum") .Then("show-result"); @switch .When("subtract") .Then(SetupOperation, id: "perform-subtract") .Then("show-result"); @switch .When("multiply") .Then(SetupOperation, id: "perform-multiply") .Then("show-result"); @switch .When("divide") .Then(SetupOperation, id: "perform-divide") .Then("show-result"); }, "inspect-selected-operation" ) .Add(x => x.TextExpression = new JavaScriptExpression("`Result: ${result}`"), "show-result") .Then(x => x.TextExpression = new LiteralExpression("Try again? (y/n)"), id: "try-again-prompt") .Then(x => x.VariableName = "retry", id: "read-try-again") .Then( x => x.ConditionExpression = new JavaScriptExpression("retry.toLowerCase() === 'y'"), ifElse => { ifElse .When(OutcomeNames.True) .Then("enter-first-number-prompt"); ifElse .When(OutcomeNames.False) .Then(x => x.TextExpression = new LiteralExpression("Bye!"), id: "say-good-bye"); }, id: "inspect-retry"); } private void SetupOperation(ArithmeticOperation operation) { operation.Values = new JavaScriptExpression("[number1, number2]"); operation.ResultVariableName = "result"; } } }