Add various sample projects

This commit is contained in:
Sipke Schoorstra 2023-04-22 00:02:02 +02:00
parent 285feea5b1
commit 9a0e609800
17 changed files with 328 additions and 20 deletions

View file

@ -0,0 +1,11 @@
using Elsa.Workflows.Core.Models;
namespace Elsa.Samples.Bookmarks.Activities;
public class MyEvent : Activity
{
protected override void Execute(ActivityExecutionContext context)
{
context.CreateBookmark();
}
}

View file

@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\bundles\Elsa\Elsa.csproj" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,42 @@
using Elsa.Extensions;
using Elsa.Samples.Bookmarks.Activities;
using Elsa.Workflows.Core.Activities;
using Elsa.Workflows.Core.Contracts;
using Elsa.Workflows.Core.Models;
using Microsoft.Extensions.DependencyInjection;
// Setup service container.
var services = new ServiceCollection();
// Add Elsa services.
services.AddElsa();
// Build service container.
var serviceProvider = services.BuildServiceProvider();
// Resolve a workflow runner to run the workflow.
var workflowRunner = serviceProvider.GetRequiredService<IWorkflowRunner>();
// Create a workflow.
var workflow = new Workflow
{
Root = new Sequence
{
Activities =
{
new WriteLine("Hello World!"),
new MyEvent(), // Blocks until the event bookmark is resumed.
new WriteLine("Event received!")
}
}
};
// Run the workflow.
var result = await workflowRunner.RunAsync(workflow);
// Resume the workflow using te created bookmark.
var bookmark = result.WorkflowState.Bookmarks.Single();
var workflowState = result.WorkflowState;
await workflowRunner.RunAsync(workflow, workflowState, new RunWorkflowOptions(bookmarkId: bookmark.Id));

View file

@ -30,7 +30,7 @@ var workflow = new Sequence
new If
{
// If aged 18 or up, beer is provided, soda otherwise.
Condition = new Input<bool>(context => ageVariable.Get<int>(context) < 18),
Condition = new (context => ageVariable.Get<int>(context) < 18),
Then = new WriteLine("Enjoy your soda!"),
Else = new WriteLine("Enjoy your beer!")
},

View file

@ -0,0 +1,28 @@
using Elsa.Extensions;
using Elsa.Workflows.Core.Models;
namespace Elsa.Samples.CustomActivities.Activities;
/// <summary>
/// Sums two numbers.
/// </summary>
public class Sum : CodeActivity<int>
{
public Sum(Variable<int> a, Variable<int> b, Variable<int> result)
{
A = new(a);
B = new(b);
Result = new(result);
}
public Input<int> A { get; set; } = default!;
public Input<int> B { get; set; } = default!;
protected override void Execute(ActivityExecutionContext context)
{
var input1 = A.Get(context);
var input2 = B.Get(context);
var result = input1 + input2;
context.SetResult(result);
}
}

View file

@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\bundles\Elsa\Elsa.csproj" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,43 @@
using Elsa.Extensions;
using Elsa.Samples.CustomActivities.Activities;
using Elsa.Workflows.Core.Activities;
using Elsa.Workflows.Core.Contracts;
using Elsa.Workflows.Core.Models;
using Microsoft.Extensions.DependencyInjection;
// Setup service container.
var services = new ServiceCollection();
// Add Elsa services.
services.AddElsa();
// Build service container.
var serviceProvider = services.BuildServiceProvider();
// Resolve a workflow runner to run the workflow.
var workflowRunner = serviceProvider.GetRequiredService<IWorkflowRunner>();
// Create a workflow.
var a = new Variable<int>();
var b = new Variable<int>();
var sum = new Variable<int>();
var workflow = new Workflow
{
Root = new Sequence
{
Variables = {a, b, sum},
Activities =
{
new WriteLine("Enter first value"),
new ReadLine(a),
new WriteLine("Enter second value"),
new ReadLine(b),
new Sum(a, b, sum),
new WriteLine(context => $"The sum of {a.Get(context)} and {b.Get(context)} is {sum.Get(context)}")
}
}
};
// Run the workflow.
await workflowRunner.RunAsync(workflow);

View file

@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\bundles\Elsa\Elsa.csproj" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,51 @@
using Elsa.Extensions;
using Elsa.Workflows.Core.Activities;
using Elsa.Workflows.Core.Activities.Flowchart.Activities;
using Elsa.Workflows.Core.Activities.Flowchart.Models;
using Elsa.Workflows.Core.Contracts;
using Elsa.Workflows.Core.Models;
using Microsoft.Extensions.DependencyInjection;
// Setup service container.
var services = new ServiceCollection();
// Add Elsa services.
services.AddElsa();
// Build service container.
var serviceProvider = services.BuildServiceProvider();
// Define a workflow variable to capture the output of the ReadLine activity.
var nameVariable = new Variable<string>();
// Create a workflow.
var writeLine1 = new WriteLine("Please tell me your name:");
var writeLine2 = new ReadLine(nameVariable);
var writeLine3 = new WriteLine(context => $"Nice to meet you, {nameVariable.Get(context)}!");
var workflow = new Flowchart
{
// Register the name variable.
Variables = { nameVariable },
// Add the activities.
Activities =
{
writeLine1,
writeLine2,
writeLine3
},
// Setup the connections between activities.
Connections =
{
new Connection(writeLine1, writeLine2),
new Connection(writeLine2, writeLine3)
}
};
// Resolve a workflow runner to run the workflow.
var workflowRunner = serviceProvider.GetRequiredService<IWorkflowRunner>();
// Run the workflow.
await workflowRunner.RunAsync(workflow);

View file

