Files
Chronos/src/Titano.Core/Advice/Advisor.cs
T
Claude 52cde1419b refactor(core): name the advisor namespace Advice; record advisor decisions
The Advisor class shared its namespace's name, forcing an alias in every
Titano.Core caller.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
2026-10-02 11:07:02 +00:00

92 lines
4.8 KiB
C#

using System.Globalization;
using Titano.Core.Analysis;
using Titano.Core.Analysis.Deep;
using Titano.Core.Localization;
using Titano.Core.Project;
namespace Titano.Core.Advice;
/// <summary>
/// The analysis advisor (BRIEF §10.3, §10.4): turns the light and deep analyses into explained recommendations
/// (<see cref="Recommend"/>), stores them as Auto values without touching manual ones (<see cref="Apply"/>), and takes
/// the heavier per-section decisions on request (<see cref="Optimise"/>). Deterministic; every reason is a resource
/// key plus arguments.
/// </summary>
public static class Advisor
{
/// <summary>
/// Recommendations for every parameter the rules cover (see <see cref="AdvisorRules"/>), in section order, plus
/// every finding of both analyses. Without a deep report, pixel-based parameters get missing-data recommendations.
/// </summary>
public static AdvisorResult Recommend(LightAnalysisReport light, DeepAnalysisReport? deep, SequenceContext context)
{
ArgumentNullException.ThrowIfNull(light);
ArgumentNullException.ThrowIfNull(context);
var findings = new List<Finding>(light.Findings);
if (deep is not null) findings.AddRange(deep.Findings.Select(f => f.ToFinding()));
return new AdvisorResult(new AdvisorRules(light, deep, context).Run(), findings);
}
/// <summary>
/// Fills <see cref="Auto{T}.AutoValue"/>, <see cref="Auto{T}.ReasonKey"/> and <see cref="Auto{T}.ReasonArgs"/> of every
/// advised parameter. Manual values stay in force; missing data changes no value (its reason is stored only where no
/// advice exists yet). Plain, non-Auto settings are left to <see cref="Optimise"/>.
/// </summary>
public static ProjectSettings Apply(ProjectSettings settings, AdvisorResult result)
{
ArgumentNullException.ThrowIfNull(settings);
ArgumentNullException.ThrowIfNull(result);
foreach (var recommendation in result.Recommendations)
{
if (ParameterBindings.Find(recommendation.Parameter)?.Advise is { } advise) settings = advise(settings, recommendation);
}
return settings;
}
/// <summary>
/// "Optimise section" (BRIEF §10.4): applies the section's advice, returns module switches (deflicker, regions, holy
/// grail, white balance, motion blur, stabilisation, timing mode, export preset) to Auto where the advice differs
/// from the value in force, and takes the plain decisions (fit-to duration, hero frame when unset, exclusions added
/// to the user's, crop placement when centred, output size cap). Missing data changes nothing.
/// Returns every effective value that changed, with its reason.
/// </summary>
public static OptimiseResult Optimise(AdvisorSection section, ProjectSettings settings, AdvisorResult result)
{
ArgumentNullException.ThrowIfNull(settings);
ArgumentNullException.ThrowIfNull(result);
var optimised = settings;
var changes = new List<SettingChange>();
foreach (var binding in ParameterBindings.All)
{
if (binding.Section != section || result.Find(binding.Parameter) is not { } recommendation) continue;
if (binding.Advise is { } advise) optimised = advise(optimised, recommendation);
if (recommendation.Value is null) continue;
if (binding.IsModuleSwitch && !ParameterBindings.SameValue(binding.Effective(optimised), recommendation.Value)) optimised = binding.ResetToAuto!(optimised);
if (binding.Set is { } set) optimised = set(optimised, recommendation);
var before = binding.Effective(settings);
var after = binding.Effective(optimised);
if (!ParameterBindings.SameValue(before, after))
changes.Add(new SettingChange(binding.Parameter, before, after, recommendation.ReasonKey, recommendation.ReasonArgs));
}
return new OptimiseResult(optimised, changes);
}
/// <summary>Localised text of a recommendation in the current UI culture.</summary>
public static string Explain(Recommendation recommendation)
{
ArgumentNullException.ThrowIfNull(recommendation);
return Loc.L(recommendation.ReasonKey, [.. recommendation.ReasonArgs]);
}
/// <summary>
/// Localised text of a stored reason (<see cref="Auto{T}.ReasonKey"/>/<see cref="Auto{T}.ReasonArgs"/>): numeric
/// arguments, stored culture-neutral, are parsed back so they format with the current culture.
/// </summary>
public static string Explain(string reasonKey, IReadOnlyList<string>? reasonArgs)
{
ArgumentNullException.ThrowIfNull(reasonKey);
var args = (reasonArgs ?? []).Select(a => double.TryParse(a, NumberStyles.Float, CultureInfo.InvariantCulture, out var number) ? (object)number : a).ToArray();
return Loc.L(reasonKey, args);
}
}