Skip to content

Commit 6dcdd70

Browse files
committed
while
1 parent 7907702 commit 6dcdd70

File tree

7 files changed

+112
-1
lines changed

7 files changed

+112
-1
lines changed

WorkflowCore.Sample10/Program.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
3+
namespace WorkflowCore.Sample10
4+
{
5+
class Program
6+
{
7+
static void Main(string[] args)
8+
{
9+
Console.WriteLine("Hello World!");
10+
}
11+
}
12+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp1.1</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="1.1.0" />
10+
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="1.1.0" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<ProjectReference Include="..\src\providers\WorkflowCore.Persistence.MongoDB\WorkflowCore.Persistence.MongoDB.csproj" />
15+
<ProjectReference Include="..\src\WorkflowCore\WorkflowCore.csproj" />
16+
</ItemGroup>
17+
18+
</Project>

WorkflowCore.sln

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 15
4-
VisualStudioVersion = 15.0.26403.7
4+
VisualStudioVersion = 15.0.26228.4
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{EF47161E-E399-451C-BDE8-E92AAD3BD761}"
77
EndProject
@@ -76,6 +76,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WorkflowCore.WebAPI", "src\
7676
EndProject
7777
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WorkflowCore.Sample09", "WorkflowCore.Sample09\WorkflowCore.Sample09.csproj", "{CFFE642C-E9ED-4662-B5BB-6586A06BC58E}"
7878
EndProject
79+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WorkflowCore.Sample10", "WorkflowCore.Sample10\WorkflowCore.Sample10.csproj", "{334DECAC-6CFC-4E0E-AC09-6AD12006A250}"
80+
EndProject
7981
Global
8082
GlobalSection(SolutionConfigurationPlatforms) = preSolution
8183
Debug|Any CPU = Debug|Any CPU
@@ -198,6 +200,10 @@ Global
198200
{CFFE642C-E9ED-4662-B5BB-6586A06BC58E}.Debug|Any CPU.Build.0 = Debug|Any CPU
199201
{CFFE642C-E9ED-4662-B5BB-6586A06BC58E}.Release|Any CPU.ActiveCfg = Release|Any CPU
200202
{CFFE642C-E9ED-4662-B5BB-6586A06BC58E}.Release|Any CPU.Build.0 = Release|Any CPU
203+
{334DECAC-6CFC-4E0E-AC09-6AD12006A250}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
204+
{334DECAC-6CFC-4E0E-AC09-6AD12006A250}.Debug|Any CPU.Build.0 = Debug|Any CPU
205+
{334DECAC-6CFC-4E0E-AC09-6AD12006A250}.Release|Any CPU.ActiveCfg = Release|Any CPU
206+
{334DECAC-6CFC-4E0E-AC09-6AD12006A250}.Release|Any CPU.Build.0 = Release|Any CPU
201207
EndGlobalSection
202208
GlobalSection(SolutionProperties) = preSolution
203209
HideSolutionNode = FALSE
@@ -235,5 +241,6 @@ Global
235241
{ED5074AF-A09E-4357-A419-FE3476C0FAE7} = {5080DB09-CBE8-4C45-9957-C3BB7651755E}
236242
{FBF8D151-A3BF-4EB3-8F80-D71618696362} = {6803696C-B19A-4B27-9193-082A02B6F205}
237243
{CFFE642C-E9ED-4662-B5BB-6586A06BC58E} = {5080DB09-CBE8-4C45-9957-C3BB7651755E}
244+
{334DECAC-6CFC-4E0E-AC09-6AD12006A250} = {5080DB09-CBE8-4C45-9957-C3BB7651755E}
238245
EndGlobalSection
239246
EndGlobal

src/WorkflowCore/Interface/IStepBuilder.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,5 +88,7 @@ public interface IStepBuilder<TData, TStepBody>
8888
IStepBuilder<TData, TStepBody> EndWorkflow();
8989

9090
IParentStepBuilder<TData, Foreach> ForEach(Expression<Func<TData, IEnumerable>> collection);
91+
92+
IParentStepBuilder<TData, While> While(Expression<Func<TData, bool>> condition);
9193
}
9294
}

src/WorkflowCore/Models/While.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System;
2+
using System.Linq;
3+
using System.Collections;
4+
using System.Collections.Generic;
5+
using System.Linq.Expressions;
6+
using System.Text;
7+
using WorkflowCore.Interface;
8+
9+
namespace WorkflowCore.Models
10+
{
11+
public class While : StepBody
12+
{
13+
public bool ConditionResult { get; set; }
14+
15+
public override ExecutionResult Run(IStepExecutionContext context)
16+
{
17+
if (ConditionResult)
18+
{
19+
return ExecutionResult.Branch(new List<object>() { null }, null);
20+
}
21+
22+
bool complete = true;
23+
foreach (var childId in context.ExecutionPointer.Children)
24+
complete = complete && IsBranchComplete(context.Workflow.ExecutionPointers, childId);
25+
26+
if (complete)
27+
return ExecutionResult.Next();
28+
29+
return ExecutionResult.Persist(context.PersistenceData);
30+
}
31+
32+
private bool IsBranchComplete(IEnumerable<ExecutionPointer> pointers, string rootId)
33+
{
34+
var root = pointers.First(x => x.Id == rootId);
35+
36+
if (root.EndTime == null)
37+
return false;
38+
39+
var list = pointers.Where(x => x.PredecessorId == rootId).ToList();
40+
41+
bool result = true;
42+
43+
foreach (var item in list)
44+
result = result && IsBranchComplete(pointers, item.Id);
45+
46+
return result;
47+
}
48+
49+
}
50+
}

src/WorkflowCore/Services/StepBuilder.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,27 @@ public IParentStepBuilder<TData, Foreach> ForEach(Expression<Func<TData, IEnumer
170170
return stepBuilder;
171171
}
172172

173+
public IParentStepBuilder<TData, While> While(Expression<Func<TData, bool>> condition)
174+
{
175+
var newStep = new WorkflowStep<While>();
176+
177+
Expression<Func<While, bool>> inputExpr = (x => x.ConditionResult);
178+
179+
var mapping = new DataMapping()
180+
{
181+
Source = condition,
182+
Target = inputExpr
183+
};
184+
newStep.Inputs.Add(mapping);
185+
186+
WorkflowBuilder.AddStep(newStep);
187+
var stepBuilder = new StepBuilder<TData, While>(WorkflowBuilder, newStep);
188+
189+
Step.Outcomes.Add(new StepOutcome() { NextStep = newStep.Id });
190+
191+
return stepBuilder;
192+
}
193+
173194
public IStepBuilder<TData, TStepBody> Do(Action<IWorkflowBuilder<TData>> builder)
174195
{
175196
builder.Invoke(WorkflowBuilder);

src/WorkflowCore/Services/WorkflowExecutor.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ public WorkflowExecutorResult Execute(WorkflowInstance workflow, WorkflowOptions
152152

153153
private void ProcessExecutionResult(WorkflowInstance workflow, WorkflowDefinition def, ExecutionPointer pointer, WorkflowStep step, ExecutionResult result)
154154
{
155+
//TODO: refactor this into it's own class
155156
pointer.PersistenceData = result.PersistenceData;
156157
if (result.SleepFor.HasValue)
157158
pointer.SleepUntil = DateTime.Now.ToUniversalTime().Add(result.SleepFor.Value);

0 commit comments

Comments
 (0)