/// <summary>
        /// Searches for a <see cref="IValueSet"/> for the given <see cref="Option"/> and <see cref="ActualFiniteState"/>.
        /// </summary>
        /// <param name="option">The <see cref="Option"/></param>
        /// <param name="actualState">The <see cref="ActualFiniteState"/></param>
        /// <returns>The <see cref="IValueSet"/></returns>
        public IValueSet QueryParameterBaseValueSet(Option option, ActualFiniteState actualState)
        {
            var valueSets = this.ValueSets.ToList();

            if (!valueSets.Any())
            {
                throw new IncompleteModelException($"{this.GetType().Name} {this.UserFriendlyName} doesn't contain any values.");
            }

            if (this.IsOptionDependent)
            {
                if (option == null)
                {
                    throw new ArgumentNullException($"{this.GetType().Name} {this.UserFriendlyName} is option dependent. The {nameof(option)} cannot be null.");
                }

                valueSets = valueSets.Where(x => x.ActualOption == option).ToList();

                if (!valueSets.Any())
                {
                    throw new ArgumentException($"{this.GetType().Name} {this.UserFriendlyName} doesn't have values for {nameof(Option)} {option.Name}.");
                }
            }

            if (this.StateDependence != null)
            {
                if (actualState == null)
                {
                    throw new ArgumentNullException($"{this.GetType().Name} {this.UserFriendlyName} is state dependent. The {nameof(actualState)} property cannot be null.");
                }

                valueSets = valueSets.Where(x => x.ActualState == actualState).ToList();

                if (!valueSets.Any())
                {
                    throw new ArgumentException($"{this.GetType().Name} {this.UserFriendlyName} doesn't have values for {nameof(ActualFiniteState)} {actualState.Name}.");
                }
            }

            if (valueSets.Count > 1)
            {
                throw new Cdp4ModelValidationException(
                          $"Multiple ValueSets found for {this.GetType().Name} {this.UserFriendlyName}" +
                          $" having {nameof(option)} = {option?.Name ?? "<empty>"} and {nameof(actualState)} = {actualState?.Name ?? "<empty>"}");
            }

            return(valueSets.First());
        }
Esempio n. 2
0
        /// <summary>
        /// Validate the value-sets of this <see cref="Parameter"/> for an option and state if applicable
        /// </summary>
        /// <param name="valueSets">The <see cref="ParameterValueSet"/>s found for the corresponding option and state</param>
        /// <param name="option">The <see cref="Option"/></param>
        /// <param name="state">The <see cref="ActualFiniteState"/></param>
        /// <returns>a list of error messages</returns>
        private IEnumerable <string> ValidateValueSets(IEnumerable <ParameterValueSet> valueSets, Option option, ActualFiniteState state)
        {
            var errorList = new List <string>();
            var valuesets = valueSets.ToList();

            if (valuesets.Count == 0)
            {
                errorList.Add($"No value-set was found for the option {((option == null) ? "-" : option.Name)} and state {((state == null) ? "-" : state.Name)}");
            }
            else if (valuesets.Count > 1)
            {
                errorList.Add($"Duplicated value-sets were found for the option {((option == null) ? "-" : option.Name)} and state {((state == null) ? "-" : state.Name)}");
            }
            else
            {
                var valueset = valuesets.Single();
                errorList.AddRange(valueset.ValidationErrors);
            }

            return(errorList);
        }