Micro Workflow engine Design

Source: Internet
Author: User
Tags fsm

Design of micro Workflow engine-Preface

Refer to Workflow Many people will think of Oa, indeed OA is a typical workflow application, but the workflow is not limited to OA, workflow should be considered as the basic framework software, mainly for the reorganization and optimization of the process, it has a wide range of applications. There are a lot of good open source workflows under Java that can be chosen such as ACTIVIT5, JPBM4, etc., but in. NET there is almost no satisfactory workflow engine available. Of course not to say that there is no open source under the. NET is only some domestic open source, but after reading the code there is no interest, and do not say the quality of the code, but also introduced a lot of things, want to apply in the project is also very difficult. In view of this I decided to develop a model. NET micro-workflow engine.

II. Basic Notes

Why is it called a micro workflow engine? is ultra-lightweight, to facilitate the use of light in the project, such as only a class library DLL, the size of hundreds of K to 1M, but we have to look back to the workflow system, it is too big, it should include:
1. Workflow Engine
2. Workflow Designer
3. Workflow Management System
4. Form Designer

For now, I only implemented the core engine, the process definition can only be edited in XML and then read into the engine or directly defined in the database, but the whole process is able to flow normally. As for the process designer, form designer, workflow management System I have the energy to develop slowly. Here I finished only a small piece, but is the core of the workflow, can be easily embedded into the business system application.

The engine provides support for the parsing of workflow definitions and process flow. The workflow definition file describes the business interaction logic, the workflow engine by parsing the workflow definition file according to the business interaction logic of the business flow, the workflow engine usually by reference to a model to design, through the scheduling algorithm to flow (process start, stop, suspend, restore, etc.), Through a variety of link scheduling algorithm to achieve the transfer of links (link merger, fork, Choice, conditional selection, etc.).

Iii. Initial Impressions

1, from the beginning of the concept of interpretation estimates that everyone will not see. Let's take a simple example to see, create a new project, reference my workflow engine class library (Chitu.Bpm.dll, named Red Rabbit).
At project startup, configure the process engine (Global.asax.cs) as follows:

Initializes the process engine bpmconfiguration    . Instance ()    . Config (@ "C:\Configration\BpmConfig.xml")    . Start ();

When used in a project, such as a new process definition:

Gets the workflow context var bpm = new Bpmcontext ()    . UseTransaction (True)    . Setactor ("Xiangin");//new process definition BPM. Newprocessdefinition ("absence process")    . Setxmlfile (@ "C:\Definition\demo1.xml")    . Setcategory ("Category 1")    . Seteffectdate (DateTime.Now)    . Setexpiredate (DateTime.Now.AddDays ())    . Setmemo ("Memo1")    . Create ()  //creates the process definition, generating only the bpm_definition_process table    . Parse ()   //parse XML    . Deploy (); Release process

Start the process:

Start the process var processes = BPM. Newprocessintance ("Leave process id", "Xiangin (Business ID)");   Create process instance processes. SetVariable ("Process variable 1", "value 1");                     Set up process variable processes. Start ();

The Human task node forwards the next step:

Task completion var task = BPM. Loadtaskinstance ("task ID"); SetVariable ("Task Variable 2", "XX"), task. Signal (); Trigger Token Flow

All operations are centralized into the bpmcontext through the facade mode, and the operation is simple and convenient.

2, next we look at the process definition of XML, the following is a process I fabricated, so that the various nodes are put in.

<?xml version= "1.0" encoding= "UTF-8"? ><process name= "Model room renovation Process" > <start name= "Decoration application" > <transition to= "decoration scheme design" > <action class= "namespace.myactionhandler" ></action> </transition> &LT;/START&G  T <task name= "decoration scheme design" > <transition to= "Renovation plan Audit" > <action script= "log. Debug (' Renovation Plan audit Action Test '); " ></action> </transition> </task> <decision name= "renovation Program review" > <transitions> < Transition to= "Decoration preparation" condition-expression= "Variable.pass = True" ></transition> <transition to= "decoration scheme set Count "condition-expression=" Variable.pass! = True "></transition> </transitions> <events> < Action event= "Enter" class= "Enterhandlerclass" ></action> <action event= "Leave" class= "Leavehandlerclass "></action> </events> <assignments> <assignment owner=" {process.starter} "></assig Nment> </assignments> <variables> <variable type= "boolean" Name= "Ispass" access= "read,required" ></variable>    </variables> </decision> <fork name= "Decoration preparation" > <transition to= "contract signing" ></transition> <transition to= "Waiting for decoration workers" ></transition> <transition to= "Decoration material budget" ></transition> </fork > <sign name= "decoration contract signed" necessary= "false" async= "true" > <transition to= "decoration construction" ></transition> </ sign> <wait name= "Waiting for decoration workers in place" > <transition to= "decoration construction" ></transition> </wait> <task name= "installed Material Procurement sub-process ></transition> </task> <subflow name= "Material sourcing sub-process" > <tra <transition to= Nsition to= "Decoration construction" ></transition> </subflow> <join name= "decoration construction" > <transition to= "Construction acceptance/Payment" &GT;&L t;/transition> </join> <auto name= "Construction Acceptance/Payment" > <transition to= "decoration complete" ></transition> </aut  o> <end name= "decoration complete" >  </end> <events> <action event= "Process-start" class= "Teststarthandler" ></action> <acti On event= "Process-end" class= "Testendhandler" ></action> </events> <variables> <variable type = "string" name= "start_id" access= "readonly,required" ></variable> <variable type= "string" Name= "Start_ Person "></variable> </variables></process>

