Example #1
0
        private bool EvaluateSituation(ref IParametersCollection parameters, IStep step, out IStep stepActiveSituation)
        {
            if (parameters is null)
            {
                throw new ArgumentNullException(nameof(parameters));
            }

            stepActiveSituation = step.Clone();
            var match = false;
            // evaluate if the situation (condition) is appropiate, otherwise skip it.
            // not in input parameters. Evaluate the
            var parameter = parameters.GetParameter(step.Situation);

            if (step.Situation.Contains(','))
            {
                foreach (var inclusiveSituation in step.Situation.Split(',')
                         .Select(x => x.Trim())
                         .Where(x => !string.IsNullOrWhiteSpace(x))
                         .ToArray())
                {
                    parameter = parameters.GetParameter(inclusiveSituation);
                    if (parameter != null && parameter.Type == TypeEnum.Boolean)
                    {
                        if ((bool)parameter.Value == true)
                        {
                            stepActiveSituation             = step.Clone();
                            stepActiveSituation.Situation   = inclusiveSituation;
                            stepActiveSituation.SemanticKey = string.Join('.', stepActiveSituation.SemanticKey.Split('.').Take(3).ToArray()) + "." + inclusiveSituation;  // this is a hack, please refactor.
                        }
                        // seach in  content nodes for this parameter in typenum steps
                        //var s = ContentNodes.FindAll(p => p.Parameter.Type == TypeEnum.Step);
                        return(true);
                    }
                }
                throw new StepException($"Can't resolve any of the inclusive situations ${step.Situation}. One of these parameters must exist and be of boolean type.", step);
            }

            if (parameter == null)
            {
                // resolve parameter value from named formula.
                var formula = GetFormula(step.Situation);
                if (formula == null)
                {
                    throw new StepException($"can't resolve parameter '${parameter.Name}' to formula", step);
                }
                // execute the formula which adds the outcome to parameters.
                var context = new FormulaExpressionContext(ref _model, ref parameters, formula, QuestionCallback, this);
                var result  = context.Evaluate();
                //var result = Execute(ref context, ref formula, ref parameters, ref executionResult);
                if (result.Value.GetType() != typeof(bool))
                {
                    throw new StepException($"Expected situation to evalute to boolean value in worflow step {step.Name} {step.Description} but got value: {result.Value} of type {result.Value.GetType().Name} instead.", step);
                }
                match = (bool)result.Value;
            }
            else
            {
                if ((bool)parameter.Value.Infer())
                {
                    // execute this step.
                    match = true;
                }
            }
            return(match);
        }