// Copyright (C) Stichting Deltares and State of the Netherlands 2024. All rights reserved.
//
// This file is part of Riskeer.
//
// Riskeer 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 Riskeer.Common.Data.Exceptions;
using Riskeer.Common.Data.IllustrationPoints;
using HydraRingWindDirectionClosingSituation = Riskeer.HydraRing.Calculation.Data.Output.IllustrationPoints.WindDirectionClosingSituation;
using HydraRingIllustrationPointTreeNode = Riskeer.HydraRing.Calculation.Data.Output.IllustrationPoints.IllustrationPointTreeNode;
using HydraRingGeneralResult = Riskeer.HydraRing.Calculation.Data.Output.IllustrationPoints.GeneralResult;
using IHydraRingIllustrationPoint = Riskeer.HydraRing.Calculation.Data.Output.IllustrationPoints.IIllustrationPoint;
using HydraRingSubMechanismIllustrationPoint = Riskeer.HydraRing.Calculation.Data.Output.IllustrationPoints.SubMechanismIllustrationPoint;
using HydraRingFaultTreeIllustrationPoint = Riskeer.HydraRing.Calculation.Data.Output.IllustrationPoints.FaultTreeIllustrationPoint;
namespace Riskeer.Common.Service.IllustrationPoints
{
///
/// Converter for related to creating a .
///
public static class GeneralResultConverter
{
private static IEnumerable GetStochasts(HydraRingGeneralResult hydraGeneralResult)
{
return hydraGeneralResult.Stochasts.Select(StochastConverter.Convert).ToArray();
}
#region SubMechanismIllustrationPoint
///
/// Creates a new instance of for top level sub
/// mechanism illustration points based on the information of .
///
/// The to base the
/// to create on.
/// The newly created .
/// Thrown when is null.
/// Thrown when the
/// cannot be converted to a with top level sub mechanism illustration points.
public static GeneralResult ConvertToGeneralResultTopLevelSubMechanismIllustrationPoint(
HydraRingGeneralResult hydraRingGeneralResult)
{
if (hydraRingGeneralResult == null)
{
throw new ArgumentNullException(nameof(hydraRingGeneralResult));
}
WindDirection governingWindDirection = WindDirectionConverter.Convert(hydraRingGeneralResult.GoverningWindDirection);
IEnumerable stochasts = GetStochasts(hydraRingGeneralResult);
IEnumerable windDirectionClosingScenarioIllustrationPoints =
GetTopLevelSubMechanismIllustrationPoints(hydraRingGeneralResult.IllustrationPoints);
return new GeneralResult(governingWindDirection,
stochasts,
windDirectionClosingScenarioIllustrationPoints);
}
///
/// Creates all the top level fault tree illustration points based on the
/// combinations of and
/// .
///
/// The collection of
/// and
/// combinations to base the on.
/// An of .
/// Thrown when the combination of
/// and
/// cannot be converted to .
private static IEnumerable GetTopLevelSubMechanismIllustrationPoints(
IDictionary hydraRingTopLevelIllustrationPoints)
{
var topLevelIlustrationPoints = new List();
foreach (KeyValuePair topLevelIllustrationPointTreeNode in hydraRingTopLevelIllustrationPoints)
{
IHydraRingIllustrationPoint hydraIllustrationPointData = topLevelIllustrationPointTreeNode.Value.Data;
HydraRingWindDirectionClosingSituation hydraWindDirectionClosingSituation = topLevelIllustrationPointTreeNode.Key;
var subMechanismIllustrationPoint = hydraIllustrationPointData as HydraRingSubMechanismIllustrationPoint;
if (subMechanismIllustrationPoint != null)
{
topLevelIlustrationPoints.Add(TopLevelSubMechanismIllustrationPointConverter.Convert(
hydraWindDirectionClosingSituation, subMechanismIllustrationPoint));
}
else
{
string exceptionMessage = $"Expected a fault tree node with data of type {typeof(HydraRingSubMechanismIllustrationPoint)} as root, " +
$"but got {hydraIllustrationPointData.GetType()}";
throw new IllustrationPointConversionException(exceptionMessage);
}
}
return topLevelIlustrationPoints;
}
#endregion
#region FaultTreeIllustrationPoint
///
/// Creates a new instance of for fault tree illustration points
/// based on the information of .
///
/// The
/// to base the to create on.
/// The newly created .
/// Thrown when
/// is null.
/// Thrown when the
/// cannot be converted to a with top level fault tree illustration points.
public static GeneralResult ConvertToGeneralResultTopLevelFaultTreeIllustrationPoint(
HydraRingGeneralResult hydraRingGeneralResult)
{
if (hydraRingGeneralResult == null)
{
throw new ArgumentNullException(nameof(hydraRingGeneralResult));
}
WindDirection governingWindDirection = WindDirectionConverter.Convert(hydraRingGeneralResult.GoverningWindDirection);
IEnumerable stochasts = GetStochasts(hydraRingGeneralResult);
IEnumerable topLevelIllustrationPoints =
GetTopLevelFaultTreeIllustrationPoints(hydraRingGeneralResult.IllustrationPoints);
return new GeneralResult(governingWindDirection,
stochasts,
topLevelIllustrationPoints);
}
///
/// Creates all the top level fault tree illustration points based on the
/// combinations of and
/// .
///
/// The collection of
/// and
/// combinations to base the on.
/// An of .
/// Thrown when the combination of
/// and
/// cannot be converted to .
private static IEnumerable GetTopLevelFaultTreeIllustrationPoints(
IDictionary hydraRingTopLevelIllustrationPoints)
{
var topLevelIllustrationPoints = new List();
foreach (KeyValuePair hydraRingillustrationPointTreeNode in hydraRingTopLevelIllustrationPoints)
{
HydraRingIllustrationPointTreeNode hydraRingIllustrationPointTreeNode = hydraRingillustrationPointTreeNode.Value;
IHydraRingIllustrationPoint hydraIllustrationPointData = hydraRingIllustrationPointTreeNode.Data;
HydraRingWindDirectionClosingSituation hydraWindDirectionClosingSituation = hydraRingillustrationPointTreeNode.Key;
var faultTreeIllustrationPoint = hydraIllustrationPointData as HydraRingFaultTreeIllustrationPoint;
if (faultTreeIllustrationPoint != null)
{
topLevelIllustrationPoints.Add(TopLevelFaultTreeIllustrationPointConverter.Convert(
hydraWindDirectionClosingSituation, hydraRingIllustrationPointTreeNode));
}
else
{
string exceptionMessage = $"Expected a fault tree node with data of type {typeof(HydraRingFaultTreeIllustrationPoint)} as root, " +
$"but got {hydraIllustrationPointData.GetType()}";
throw new IllustrationPointConversionException(exceptionMessage);
}
}
return topLevelIllustrationPoints;
}
#endregion
}
}