34 lines
4.8 KiB
C#
34 lines
4.8 KiB
C#
using PlotLine.Data;
|
|
using PlotLine.Models.Investigations;
|
|
namespace PlotLine.Services.Investigations;
|
|
public sealed class InvestigationAccess(IWebHostEnvironment environment,IConfiguration configuration)
|
|
{
|
|
public bool Enabled=>environment.IsDevelopment()&&configuration.GetValue("WriterInvestigations:Enabled",true);
|
|
}
|
|
public sealed class InvestigationService(IInvestigationRepository repository,IBookRepository books,IProjectAccessService access,TimeProvider clock,IConfiguration configuration)
|
|
{
|
|
public async Task<int> Project(int book){if(!await access.CanAccessEntityAsync("Book",book))throw new UnauthorizedAccessException();var b=await books.GetAsync(book);if(b==null||b.IsArchived)throw new UnauthorizedAccessException();return b.ProjectID;}
|
|
public static HashSet<string> Available(Catalogue c,Investigation i)=>c.Capabilities.Where(d=>DestinationRegistry.Resolve(d,i.ProjectID,i.BookID,true,i.Answers.ToDictionary(a=>a.Question))!=null).Select(d=>d.Id).ToHashSet();
|
|
public static bool CanOpen(Catalogue c,Evaluation state,string capability)=>state.AvailableCapabilities.Contains(capability)&&state.Recommendations.Any(id=>c.Hypotheses.Single(h=>h.Id==id).Capabilities.Contains(capability));
|
|
public static Evaluation Evaluate(Catalogue c,Investigation i)=>InvestigationEngine.Evaluate(c,i.Concern,i.Answers,Available(c,i),i.ContinueUntil);
|
|
public Evaluation Current(Catalogue c,Investigation i)=>InvestigationEngine.Evaluate(c,i.Concern,i.Answers,Available(c,i).Where(id=>configuration.GetValue($"WriterInvestigations:Capabilities:{id}:Enabled",true)).ToHashSet(),i.ContinueUntil);
|
|
public async Task<(Investigation,Catalogue)> Read(int user,int book,Guid id){var project=await Project(book);var row=await repository.Read(user,book,id)??throw new KeyNotFoundException();if(row.UserID!=user||row.BookID!=book||row.ProjectID!=project)throw new UnauthorizedAccessException();return(row,await repository.Catalogue(row.CatalogueVersion)??throw new KeyNotFoundException());}
|
|
public Task<Catalogue?> AuthorCatalogue()=>repository.Catalogue(configuration.GetValue("WriterInvestigations:AuthorCatalogue","guided-investigation-production-v0.1"));
|
|
public async Task<Investigation> Start(int user,int book,string concern,string? catalogueVersion=null,Guid? requestId=null){var project=await Project(book);if(requestId is {} existing&&existing!=Guid.Empty&&await repository.Read(user,book,existing) is {} prior){if(prior.UserID!=user||prior.BookID!=book||prior.Concern!=concern)throw new ArgumentException("Start request changed. Refresh the starting concerns.");return prior;}var c=await (catalogueVersion==null?AuthorCatalogue():repository.Catalogue(catalogueVersion))??throw new ArgumentException("The investigation catalogue is not yet available.");var i=new Investigation{ID=requestId is {} key&&key!=Guid.Empty?key:Guid.NewGuid(),UserID=user,ProjectID=project,BookID=book,CatalogueVersion=c.Version,Concern=concern,CreatedUtc=clock.GetUtcNow().UtcDateTime};await Persist(i,c,0,"start");return i;}
|
|
public async Task Persist(Investigation i,Catalogue c,int expected,string operation)
|
|
{
|
|
var state=Current(c,i);i.CurrentQuestion=state.NextQuestion;
|
|
if(state.NextQuestion!=null&&!i.Presented.Contains(state.NextQuestion))i.Presented.Add(state.NextQuestion);
|
|
if(state.Outcome!="Investigating"&&(i.Recommendations.LastOrDefault() is not {} last||last.AnswerCount!=i.Answers.Count||last.Outcome!=state.Outcome||!last.Hypotheses.SequenceEqual(state.Recommendations)))i.Recommendations.Add(new(i.Answers.Count,state.Outcome,state.Recommendations.ToList(),clock.GetUtcNow().UtcDateTime));
|
|
i.UpdatedUtc=clock.GetUtcNow().UtcDateTime;await repository.Save(i,expected,operation,state);
|
|
}
|
|
public static void Answer(Investigation i,Catalogue c,string question,string option,string? explanation)
|
|
{
|
|
if(!i.Presented.Contains(question))throw new ArgumentException("This question has not been presented.");var q=c.Questions.SingleOrDefault(q=>q.Id==question)??throw new ArgumentException("Unknown question.");if(!q.Options.Any(o=>o.Id==option))throw new ArgumentException("Choose an answer.");if((explanation?.Length??0)>2000)throw new ArgumentException("Please keep your explanation within2,000 characters.");
|
|
var existing=i.Answers.FindIndex(a=>a.Question==question);var next=new Answer(question,option,q.AllowExplanation?explanation??"":"");if(existing>=0)i.Answers[existing]=next;else i.Answers.Add(next);
|
|
// Revision may invalidate later prerequisite-dependent answers. Keep them in event history, not live evidence.
|
|
bool removed;do{removed=false;foreach(var a in i.Answers.ToArray()){var others=i.Answers.Where(x=>x.Question!=a.Question).ToDictionary(x=>x.Question);if(!InvestigationEngine.Allows(c.Questions.Single(q=>q.Id==a.Question).Eligibility,others)){i.Answers.Remove(a);removed=true;}}}while(removed);
|
|
i.DraftQuestion=null;i.DraftExplanation=null;i.Accepted=false;i.SelectedDestination=null;
|
|
}
|
|
}
|