The root node is defined as a process (process), under which the Task Node (node) is divided into:
Start node
Auto Automatic node
Task Human Node
Decisioin decision Node
Fork divergence Node
Join Aggregation Node
Sublfow Sub-process node
Sign to sign the node
Wait for node
End ending node

The task node can include the routing (Transition) action (action) and the person assignment (Assignment) variable definition (variable)
There are several types of action: 1, Class 2, Script 3, SQL 4, WebService 5, expression
You can directly access the Process.xxx property or the Task.xxx property or variable.xxx in script or expression to simplify the use of Dynamic C # statements.
Of course, there are many other attribute definitions in the XML definition that I have not listed here, and I will use it later to say it carefully.

3, about the database design, here is only the flow of the core needs of the table, there is no Latin America relations between the table

Part Four function analysis

    A, I divide it into major modules: engine configuration, process definition, instance flow, log processing, scheduled task  
         Engine configuration: Database connection, log configuration, parameter setting, etc. of the configuration engine instance.  
        process definition: Use XML to describe the process, mainly define the task node, routing, action events, variables, personnel allocation and other  
         instance Flow: Run Process instance   by definition;
        Log processing: output log  
        Task Scheduler: A service is started for processing such as delayed start, task expiration, and so on

    B, the key class design in circulation includes:  
        1, process objects (processes)  
        2, Work Task (Task)  
        3, Routing (trasition)  
        4, token  
         5, Event bus and action processing (Eventbus, Actionhandler)  
         6, personnel allocation and delegation mechanism (assignment, depute)  
        7, Process fallback processing (rollbackservice)  
        8, Message Services (Notifyservice)

c, usually the engine control flow scheduling flow core scheduling algorithm is mainly FSM and petrinet two, based on scheduling algorithm to complete the flow of the process:
1. FSM (finite state machine)
An FSM is defined as a calculation model that contains a set of state sets (states), a starting state (start), a set of input symbol sets (alphabet), a transformation function that maps the input symbol and the current state to the next state (transition function). When the symbol string is entered, the model enters the starting state. It's going to change to a new state, depending on the conversion function. In finite state machines, there are many variables, for example, a state machine has many actions associated with an action (actions) Transformation (mealy) or state (mole machine), multiple start states, a conversion based on no input symbols, or multiple transformations that specify symbols and states (non-finite state machines). One or more states assigned to the receive state (the recognizer), and so on. Follow the FSM process engine through state switching to complete the flow of the process.
2, Petrinet
An abstract, formal model of the information flow. The static and dynamic properties of a system are pointed out. Petrinet are usually represented as graphs. Follow the petrinet process engine to determine the flow of the process through tokens.
I'm using the second petrinet algorithm. Token is used to represent the location where the current instance is running, and the transfer of token between various points in the process is used to represent the progress of the process, as shown in:

Token flow logic, I put the following class methods to do a simplification omit the Routing and Node processing details, so that you understand the flow of tokens:

Token token class signalpublic void Signal () {    fromtask.leave (executecontext);} leavepublic void Leave (ExecutionContext executioncontext) {transition) in the Tasks task Class    . Take (ExecutionContext);} Route takepublic Void Take (ExecutionContext executioncontext) {    totask.enter (executioncontext) in the Transition class;} enterpublic void Enter (ExecutionContext executioncontext) {Run (ExecutionContext) in the Tasks task class    ;}

At this point the token is successfully transferred from one node to the next node, the flow of tokens is the key to the workflow, of course, different node processing is the difference, the most complex when the number of divergent nodes and aggregation nodes. Here is introduced here, no longer give you a detailed introduction.

D, currently implemented in my engine mainly includes the following features:
1. Explain the process definition
2. Control process Instances-Create, activate, suspend, terminate, etc.
3. Control Flow Scheduling
4. Custom action and event release
5. Process variables and working variable handling
6, task scheduling, such as delayed start, task expiration, etc.
7, commissioned services, commissioned by the agency
8, fallback service, back to any node or recall
9, message services, such as claim notification, to-do reminders, reminders news ...
10. Process Task Monitoring Service
11. Log processing and history record
12. Task assignment and claim
13. Participant Organization Model interface

V. Summary

Now that my workflow engine is still being perfected, I summarize its pros and cons:
Advantages:
1, it is a super lightweight or micro-work flow engine, and green pollution-free, it only a DLL, size only about 1M.
2, it currently supports SQL Server, MYSQL, Oracle, SQLite, PostgreSQL and many other databases
3, the body said that although it is micro, but the function is not a miniature, its design combined with modern OA and traditional workflow, basically can achieve most of our functional needs.
4, it is actually designed for developers, from the initial impression of the example code in the above can be seen, its interface is very centralized, streamlined, friendly, so that developers easy to understand and use more convenient and simple. So it is more suitable for embedding into the project development.
Disadvantages:
1, it now has no process designer, management system, form designer, etc., at best can only be considered a class library, not directly can be used.
2, has just completed the first internal version, and currently only used in our internal projects, so it is not mature, although we will continue to improve and improve.
3, lack of successful application of the case.
For ourselves, these shortcomings are where we need to continue our efforts, and it may take a lot of time to complete. At present, we do not intend to open source, and so it slowly stable mature we will consider whether it is open source. If you have any good suggestions or doubts, I would be happy to answer them, or you can design and develop your own workflow, and we will communicate with each other.

Micro Workflow engine Design

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.