构建工作流平台
1. 平台类
using System;
namespace wfTest
{
#region 工作流引擎
//引擎类
public class wfEngine
{
//实例池
protected System.Collections.SortedList InstancePool;
public wfEngine()
{
InstancePool=new System.Collections.SortedList();
}
/// <summary>
/// 添加实例到引擎
/// </summary>
/// <param name="Instance">工作流实例</param>
/// <returns>实例ID</returns>
public string AddInstance(wfTemplet Instance)
{
InstancePool.Add(Instance.InstanceID ,Instance.CurrentNode);
return Instance.InstanceID;
}
/// <summary>
/// 工作流下移一结点
/// </summary>
/// <param name="InstanceID">实例ID</param>
/// <returns></returns>
public bool ExecuteNextNode(string InstanceID)
{
if(InstancePool.Contains(InstanceID))
{
if(((wfNode)InstancePool[InstanceID]).NextNode !=null)
{
InstancePool[InstanceID]=((wfNode)InstancePool[InstanceID]).execute();
return true;
}
else
{
((wfNode)InstancePool[InstanceID]).execute();
InstancePool.Remove(InstanceID);
return false;
}
}
return false;
}
/// <summary>
/// 一次执行完实例的所有结点
/// </summary>
/// <param name="InstanceID">实例ID</param>
public void AutoExecute(string InstanceID)
{
while (ExecuteNextNode(InstanceID))
{
AutoExecute(InstanceID);
}
}
//得到实例状态
public string getInstanceState(string InstanceID)
{
if(InstancePool.Contains(InstanceID))
{
return ((wfNode)InstancePool[InstanceID]).NodeId;
}
return null;
}
//得到引擎内的所有实例
public System.Collections.SortedList getInstances()
{
return this.InstancePool;
}
//停止实例
public void StopInstance(string InstanceID)
{}
//继续被停止实例
public void runInstance(string InstanceID)
{}
}
#endregion
//---------------------------------------------------------------------
#region 工作流模板
//工作流类
public class wfTemplet
{
public string InstanceID;//实例ID
/// <summary>
/// 构造函数
/// </summary>
/// <param name="InstanceID">实例ID</param>
public wfTemplet(string InstanceID)
{
this.InstanceID=InstanceID;
}
public wfNode CurrentNode;//当前活动结点
}
#endregion
//---------------------------------------------------------------------
#region 结点模板
//结点模板类
public abstract class wfNode
{
public string InstanceId;//实例ID
public string NodeId;//结点ID
/// <summary>
/// 构造函数
/// </summary>
/// <param name="InstanceID">实例ID</param>
/// <param name="NodeId">结点ID</param>
public wfNode(string InstanceID,string NodeId)
{
this.InstanceId=InstanceID;
this.NodeId =NodeId;
}
//下一结点
public wfNode NextNode;
//执行区
public abstract wfNode execute();
}
#endregion
//---------------------------------------------------------------------
#region 代码结点模板
//要执行的代码委托
public delegate void codeMethod(wfNode sender ,object data);
//代码结点模板类
public class codeNode:wfNode
{
//构造函数
public codeNode(string InstanceID,string NodeId)
:base(InstanceID, NodeId)
{}
public event codeMethod Code;//要执行的代码
public object CodeParameter ;//要执行的参数
//执行区
public override wfNode execute()
{
if(Code !=null)
{
Code(this,CodeParameter);
}
return this.NextNode;
}
}
#endregion
//---------------------------------------------------------------------
#region 条件结点模板
//条件表达式类
public class conditionExpression
{
public object leftExpression;//左表达式
public object rightExpression;//右表达式
//比效方法
//可以跟据需要自定义左、右表达式,并重载比效运算符
public bool contrast()
{
if(leftExpression.Equals(rightExpression))
{return true;}
else
{return false;}
}
}
//条件结点模板类
public class conditionNode:wfNode
{
//构造函数
public conditionNode(string InstanceID,string NodeId)
:base(InstanceID, NodeId)
{}
public conditionExpression expression;//表达式对象
public wfNode ElseNode;//否则结点
//执行区
public override wfNode execute()
{
if(expression.contrast())
{return this.NextNode;}
else
{return this.ElseNode;}
}
}
#endregion
//---------------------------------------------------------------------
}
2. 测试程序
using System;
namespace Test
{
public class begin
{
static void Main ()
{
wfTest.wfEngine 引擎=new wfTest.wfEngine();
一个工作流 obj1=new 一个工作流("12345");
一个工作流 obj2=new 一个工作流("12347");
引擎.AddInstance(obj1);
引擎.AddInstance(obj2);
引擎.AutoExecute(obj1.InstanceID);
引擎.AutoExecute(obj2.InstanceID);
System.Console.Read();
}
}
class 一个工作流:wfTest.wfTemplet
{
public 一个工作流(string InstanceID)
:base(InstanceID)
{
/*
* 结点A
* |
* |
* 结点B
* (true)/ /(false)
* / /
* 结点C 结点D
* / /
* / /
* 结点E
*/
//结点A:代码结点
wfTest.codeNode 结点A =new wfTest.codeNode(this.InstanceID,"结点A");
结点A.Code+=new wfTest.codeMethod(结点A_Code);
结点A.CodeParameter="hello wf";
//结点B:条件结点
wfTest.conditionNode 结点B=new wfTest.conditionNode(this.InstanceID,"结点B");
//结点B的条件表达式
wfTest.conditionExpression 条件表达式=new wfTest.conditionExpression();
条件表达式.leftExpression="123";//条件表达式左值
条件表达式.rightExpression ="123";//条件表达式右值
结点B.expression=条件表达式; //绑定条件表达式到结点B
//结点C:代码结点
wfTest.codeNode 结点C =new wfTest.codeNode(this.InstanceID,"结点C");
结点C.Code+=new wfTest.codeMethod(结点C_Code);
结点C.CodeParameter="i am 结点C,条件的真结点";
//结点D:代码结点
wfTest.codeNode 结点D =new wfTest.codeNode(this.InstanceID,"结点D");
结点D.Code+=new wfTest.codeMethod(结点D_Code);
结点D.CodeParameter="这是_结点D,条件的假结点";
//结点E:代码结点
wfTest.codeNode 结点E =new wfTest.codeNode(this.InstanceID,"结点E");
结点E.Code+=new wfTest.codeMethod(结点E_Code);
结点E.CodeParameter="完成了";
//指定入口结点
this.CurrentNode=结点A;
//设置结点关系
结点A.NextNode =结点B;
结点B.NextNode =结点C;
结点B.ElseNode =结点D;
结点C.NextNode =结点E;
结点D.NextNode =结点E;
}
//结点A:代码
private void 结点A_Code(wfTest.wfNode sender, object data)
{
System.Console.WriteLine(sender.InstanceId + ":" + sender.NodeId + ":" + data.ToString());
}
//结点C:代码
private void 结点C_Code(wfTest.wfNode sender, object data)
{
System.Console.WriteLine(sender.InstanceId + ":" + sender.NodeId + ":" + data.ToString());
}
//结点B:代码
private void 结点D_Code(wfTest.wfNode sender, object data)
{
System.Console.WriteLine(sender.InstanceId + ":" + sender.NodeId + ":" + data.ToString());
}
//结点E:代码
private void 结点E_Code(wfTest.wfNode sender, object data)
{
System.Console.WriteLine(sender.InstanceId + ":" + sender.NodeId + ":" + data.ToString());
}
}
}