PlotDirector/PlotLine/Services/Investigations/EditorialQuestionSelection.cs

31 lines
2.2 KiB
C#

using PlotLine.Models.Investigations;
namespace PlotLine.Services.Investigations;
// An answer activates relevant candidates, never a fixed next-question pointer.
public static class EditorialQuestionSelection
{
public static List<CandidateQuestion> Select(Catalogue c,string concern,Dictionary<string,Answer> answers,Evaluation e)
{
var editorial=c.Editorial!;var active=editorial.Routes[concern].ToHashSet();
foreach(var a in answers.Values)active.UnionWith(editorial.FollowUps[a.Question+"."+a.Option].Questions);
var leaders=e.Hypotheses.Where(h=>h.Support+h.Opposition>=2).Take(3).ToArray();
foreach(var h in leaders)foreach(var q in c.Questions.Where(q=>q.Hypotheses.Contains(h.Id)))active.Add(q.Id);
var covered=e.Evidence.Where(v=>!v.Uncertain&&v.Raw!=0).Select(v=>(v.Hypothesis,v.Dimension)).ToHashSet();
var groups=e.Evidence.Where(v=>!v.Uncertain&&v.Raw>0).Select(v=>v.Group).ToHashSet();
var candidates=new List<CandidateQuestion>();
foreach(var q in c.Questions.Where(q=>q.Id!=editorial.OpeningQuestion&&!q.Retired&&!answers.ContainsKey(q.Id)&&active.Contains(q.Id)&&InvestigationEngine.Allows(q.Eligibility,answers)))
{
var effects=q.Options.Where(o=>!o.Uncertain).SelectMany(o=>o.Contributions).Where(v=>v.Weight!=0).ToArray();
// Preference/symptom repetitions sharing one authored group cannot buy another question or corroboration.
if(q.Purpose=="Discrimination"&&groups.Contains(q.Group))continue;
var relevant=leaders.Count(h=>q.Hypotheses.Contains(h.Id));
var gaps=effects.Select(v=>(v.Hypothesis,v.Dimension)).Distinct().Count(v=>!covered.Contains(v));
var challenge=leaders.Count(h=>!h.Challenged&&editorial.Challenges.GetValueOrDefault(h.Id,[]).Contains(q.Id));
var conflicts=leaders.Count(h=>h.Conflicts.Length>0&&q.Hypotheses.Contains(h.Id));
var rank=q.InformationValue+relevant*5+Math.Min(4,gaps)+challenge*12+conflicts*3+(editorial.Routes[concern].Contains(q.Id)?6:0);
candidates.Add(new(q.Id,rank,$"Authored relevance links; leading explanations {relevant}; new evidence dimensions {gaps}; challenge opportunities {challenge}; conflicting explanations {conflicts}; opening-route relevance {editorial.Routes[concern].Contains(q.Id)}."));
}
return candidates;
}
}