// 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;
using Core.Common.Base.Geometry;
using Ringtoets.Common.Data;
using Ringtoets.Piping.Primitives;
using RingtoetsCommonDataResources = Ringtoets.Common.Data.Properties.Resources;
namespace Ringtoets.Piping.Data.SoilProfile
{
///
/// This class represents a piping specific stochastic soil model which consists out of a
/// collection of .
/// A stochastic soil model contains a segment for which the model applies.
///
public class PipingStochasticSoilModel : Observable, IMechanismStochasticSoilModel
{
///
/// Creates a new instance of .
///
/// Name of the segment soil model.
///
/// Thrown when any parameter is
/// null.
/// Thrown when
/// is empty.
public PipingStochasticSoilModel(string name, IEnumerable geometry)
{
if (name == null)
{
throw new ArgumentNullException(nameof(name));
}
if (geometry == null)
{
throw new ArgumentNullException(nameof(geometry));
}
if (!geometry.Any())
{
string message = string.Format(RingtoetsCommonDataResources.StochasticSoilModel_Geometry_of_StochasticSoilModelName_0_must_contain_a_geometry, name);
throw new ArgumentException(message);
}
Name = name;
Geometry = geometry;
StochasticSoilProfiles = new List();
}
///
/// Gets the name of the soil model.
///
public string Name { get; private set; }
///
/// Gets the geometry points.
///
public IEnumerable Geometry { get; private set; }
///
/// Gets the list of .
///
public List StochasticSoilProfiles { get; }
///
/// Updates the with the properties from .
///
/// The to
/// obtain the property values from.
/// The differences summed up in an instance of .
/// Thrown when
/// is null.
/// Thrown when
/// contains multiple profiles with the same name, and also contains a
/// profile with the same name.
///
public PipingStochasticSoilModelProfileDifference Update(PipingStochasticSoilModel fromModel)
{
if (fromModel == null)
{
throw new ArgumentNullException(nameof(fromModel));
}
Name = fromModel.Name;
Geometry = fromModel.Geometry;
var newSoilProfiles = new List();
var updatedProfiles = new List();
var addedProfiles = new List();
var removedProfiles = new List();
foreach (PipingStochasticSoilProfile fromProfile in fromModel.StochasticSoilProfiles)
{
PipingStochasticSoilProfile sameProfile = StochasticSoilProfiles.SingleOrDefault(
sp => IsSame(sp, fromProfile)
);
if (sameProfile != null)
{
if (!sameProfile.Equals(fromProfile))
{
sameProfile.Update(fromProfile);
updatedProfiles.Add(sameProfile);
}
}
else
{
StochasticSoilProfiles.Add(fromProfile);
addedProfiles.Add(fromProfile);
}
newSoilProfiles.Add(fromProfile.SoilProfile);
}
PipingStochasticSoilProfile[] remainingProfiles = StochasticSoilProfiles.Where(
sp => !newSoilProfiles.Any(newSp => IsSame(newSp, sp.SoilProfile))).ToArray();
foreach (PipingStochasticSoilProfile profileToRemove in remainingProfiles)
{
StochasticSoilProfiles.Remove(profileToRemove);
removedProfiles.Add(profileToRemove);
}
return new PipingStochasticSoilModelProfileDifference(addedProfiles, updatedProfiles, removedProfiles);
}
public override string ToString()
{
return Name;
}
private static bool IsSame(PipingSoilProfile pipingSoilProfile, PipingSoilProfile otherPipingSoilProfile)
{
return pipingSoilProfile.Name.Equals(otherPipingSoilProfile.Name)
&& pipingSoilProfile.SoilProfileSourceType.Equals(otherPipingSoilProfile.SoilProfileSourceType);
}
private static bool IsSame(PipingStochasticSoilProfile stochasticSoilProfile, PipingStochasticSoilProfile otherStochasticSoilProfile)
{
return IsSame(stochasticSoilProfile.SoilProfile, otherStochasticSoilProfile.SoilProfile);
}
}
}