// 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.MacroStabilityInwards.Primitives;
namespace Ringtoets.MacroStabilityInwards.Data
{
///
/// This class represents a stochastic soil model which consists out of a collection of .
/// A stochastic soil model contains a segment for which the model applies.
///
public class StochasticSoilModel : Observable
{
///
/// Creates a new instance of .
///
/// Name of the segment soil model.
public StochasticSoilModel(string name)
{
Name = name;
Geometry = new List();
StochasticSoilProfiles = new List();
}
///
/// Gets the name of the segment soil model.
///
public string Name { get; private set; }
///
/// Gets the list of geometry points.
///
public List Geometry { get; }
///
/// Gets the list of .
///
public List StochasticSoilProfiles { get; }
///
/// Updates the with the properties
/// from .
///
/// The to
/// obtain the property values from.
/// Thrown when
/// is null.
/// Thrown when
/// contains multiple profiles with the same name, and also contains a
/// profile with the same name.
///
public StochasticSoilModelProfileDifference Update(StochasticSoilModel fromModel)
{
if (fromModel == null)
{
throw new ArgumentNullException(nameof(fromModel));
}
Name = fromModel.Name;
Geometry.Clear();
foreach (Point2D point in fromModel.Geometry)
{
Geometry.Add(point);
}
var newSoilProfiles = new List();
var updatedProfiles = new List();
var addedProfiles = new List();
var removedProfiles = new List();
foreach (StochasticSoilProfile fromProfile in fromModel.StochasticSoilProfiles)
{
StochasticSoilProfile sameProfile = StochasticSoilProfiles.SingleOrDefault(
sp => IsSame(sp, fromProfile)
);
if (sameProfile != null)
{
if (sameProfile.Update(fromProfile))
{
updatedProfiles.Add(sameProfile);
}
}
else
{
StochasticSoilProfiles.Add(fromProfile);
addedProfiles.Add(fromProfile);
}
newSoilProfiles.Add(fromProfile.SoilProfile);
}
foreach (StochasticSoilProfile profileToRemove in StochasticSoilProfiles.Where(
sp => !newSoilProfiles.Any(newSp => IsSame(newSp, sp.SoilProfile))).ToArray())
{
StochasticSoilProfiles.Remove(profileToRemove);
removedProfiles.Add(profileToRemove);
}
return new StochasticSoilModelProfileDifference(addedProfiles, updatedProfiles, removedProfiles);
}
public override string ToString()
{
return Name;
}
private static bool IsSame(ISoilProfile soilProfile, ISoilProfile otherSoilProfile)
{
bool equalNames = soilProfile.Name.Equals(otherSoilProfile.Name);
bool equalTypes = soilProfile.GetType() == otherSoilProfile.GetType();
return equalNames && equalTypes;
}
private static bool IsSame(StochasticSoilProfile stochasticSoilProfile, StochasticSoilProfile otherStochasticSoilProfile)
{
return IsSame(stochasticSoilProfile.SoilProfile, otherStochasticSoilProfile.SoilProfile);
}
}
}