public StateMachineWorkflowInstance(WorkflowRuntime runtime, Guid instanceId) { if (runtime == null) throw new ArgumentNullException("runtime"); if (instanceId == Guid.Empty) throw new ArgumentNullException("instanceId"); _runtime = runtime; _instanceId = instanceId; _workflowInstance = runtime.GetWorkflow(instanceId); _stateMachineWorkflow = _workflowInstance.GetWorkflowDefinition() as StateMachineWorkflowActivity; if (_stateMachineWorkflow == null) throw new ArgumentException(SR.GetStateMachineWorkflowRequired(), "instanceId"); }
private ReadOnlyCollection <string> GetPossibleStateTransitions() { List <string> targetStates = new List <string>(); ReadOnlyCollection <WorkflowQueueInfo> workflowQueuedInfos = this.WorkflowInstance.GetWorkflowQueueData(); StateMachineWorkflowActivity stateMachineWorkflow = this.StateMachineWorkflow; foreach (WorkflowQueueInfo queueInfo in workflowQueuedInfos) { foreach (string subscribedActivityName in queueInfo.SubscribedActivityNames) { Activity subscribedActivity = StateMachineHelpers.FindActivityByName(stateMachineWorkflow, subscribedActivityName); IEventActivity eventActivity = subscribedActivity as IEventActivity; if (eventActivity == null) { continue; } EventDrivenActivity eventDriven = StateMachineHelpers.GetParentEventDriven(eventActivity); Debug.Assert(eventDriven != null); Queue <Activity> activities = new Queue <Activity>(); activities.Enqueue(eventDriven); while (activities.Count > 0) { Activity activity = activities.Dequeue(); SetStateActivity setState = activity as SetStateActivity; if (setState != null) { targetStates.Add(setState.TargetStateName); } else { CompositeActivity compositeActivity = activity as CompositeActivity; if (compositeActivity != null) { foreach (Activity childActivity in compositeActivity.EnabledActivities) { activities.Enqueue(childActivity); } } } } } } return(targetStates.AsReadOnly()); }
public StateMachineWorkflowInstance(WorkflowRuntime runtime, Guid instanceId) { if (runtime == null) { throw new ArgumentNullException("runtime"); } if (instanceId == Guid.Empty) { throw new ArgumentNullException("instanceId"); } this._runtime = runtime; this._instanceId = instanceId; this._workflowInstance = runtime.GetWorkflow(instanceId); this._stateMachineWorkflow = this._workflowInstance.GetWorkflowDefinition() as StateMachineWorkflowActivity; if (this._stateMachineWorkflow == null) { throw new ArgumentException(SR.GetStateMachineWorkflowRequired(), "instanceId"); } }
private ReadOnlyCollection <string> GetPossibleStateTransitions() { List <string> list = new List <string>(); ReadOnlyCollection <WorkflowQueueInfo> workflowQueueData = this.WorkflowInstance.GetWorkflowQueueData(); StateMachineWorkflowActivity stateMachineWorkflow = this.StateMachineWorkflow; foreach (WorkflowQueueInfo info in workflowQueueData) { foreach (string str in info.SubscribedActivityNames) { IEventActivity eventActivity = StateMachineHelpers.FindActivityByName(stateMachineWorkflow, str) as IEventActivity; if (eventActivity != null) { EventDrivenActivity parentEventDriven = StateMachineHelpers.GetParentEventDriven(eventActivity); Queue <Activity> queue = new Queue <Activity>(); queue.Enqueue(parentEventDriven); while (queue.Count > 0) { Activity activity5 = queue.Dequeue(); SetStateActivity activity6 = activity5 as SetStateActivity; if (activity6 != null) { list.Add(activity6.TargetStateName); } else { CompositeActivity activity7 = activity5 as CompositeActivity; if (activity7 != null) { foreach (Activity activity8 in activity7.EnabledActivities) { queue.Enqueue(activity8); } continue; } } } } } } return(list.AsReadOnly()); }
private StateActivity GetCurrentState() { ReadOnlyCollection <WorkflowQueueInfo> workflowQueuedInfos = this.WorkflowInstance.GetWorkflowQueueData(); foreach (WorkflowQueueInfo queueInfo in workflowQueuedInfos) { if (queueInfo.QueueName.Equals(StateMachineWorkflowActivity.SetStateQueueName)) { if (queueInfo.SubscribedActivityNames.Count == 0) { return(null); } Debug.Assert(queueInfo.SubscribedActivityNames.Count == 1); StateMachineWorkflowActivity stateMachineWorkflow = this.StateMachineWorkflow; StateActivity currentState = StateMachineHelpers.FindStateByName(stateMachineWorkflow, queueInfo.SubscribedActivityNames[0]); return(currentState); } } return(null); }
/// <summary> /// 启动与工作流程相同类型流程,查询对应节点用户 /// </summary> /// <param name="CompanyID">公司ID</param> /// <param name="ModelCode">模块代码</param> /// <param name="FlowGUID">待审批流GUID,新增时为空或者为StartFlow</param> /// <returns></returns> public DataResult GetAppUser(OracleConnection con, string CompanyID, string ModelCode, string FlowGUID, string xml) { DataResult GetAppUserResult = new DataResult(); try { string StateName = null; if (FlowGUID == "" || FlowGUID == "StartFlow") { StateName = "StartFlow"; } else { //根据待审批流程GUID,检索待审批状态节点代码 List<FLOW_FLOWRECORDDETAIL_T> FlowRecord = FlowBLL2.GetFlowInfo(con, "", FlowGUID, "", "", "", "", "", null); if (FlowRecord == null) { GetAppUserResult.Err = "没有待处理的审核"; GetAppUserResult.UserInfo = null; return GetAppUserResult; } StateName = FlowRecord[0].STATECODE; } //根据公司ID,模块代码获取配置的流程 WorkflowInstance instance = null; LogHelper.WriteLog("根据公司ID,模块代码获取配置的流程FlowBLL2.GetFlowByModelName:OgrType='0'"); List<FLOW_MODELFLOWRELATION_T> MODELFLOWRELATION = FlowBLL2.GetFlowByModelName(con, CompanyID, "", ModelCode, "0"); if (MODELFLOWRELATION == null || MODELFLOWRELATION.Count == 0) { GetAppUserResult.Err = "没有可使用的流程"; GetAppUserResult.UserInfo = null; return GetAppUserResult; } FLOW_FLOWDEFINE_T Xoml = MODELFLOWRELATION[0].FLOW_FLOWDEFINE_T; XmlReader readerxoml, readerule; StringReader strXoml = new StringReader(Xoml.XOML); StringReader strRules = new StringReader(Xoml.RULES == null ? "" : Xoml.RULES); readerxoml = XmlReader.Create(strXoml); readerule = XmlReader.Create(strRules); WorkflowRuntime workflowRuntime = new WorkflowRuntime(); workflowRuntime.StartRuntime(); FlowEvent ExternalEvent = new FlowEvent(); ExternalDataExchangeService objService = new ExternalDataExchangeService(); workflowRuntime.AddService(objService); objService.AddService(ExternalEvent); TypeProvider typeProvider = new TypeProvider(null); workflowRuntime.AddService(typeProvider); //XmlReader readerxoml = XmlReader.Create(HttpContext.Current.Server.MapPath ("TestFlow.xml")); // instance = workflowRuntime.CreateWorkflow(readerxoml); if (Xoml.RULES == null) instance = workflowRuntime.CreateWorkflow(readerxoml); else instance = workflowRuntime.CreateWorkflow(readerxoml, readerule, null); // instance = workflowRuntime.CreateWorkflow(typeof(TestFlow)); instance.Start(); StateMachineWorkflowInstance workflowinstance = new StateMachineWorkflowInstance(workflowRuntime, instance.InstanceId); //从实例中获取定义 if (1 == 2) { System.Workflow.Activities.StateMachineWorkflowActivity smworkflow = new StateMachineWorkflowActivity(); smworkflow = workflowinstance.StateMachineWorkflow; RuleDefinitions ruleDefinitions = smworkflow.GetValue(RuleDefinitions.RuleDefinitionsProperty) as RuleDefinitions; WorkflowMarkupSerializer markupSerializer = new WorkflowMarkupSerializer(); StringBuilder xoml = new StringBuilder(); StringBuilder rule = new StringBuilder(); XmlWriter xmlWriter = XmlWriter.Create(xoml); XmlWriter ruleWriter = XmlWriter.Create(rule); markupSerializer.Serialize(xmlWriter, smworkflow); markupSerializer.Serialize(ruleWriter, ruleDefinitions); xmlWriter.Close(); ruleWriter.Close(); StringReader readxoml = new StringReader(xoml.ToString()); StringReader readrule = new StringReader(rule.ToString()); XmlReader readerxoml2 = XmlReader.Create(readxoml); XmlReader readerrule2 = XmlReader.Create(readrule); WorkflowInstance instance1 = workflowRuntime.CreateWorkflow(readerxoml2, readerrule2, null); instance1.Start(); StateMachineWorkflowInstance workflowinstance1 = new StateMachineWorkflowInstance(workflowRuntime, instance1.InstanceId); workflowinstance1.SetState(StateName); } //从实例中获取定义并启动新实例 //跳转到节点StateName workflowinstance.SetState(StateName); FlowDataType.FlowData FlowData = new FlowDataType.FlowData(); FlowData.xml = xml; // FlowData.Flow_FlowRecord_T = null; ExternalEvent.OnDoFlow(instance.InstanceId, FlowData);//激发流程引擎流转到下一状态 System.Threading.Thread.Sleep(1000); PermissionServiceClient WcfPermissionService = new PermissionServiceClient(); string CurrentStateName = workflowinstance.CurrentStateName == null ? "End" : workflowinstance.CurrentStateName; //取得当前状态 List<UserInfo> listUser = new List<UserInfo>(); if (CurrentStateName != "End") { if (CurrentStateName.Substring(0, 5) == "State") { CurrentStateName = CurrentStateName.Substring(5); } string WFCurrentStateName = new Guid(CurrentStateName).ToString("D"); T_SYS_USER[] User = WcfPermissionService.GetSysUserByRole(WFCurrentStateName); //检索本状态(角色)对应用户 if (User != null) for (int i = 0; i < User.Length; i++) { UserInfo tmp = new UserInfo(); tmp.UserID = User[i].EMPLOYEEID; tmp.UserName = User[i].EMPLOYEENAME; listUser.Add(tmp); } } else { //已经到流程结束状态 UserInfo tmp = new UserInfo(); tmp.UserID = "End"; tmp.UserName = "******"; listUser.Add(tmp); } GetAppUserResult.UserInfo = listUser.Count > 0 ? listUser : null; if (GetAppUserResult.UserInfo == null) GetAppUserResult.Err = "没有找到用户"; return GetAppUserResult; // return listUser; //return workflowinstance.CurrentStateName == null ? "End" : workflowinstance.CurrentStateName; } catch (Exception ex) { GetAppUserResult.Err = ex.Message; GetAppUserResult.UserInfo = null; return GetAppUserResult; } }
/// <summary> /// 克隆一个实例 /// </summary> /// <param name="WfRuntimeClone"></param> /// <param name="instanceClone"></param> /// <param name="WfRuntime"></param> /// <returns></returns> public static WorkflowInstance CloneWorkflowInstance(WorkflowRuntime WfRuntimeClone, WorkflowInstance instanceClone, WorkflowRuntime WfRuntime) { try { if (!WfRuntimeClone.IsStarted) { WfRuntimeClone.StartRuntime(); } StateMachineWorkflowInstance workflowinstance = new StateMachineWorkflowInstance(WfRuntimeClone, instanceClone.InstanceId); System.Workflow.Activities.StateMachineWorkflowActivity smworkflow = new StateMachineWorkflowActivity(); smworkflow = workflowinstance.StateMachineWorkflow; RuleDefinitions ruleDefinitions = smworkflow.GetValue(RuleDefinitions.RuleDefinitionsProperty) as RuleDefinitions; WorkflowMarkupSerializer markupSerializer = new WorkflowMarkupSerializer(); StringBuilder xoml = new StringBuilder(); StringBuilder rule = new StringBuilder(); XmlWriter xmlWriter = XmlWriter.Create(xoml); XmlWriter ruleWriter = XmlWriter.Create(rule); markupSerializer.Serialize(xmlWriter, smworkflow); if (ruleDefinitions != null) markupSerializer.Serialize(ruleWriter, ruleDefinitions); xmlWriter.Close(); ruleWriter.Close(); StringReader readxoml = new StringReader(xoml.ToString()); XmlReader readerxoml = XmlReader.Create(readxoml); WorkflowInstance instance; if (ruleDefinitions == null) instance = WfRuntime.CreateWorkflow(readerxoml); else { StringReader readrule = new StringReader(rule.ToString()); XmlReader readerrule = XmlReader.Create(readrule); instance = WfRuntime.CreateWorkflow(readerxoml, readerrule, null); } instance.Start(); return instance; } catch (Exception ex) { LogHelper.WriteLog("CloneWorkflowInstance异常信息 :" + ex.ToString()); throw new Exception(ex.Message); } }