// 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.ComponentModel;
using System.Linq;
using Core.Common.Controls.DataGrid;
using Ringtoets.AssemblyTool.Data;
using Ringtoets.Common.Data.AssessmentSection;
using Ringtoets.Common.Data.Calculation;
using Ringtoets.Common.Data.Exceptions;
using Ringtoets.Common.Forms.Helpers;
using Ringtoets.Common.Forms.TypeConverters;
using Ringtoets.Common.Forms.Views;
using Ringtoets.Common.Primitives;
using Ringtoets.MacroStabilityInwards.Data;
using RingtoetsCommonFormsResources = Ringtoets.Common.Forms.Properties.Resources;
namespace Ringtoets.MacroStabilityInwards.Forms.Views
{
///
/// Container of a , which takes care of the
/// representation of properties in a grid.
///
public class MacroStabilityInwardsFailureMechanismSectionResultRow : FailureMechanismSectionResultRow
{
private readonly int simpleAssessmentResultIndex;
private readonly int detailedAssessmentResultIndex;
private readonly int detailedAssessmentProbabilityIndex;
private readonly int tailorMadeAssessmentResultIndex;
private readonly int tailorMadeAssessmentProbabilityIndex;
private readonly int simpleAssemblyCategoryGroupIndex;
private readonly int detailedAssemblyCategoryGroupIndex;
private readonly int tailorMadeAssemblyCategoryGroupIndex;
private readonly int combinedAssemblyCategoryGroupIndex;
private readonly int combinedAssemblyProbabilityIndex;
private readonly int manualAssemblyProbabilityIndex;
private readonly IEnumerable calculations;
private readonly MacroStabilityInwardsFailureMechanism failureMechanism;
private readonly IAssessmentSection assessmentSection;
private FailureMechanismSectionAssemblyCategoryGroup simpleAssemblyCategoryGroup;
private FailureMechanismSectionAssemblyCategoryGroup detailedAssemblyCategoryGroup;
private FailureMechanismSectionAssemblyCategoryGroup tailorMadeAssemblyCategoryGroup;
private FailureMechanismSectionAssemblyCategoryGroup combinedAssemblyCategoryGroup;
///
/// Creates a new instance of .
///
/// The that is
/// the source of this row.
/// All calculations in the failure mechanism.
/// The failure mechanism the section result belongs to.
/// The assessment section the section result belongs to.
/// The property values required to create an instance of
/// .
/// Throw when any parameter is null.
/// Thrown when
/// is a valid value, but unsupported.
internal MacroStabilityInwardsFailureMechanismSectionResultRow(MacroStabilityInwardsFailureMechanismSectionResult sectionResult,
IEnumerable calculations,
MacroStabilityInwardsFailureMechanism failureMechanism,
IAssessmentSection assessmentSection,
ConstructionProperties constructionProperties)
: base(sectionResult)
{
if (calculations == null)
{
throw new ArgumentNullException(nameof(calculations));
}
if (failureMechanism == null)
{
throw new ArgumentNullException(nameof(failureMechanism));
}
if (assessmentSection == null)
{
throw new ArgumentNullException(nameof(assessmentSection));
}
if (constructionProperties == null)
{
throw new ArgumentNullException(nameof(constructionProperties));
}
this.calculations = calculations;
this.failureMechanism = failureMechanism;
this.assessmentSection = assessmentSection;
simpleAssessmentResultIndex = constructionProperties.SimpleAssessmentResultIndex;
detailedAssessmentResultIndex = constructionProperties.DetailedAssessmentResultIndex;
detailedAssessmentProbabilityIndex = constructionProperties.DetailedAssessmentProbabilityIndex;
tailorMadeAssessmentResultIndex = constructionProperties.TailorMadeAssessmentResultIndex;
tailorMadeAssessmentProbabilityIndex = constructionProperties.TailorMadeAssessmentProbabilityIndex;
simpleAssemblyCategoryGroupIndex = constructionProperties.SimpleAssemblyCategoryGroupIndex;
detailedAssemblyCategoryGroupIndex = constructionProperties.DetailedAssemblyCategoryGroupIndex;
tailorMadeAssemblyCategoryGroupIndex = constructionProperties.TailorMadeAssemblyCategoryGroupIndex;
combinedAssemblyCategoryGroupIndex = constructionProperties.CombinedAssemblyCategoryGroupIndex;
combinedAssemblyProbabilityIndex = constructionProperties.CombinedAssemblyProbabilityIndex;
manualAssemblyProbabilityIndex = constructionProperties.ManualAssemblyProbabilityIndex;
CreateColumnStateDefinitions();
Update();
}
///
/// Gets or sets the value representing the simple assessment result.
///
/// Thrown when
/// is a valid value, but unsupported.
public SimpleAssessmentResultType SimpleAssessmentResult
{
get
{
return SectionResult.SimpleAssessmentResult;
}
set
{
SectionResult.SimpleAssessmentResult = value;
UpdateInternalData();
}
}
///
/// Gets or sets the value representing the detailed assessment result.
///
/// Thrown when
/// is a valid value, but unsupported.
public DetailedAssessmentProbabilityOnlyResultType DetailedAssessmentResult
{
get
{
return SectionResult.DetailedAssessmentResult;
}
set
{
SectionResult.DetailedAssessmentResult = value;
UpdateInternalData();
}
}
///
/// Gets the detailed assessment probability a of the .
///
[TypeConverter(typeof(NoProbabilityValueDoubleConverter))]
public double DetailedAssessmentProbability
{
get
{
return SectionResult.GetDetailedAssessmentProbability(calculations, failureMechanism, assessmentSection);
}
}
///
/// Gets or sets the value representing the tailor made assessment result.
///
/// Thrown when
/// is a valid value, but unsupported.
public TailorMadeAssessmentProbabilityCalculationResultType TailorMadeAssessmentResult
{
get
{
return SectionResult.TailorMadeAssessmentResult;
}
set
{
SectionResult.TailorMadeAssessmentResult = value;
UpdateInternalData();
}
}
///
/// Gets or sets the value of the tailored assessment of safety.
///
/// Thrown when is
/// not in the range [0,1].
/// Thrown when
/// is a valid value, but unsupported.
[TypeConverter(typeof(NoProbabilityValueDoubleConverter))]
public double TailorMadeAssessmentProbability
{
get
{
return SectionResult.TailorMadeAssessmentProbability;
}
set
{
SectionResult.TailorMadeAssessmentProbability = value;
UpdateInternalData();
}
}
///
/// Gets the simple assembly category group.
///
public string SimpleAssemblyCategoryGroup
{
get
{
return FailureMechanismSectionAssemblyCategoryGroupHelper.GetCategoryGroupDisplayName(simpleAssemblyCategoryGroup);
}
}
///
/// Gets the detailed assembly category group.
///
public string DetailedAssemblyCategoryGroup
{
get
{
return FailureMechanismSectionAssemblyCategoryGroupHelper.GetCategoryGroupDisplayName(detailedAssemblyCategoryGroup);
}
}
///
/// Gets the tailor made assembly category group.
///
public string TailorMadeAssemblyCategoryGroup
{
get
{
return FailureMechanismSectionAssemblyCategoryGroupHelper.GetCategoryGroupDisplayName(tailorMadeAssemblyCategoryGroup);
}
}
///
/// Gets the combined assembly category group.
///
public string CombinedAssemblyCategoryGroup
{
get
{
return FailureMechanismSectionAssemblyCategoryGroupHelper.GetCategoryGroupDisplayName(combinedAssemblyCategoryGroup);
}
}
///
/// Gets the combined assembly probability.
///
[TypeConverter(typeof(NoProbabilityValueDoubleConverter))]
public double CombinedAssemblyProbability { get; private set; }
///
/// Gets or sets the indicator whether the combined assembly probability
/// should be overwritten by .
///
/// Thrown when
/// is a valid value, but unsupported.
public bool UseManualAssemblyProbability
{
get
{
return SectionResult.UseManualAssemblyProbability;
}
set
{
SectionResult.UseManualAssemblyProbability = value;
UpdateInternalData();
}
}
///
/// Gets or sets the manually entered assembly probability.
///
/// Thrown when is
/// not in the range [0,1].
/// Thrown when
/// is a valid value, but unsupported.
[TypeConverter(typeof(NoProbabilityValueDoubleConverter))]
public double ManualAssemblyProbability
{
get
{
return SectionResult.ManualAssemblyProbability;
}
set
{
SectionResult.ManualAssemblyProbability = value;
UpdateInternalData();
}
}
public override void Update()
{
UpdateDerivedData();
UpdateColumnStateDefinitions();
UpdateDetailedAssessmentProbabilityError();
}
private void UpdateDetailedAssessmentProbabilityError()
{
if (FailureMechanismSectionResultRowHelper.SimpleAssessmentIsSufficient(SimpleAssessmentResult)
|| !FailureMechanismSectionResultRowHelper.DetailedAssessmentResultIsProbability(DetailedAssessmentResult)
|| UseManualAssemblyProbability)
{
ColumnStateDefinitions[detailedAssessmentProbabilityIndex].ErrorText = string.Empty;
}
else
{
ColumnStateDefinitions[detailedAssessmentProbabilityIndex].ErrorText = GetDetailedAssessmentProbabilityError();
}
}
private string GetDetailedAssessmentProbabilityError()
{
MacroStabilityInwardsCalculationScenario[] relevantScenarios = SectionResult.GetCalculationScenarios(calculations).ToArray();
bool relevantScenarioAvailable = relevantScenarios.Length != 0;
if (!relevantScenarioAvailable)
{
return RingtoetsCommonFormsResources.FailureMechanismResultView_DataGridViewCellFormatting_Not_any_calculation_set;
}
if (Math.Abs(SectionResult.GetTotalContribution(relevantScenarios) - 1.0) > 1e-6)
{
return RingtoetsCommonFormsResources.FailureMechanismResultView_DataGridViewCellFormatting_Scenario_contribution_for_this_section_not_100;
}
CalculationScenarioStatus calculationScenarioStatus = SectionResult.GetCalculationScenarioStatus(relevantScenarios);
if (calculationScenarioStatus == CalculationScenarioStatus.NotCalculated)
{
return RingtoetsCommonFormsResources.FailureMechanismResultView_DataGridViewCellFormatting_Not_all_calculations_have_been_executed;
}
if (double.IsNaN(SectionResult.GetDetailedAssessmentProbability(calculations, failureMechanism, assessmentSection)))
{
return RingtoetsCommonFormsResources.FailureMechanismResultView_DataGridViewCellFormatting_All_calculations_must_have_valid_output;
}
return string.Empty;
}
private void CreateColumnStateDefinitions()
{
ColumnStateDefinitions.Add(simpleAssessmentResultIndex, new DataGridViewColumnStateDefinition());
ColumnStateDefinitions.Add(detailedAssessmentResultIndex, new DataGridViewColumnStateDefinition());
ColumnStateDefinitions.Add(detailedAssessmentProbabilityIndex, new DataGridViewColumnStateDefinition());
ColumnStateDefinitions.Add(tailorMadeAssessmentResultIndex, new DataGridViewColumnStateDefinition());
ColumnStateDefinitions.Add(tailorMadeAssessmentProbabilityIndex, new DataGridViewColumnStateDefinition());
ColumnStateDefinitions.Add(simpleAssemblyCategoryGroupIndex, DataGridViewColumnStateDefinitionFactory.CreateReadOnlyColumnStateDefinition());
ColumnStateDefinitions.Add(detailedAssemblyCategoryGroupIndex, DataGridViewColumnStateDefinitionFactory.CreateReadOnlyColumnStateDefinition());
ColumnStateDefinitions.Add(tailorMadeAssemblyCategoryGroupIndex, DataGridViewColumnStateDefinitionFactory.CreateReadOnlyColumnStateDefinition());
ColumnStateDefinitions.Add(combinedAssemblyCategoryGroupIndex, DataGridViewColumnStateDefinitionFactory.CreateReadOnlyColumnStateDefinition());
ColumnStateDefinitions.Add(combinedAssemblyProbabilityIndex, DataGridViewColumnStateDefinitionFactory.CreateReadOnlyColumnStateDefinition());
ColumnStateDefinitions.Add(manualAssemblyProbabilityIndex, new DataGridViewColumnStateDefinition());
}
private void UpdateDerivedData()
{
ResetErrorTexts();
TryGetSimpleAssemblyCategoryGroup();
TryGetDetailedAssemblyCategoryGroup();
TryGetTailorMadeAssemblyCategoryGroup();
TryGetCombinedAssemblyCategoryGroup();
}
private void ResetErrorTexts()
{
ColumnStateDefinitions[simpleAssemblyCategoryGroupIndex].ErrorText = string.Empty;
ColumnStateDefinitions[detailedAssemblyCategoryGroupIndex].ErrorText = string.Empty;
ColumnStateDefinitions[tailorMadeAssemblyCategoryGroupIndex].ErrorText = string.Empty;
ColumnStateDefinitions[combinedAssemblyCategoryGroupIndex].ErrorText = string.Empty;
ColumnStateDefinitions[combinedAssemblyProbabilityIndex].ErrorText = string.Empty;
}
private void TryGetSimpleAssemblyCategoryGroup()
{
try
{
simpleAssemblyCategoryGroup = MacroStabilityInwardsFailureMechanismAssemblyFactory.AssembleSimpleAssessment(SectionResult).Group;
}
catch (AssemblyException e)
{
simpleAssemblyCategoryGroup = FailureMechanismSectionAssemblyCategoryGroup.None;
ColumnStateDefinitions[simpleAssemblyCategoryGroupIndex].ErrorText = e.Message;
}
}
private void TryGetDetailedAssemblyCategoryGroup()
{
try
{
detailedAssemblyCategoryGroup = MacroStabilityInwardsFailureMechanismAssemblyFactory.AssembleDetailedAssessment(
SectionResult,
calculations,
failureMechanism,
assessmentSection).Group;
}
catch (AssemblyException e)
{
detailedAssemblyCategoryGroup = FailureMechanismSectionAssemblyCategoryGroup.None;
ColumnStateDefinitions[detailedAssemblyCategoryGroupIndex].ErrorText = e.Message;
}
}
private void TryGetTailorMadeAssemblyCategoryGroup()
{
try
{
tailorMadeAssemblyCategoryGroup = MacroStabilityInwardsFailureMechanismAssemblyFactory.AssembleTailorMadeAssessment(
SectionResult,
failureMechanism,
assessmentSection).Group;
}
catch (AssemblyException e)
{
tailorMadeAssemblyCategoryGroup = FailureMechanismSectionAssemblyCategoryGroup.None;
ColumnStateDefinitions[tailorMadeAssemblyCategoryGroupIndex].ErrorText = e.Message;
}
}
private void TryGetCombinedAssemblyCategoryGroup()
{
try
{
FailureMechanismSectionAssembly combinedAssembly =
MacroStabilityInwardsFailureMechanismAssemblyFactory.AssembleCombinedAssessment(
SectionResult,
calculations,
failureMechanism,
assessmentSection);
combinedAssemblyCategoryGroup = combinedAssembly.Group;
CombinedAssemblyProbability = combinedAssembly.Probability;
}
catch (AssemblyException e)
{
combinedAssemblyCategoryGroup = FailureMechanismSectionAssemblyCategoryGroup.None;
CombinedAssemblyProbability = double.NaN;
ColumnStateDefinitions[combinedAssemblyCategoryGroupIndex].ErrorText = e.Message;
ColumnStateDefinitions[combinedAssemblyProbabilityIndex].ErrorText = e.Message;
}
}
///
/// Updates the column state definitions.
///
/// Thrown when
/// is a valid value, but unsupported.
private void UpdateColumnStateDefinitions()
{
bool simpleAssessmentSufficient = FailureMechanismSectionResultRowHelper.SimpleAssessmentIsSufficient(SimpleAssessmentResult);
FailureMechanismSectionResultRowHelper.SetColumnState(ColumnStateDefinitions[simpleAssessmentResultIndex], UseManualAssemblyProbability);
FailureMechanismSectionResultRowHelper.SetColumnState(ColumnStateDefinitions[detailedAssessmentResultIndex], simpleAssessmentSufficient
|| UseManualAssemblyProbability);
if (simpleAssessmentSufficient
|| !FailureMechanismSectionResultRowHelper.DetailedAssessmentResultIsProbability(DetailedAssessmentResult)
|| UseManualAssemblyProbability)
{
FailureMechanismSectionResultRowHelper.DisableColumn(ColumnStateDefinitions[detailedAssessmentProbabilityIndex]);
}
else
{
FailureMechanismSectionResultRowHelper.EnableColumn(ColumnStateDefinitions[detailedAssessmentProbabilityIndex], true);
}
FailureMechanismSectionResultRowHelper.SetColumnState(ColumnStateDefinitions[tailorMadeAssessmentResultIndex],
simpleAssessmentSufficient
|| UseManualAssemblyProbability);
FailureMechanismSectionResultRowHelper.SetColumnState(ColumnStateDefinitions[tailorMadeAssessmentProbabilityIndex],
simpleAssessmentSufficient
|| !FailureMechanismSectionResultRowHelper.TailorMadeAssessmentResultIsProbability(TailorMadeAssessmentResult)
|| UseManualAssemblyProbability);
if (UseManualAssemblyProbability)
{
FailureMechanismSectionResultRowHelper.DisableColumn(ColumnStateDefinitions[simpleAssemblyCategoryGroupIndex]);
FailureMechanismSectionResultRowHelper.DisableColumn(ColumnStateDefinitions[detailedAssemblyCategoryGroupIndex]);
FailureMechanismSectionResultRowHelper.DisableColumn(ColumnStateDefinitions[tailorMadeAssemblyCategoryGroupIndex]);
FailureMechanismSectionResultRowHelper.DisableColumn(ColumnStateDefinitions[combinedAssemblyCategoryGroupIndex]);
FailureMechanismSectionResultRowHelper.DisableColumn(ColumnStateDefinitions[combinedAssemblyProbabilityIndex]);
}
else
{
FailureMechanismSectionResultRowHelper.SetAssemblyCategoryGroupStyle(ColumnStateDefinitions[simpleAssemblyCategoryGroupIndex],
simpleAssemblyCategoryGroup);
FailureMechanismSectionResultRowHelper.SetAssemblyCategoryGroupStyle(ColumnStateDefinitions[detailedAssemblyCategoryGroupIndex],
detailedAssemblyCategoryGroup);
FailureMechanismSectionResultRowHelper.SetAssemblyCategoryGroupStyle(ColumnStateDefinitions[tailorMadeAssemblyCategoryGroupIndex],
tailorMadeAssemblyCategoryGroup);
FailureMechanismSectionResultRowHelper.SetAssemblyCategoryGroupStyle(ColumnStateDefinitions[combinedAssemblyCategoryGroupIndex],
combinedAssemblyCategoryGroup);
FailureMechanismSectionResultRowHelper.EnableColumn(ColumnStateDefinitions[combinedAssemblyProbabilityIndex], true);
}
FailureMechanismSectionResultRowHelper.SetColumnState(ColumnStateDefinitions[manualAssemblyProbabilityIndex], !UseManualAssemblyProbability);
}
///
/// Class holding the various construction parameters for .
///
public class ConstructionProperties
{
///
/// Sets the simple assessment result index.
///
public int SimpleAssessmentResultIndex { internal get; set; }
///
/// Sets the detailed assessment result index.
///
public int DetailedAssessmentResultIndex { internal get; set; }
///
/// Sets the detailed assessment probability index.
///
public int DetailedAssessmentProbabilityIndex { internal get; set; }
///
/// Sets the tailor made assessment result index.
///
public int TailorMadeAssessmentResultIndex { internal get; set; }
///
/// Sets the tailor made assessment probability index.
///
public int TailorMadeAssessmentProbabilityIndex { internal get; set; }
///
/// Sets the simple assembly category group index.
///
public int SimpleAssemblyCategoryGroupIndex { internal get; set; }
///
/// Sets the detailed assembly category group index.
///
public int DetailedAssemblyCategoryGroupIndex { internal get; set; }
///
/// Sets the tailor made assembly category group index.
///
public int TailorMadeAssemblyCategoryGroupIndex { internal get; set; }
///
/// Sets the combined assembly category group index.
///
public int CombinedAssemblyCategoryGroupIndex { internal get; set; }
///
/// Sets the combined assembly probability index.
///
public int CombinedAssemblyProbabilityIndex { internal get; set; }
///
/// Sets the manual assembly category group index.
///
public int ManualAssemblyProbabilityIndex { internal get; set; }
}
}
}