Add IfElse sample workflow

This commit is contained in:
Sipke Schoorstra 2020-10-29 21:36:28 +01:00
parent c91821cacf
commit bcaab99dbe
4 changed files with 122 additions and 0 deletions

View file

@ -104,6 +104,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Elsa.Samples.WhileLoopConso
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Elsa.Samples.WhileLoopWorker", "src\samples\Elsa.Samples.WhileLoopWorker\Elsa.Samples.WhileLoopWorker.csproj", "{85A70E7C-A4AC-49BA-942B-635F974DE255}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Elsa.Samples.IfElseConsole", "src\samples\Elsa.Samples.IfElseConsole\Elsa.Samples.IfElseConsole.csproj", "{E508C1B6-916F-40F4-8731-58279B8F45B2}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -243,6 +245,10 @@ Global
{85A70E7C-A4AC-49BA-942B-635F974DE255}.Debug|Any CPU.Build.0 = Debug|Any CPU
{85A70E7C-A4AC-49BA-942B-635F974DE255}.Release|Any CPU.ActiveCfg = Release|Any CPU
{85A70E7C-A4AC-49BA-942B-635F974DE255}.Release|Any CPU.Build.0 = Release|Any CPU
{E508C1B6-916F-40F4-8731-58279B8F45B2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E508C1B6-916F-40F4-8731-58279B8F45B2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E508C1B6-916F-40F4-8731-58279B8F45B2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E508C1B6-916F-40F4-8731-58279B8F45B2}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -291,6 +297,7 @@ Global
{E4485289-F1DF-4322-B07D-9A0C9B002D31} = {5E5E1E84-DDBC-40D6-B891-0D563A15A44A}
{6B83CCE1-D523-4E3F-84EB-8D79AAF6F7C2} = {5E5E1E84-DDBC-40D6-B891-0D563A15A44A}
{85A70E7C-A4AC-49BA-942B-635F974DE255} = {5E5E1E84-DDBC-40D6-B891-0D563A15A44A}
{E508C1B6-916F-40F4-8731-58279B8F45B2} = {5E5E1E84-DDBC-40D6-B891-0D563A15A44A}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {8B0975FD-7050-48B0-88C5-48C33378E158}

View file

@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.9" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\core\Elsa\Elsa.csproj" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,66 @@
using System;
using Elsa.Activities.Console;
using Elsa.Activities.ControlFlow;
using Elsa.Builders;
using Elsa.Services.Models;
namespace Elsa.Samples.IfElseConsole
{
public class HappinessWorkflow : IWorkflow
{
private readonly Random _random;
public HappinessWorkflow()
{
_random = new Random();
}
public void Build(IWorkflowBuilder workflow)
{
workflow
.WriteLine("--POND OF HAPPINESS--", "Start")
.WriteLine("Throw some Rupees in and your wishes will surely come true.")
.WriteLine("Do you want to throw Rupees?")
.ReadLine()
.IfElse(
activity => activity.WithCondition(context => IsYes(context.Input)),
ifElse =>
{
ifElse
.When(IfElse.True)
.WriteLine(GetCurse)
.WriteLine("...")
.Then("Start");
ifElse
.When(IfElse.False)
.WriteLine("Bye.");
});
}
private static bool IsYes(object? value)
{
var text = ((string)value!).ToLowerInvariant();
return text switch
{
"yes" => true,
"y" => true,
"sure" => true,
_ => false
};
}
private string GetCurse()
{
var curses = new[]
{
"Today, you will have Great Luck",
"Today, you will have a Little Luck",
"Today, you will have a Big Trouble"
};
var index = _random.Next(0, 3);
return curses[index];
}
}
}

View file

@ -0,0 +1,32 @@
using System.Threading.Tasks;
using Elsa.Services;
using Microsoft.Extensions.DependencyInjection;
namespace Elsa.Samples.IfElseConsole
{
/// <summary>
/// Demonstrates a workflow with a While looping construct.
/// </summary>
static class Program
{
private static async Task Main()
{
// Create a service container with Elsa services.
var services = new ServiceCollection()
.AddElsa()
.AddConsoleActivities()
.AddWorkflow<HappinessWorkflow>()
.BuildServiceProvider();
// Run startup actions (not needed when registering Elsa with a Host).
var startupRunner = services.GetRequiredService<IStartupRunner>();
await startupRunner.StartupAsync();
// Get a workflow runner.
var workflowRunner = services.GetService<IWorkflowRunner>();
// Execute the workflow.
await workflowRunner.RunWorkflowAsync<HappinessWorkflow>();
}
}
}