private bool TrySelectDefault(ItemWithScore[] itemsWithCalculatedScore, out ItemEvaluation? defaultItem) { var itemsWithNoReferences = itemsWithCalculatedScore.Where(x => !x.Item.ConditionsEvaluation.Any()); if (itemsWithNoReferences.Count() != 1) { defaultItem = null; return false; } defaultItem = itemsWithNoReferences.Single().Item; return true; }
private bool IsRelevant(ItemEvaluation itemEvaluation) { var groupedBySubject = itemEvaluation.ConditionsEvaluation.GroupBy(x => x.Context.CurrentRuntimeContextItem.Key); foreach (var group in groupedBySubject) { if(!IsRelevantToSubject(group)) { return false; } } return true; }
private ItemEvaluation SelectBestMatchItem(ItemEvaluation[] evaluatedItems) { var itemsWithCalculatedScore = CalculateScore(evaluatedItems); var highestScore = itemsWithCalculatedScore.Max(x => x.Score); var itemWithHighestScores = itemsWithCalculatedScore.Where(x => x.Score == highestScore).ToArray(); ItemEvaluation? defaultItem; if (itemWithHighestScores.Count() > 1) { if (this.TrySelectDefault(itemsWithCalculatedScore, out defaultItem)) { return defaultItem.Value; } else { throw new ItemsWithConflictingHighestScoreException(itemsWithConflictingScore: itemWithHighestScores, allItems: itemsWithCalculatedScore, highestScore: highestScore); } } return itemWithHighestScores.Single().Item; }