├── .gitattributes ├── .gitignore ├── LICENSE ├── README.md └── Rabbit.Workflow ├── Providers └── Workflow.Provider.Memory │ ├── ActivityDefinitionService.cs │ ├── Properties │ └── AssemblyInfo.cs │ └── Workflow.Provider.Memory.csproj ├── Rabbit.Workflow.sln ├── Shell ├── App.config ├── Program.cs ├── Properties │ └── AssemblyInfo.cs └── Shell.csproj └── Workflow.Engine ├── ActivityContext.cs ├── CancellationToken.cs ├── IActivity.cs ├── Models ├── ActivityDefinition.cs ├── ActivityStateEnum.cs ├── AwaitingActivityDefinition.cs ├── TransitionDefinition.cs ├── WorkflowDefinition.cs ├── WorkflowInstance.cs └── WorkflowStateEnum.cs ├── Properties └── AssemblyInfo.cs ├── Services ├── ActivitiesManager.cs ├── IActivityDefinitionService.cs └── WorkflowManager.cs ├── Task.cs ├── Workflow.Engine.csproj └── WorkflowContext.cs /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | x64/ 19 | x86/ 20 | build/ 21 | bld/ 22 | [Bb]in/ 23 | [Oo]bj/ 24 | 25 | # Visual Studo 2015 cache/options directory 26 | .vs/ 27 | 28 | # MSTest test Results 29 | [Tt]est[Rr]esult*/ 30 | [Bb]uild[Ll]og.* 31 | 32 | # NUNIT 33 | *.VisualState.xml 34 | TestResult.xml 35 | 36 | # Build Results of an ATL Project 37 | [Dd]ebugPS/ 38 | [Rr]eleasePS/ 39 | dlldata.c 40 | 41 | *_i.c 42 | *_p.c 43 | *_i.h 44 | *.ilk 45 | *.meta 46 | *.obj 47 | *.pch 48 | *.pdb 49 | *.pgc 50 | *.pgd 51 | *.rsp 52 | *.sbr 53 | *.tlb 54 | *.tli 55 | *.tlh 56 | *.tmp 57 | *.tmp_proj 58 | *.log 59 | *.vspscc 60 | *.vssscc 61 | .builds 62 | *.pidb 63 | *.svclog 64 | *.scc 65 | 66 | # Chutzpah Test files 67 | _Chutzpah* 68 | 69 | # Visual C++ cache files 70 | ipch/ 71 | *.aps 72 | *.ncb 73 | *.opensdf 74 | *.sdf 75 | *.cachefile 76 | 77 | # Visual Studio profiler 78 | *.psess 79 | *.vsp 80 | *.vspx 81 | 82 | # TFS 2012 Local Workspace 83 | $tf/ 84 | 85 | # Guidance Automation Toolkit 86 | *.gpState 87 | 88 | # ReSharper is a .NET coding add-in 89 | _ReSharper*/ 90 | *.[Rr]e[Ss]harper 91 | *.DotSettings.user 92 | 93 | # JustCode is a .NET coding addin-in 94 | .JustCode 95 | 96 | # TeamCity is a build add-in 97 | _TeamCity* 98 | 99 | # DotCover is a Code Coverage Tool 100 | *.dotCover 101 | 102 | # NCrunch 103 | _NCrunch_* 104 | .*crunch*.local.xml 105 | 106 | # MightyMoose 107 | *.mm.* 108 | AutoTest.Net/ 109 | 110 | # Web workbench (sass) 111 | .sass-cache/ 112 | 113 | # Installshield output folder 114 | [Ee]xpress/ 115 | 116 | # DocProject is a documentation generator add-in 117 | DocProject/buildhelp/ 118 | DocProject/Help/*.HxT 119 | DocProject/Help/*.HxC 120 | DocProject/Help/*.hhc 121 | DocProject/Help/*.hhk 122 | DocProject/Help/*.hhp 123 | DocProject/Help/Html2 124 | DocProject/Help/html 125 | 126 | # Click-Once directory 127 | publish/ 128 | 129 | # Publish Web Output 130 | *.[Pp]ublish.xml 131 | *.azurePubxml 132 | # TODO: Comment the next line if you want to checkin your web deploy settings 133 | # but database connection strings (with potential passwords) will be unencrypted 134 | *.pubxml 135 | *.publishproj 136 | 137 | # NuGet Packages 138 | *.nupkg 139 | # The packages folder can be ignored because of Package Restore 140 | **/packages/* 141 | # except build/, which is used as an MSBuild target. 142 | !**/packages/build/ 143 | # Uncomment if necessary however generally it will be regenerated when needed 144 | #!**/packages/repositories.config 145 | 146 | # Windows Azure Build Output 147 | csx/ 148 | *.build.csdef 149 | 150 | # Windows Store app package directory 151 | AppPackages/ 152 | 153 | # Others 154 | *.[Cc]ache 155 | ClientBin/ 156 | [Ss]tyle[Cc]op.* 157 | ~$* 158 | *~ 159 | *.dbmdl 160 | *.dbproj.schemaview 161 | *.pfx 162 | *.publishsettings 163 | node_modules/ 164 | bower_components/ 165 | 166 | # RIA/Silverlight projects 167 | Generated_Code/ 168 | 169 | # Backup & report files from converting an old project file 170 | # to a newer Visual Studio version. Backup files are not needed, 171 | # because we have git ;-) 172 | _UpgradeReport_Files/ 173 | Backup*/ 174 | UpgradeLog*.XML 175 | UpgradeLog*.htm 176 | 177 | # SQL Server files 178 | *.mdf 179 | *.ldf 180 | 181 | # Business Intelligence projects 182 | *.rdl.data 183 | *.bim.layout 184 | *.bim_*.settings 185 | 186 | # Microsoft Fakes 187 | FakesAssemblies/ 188 | 189 | # Node.js Tools for Visual Studio 190 | .ntvs_analysis.dat 191 | 192 | # Visual Studio 6 build log 193 | *.plg 194 | 195 | # Visual Studio 6 workspace options file 196 | *.opt 197 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Workflow 2 | Workflow 3 | -------------------------------------------------------------------------------- /Rabbit.Workflow/Providers/Workflow.Provider.Memory/ActivityDefinitionService.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq; 3 | using Workflow.Engine.Models; 4 | using Workflow.Engine.Services; 5 | 6 | namespace Workflow.Provider.Memory 7 | { 8 | public sealed class ActivityDefinitionService : IActivityDefinitionService 9 | { 10 | private static readonly List WorkflowDefinitions = new List(); 11 | 12 | static ActivityDefinitionService() 13 | { 14 | var workflowDefinition = new WorkflowDefinition 15 | { 16 | Name = "流程1" 17 | }; 18 | var definition2 = new ActivityDefinition 19 | { 20 | Name = "Task2", 21 | Start = false, 22 | // State = null, 23 | WorkflowDefinition = workflowDefinition 24 | }; 25 | var definition = new ActivityDefinition 26 | { 27 | Name = "Task1", 28 | Start = true, 29 | // State = null, 30 | WorkflowDefinition = workflowDefinition 31 | }; 32 | workflowDefinition.Activitys = new[] { definition, definition2 }; 33 | workflowDefinition.Transitions = new List 34 | { 35 | new TransitionDefinition 36 | { 37 | SourceActivity = definition, 38 | DestinationActivity = definition2, 39 | WorkflowDefinition = workflowDefinition 40 | } 41 | }; 42 | 43 | WorkflowDefinitions.Add(workflowDefinition); 44 | } 45 | 46 | #region Implementation of IActivityDefinitionService 47 | 48 | public IEnumerable GetStartActivityDefinitions(string name) 49 | { 50 | return WorkflowDefinitions.SelectMany(i => i.Activitys.Where(z => z.Start && z.Name == name)).ToArray(); 51 | } 52 | 53 | public IEnumerable GetAwaitingActivityDefinitions(string name) 54 | { 55 | /*return new[] 56 | { 57 | new AwaitingActivityDefinition 58 | { 59 | ActivityDefinition = new ActivityDefinition(), 60 | WorkflowInstance = new WorkflowInstance() 61 | } 62 | }*/ 63 | return new AwaitingActivityDefinition[0]; 64 | } 65 | 66 | #endregion Implementation of IActivityDefinitionService 67 | } 68 | } -------------------------------------------------------------------------------- /Rabbit.Workflow/Providers/Workflow.Provider.Memory/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.InteropServices; 3 | 4 | // 有关程序集的一般信息由以下 5 | // 控制。更改这些特性值可修改 6 | // 与程序集关联的信息。 7 | [assembly: AssemblyTitle("Workflow.Provider.Memory")] 8 | [assembly: AssemblyDescription("")] 9 | [assembly: AssemblyConfiguration("")] 10 | [assembly: AssemblyCompany("")] 11 | [assembly: AssemblyProduct("Workflow.Provider.Memory")] 12 | [assembly: AssemblyCopyright("Copyright © 2015")] 13 | [assembly: AssemblyTrademark("")] 14 | [assembly: AssemblyCulture("")] 15 | 16 | //将 ComVisible 设置为 false 将使此程序集中的类型 17 | //对 COM 组件不可见。 如果需要从 COM 访问此程序集中的类型, 18 | //请将此类型的 ComVisible 特性设置为 true。 19 | [assembly: ComVisible(false)] 20 | 21 | // 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID 22 | [assembly: Guid("d95bfa73-8918-4489-8602-079a6c75dea9")] 23 | 24 | // 程序集的版本信息由下列四个值组成: 25 | // 26 | // 主版本 27 | // 次版本 28 | // 生成号 29 | // 修订号 30 | // 31 | //可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值, 32 | // 方法是按如下所示使用“*”: : 33 | // [assembly: AssemblyVersion("1.0.*")] 34 | [assembly: AssemblyVersion("1.0.0.0")] 35 | [assembly: AssemblyFileVersion("1.0.0.0")] -------------------------------------------------------------------------------- /Rabbit.Workflow/Providers/Workflow.Provider.Memory/Workflow.Provider.Memory.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {D95BFA73-8918-4489-8602-079A6C75DEA9} 8 | Library 9 | Properties 10 | Workflow.Provider.Memory 11 | Workflow.Provider.Memory 12 | v4.5 13 | 512 14 | 15 | 16 | true 17 | full 18 | false 19 | bin\Debug\ 20 | DEBUG;TRACE 21 | prompt 22 | 4 23 | 24 | 25 | pdbonly 26 | true 27 | bin\Release\ 28 | TRACE 29 | prompt 30 | 4 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | {72827388-3b0c-4328-a534-7ce657dff62a} 48 | Workflow.Engine 49 | 50 | 51 | 52 | 59 | -------------------------------------------------------------------------------- /Rabbit.Workflow/Rabbit.Workflow.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 14 4 | VisualStudioVersion = 14.0.23107.0 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Workflow.Engine", "Workflow.Engine\Workflow.Engine.csproj", "{72827388-3B0C-4328-A534-7CE657DFF62A}" 7 | EndProject 8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Shell", "Shell\Shell.csproj", "{D78E8713-EE35-489D-A02D-D257BC699D1C}" 9 | EndProject 10 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Providers", "Providers", "{C685C73C-853C-4F0E-A5A7-60CE8AEE6310}" 11 | EndProject 12 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Workflow.Provider.Memory", "Providers\Workflow.Provider.Memory\Workflow.Provider.Memory.csproj", "{D95BFA73-8918-4489-8602-079A6C75DEA9}" 13 | EndProject 14 | Global 15 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 16 | Debug|Any CPU = Debug|Any CPU 17 | Release|Any CPU = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 20 | {72827388-3B0C-4328-A534-7CE657DFF62A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 21 | {72827388-3B0C-4328-A534-7CE657DFF62A}.Debug|Any CPU.Build.0 = Debug|Any CPU 22 | {72827388-3B0C-4328-A534-7CE657DFF62A}.Release|Any CPU.ActiveCfg = Release|Any CPU 23 | {72827388-3B0C-4328-A534-7CE657DFF62A}.Release|Any CPU.Build.0 = Release|Any CPU 24 | {D78E8713-EE35-489D-A02D-D257BC699D1C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 25 | {D78E8713-EE35-489D-A02D-D257BC699D1C}.Debug|Any CPU.Build.0 = Debug|Any CPU 26 | {D78E8713-EE35-489D-A02D-D257BC699D1C}.Release|Any CPU.ActiveCfg = Release|Any CPU 27 | {D78E8713-EE35-489D-A02D-D257BC699D1C}.Release|Any CPU.Build.0 = Release|Any CPU 28 | {D95BFA73-8918-4489-8602-079A6C75DEA9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 29 | {D95BFA73-8918-4489-8602-079A6C75DEA9}.Debug|Any CPU.Build.0 = Debug|Any CPU 30 | {D95BFA73-8918-4489-8602-079A6C75DEA9}.Release|Any CPU.ActiveCfg = Release|Any CPU 31 | {D95BFA73-8918-4489-8602-079A6C75DEA9}.Release|Any CPU.Build.0 = Release|Any CPU 32 | EndGlobalSection 33 | GlobalSection(SolutionProperties) = preSolution 34 | HideSolutionNode = FALSE 35 | EndGlobalSection 36 | GlobalSection(NestedProjects) = preSolution 37 | {D95BFA73-8918-4489-8602-079A6C75DEA9} = {C685C73C-853C-4F0E-A5A7-60CE8AEE6310} 38 | EndGlobalSection 39 | EndGlobal 40 | -------------------------------------------------------------------------------- /Rabbit.Workflow/Shell/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Rabbit.Workflow/Shell/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using Workflow.Engine; 4 | using Workflow.Engine.Services; 5 | using Workflow.Provider.Memory; 6 | 7 | namespace Shell 8 | { 9 | internal class Task1 : Task 10 | { 11 | #region Implementation of IActivity 12 | 13 | /// 14 | /// 名称。 15 | /// 16 | public override string Name => "Task1"; 17 | 18 | /// 19 | /// 分类。 20 | /// 21 | public override string Category => "Default"; 22 | 23 | /// 24 | /// 描述。 25 | /// 26 | public override string Description { get; } 27 | 28 | /// 29 | /// 执行活动。 30 | /// 31 | /// 工作流上下文。 32 | /// 活动上下文。 33 | /// 接下来需要执行的动作。 34 | public override IEnumerable Execute(WorkflowContext workflowContext, ActivityContext activityContext) 35 | { 36 | Console.WriteLine(GetType().FullName); 37 | return new[] { "Task2" }; 38 | } 39 | 40 | #endregion Implementation of IActivity 41 | } 42 | 43 | internal class Task2 : Task 44 | { 45 | #region Implementation of IActivity 46 | 47 | /// 48 | /// 名称。 49 | /// 50 | public override string Name => "Task2"; 51 | 52 | /// 53 | /// 分类。 54 | /// 55 | public override string Category => "Default"; 56 | 57 | /// 58 | /// 描述。 59 | /// 60 | public override string Description { get; } 61 | 62 | /// 63 | /// 执行活动。 64 | /// 65 | /// 工作流上下文。 66 | /// 活动上下文。 67 | /// 接下来需要执行的动作。 68 | public override IEnumerable Execute(WorkflowContext workflowContext, ActivityContext activityContext) 69 | { 70 | Console.WriteLine(GetType().FullName); 71 | return new string[0]; 72 | } 73 | 74 | #endregion Implementation of IActivity 75 | } 76 | 77 | internal class Program 78 | { 79 | private static void Main(string[] args) 80 | { 81 | IActivitiesManager activitiesManager = new ActivitiesManager(new IActivity[] { new Task1(), new Task2() }); 82 | IActivityDefinitionService activityDefinitionService = new ActivityDefinitionService(); 83 | IWorkflowManager workflowManager = new WorkflowManager(activitiesManager, activityDefinitionService); 84 | workflowManager.TriggerEvent("Task1", () => new Dictionary()); 85 | } 86 | } 87 | } -------------------------------------------------------------------------------- /Rabbit.Workflow/Shell/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.InteropServices; 3 | 4 | // 有关程序集的一般信息由以下 5 | // 控制。更改这些特性值可修改 6 | // 与程序集关联的信息。 7 | [assembly: AssemblyTitle("Shell")] 8 | [assembly: AssemblyDescription("")] 9 | [assembly: AssemblyConfiguration("")] 10 | [assembly: AssemblyCompany("")] 11 | [assembly: AssemblyProduct("Shell")] 12 | [assembly: AssemblyCopyright("Copyright © 2015")] 13 | [assembly: AssemblyTrademark("")] 14 | [assembly: AssemblyCulture("")] 15 | 16 | //将 ComVisible 设置为 false 将使此程序集中的类型 17 | //对 COM 组件不可见。 如果需要从 COM 访问此程序集中的类型, 18 | //请将此类型的 ComVisible 特性设置为 true。 19 | [assembly: ComVisible(false)] 20 | 21 | // 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID 22 | [assembly: Guid("d78e8713-ee35-489d-a02d-d257bc699d1c")] 23 | 24 | // 程序集的版本信息由下列四个值组成: 25 | // 26 | // 主版本 27 | // 次版本 28 | // 生成号 29 | // 修订号 30 | // 31 | //可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值, 32 | // 方法是按如下所示使用“*”: : 33 | // [assembly: AssemblyVersion("1.0.*")] 34 | [assembly: AssemblyVersion("1.0.0.0")] 35 | [assembly: AssemblyFileVersion("1.0.0.0")] -------------------------------------------------------------------------------- /Rabbit.Workflow/Shell/Shell.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {D78E8713-EE35-489D-A02D-D257BC699D1C} 8 | Exe 9 | Properties 10 | Shell 11 | Shell 12 | v4.5 13 | 512 14 | 15 | 16 | AnyCPU 17 | true 18 | full 19 | false 20 | bin\Debug\ 21 | DEBUG;TRACE 22 | prompt 23 | 4 24 | 25 | 26 | AnyCPU 27 | pdbonly 28 | true 29 | bin\Release\ 30 | TRACE 31 | prompt 32 | 4 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | {D95BFA73-8918-4489-8602-079A6C75DEA9} 53 | Workflow.Provider.Memory 54 | 55 | 56 | {72827388-3b0c-4328-a534-7ce657dff62a} 57 | Workflow.Engine 58 | 59 | 60 | 61 | 68 | -------------------------------------------------------------------------------- /Rabbit.Workflow/Workflow.Engine/ActivityContext.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Workflow.Engine 4 | { 5 | /// 6 | /// 活动上下文。 7 | /// 8 | public class ActivityContext 9 | { 10 | /// 11 | /// 活动实例。 12 | /// 13 | public IActivity Activity { get; set; } 14 | 15 | /// 16 | /// 上下文环境。 17 | /// 18 | public IDictionary Environment { get; set; } 19 | } 20 | } -------------------------------------------------------------------------------- /Rabbit.Workflow/Workflow.Engine/CancellationToken.cs: -------------------------------------------------------------------------------- 1 | namespace Workflow.Engine 2 | { 3 | /// 4 | /// 用于取消操作的令牌。 5 | /// 6 | public class CancellationToken 7 | { 8 | /// 9 | /// 取消操作。 10 | /// 11 | public void Cancel() 12 | { 13 | IsCancelled = true; 14 | } 15 | 16 | /// 17 | /// 是否取消。 18 | /// 19 | public bool IsCancelled { get; private set; } 20 | } 21 | } -------------------------------------------------------------------------------- /Rabbit.Workflow/Workflow.Engine/IActivity.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Workflow.Engine 4 | { 5 | /// 6 | /// 一个抽象的活动。 7 | /// 8 | public interface IActivity 9 | { 10 | /// 11 | /// 名称。 12 | /// 13 | string Name { get; } 14 | 15 | /// 16 | /// 分类。 17 | /// 18 | string Category { get; } 19 | 20 | /// 21 | /// 描述。 22 | /// 23 | string Description { get; } 24 | 25 | /// 26 | /// 是否可以执行。 27 | /// 28 | /// 工作流上下文。 29 | /// 活动上下文。 30 | /// 31 | bool CanExecute(WorkflowContext workflowContext, ActivityContext activityContext); 32 | 33 | /// 34 | /// 执行活动。 35 | /// 36 | /// 工作流上下文。 37 | /// 活动上下文。 38 | /// 接下来需要执行的动作。 39 | IEnumerable Execute(WorkflowContext workflowContext, ActivityContext activityContext); 40 | 41 | /// 42 | /// 工作流开始前执行。 43 | /// 44 | /// 工作流上下文。 45 | /// 可用于取消的令牌。 46 | void OnWorkflowStarting(WorkflowContext workflowContext, CancellationToken cancellationToken); 47 | 48 | /// 49 | /// 工作流开始之后执行。 50 | /// 51 | /// 工作流上下文。 52 | void OnWorkflowStarted(WorkflowContext workflowContext); 53 | 54 | /// 55 | /// 工作流恢复前执行。 56 | /// 57 | /// 工作流上下文。 58 | /// 可用于取消的令牌。 59 | void OnWorkflowResuming(WorkflowContext workflowContext, CancellationToken cancellationToken); 60 | 61 | /// 62 | /// 工作流恢复之后执行。 63 | /// 64 | /// 工作流上下文。 65 | void OnWorkflowResumed(WorkflowContext workflowContext); 66 | 67 | /// 68 | /// 活动执行前执行。 69 | /// 70 | /// 工作流上下文。 71 | /// 活动上下文。 72 | /// 可用于取消的令牌。 73 | void OnActivityExecuting(WorkflowContext workflowContext, ActivityContext activityContext, CancellationToken cancellationToken); 74 | 75 | /// 76 | /// 活动执行后执行。 77 | /// 78 | /// 工作流上下文。 79 | /// 活动上下文。 80 | void OnActivityExecuted(WorkflowContext workflowContext, ActivityContext activityContext); 81 | } 82 | } -------------------------------------------------------------------------------- /Rabbit.Workflow/Workflow.Engine/Models/ActivityDefinition.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Workflow.Engine.Models 4 | { 5 | /// 6 | /// 活动定义。 7 | /// 8 | public class ActivityDefinition 9 | { 10 | /// 11 | /// 活动名称。 12 | /// 13 | public string Name { get; set; } 14 | 15 | /// 16 | /// 活动环境。 17 | /// 18 | public IDictionary Environment { get; set; } 19 | 20 | /// 21 | /// 是否流程中起始的活动。 22 | /// 23 | public bool Start { get; set; } 24 | 25 | /// 26 | /// 工作流定义。 27 | /// 28 | public WorkflowDefinition WorkflowDefinition { get; set; } 29 | } 30 | } -------------------------------------------------------------------------------- /Rabbit.Workflow/Workflow.Engine/Models/ActivityStateEnum.cs: -------------------------------------------------------------------------------- 1 | namespace Workflow.Engine.Models 2 | { 3 | /// 4 | /// 活动状态枚举。 5 | /// 6 | public enum ActivityStateEnum 7 | { 8 | /// 9 | /// 未处理。 10 | /// 11 | Untreated = 0, 12 | 13 | /// 14 | /// 处理中。 15 | /// 16 | Processing = 1, 17 | 18 | /// 19 | /// 处理完成。 20 | /// 21 | Processed = 3 22 | } 23 | } -------------------------------------------------------------------------------- /Rabbit.Workflow/Workflow.Engine/Models/AwaitingActivityDefinition.cs: -------------------------------------------------------------------------------- 1 | namespace Workflow.Engine.Models 2 | { 3 | /// 4 | /// 等待执行的活动定义。 5 | /// 6 | public class AwaitingActivityDefinition 7 | { 8 | /// 9 | /// 活动定义。 10 | /// 11 | public ActivityDefinition ActivityDefinition { get; set; } 12 | 13 | /// 14 | /// 工作流实例。 15 | /// 16 | public WorkflowInstance WorkflowInstance { get; set; } 17 | 18 | /// 19 | /// 活动状态。 20 | /// 21 | public ActivityStateEnum State { get; set; } 22 | 23 | /// 24 | /// 设置状态为完成。 25 | /// 26 | public void Complete() 27 | { 28 | State = ActivityStateEnum.Processed; 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /Rabbit.Workflow/Workflow.Engine/Models/TransitionDefinition.cs: -------------------------------------------------------------------------------- 1 | namespace Workflow.Engine.Models 2 | { 3 | /// 4 | /// 活动连接定义。 5 | /// 6 | public class TransitionDefinition 7 | { 8 | /// 9 | /// 源活动。 10 | /// 11 | public ActivityDefinition SourceActivity { get; set; } 12 | 13 | /// 14 | /// 目标活动。 15 | /// 16 | public ActivityDefinition DestinationActivity { get; set; } 17 | 18 | /// 19 | /// 工作流定义。 20 | /// 21 | public WorkflowDefinition WorkflowDefinition { get; set; } 22 | } 23 | } -------------------------------------------------------------------------------- /Rabbit.Workflow/Workflow.Engine/Models/WorkflowDefinition.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Workflow.Engine.Models 4 | { 5 | /// 6 | /// 工作流定义。 7 | /// 8 | public class WorkflowDefinition 9 | { 10 | /// 11 | /// 名称。 12 | /// 13 | public string Name { get; set; } 14 | 15 | /// 16 | /// 包含的活动。 17 | /// 18 | public IList Activitys { get; set; } 19 | 20 | /// 21 | /// 活动连接定义。 22 | /// 23 | public IList Transitions { get; set; } 24 | } 25 | } -------------------------------------------------------------------------------- /Rabbit.Workflow/Workflow.Engine/Models/WorkflowInstance.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Workflow.Engine.Models 4 | { 5 | /// 6 | /// 工作流实例。 7 | /// 8 | public class WorkflowInstance 9 | { 10 | /// 11 | /// 工作流环境。 12 | /// 13 | public IDictionary Environment { get; set; } 14 | 15 | /// 16 | /// 等待执行的活动。 17 | /// 18 | public virtual IList AwaitingActivities { get; set; } 19 | 20 | /// 21 | /// 工作流定义。 22 | /// 23 | public virtual WorkflowDefinition WorkflowDefinition { get; set; } 24 | 25 | /// 26 | /// 工作流状态。 27 | /// 28 | public WorkflowStateEnum State { get; set; } 29 | 30 | /// 31 | /// 设置状态为完成。 32 | /// 33 | public void Complete() 34 | { 35 | State = WorkflowStateEnum.Processed; 36 | } 37 | } 38 | } -------------------------------------------------------------------------------- /Rabbit.Workflow/Workflow.Engine/Models/WorkflowStateEnum.cs: -------------------------------------------------------------------------------- 1 | namespace Workflow.Engine.Models 2 | { 3 | /// 4 | /// 工作流状态枚举。 5 | /// 6 | public enum WorkflowStateEnum 7 | { 8 | /// 9 | /// 未处理。 10 | /// 11 | Untreated = 0, 12 | 13 | /// 14 | /// 处理中。 15 | /// 16 | Processing = 1, 17 | 18 | /// 19 | /// 挂起。 20 | /// 21 | Pending = 2, 22 | 23 | /// 24 | /// 处理完成。 25 | /// 26 | Processed = 3 27 | } 28 | } -------------------------------------------------------------------------------- /Rabbit.Workflow/Workflow.Engine/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.InteropServices; 3 | 4 | // 有关程序集的一般信息由以下 5 | // 控制。更改这些特性值可修改 6 | // 与程序集关联的信息。 7 | [assembly: AssemblyTitle("Workflow.Engine")] 8 | [assembly: AssemblyDescription("")] 9 | [assembly: AssemblyConfiguration("")] 10 | [assembly: AssemblyCompany("")] 11 | [assembly: AssemblyProduct("Workflow.Engine")] 12 | [assembly: AssemblyCopyright("Copyright © 2015")] 13 | [assembly: AssemblyTrademark("")] 14 | [assembly: AssemblyCulture("")] 15 | 16 | //将 ComVisible 设置为 false 将使此程序集中的类型 17 | //对 COM 组件不可见。 如果需要从 COM 访问此程序集中的类型, 18 | //请将此类型的 ComVisible 特性设置为 true。 19 | [assembly: ComVisible(false)] 20 | 21 | // 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID 22 | [assembly: Guid("72827388-3b0c-4328-a534-7ce657dff62a")] 23 | 24 | // 程序集的版本信息由下列四个值组成: 25 | // 26 | // 主版本 27 | // 次版本 28 | // 生成号 29 | // 修订号 30 | // 31 | //可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值, 32 | // 方法是按如下所示使用“*”: : 33 | // [assembly: AssemblyVersion("1.0.*")] 34 | [assembly: AssemblyVersion("1.0.0.0")] 35 | [assembly: AssemblyFileVersion("1.0.0.0")] 36 | -------------------------------------------------------------------------------- /Rabbit.Workflow/Workflow.Engine/Services/ActivitiesManager.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq; 3 | 4 | namespace Workflow.Engine.Services 5 | { 6 | public interface IActivitiesManager 7 | { 8 | IEnumerable GetActivities(); 9 | 10 | IActivity GetActivityByName(string name); 11 | } 12 | 13 | public class ActivitiesManager : IActivitiesManager 14 | { 15 | private readonly IEnumerable _activities; 16 | 17 | public ActivitiesManager(IEnumerable activities) 18 | { 19 | _activities = activities; 20 | } 21 | 22 | public IEnumerable GetActivities() 23 | { 24 | return _activities.OrderBy(x => x.Name).ToArray(); 25 | } 26 | 27 | public IActivity GetActivityByName(string name) 28 | { 29 | return _activities.FirstOrDefault(x => x.Name == name); 30 | } 31 | } 32 | } -------------------------------------------------------------------------------- /Rabbit.Workflow/Workflow.Engine/Services/IActivityDefinitionService.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using Workflow.Engine.Models; 3 | 4 | namespace Workflow.Engine.Services 5 | { 6 | public interface IActivityDefinitionService 7 | { 8 | IEnumerable GetStartActivityDefinitions(string name); 9 | 10 | IEnumerable GetAwaitingActivityDefinitions(string name); 11 | } 12 | } -------------------------------------------------------------------------------- /Rabbit.Workflow/Workflow.Engine/Services/WorkflowManager.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using Workflow.Engine.Models; 5 | 6 | namespace Workflow.Engine.Services 7 | { 8 | public interface IWorkflowManager 9 | { 10 | void TriggerEvent(string name, Func> environmentContext); 11 | } 12 | 13 | public sealed class WorkflowManager : IWorkflowManager 14 | { 15 | private readonly IActivitiesManager _activitiesManager; 16 | private readonly IActivityDefinitionService _activityDefinitionService; 17 | 18 | public WorkflowManager(IActivitiesManager activitiesManager, IActivityDefinitionService activityDefinitionService) 19 | { 20 | _activitiesManager = activitiesManager; 21 | _activityDefinitionService = activityDefinitionService; 22 | } 23 | 24 | #region Implementation of IWorkflowManager 25 | 26 | public void TriggerEvent(string name, Func> environmentContext) 27 | { 28 | var environment = environmentContext(); 29 | var activity = _activitiesManager.GetActivityByName(name); 30 | 31 | var startedWorkflows = _activityDefinitionService.GetStartActivityDefinitions(name).AsQueryable(); 32 | var awaitingActivities = _activityDefinitionService.GetAwaitingActivityDefinitions(name).AsQueryable(); 33 | 34 | if (!startedWorkflows.Any() && !awaitingActivities.Any()) 35 | return; 36 | 37 | foreach (var awaitingActivityDefinition in awaitingActivities) 38 | { 39 | var workflowContext = new WorkflowContext 40 | { 41 | Environment = environment 42 | }; 43 | 44 | var activityContext = CreateActivityContext(awaitingActivityDefinition.ActivityDefinition, environment); 45 | try 46 | { 47 | if (!activity.CanExecute(workflowContext, activityContext)) 48 | { 49 | continue; 50 | } 51 | } 52 | catch (Exception e) 53 | { 54 | continue; 55 | } 56 | 57 | ResumeWorkflow(awaitingActivityDefinition, workflowContext, environment); 58 | } 59 | 60 | foreach (var activityRecord in startedWorkflows) 61 | { 62 | var workflowContext = new WorkflowContext 63 | { 64 | Environment = environment 65 | }; 66 | 67 | var workflowRecord = new WorkflowInstance 68 | { 69 | WorkflowDefinition = activityRecord.WorkflowDefinition 70 | }; 71 | 72 | workflowContext.WorkflowInstance = workflowRecord; 73 | 74 | var activityContext = CreateActivityContext(activityRecord, environment); 75 | 76 | try 77 | { 78 | if (!activity.CanExecute(workflowContext, activityContext)) 79 | { 80 | continue; 81 | } 82 | } 83 | catch (Exception e) 84 | { 85 | continue; 86 | } 87 | 88 | StartWorkflow(workflowContext, activityRecord, environment); 89 | } 90 | } 91 | 92 | private void ResumeWorkflow(AwaitingActivityDefinition awaitingActivityDefinition, WorkflowContext workflowContext, Dictionary environment) 93 | { 94 | var cancellationToken = new CancellationToken(); 95 | InvokeActivities(activity => activity.OnWorkflowResuming(workflowContext, cancellationToken)); 96 | if (cancellationToken.IsCancelled) 97 | return; 98 | InvokeActivities(activity => activity.OnWorkflowResumed(workflowContext)); 99 | var workflowInstance = awaitingActivityDefinition.WorkflowInstance; 100 | workflowContext.WorkflowInstance = workflowInstance; 101 | workflowInstance.AwaitingActivities.Remove(awaitingActivityDefinition); 102 | var blockedOn = ExecuteWorkflow(workflowContext, awaitingActivityDefinition.ActivityDefinition, environment).ToList(); 103 | 104 | if (!blockedOn.Any() && !workflowInstance.AwaitingActivities.Any()) 105 | { 106 | awaitingActivityDefinition.WorkflowInstance.Complete(); 107 | } 108 | else 109 | { 110 | foreach (var blocking in blockedOn) 111 | { 112 | workflowInstance.AwaitingActivities.Add(new AwaitingActivityDefinition 113 | { 114 | ActivityDefinition = blocking, 115 | WorkflowInstance = workflowInstance 116 | }); 117 | } 118 | } 119 | } 120 | 121 | private ActivityContext CreateActivityContext(ActivityDefinition activityDefinition, IDictionary environment) 122 | { 123 | return new ActivityContext 124 | { 125 | Activity = _activitiesManager.GetActivityByName(activityDefinition.Name), 126 | Environment = environment 127 | }; 128 | } 129 | 130 | private void InvokeActivities(Action action) 131 | { 132 | foreach (var activity in _activitiesManager.GetActivities()) 133 | { 134 | action(activity); 135 | } 136 | } 137 | 138 | public IEnumerable ExecuteWorkflow(WorkflowContext workflowContext, 139 | ActivityDefinition activityDefinition, IDictionary environment) 140 | { 141 | var scheduled = new Stack(); 142 | 143 | scheduled.Push(activityDefinition); 144 | 145 | while (scheduled.Any()) 146 | { 147 | activityDefinition = scheduled.Pop(); 148 | 149 | var activityContext = CreateActivityContext(activityDefinition, environment); 150 | 151 | var cancellationToken = new CancellationToken(); 152 | InvokeActivities(activity => activity.OnActivityExecuting(workflowContext, activityContext, cancellationToken)); 153 | 154 | if (cancellationToken.IsCancelled) 155 | continue; 156 | 157 | var outcomes = activityContext.Activity.Execute(workflowContext, activityContext).ToList(); 158 | 159 | InvokeActivities(activity => activity.OnActivityExecuted(workflowContext, activityContext)); 160 | 161 | foreach (var outcome in outcomes) 162 | { 163 | var transition = workflowContext.WorkflowInstance.WorkflowDefinition.Transitions.FirstOrDefault(x => x.SourceActivity == activityDefinition && x.DestinationActivity.Name == outcome); 164 | 165 | if (transition != null) 166 | { 167 | scheduled.Push(transition.DestinationActivity); 168 | } 169 | } 170 | } 171 | return new ActivityDefinition[0]; 172 | } 173 | 174 | private void StartWorkflow(WorkflowContext workflowContext, ActivityDefinition activityDefinition, IDictionary environment) 175 | { 176 | var cancellationToken = new CancellationToken(); 177 | InvokeActivities(activity => activity.OnWorkflowStarting(workflowContext, cancellationToken)); 178 | 179 | if (cancellationToken.IsCancelled) 180 | return; 181 | 182 | InvokeActivities(activity => activity.OnWorkflowStarted(workflowContext)); 183 | 184 | var blockedOn = ExecuteWorkflow(workflowContext, activityDefinition, environment).ToList(); 185 | 186 | if (blockedOn.Any()) 187 | { 188 | foreach (var blocking in blockedOn) 189 | { 190 | workflowContext.WorkflowInstance.AwaitingActivities.Add(new AwaitingActivityDefinition 191 | { 192 | ActivityDefinition = blocking, 193 | WorkflowInstance = workflowContext.WorkflowInstance 194 | }); 195 | } 196 | } 197 | } 198 | 199 | #endregion Implementation of IWorkflowManager 200 | } 201 | } -------------------------------------------------------------------------------- /Rabbit.Workflow/Workflow.Engine/Task.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Workflow.Engine 4 | { 5 | /// 6 | /// 表示一个任务。 7 | /// 8 | public abstract class Task : IActivity 9 | { 10 | #region Implementation of IActivity 11 | 12 | /// 13 | /// 名称。 14 | /// 15 | public abstract string Name { get; } 16 | 17 | /// 18 | /// 分类。 19 | /// 20 | public abstract string Category { get; } 21 | 22 | /// 23 | /// 描述。 24 | /// 25 | public abstract string Description { get; } 26 | 27 | /// 28 | /// 是否可以执行。 29 | /// 30 | /// 工作流上下文。 31 | /// 活动上下文。 32 | /// 33 | public virtual bool CanExecute(WorkflowContext workflowContext, ActivityContext activityContext) 34 | { 35 | return true; 36 | } 37 | 38 | /// 39 | /// 执行活动。 40 | /// 41 | /// 工作流上下文。 42 | /// 活动上下文。 43 | /// 接下来需要执行的动作。 44 | public abstract IEnumerable Execute(WorkflowContext workflowContext, ActivityContext activityContext); 45 | 46 | /// 47 | /// 工作流开始前执行。 48 | /// 49 | /// 工作流上下文。 50 | /// 可用于取消的令牌。 51 | public virtual void OnWorkflowStarting(WorkflowContext workflowContext, CancellationToken cancellationToken) 52 | { 53 | } 54 | 55 | /// 56 | /// 工作流开始之后执行。 57 | /// 58 | /// 工作流上下文。 59 | public virtual void OnWorkflowStarted(WorkflowContext workflowContext) 60 | { 61 | } 62 | 63 | /// 64 | /// 工作流恢复前执行。 65 | /// 66 | /// 工作流上下文。 67 | /// 可用于取消的令牌。 68 | public virtual void OnWorkflowResuming(WorkflowContext workflowContext, CancellationToken cancellationToken) 69 | { 70 | } 71 | 72 | /// 73 | /// 工作流恢复之后执行。 74 | /// 75 | /// 工作流上下文。 76 | public virtual void OnWorkflowResumed(WorkflowContext workflowContext) 77 | { 78 | } 79 | 80 | /// 81 | /// 活动执行前执行。 82 | /// 83 | /// 工作流上下文。 84 | /// 活动上下文。 85 | /// 可用于取消的令牌。 86 | public virtual void OnActivityExecuting(WorkflowContext workflowContext, ActivityContext activityContext, 87 | CancellationToken cancellationToken) 88 | { 89 | } 90 | 91 | /// 92 | /// 活动执行后执行。 93 | /// 94 | /// 工作流上下文。 95 | /// 活动上下文。 96 | public virtual void OnActivityExecuted(WorkflowContext workflowContext, ActivityContext activityContext) 97 | { 98 | } 99 | 100 | #endregion Implementation of IActivity 101 | } 102 | } -------------------------------------------------------------------------------- /Rabbit.Workflow/Workflow.Engine/Workflow.Engine.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {72827388-3B0C-4328-A534-7CE657DFF62A} 8 | Library 9 | Properties 10 | Workflow.Engine 11 | Workflow.Engine 12 | v4.5 13 | 512 14 | 15 | 16 | true 17 | full 18 | false 19 | bin\Debug\ 20 | DEBUG;TRACE 21 | prompt 22 | 4 23 | 24 | 25 | pdbonly 26 | true 27 | bin\Release\ 28 | TRACE 29 | prompt 30 | 4 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 68 | -------------------------------------------------------------------------------- /Rabbit.Workflow/Workflow.Engine/WorkflowContext.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using Workflow.Engine.Models; 3 | 4 | namespace Workflow.Engine 5 | { 6 | /// 7 | /// 工作流上下文。 8 | /// 9 | public class WorkflowContext 10 | { 11 | /// 12 | /// 工作流实例。 13 | /// 14 | public WorkflowInstance WorkflowInstance { get; set; } 15 | 16 | /// 17 | /// 工作流上下文环境。 18 | /// 19 | public IDictionary Environment { get; set; } 20 | } 21 | } --------------------------------------------------------------------------------