// 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.Utils.Extensions;
using Ringtoets.Common.Data.Properties;
namespace Ringtoets.Common.Data.IllustrationPoints
{
///
/// The general illustration point result.
///
/// The type of
/// that the general result holds.
public class GeneralResult : ICloneable
where T : TopLevelIllustrationPointBase
{
///
/// Creates a new instance of
///
/// The governing wind direction.
/// The general alpha values.
/// A collection of illustration points
/// for every combination of wind directions and closing situations.
/// Thrown when any of the input
/// parameters is null.
public GeneralResult(WindDirection governingWindDirection,
IEnumerable stochasts,
IEnumerable topLevelIllustrationPoints)
{
if (governingWindDirection == null)
{
throw new ArgumentNullException(nameof(governingWindDirection));
}
if (stochasts == null)
{
throw new ArgumentNullException(nameof(stochasts));
}
if (topLevelIllustrationPoints == null)
{
throw new ArgumentNullException(nameof(topLevelIllustrationPoints));
}
IEnumerable points = topLevelIllustrationPoints.OfType();
if (points.Any())
{
ValidateStochastInChildren(points, stochasts);
}
ValidateStochasts(stochasts);
ValidateTopLevelIllustrationPoints(topLevelIllustrationPoints);
GoverningWindDirection = governingWindDirection;
Stochasts = stochasts;
TopLevelIllustrationPoints = topLevelIllustrationPoints;
}
///
/// Gets the governing wind direction.
///
public WindDirection GoverningWindDirection { get; private set; }
///
/// Gets the general alpha values.
///
public IEnumerable Stochasts { get; private set; }
///
/// Gets the collection of illustration points for every combination of a wind direction and a closing situation.
///
public IEnumerable TopLevelIllustrationPoints { get; private set; }
public object Clone()
{
var clone = (GeneralResult) MemberwiseClone();
clone.GoverningWindDirection = (WindDirection) GoverningWindDirection.Clone();
clone.Stochasts = Stochasts.Select(s => (Stochast) s.Clone()).ToArray();
clone.TopLevelIllustrationPoints = TopLevelIllustrationPoints.Select(s => (T) s.Clone()).ToArray();
return clone;
}
private void ValidateStochastInChildren(IEnumerable topLevelFaultTreeIllustrationPoints, IEnumerable stochasts)
{
var childStochastNames = new List();
foreach (TopLevelFaultTreeIllustrationPoint topLevelFaultTreeIllustrationPoint in topLevelFaultTreeIllustrationPoints)
{
IllustrationPointNode nodeRoot = topLevelFaultTreeIllustrationPoint.FaultTreeNodeRoot;
childStochastNames.AddRange(GetStochastNamesFromChildren(nodeRoot));
}
childStochastNames = childStochastNames.Distinct().ToList();
IEnumerable topLevelStochastNames = stochasts.Select(s => s.Name);
if (childStochastNames.Except(topLevelStochastNames).Any())
{
throw new ArgumentException(string.Format(Resources.GeneralResult_Imported_with_incorrect_top_level_stochasts));
}
}
private IEnumerable GetStochastNamesFromChildren(IllustrationPointNode nodeRoot)
{
var stochastNames = new List();
var faultTreeData = nodeRoot.Data as FaultTreeIllustrationPoint;
if (faultTreeData != null)
{
stochastNames.AddRange(faultTreeData.Stochasts.Select(s => s.Name));
foreach (IllustrationPointNode illustrationPointNode in nodeRoot.Children)
{
stochastNames.AddRange(GetStochastNamesFromChildren(illustrationPointNode));
}
return stochastNames;
}
var subMechanismData = nodeRoot.Data as SubMechanismIllustrationPoint;
if (subMechanismData != null)
{
stochastNames.AddRange(subMechanismData.Stochasts.Select(s => s.Name));
}
return stochastNames;
}
private static void ValidateTopLevelIllustrationPoints(IEnumerable topLevelIllustrationPoints)
{
bool hasNonDistinctIllustrationPointsPerWindDirection =
topLevelIllustrationPoints.AnyNonDistinct(t => $"{t.ClosingSituation} {t.WindDirection.Angle}");
if (hasNonDistinctIllustrationPointsPerWindDirection)
{
throw new ArgumentException(string.Format(Resources.GeneralResult_Imported_non_unique_closing_situations_or_wind_direction));
}
}
private static void ValidateStochasts(IEnumerable stochasts)
{
bool hasNonDistinctStochasts = stochasts.AnyNonDistinct(s => s.Name);
if (hasNonDistinctStochasts)
{
throw new ArgumentException(string.Format(Resources.GeneralResult_Imported_non_unique_stochasts));
}
}
}
}