/// <summary> /// 将步骤分段 /// </summary> void StepToSegment() { while (stepLst.Count > 0) { // 查找一个输入数为0的步骤,作为线段的开头 var startStep = stepLst.Find(step => (int)step["IN_NUM_NOW"] == 0); // 创建一个线段 StepSegment segment = new StepSegment(this); AddStepToSegment(startStep, segment); while (!segment.IsEnd) { // 因为segment未结尾,所以有且只有一个relation var relation = relationLst.Find(rel => Equals(rel["FRONT_STEP_ID"], segment.LastStepId)); // 下一步骤 var nextStep = stepLst.Find(step => Equals(step["ID"], relation["AFTER_STEP_ID"])); // 存在下一步骤,且下一步骤输入数为1 if (nextStep != null && (int)nextStep["IN_NUM"] == 1) { AddStepToSegment(nextStep, segment); relationLst.Remove(relation); } else { break; } } segmentList.Add(segment); } }
/// <summary> /// 按顺序执行步骤 /// </summary> public bool Execute() { object lastOutput = null; try { CreateLog(); foreach (StepSegment segment in segmentList) { var inputDict = new Dictionary <string, object>(); // 查找所有输入 var frontRelations = relationLst.Where(rel => Equals(rel["AFTER_STEP_ID"], segment.FirstStepId)); foreach (var relation in frontRelations) { StepSegment frontSegment = segmentList.Find(seg => Equals(relation["FRONT_STEP_ID"], seg.LastStepId)); string inputName = relation["INPUT_NAME"].ToString(); inputDict.Add(inputName, frontSegment.Output); } segment.Input = inputDict; segment.Execute(); lastOutput = segment.Output; } FinishLog(1, "成功"); return(true); } catch (Exception ex) { Console.WriteLine(ex); FinishLog(-1, ex.Message); return(false); } }
/// <summary> /// 将线段分级 /// </summary> void ClassifySegment(StepSegment segment) { // 查找所有前置线段 var frontSegments = relationLst.Where(rel => Equals(rel["AFTER_STEP_ID"], segment.FirstStepId)) .Select(rel => segmentList.Find(seg => Equals(rel["FRONT_STEP_ID"], seg.LastStepId))); foreach (var frontSegment in frontSegments) { // 评级以【与初始评级之差的绝对值】大的为准 if (frontSegment.Level == int.MinValue || Math.Abs(segment.Level - 1) > Math.Abs(frontSegment.Level - StepSegment.InitLevel)) { frontSegment.Level = segment.Level - 1; ClassifySegment(frontSegment); } } // 查找所有后置线段 var afterSegments = relationLst.Where(rel => Equals(rel["FRONT_STEP_ID"], segment.LastStepId)) .Select(rel => segmentList.Find(seg => Equals(rel["AFTER_STEP_ID"], seg.FirstStepId))); foreach (var afterSegment in afterSegments) { // 评级以【与初始评级之差的绝对值】大的为准 if (afterSegment.Level == int.MinValue || Math.Abs(segment.Level + 1) > Math.Abs(afterSegment.Level - StepSegment.InitLevel)) { afterSegment.Level = segment.Level + 1; ClassifySegment(afterSegment); } } }
/// <summary> /// 将步骤添加到线段中,并更新下一步骤的输入数 /// </summary> /// <param name="moveStep">要添加的步骤</param> /// <param name="segment"></param> void AddStepToSegment(Dictionary <string, object> moveStep, StepSegment segment) { // 查找关联 var relations = relationLst.Where(rel => Equals(rel["FRONT_STEP_ID"], moveStep["ID"])); // 查找下一步骤,并将输入数减一 foreach (var relation in relations) { var nextStep = stepLst.Find(step => Equals(step["ID"], relation["AFTER_STEP_ID"])); // 下一步骤 nextStep["IN_NUM_NOW"] = (int)nextStep["IN_NUM_NOW"] - 1; } // 移动步骤 segment.Add(moveStep); stepLst.Remove(moveStep); }