using System;
using System.Collections.Generic;
using System.Linq;
using Ringtoets.AssemblyTool.Data;
using Ringtoets.ClosingStructures.Data;
using Ringtoets.Common.Data.AssessmentSection;
using Ringtoets.Common.Data.Exceptions;
using Ringtoets.Integration.IO.Assembly;
namespace Ringtoets.Integration.IO.Factories
{
///
/// Factory to create instances of
/// with assembly results for closing structures.
///
public static class ExportableClosingStructuresFailureMechanismFactory
{
private const ExportableFailureMechanismType failureMechanismCode = ExportableFailureMechanismType.BSKW;
private const ExportableFailureMechanismGroup failureMechanismGroup = ExportableFailureMechanismGroup.Group1;
private const ExportableAssemblyMethod failureMechanismAssemblyMethod = ExportableAssemblyMethod.WBI1B1;
///
/// Creates a
/// with assembly results based on the input parameters.
///
/// The to create a
/// for.
/// The assessment section this failure mechanism belongs to.
/// A with assembly results.
/// Thrown when any parameter is null.
/// Thrown when assembly results cannot be created.
public static ExportableFailureMechanism CreateExportableFailureMechanism(
ClosingStructuresFailureMechanism failureMechanism,
IAssessmentSection assessmentSection)
{
if (failureMechanism == null)
{
throw new ArgumentNullException(nameof(failureMechanism));
}
if (assessmentSection == null)
{
throw new ArgumentNullException(nameof(assessmentSection));
}
if (!failureMechanism.IsRelevant)
{
return ExportableFailureMechanismFactory.CreateDefaultExportableFailureMechanismWithProbability(failureMechanismCode,
failureMechanismGroup,
failureMechanismAssemblyMethod);
}
FailureMechanismAssembly failureMechanismAssembly = ClosingStructuresFailureMechanismAssemblyFactory.AssembleFailureMechanism(failureMechanism, assessmentSection);
IEnumerable exportableFailureMechanismSections =
ExportableFailureMechanismSectionFactory.CreateExportableFailureMechanismSections(failureMechanism.Sections);
Dictionary failureMechanismSectionsLookup =
failureMechanism.SectionResults
.Zip(exportableFailureMechanismSections, (sectionResult, exportableSection) => new
{
key = sectionResult,
value = exportableSection
})
.ToDictionary(pair => pair.key, pair => pair.value);
return new ExportableFailureMechanism(
new ExportableFailureMechanismAssemblyResultWithProbability(failureMechanismAssemblyMethod,
failureMechanismAssembly.Group,
failureMechanismAssembly.Probability),
failureMechanismSectionsLookup.Values,
CreateExportableFailureMechanismSectionResults(failureMechanismSectionsLookup,
failureMechanism,
assessmentSection),
failureMechanismCode,
failureMechanismGroup);
}
///
/// Creates a collection of
/// with assembly results based on the sections in .
///
/// The mapping between the
/// and .
/// The the sections belong to.
/// The assessment section the sections belong to.
/// A collection of .
/// Thrown when assembly results cannot be created.
private static IEnumerable CreateExportableFailureMechanismSectionResults(
Dictionary failureMechanismSections,
ClosingStructuresFailureMechanism failureMechanism,
IAssessmentSection assessmentSection)
{
var exportableResults = new List();
foreach (KeyValuePair failureMechanismSectionPair in failureMechanismSections)
{
ClosingStructuresFailureMechanismSectionResult failureMechanismSectionResult = failureMechanismSectionPair.Key;
FailureMechanismSectionAssembly simpleAssembly =
ClosingStructuresFailureMechanismAssemblyFactory.AssembleSimpleAssessment(failureMechanismSectionResult);
FailureMechanismSectionAssembly detailedAssembly =
ClosingStructuresFailureMechanismAssemblyFactory.AssembleDetailedAssessment(failureMechanismSectionResult,
failureMechanism,
assessmentSection);
FailureMechanismSectionAssembly tailorMadeAssembly =
ClosingStructuresFailureMechanismAssemblyFactory.AssembleTailorMadeAssessment(failureMechanismSectionResult,
failureMechanism,
assessmentSection);
FailureMechanismSectionAssembly combinedAssembly =
ClosingStructuresFailureMechanismAssemblyFactory.AssembleCombinedAssessment(failureMechanismSectionResult,
failureMechanism,
assessmentSection);
exportableResults.Add(
new ExportableAggregatedFailureMechanismSectionAssemblyResultWithProbability(
failureMechanismSectionPair.Value,
ExportableSectionAssemblyResultFactory.CreateExportableSectionAssemblyResultWithProbability(simpleAssembly, ExportableAssemblyMethod.WBI0E1),
ExportableSectionAssemblyResultFactory.CreateExportableSectionAssemblyResultWithProbability(detailedAssembly, ExportableAssemblyMethod.WBI0G3),
ExportableSectionAssemblyResultFactory.CreateExportableSectionAssemblyResultWithProbability(tailorMadeAssembly, ExportableAssemblyMethod.WBI0T3),
ExportableSectionAssemblyResultFactory.CreateExportableSectionAssemblyResultWithProbability(combinedAssembly, ExportableAssemblyMethod.WBI0A1)));
}
return exportableResults;
}
}
}