// Copyright (C) Stichting Deltares 2017. All rights reserved.
//
// This file is part of Ringtoets.
//
// Ringtoets is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see .
//
// All names, logos, and references to "Deltares" are registered trademarks of
// Stichting Deltares and remain full property of Stichting Deltares at all times.
// All rights reserved.
using System;
using System.Collections.Generic;
using System.Linq;
using Core.Common.Base.Data;
using log4net;
using Ringtoets.Common.Data.Calculation;
using Ringtoets.Common.Forms.Helpers;
using Ringtoets.MacroStabilityInwards.Data;
using Ringtoets.MacroStabilityInwards.Forms.Properties;
using Ringtoets.MacroStabilityInwards.Primitives;
namespace Ringtoets.MacroStabilityInwards.Forms
{
///
/// Class holds methods to help views when dealing with
///
public static class PipingCalculationConfigurationHelper
{
private static readonly ILog log = LogManager.GetLogger(typeof(PipingCalculationConfigurationHelper));
///
/// Creates a structure of and based on combination of the
/// and the .
///
/// Surface lines to generate the structure for and to use to configure
/// with.
/// The soil models from which profiles are taken to configure with.
/// General input to assign to each generated piping calculation.
/// A structure of matching combinations of and
/// profiles of intersecting .
/// Throw when either:
///
/// - is null
/// - is null
/// - is null
///
public static IEnumerable GenerateCalculationItemsStructure(IEnumerable surfaceLines, IEnumerable soilModels, GeneralMacroStabilityInwardsInput generalInput)
{
if (surfaceLines == null)
{
throw new ArgumentNullException(nameof(surfaceLines));
}
if (soilModels == null)
{
throw new ArgumentNullException(nameof(soilModels));
}
if (generalInput == null)
{
throw new ArgumentNullException(nameof(generalInput));
}
var groups = new List();
foreach (RingtoetsPipingSurfaceLine surfaceLine in surfaceLines)
{
CalculationGroup group = CreateCalculationGroup(surfaceLine, soilModels, generalInput);
if (group.GetCalculations().Any())
{
groups.Add(group);
}
else
{
log.WarnFormat(
Resources.PipingCalculationConfigurationHelper_GenerateCalculationsStructure_No_PipingSoilProfile_found_for_RingtoetsPipingSurfaceLine_0_skipped,
surfaceLine.Name);
}
}
return groups;
}
///
/// Gets the stochastic soil models matching the input of a calculation.
///
/// The surface line used to match a .
/// The available stochastic soil models.
/// The (sub)set of stochastic soil models from
/// or empty if no matching instances can be found
/// or when there is not enough information to associate soil profiles to the calculation.
public static IEnumerable GetStochasticSoilModelsForSurfaceLine(RingtoetsPipingSurfaceLine surfaceLine, IEnumerable availableSoilModels)
{
if (surfaceLine == null)
{
return Enumerable.Empty();
}
return availableSoilModels.Where(stochasticSoilModel => stochasticSoilModel.StochasticSoilProfiles.Any() &&
stochasticSoilModel.IntersectsWithSurfaceLineGeometry(surfaceLine))
.ToList();
}
private static CalculationGroup CreateCalculationGroup(RingtoetsPipingSurfaceLine surfaceLine, IEnumerable soilModels, GeneralMacroStabilityInwardsInput generalInput)
{
var calculationGroup = new CalculationGroup(surfaceLine.Name, true);
IEnumerable stochasticSoilModels = GetStochasticSoilModelsForSurfaceLine(surfaceLine, soilModels);
foreach (StochasticSoilModel stochasticSoilModel in stochasticSoilModels)
{
foreach (StochasticSoilProfile soilProfile in stochasticSoilModel.StochasticSoilProfiles)
{
calculationGroup.Children.Add(CreatePipingCalculation(surfaceLine, stochasticSoilModel, soilProfile, calculationGroup.Children, generalInput));
}
}
return calculationGroup;
}
private static ICalculationBase CreatePipingCalculation(RingtoetsPipingSurfaceLine surfaceLine, StochasticSoilModel stochasticSoilModel, StochasticSoilProfile stochasticSoilProfile, IEnumerable calculations, GeneralMacroStabilityInwardsInput generalInput)
{
string nameBase = $"{surfaceLine.Name} {stochasticSoilProfile}";
string name = NamingHelper.GetUniqueName(calculations, nameBase, c => c.Name);
return new MacroStabilityInwardsCalculationScenario(generalInput)
{
Name = name,
InputParameters =
{
SurfaceLine = surfaceLine,
StochasticSoilModel = stochasticSoilModel,
StochasticSoilProfile = stochasticSoilProfile
},
Contribution = (RoundedDouble) stochasticSoilProfile.Probability
};
}
}
}