Example #1
0
        /// <summary>
        /// Find an available setting value.
        /// </summary>
        /// <param name="availableSetting">The available setting that the value belongs to. If this is null, then null is returned.</param>
        /// <param name="settingValue">The name of the setting value to look for.</param>
        /// <returns>The available setting value or null if no such value was found.</returns>
        public AvailableSettingValue FindSettingValue(AvailableSetting availableSetting, string settingValue)
        {
            if (availableSetting == null)
            {
                throw new ArgumentNullException(nameof(availableSetting));
            }

            if (availableSetting != null && availableSetting.Values != null)
            {
                foreach (var availableValue in availableSetting.Values)
                {
                    if (Util.NullSafeEquals(availableValue.CanonicalName, settingValue))
                    {
                        return(availableValue);
                    }
                }
            }

            return(null);
        }
Example #2
0
        private IList <T> ApplySelectionToSettings <T>(AutomotiveSkillState state, List <string> settingEntities, IList <T> changesOrStatuses)
            where T : SettingOperation
        {
            if (state == null)
            {
                throw new ArgumentNullException(nameof(state));
            }

            if (settingEntities == null)
            {
                throw new ArgumentNullException(nameof(settingEntities));
            }

            if (changesOrStatuses == null)
            {
                throw new ArgumentNullException(nameof(changesOrStatuses));
            }

            var settingNames = state.GetUniqueSettingNames();

            ISet <string> selectedSettingNames = new HashSet <string>();

            if (settingEntities.Any() && settingNames.Any())
            {
                IList <AvailableSetting> resolvedSettings = new List <AvailableSetting>();
                foreach (var settingName in settingNames)
                {
                    var setting = this.settingList.FindSetting(settingName);
                    if (setting != null)
                    {
                        resolvedSettings.Add(setting);
                    }
                    else
                    {
                        setting = new AvailableSetting
                        {
                            CanonicalName = settingName
                        };
                        resolvedSettings.Add(setting);
                    }
                }

                IList <AvailableSetting> settings_to_select_from = Util.CopyList(resolvedSettings);
                foreach (var setting in resolvedSettings)
                {
                    if (setting.IncludedSettings != null)
                    {
                        foreach (var included_setting_name in setting.IncludedSettings)
                        {
                            if (!settingNames.Contains(included_setting_name))
                            {
                                var included_setting = this.settingList.FindSetting(included_setting_name);
                                if (included_setting == null)
                                {
                                    // Unreachable.
                                    throw new Exception("The included settings of setting \"" + setting.CanonicalName
                                                        + "\" must be canonical names of other settings, but \"" + included_setting_name
                                                        + "\" is not and this should already have been checked when loading the SettingList.");
                                }

                                settings_to_select_from.Add(included_setting);
                            }
                        }
                    }
                }

                var setting_matcher   = new SettingMatcher(this.settingList.CreateSubList(settings_to_select_from));
                var selected_settings = setting_matcher.MatchSettingNamesExactly(settingEntities.First());

                if (!selected_settings.Any())
                {
                    selected_settings = setting_matcher.MatchSettingNames(
                        settingEntities,
                        SettingNameScoreThreshold, SettingNameAntonymDisambPercentageOfMax, true);
                }

                foreach (var setting_info in selected_settings)
                {
                    selectedSettingNames.Add(setting_info.CanonicalName);
                }
            }

            IList <T>     newCandidates     = new List <T>();
            ISet <string> addedSettingNames = new HashSet <string>();

            foreach (var candidate in changesOrStatuses)
            {
                if (candidate == null)
                {
                    continue;
                }

                if (selectedSettingNames.Contains(candidate.SettingName))
                {
                    newCandidates.Add(candidate);
                    addedSettingNames.Add(candidate.SettingName);
                }
            }

            // If NLP tells us to select something that isn't on the list,
            // it's because it's included in one of the settings on the list.
            foreach (var selectedName in selectedSettingNames)
            {
                if (!addedSettingNames.Contains(selectedName))
                {
                    // This search is inefficient, but the lists will be short, so it doesn't matter.
                    foreach (var candidate in changesOrStatuses)
                    {
                        var supportedSetting = settingList.FindSetting(candidate.SettingName);
                        if (supportedSetting != null && supportedSetting.IncludedSettings != null && supportedSetting.IncludedSettings.Contains(selectedName))
                        {
                            var newCandidate = (T)candidate.Clone();
                            newCandidate.SettingName = selectedName;
                            newCandidates.Add(newCandidate);
                            break;
                        }
                    }
                }
            }

            if (!Util.IsNullOrEmpty(newCandidates))
            {
                return(newCandidates);
            }

            return(changesOrStatuses);
        }