private int CheckDecision(int flowStepIndex, DecisionFlowStepBase decisionFlowStep, FlowDefinition <TFlowRequest, TFlowResponse> flowDefinition, FlowContext stepFlowContext, FlowValues flowValues, FlowTrace flowTrace) { var decisionRequest = (FlowDecisionBase)CreateRequest(stepFlowContext, decisionFlowStep, flowValues); // TODO: Pass the targets directly into GetMatchingBranchIndex decisionFlowStep.Branches.ForEach(b => decisionRequest.AddBranch(b.Targets)); _logger?.LogDecisionRequest(stepFlowContext, decisionRequest); var branchIndex = decisionRequest.GetMatchingBranchIndex(); if (branchIndex < 0 || branchIndex >= decisionFlowStep.Branches.Count) { throw new FlowException($"The branch index returned was out of bounds of the branch array: {branchIndex}"); } var branch = decisionFlowStep.Branches[branchIndex]; flowTrace.AddStep(new FlowTraceStep { StepType = FlowTraceStepType.Decision, Name = decisionFlowStep.Name, BranchTargets = branch.Targets }); _logger?.LogDecisionResponse(stepFlowContext, branch); if (branch.IsEnd) { return(int.MaxValue); } if (branch.IsUnhandled) { throw new FlowUnhandledElseException($"Unhandled ELSE for decision '{decisionFlowStep.Name}'"); } var isContinue = (branch.NextStepName == null); if (isContinue) { return(flowStepIndex + 1); } var nextFlowStepIndex = flowDefinition.GetStepIndex(branch.NextStepName); return(nextFlowStepIndex); }
private async Task <int> PerformFlowStep(FlowContext stepFlowContext, FlowDefinition <TFlowRequest, TFlowResponse> flowDefinition, FlowStep flowStep, int flowStepIndex, FlowValues flowValues, FlowTrace flowTrace, CancellationToken cancellationToken) { int nextFlowStepIndex; switch (flowStep) { case ActivityFlowStep activityFlowStep: await DoActivity( activityFlowStep, stepFlowContext, flowValues, flowTrace, cancellationToken); nextFlowStepIndex = flowStepIndex + 1; break; case DecisionFlowStepBase decisionFlowStep: nextFlowStepIndex = CheckDecision( flowStepIndex, decisionFlowStep, flowDefinition, stepFlowContext, flowValues, flowTrace); break; case GotoFlowStep gotoFlowStep: _logger.LogGoto(stepFlowContext, gotoFlowStep.NextStepName); nextFlowStepIndex = flowDefinition.GetStepIndex(gotoFlowStep.NextStepName); break; case LabelFlowStep _: RecordLabelStep(flowTrace, stepFlowContext); nextFlowStepIndex = flowStepIndex + 1; break; case EndFlowStep _: nextFlowStepIndex = int.MaxValue; break; default: throw new FlowException( $"Unexpected flow activityFlowStep type {flowStep.GetType().FullName}"); } return(nextFlowStepIndex); }