-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add data processing sample (Sample25)
- Loading branch information
1 parent
00e6df0
commit 4705c9b
Showing
10 changed files
with
1,395 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Elsa.Expressions; | ||
using Elsa.Results; | ||
using Elsa.Services; | ||
using Elsa.Services.Models; | ||
|
||
namespace Sample25.Activities | ||
{ | ||
public class Absolute : Activity | ||
{ | ||
public WorkflowExpression<double> ValueExpression | ||
{ | ||
get => GetState<WorkflowExpression<double>>(); | ||
set => SetState(value); | ||
} | ||
|
||
protected override async Task<ActivityExecutionResult> OnExecuteAsync(WorkflowExecutionContext context, CancellationToken cancellationToken) | ||
{ | ||
var value = await context.EvaluateAsync(ValueExpression, cancellationToken); | ||
var result = Math.Abs(value); | ||
|
||
Output.SetVariable("Result", result); | ||
return Done(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Elsa; | ||
using Elsa.Activities.ControlFlow.Activities; | ||
using Elsa.Expressions; | ||
using Elsa.Results; | ||
using Elsa.Services.Models; | ||
|
||
namespace Sample25.Activities | ||
{ | ||
public abstract class ArithmeticOperation : Join | ||
{ | ||
public ArithmeticOperation() | ||
{ | ||
Mode = JoinMode.WaitAll; | ||
} | ||
|
||
public WorkflowExpression<double[]> Values | ||
{ | ||
get => GetState<WorkflowExpression<double[]>>(); | ||
set => SetState(value); | ||
} | ||
|
||
protected override async Task<ActivityExecutionResult> OnExecuteAsync(WorkflowExecutionContext context, CancellationToken cancellationToken) | ||
{ | ||
var activityExecutionResult = await base.OnExecuteAsync(context, cancellationToken); | ||
|
||
if (IsCompleted(activityExecutionResult)) | ||
{ | ||
var values = await context.EvaluateAsync(Values, cancellationToken); | ||
var result = Calculate(values); | ||
|
||
Output.SetVariable("Result", result); | ||
} | ||
|
||
return activityExecutionResult; | ||
} | ||
|
||
protected abstract double Calculate(params double[] values); | ||
|
||
private bool IsCompleted(IActivityExecutionResult result) => result is OutcomeResult outcome && outcome.EndpointNames.Any(x => x == OutcomeNames.Done); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using Elsa.Results; | ||
using Elsa.Services; | ||
using Elsa.Services.Models; | ||
|
||
namespace Sample25.Activities | ||
{ | ||
/// <summary> | ||
/// Produces a single value. | ||
/// </summary> | ||
public class Channel : Activity | ||
{ | ||
/// <summary> | ||
/// The ID of the sensor to observe. | ||
/// </summary> | ||
public string SensorId | ||
{ | ||
get => GetState<string>(); | ||
set => SetState(value); | ||
} | ||
|
||
// Execute only if we received data from the sensor being observed. | ||
protected override bool OnCanExecute(WorkflowExecutionContext context) => context.Workflow.Input.ContainsKey(SensorId); | ||
|
||
// Halt workflow execution until sensor data is received. | ||
protected override ActivityExecutionResult OnExecute(WorkflowExecutionContext context) => Halt(); | ||
|
||
protected override ActivityExecutionResult OnResume(WorkflowExecutionContext context) | ||
{ | ||
// Read sensor output provided as workflow input. | ||
var value = context.Workflow.Input.GetVariable<double>(SensorId); | ||
|
||
// Set the value as an output of this activity. | ||
Output.SetVariable("Value", value); | ||
return Done(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System.Linq; | ||
|
||
namespace Sample25.Activities | ||
{ | ||
/// <summary> | ||
/// Subtracts two incoming inputs | ||
/// </summary> | ||
public class Subtract : ArithmeticOperation | ||
{ | ||
protected override double Calculate(params double[] values) => values.Aggregate((left, right) => left - right); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using Elsa; | ||
using Elsa.Activities.Console.Activities; | ||
using Elsa.Activities.ControlFlow.Activities; | ||
using Elsa.Expressions; | ||
using Elsa.Scripting.JavaScript; | ||
using Elsa.Services; | ||
using Elsa.Services.Models; | ||
using Sample25.Activities; | ||
|
||
namespace Sample25 | ||
{ | ||
public class DataProcessingWorkflow : IWorkflow | ||
{ | ||
public void Build(IWorkflowBuilder builder) | ||
{ | ||
builder | ||
.StartWith<WriteLine>(x => x.TextExpression = new LiteralExpression("Waiting for sensor input.")) | ||
|
||
// Fork execution into two branches to wait for external stimuli from the two channels in parallel. | ||
.Then<Fork>( | ||
fork => fork.Branches = new[] { "Channel 1", "Channel 2" }, | ||
fork => | ||
{ | ||
fork | ||
.When("Channel 1") | ||
.Then<Channel>(x => x.SensorId = "Sensor1").WithName("Channel1") | ||
.Then("Subtract"); // Connect to Subtract activity. | ||
|
||
fork | ||
.When("Channel 2") | ||
.Then<Channel>(x => x.SensorId = "Sensor2").WithName("Channel2") | ||
.Then("Subtract"); // Connect to Subtract activity. | ||
}) | ||
|
||
// Subtract the specified values. | ||
.Then<Subtract>(x => x.Values = new JavaScriptExpression<double[]>("[Channel1.Value, Channel2.Value]")).WithName("Subtract") | ||
|
||
// Calculate the absolute value of the subtraction. | ||
.Then<Absolute>(x => x.ValueExpression = new JavaScriptExpression<double>("(Subtract.Result)")).WithName("Absolute") | ||
|
||
// Compare the absolute value against a constant threshold, and write the appropriate output. | ||
.Then<IfElse>( | ||
x => x.ConditionExpression = new JavaScriptExpression<bool>("(Absolute.Result) > 0.5"), | ||
ifElse => | ||
{ | ||
ifElse | ||
.When(OutcomeNames.False) | ||
.Then<WriteLine>(x => x.TextExpression = new LiteralExpression("Data does not exceed threshold (FALSE)")); | ||
|
||
ifElse | ||
.When(OutcomeNames.True) | ||
.Then<WriteLine>(x => x.TextExpression = new LiteralExpression("Data exceeds threshold (TRUE)")); | ||
}) | ||
.Then<WriteLine>(x => x.TextExpression = new JavaScriptExpression<string>("(`Finished data processing. Result: ${Absolute.Result}`)")); | ||
} | ||
} | ||
} |
Oops, something went wrong.