@ -15,11 +15,14 @@ var serviceProvider = services.BuildServiceProvider();
// Resolve a workflow runner to run the workflow.
var workflowRunner = serviceProvider.GetRequiredService<IWorkflowRunner>();
// Run the Hello workflow.
await workflowRunner.RunUntilEndAsync(HelloWorkflow.Create());
// Run the For workflow.
await workflowRunner.RunAsync(ForWorkflow.Create());
await workflowRunner.RunUntilEndAsync(ForWorkflow.Create());
// Run the For Each workflow.
await workflowRunner.RunAsync(ForEachWorkflow.Create());
await workflowRunner.RunUntilEndAsync(ForEachWorkflow.Create());
// Run the While workflow.
await workflowRunner.RunAsync(WhileWorkflow.Create());
await workflowRunner.RunUntilEndAsync(WhileWorkflow.Create());

View file

@ -27,13 +27,7 @@ public static class ForEachWorkflow
Activities =
{
new WriteLine(context => $"- [ ] {currentValueVariable.Get(context)}"),
new Delay
{
// The strategy determines whether the workflow should suspend or wait synchronously.
// For console applications without a host, long-running workflows aren't supported, so we use the Blocking strategy.
Strategy = new(DelayBlockingStrategy.Blocking),
TimeSpan = new Input<TimeSpan>(TimeSpan.FromMilliseconds(250))
}
new Delay(TimeSpan.FromMilliseconds(250))
}
}
},

View file

@ -26,13 +26,7 @@ public static class ForWorkflow
Activities =
{
new WriteLine(context => $"Current value: {currentValueVariable.Get(context)}"),
new Delay
{
// The strategy determines whether the workflow should suspend or wait synchronously.
// For console applications without a host, long-running workflows aren't supported, so we use the Blocking strategy.
Strategy = new(DelayBlockingStrategy.Blocking),
TimeSpan = new Input<TimeSpan>(TimeSpan.FromSeconds(1))
}
new Delay(TimeSpan.FromSeconds(1))
}
}
},

View file

@ -0,0 +1,24 @@
using Elsa.Workflows.Core.Activities;
using Elsa.Workflows.Core.Contracts;
using Elsa.Workflows.Core.Models;
namespace Elsa.Samples.LoopingWorkflows.Workflows;
/// <summary>
/// Demonstrates dynamic workflow creation.
/// </summary>
public static class HelloWorkflow
{
public static IActivity Create()
{
var messages = new[] { "Hello, world!", "Goodbye, cruel world..." };
return new Workflow
{
Root = new Sequence
{
Activities = messages.Select(x => new WriteLine(x)).ToArray()
}
};
}
}

View file

@ -27,11 +27,11 @@ Console.WriteLine("Result: {0}", result1.Result);
// AddInputsWorkflow:
Console.WriteLine("=== AddInputsWorkflow ===");
var input = new { a = 5.6, b = 9.4 }.ToDictionary();
var result2 = await workflowRunner.RunAsync<AddInputsWorkflow, float>(new RunWorkflowOptions(Input: input));
var result2 = await workflowRunner.RunAsync<AddInputsWorkflow, float>(new RunWorkflowOptions(input: input));
Console.WriteLine("Result: {0}", result2);
// SumInputsWorkflow:
Console.WriteLine("=== SumInputsWorkflow ===");
input = new { numbers = new[]{ 9.6f, 2.5f, 1.2f, 4.8f } }.ToDictionary();
var result3 = await workflowRunner.RunAsync<SumInputsWorkflow, float>(new RunWorkflowOptions(Input: input));
var result3 = await workflowRunner.RunAsync<SumInputsWorkflow, float>(new RunWorkflowOptions(input: input));
Console.WriteLine("Result: {0}", result3);

View file

@ -0,0 +1,18 @@
FROM mcr.microsoft.com/dotnet/runtime:7.0 AS base
WORKDIR /app
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src
COPY ["src/samples/console/Elsa.Samples.WorkflowInput/Elsa.Samples.WorkflowInput.csproj", "src/samples/console/Elsa.Samples.WorkflowInput/"]
RUN dotnet restore "src/samples/console/Elsa.Samples.WorkflowInput/Elsa.Samples.WorkflowInput.csproj"
COPY . .
WORKDIR "/src/src/samples/console/Elsa.Samples.WorkflowInput"
RUN dotnet build "Elsa.Samples.WorkflowInput.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Elsa.Samples.WorkflowInput.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Elsa.Samples.WorkflowInput.dll"]

View file

@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\bundles\Elsa\Elsa.csproj" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,40 @@
using Elsa.Extensions;
using Elsa.Workflows.Core.Activities;
using Elsa.Workflows.Core.Contracts;
using Microsoft.Extensions.DependencyInjection;
// Setup service container.
var services = new ServiceCollection();
// Add Elsa services.
services.AddElsa();
// Build service container.
var serviceProvider = services.BuildServiceProvider();
// Create a workflow that echoes some message it receives via input.
var workflow = new WriteLine(context => $"Echo: {context.GetInput<string>("Message")}!");
// Resolve a workflow runner to run the workflow.
var workflowRunner = serviceProvider.GetRequiredService<IWorkflowRunner>();
// Create an input dictionary.
var input = new Dictionary<string, object>
{
["Message"] = "Hello World!"
};
// Run the workflow and provide the input.
await workflowRunner.RunAsync(workflow, new RunWorkflowOptions(input: input));
// Create a workflow that returns some output.
var workflow2 = new Inline(context => context.WorkflowExecutionContext.Output["Message"] = "Hello from workflow!");
// Run the workflow and hold on to its workflow state.
var result = await workflowRunner.RunAsync(workflow2);
// Get the output.
var output = result.WorkflowState.Output["Message"];
Console.WriteLine($"Output: {output}");