// 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.Collections.ObjectModel; using System.Linq; using Core.Common.Base; using Ringtoets.Common.Data.Calculation; using Ringtoets.Common.Data.Hydraulics; using Ringtoets.Common.Service; using Ringtoets.MacroStabilityInwards.Data; using Ringtoets.MacroStabilityInwards.Primitives; namespace Ringtoets.MacroStabilityInwards.Service { /// /// Service for synchronizing macro stability inwards data. /// public static class MacroStabilityInwardsDataSynchronizationService { /// /// Clears the output for all calculations in the . /// /// The which contains the calculations. /// An of calculations which are affected by clearing the output. /// Thrown when is null. public static IEnumerable ClearAllCalculationOutput(MacroStabilityInwardsFailureMechanism failureMechanism) { if (failureMechanism == null) { throw new ArgumentNullException(nameof(failureMechanism)); } return failureMechanism.Calculations .Cast() .SelectMany(ClearCalculationOutput) .ToArray(); } /// /// Clears the output of the given . /// /// The to clear the output for. /// Thrown when is null. /// All objects that have been changed. public static IEnumerable ClearCalculationOutput(MacroStabilityInwardsCalculation calculation) { if (calculation == null) { throw new ArgumentNullException(nameof(calculation)); } if (calculation.HasOutput) { calculation.ClearOutput(); return new[] { calculation }; } return Enumerable.Empty(); } /// /// Clears the and output for all the calculations in the . /// /// The which contains the calculations. /// An of objects which are affected by removing data. /// Thrown when is null. public static IEnumerable ClearAllCalculationOutputAndHydraulicBoundaryLocations(MacroStabilityInwardsFailureMechanism failureMechanism) { if (failureMechanism == null) { throw new ArgumentNullException(nameof(failureMechanism)); } var affectedItems = new List(); foreach (MacroStabilityInwardsCalculation calculation in failureMechanism.Calculations.Cast()) { affectedItems.AddRange(ClearCalculationOutput(calculation) .Concat(ClearHydraulicBoundaryLocation(calculation.InputParameters))); } return affectedItems; } /// /// Clears all data dependent, either directly or indirectly, on the parent reference line. /// /// The failure mechanism to be cleared. /// The results of the clear action. /// Thrown when /// is null. public static ClearResults ClearReferenceLineDependentData(MacroStabilityInwardsFailureMechanism failureMechanism) { if (failureMechanism == null) { throw new ArgumentNullException(nameof(failureMechanism)); } var changedObjects = new Collection(); object[] removedObjects = failureMechanism.Sections.OfType() .Concat(failureMechanism.SectionResults) .Concat(failureMechanism.CalculationsGroup.GetAllChildrenRecursive()) .Concat(failureMechanism.StochasticSoilModels) .Concat(failureMechanism.SurfaceLines) .ToArray(); failureMechanism.ClearAllSections(); changedObjects.Add(failureMechanism); failureMechanism.CalculationsGroup.Children.Clear(); changedObjects.Add(failureMechanism.CalculationsGroup); failureMechanism.StochasticSoilModels.Clear(); changedObjects.Add(failureMechanism.StochasticSoilModels); failureMechanism.SurfaceLines.Clear(); changedObjects.Add(failureMechanism.SurfaceLines); return new ClearResults(changedObjects, removedObjects); } /// /// Removes a given from the /// and clears all data that depends on it, either directly or indirectly. /// /// The failure mechanism containing . /// The surface line residing in /// that should be removed. /// All observable objects affected by this method. /// Thrown when /// or is null. public static IEnumerable RemoveSurfaceLine(MacroStabilityInwardsFailureMechanism failureMechanism, MacroStabilityInwardsSurfaceLine surfaceLine) { if (failureMechanism == null) { throw new ArgumentNullException(nameof(failureMechanism)); } if (surfaceLine == null) { throw new ArgumentNullException(nameof(surfaceLine)); } IEnumerable calculationScenarios = failureMechanism.Calculations .Cast() .Where(pcs => ReferenceEquals(pcs.InputParameters.SurfaceLine, surfaceLine)); IList changedObservables = RemoveSurfaceLineDependentData(calculationScenarios).ToList(); failureMechanism.SurfaceLines.Remove(surfaceLine); changedObservables.Add(failureMechanism.SurfaceLines); return changedObservables; } /// /// Removes all from the /// and clears all data that depends on it, either directly or indirectly. /// /// The failure mechanism. /// All objects that are affected by this operation. /// Thrown when /// is null. public static IEnumerable RemoveAllSurfaceLines(MacroStabilityInwardsFailureMechanism failureMechanism) { if (failureMechanism == null) { throw new ArgumentNullException(nameof(failureMechanism)); } IEnumerable affectedCalculationScenarios = failureMechanism.Calculations .Cast() .Where(calc => calc.InputParameters.SurfaceLine != null).ToArray(); List affectedObjects = RemoveSurfaceLineDependentData(affectedCalculationScenarios).ToList(); failureMechanism.SurfaceLines.Clear(); affectedObjects.Add(failureMechanism.SurfaceLines); return affectedObjects; } /// /// Removes a given from the /// and clears all data that depends on it, either directly or indirectly. /// /// The failure mechanism containing . /// The soil model residing in /// that should be removed. /// All observable objects affected by this method. /// Thrown when /// or is null. public static IEnumerable RemoveStochasticSoilModel(MacroStabilityInwardsFailureMechanism failureMechanism, StochasticSoilModel soilModel) { if (failureMechanism == null) { throw new ArgumentNullException(nameof(failureMechanism)); } if (soilModel == null) { throw new ArgumentNullException(nameof(soilModel)); } var changedObservables = new List(); IEnumerable calculationScenarios = failureMechanism.Calculations .Cast() .Where(pcs => ReferenceEquals(pcs.InputParameters.StochasticSoilModel, soilModel)); foreach (MacroStabilityInwardsCalculation calculationScenario in calculationScenarios) { changedObservables.AddRange(RingtoetsCommonDataSynchronizationService.ClearCalculationOutput(calculationScenario)); changedObservables.AddRange(ClearStochasticSoilModel(calculationScenario.InputParameters)); } failureMechanism.StochasticSoilModels.Remove(soilModel); changedObservables.Add(failureMechanism.StochasticSoilModels); return changedObservables; } /// /// Removes all from the /// and clears all data that depends on it, either directly or indirectly. /// /// The failure mechanism. /// All objects that are affected by this operation. /// Thrown when /// is null. public static IEnumerable RemoveAllStochasticSoilModels(MacroStabilityInwardsFailureMechanism failureMechanism) { if (failureMechanism == null) { throw new ArgumentNullException(nameof(failureMechanism)); } IEnumerable affectedCalculationScenarios = failureMechanism.Calculations .Cast() .Where(calc => calc.InputParameters.StochasticSoilModel != null).ToArray(); var affectedObjects = new List(); foreach (MacroStabilityInwardsCalculation calculation in affectedCalculationScenarios) { affectedObjects.AddRange(RingtoetsCommonDataSynchronizationService.ClearCalculationOutput(calculation)); affectedObjects.AddRange(ClearStochasticSoilModel(calculation.InputParameters)); } failureMechanism.StochasticSoilModels.Clear(); affectedObjects.Add(failureMechanism.StochasticSoilModels); return affectedObjects; } /// /// Removes a given from calculations in the /// and clears all data that depends on it, either directly or indirectly. /// /// The failure mechanism containing . /// The soil profile residing in /// that has beeen removed. /// All observable objects affected by this method. /// Thrown when /// or is null. public static IEnumerable RemoveStochasticSoilProfileFromInput(MacroStabilityInwardsFailureMechanism failureMechanism, StochasticSoilProfile soilProfile) { if (failureMechanism == null) { throw new ArgumentNullException(nameof(failureMechanism)); } if (soilProfile == null) { throw new ArgumentNullException(nameof(soilProfile)); } var changedObservables = new List(); foreach (MacroStabilityInwardsCalculation calculationScenario in GetCalculationsWithSoilProfileAssigned(failureMechanism, soilProfile)) { changedObservables.AddRange(RingtoetsCommonDataSynchronizationService.ClearCalculationOutput(calculationScenario)); changedObservables.AddRange(ClearStochasticSoilProfile(calculationScenario.InputParameters)); } return changedObservables; } /// /// Clears data dependent on a given , either directly or indirectly, /// from calculations in the . /// /// The failure mechanism containing . /// The soil profile residing in /// that has been updated. /// All observable objects affected by this method. /// Thrown when /// or is null. public static IEnumerable ClearStochasticSoilProfileDependentData(MacroStabilityInwardsFailureMechanism failureMechanism, StochasticSoilProfile soilProfile) { if (failureMechanism == null) { throw new ArgumentNullException(nameof(failureMechanism)); } if (soilProfile == null) { throw new ArgumentNullException(nameof(soilProfile)); } var changedObservables = new List(); foreach (MacroStabilityInwardsCalculation calculation in GetCalculationsWithSoilProfileAssigned(failureMechanism, soilProfile)) { changedObservables.AddRange(RingtoetsCommonDataSynchronizationService.ClearCalculationOutput(calculation)); changedObservables.Add(calculation.InputParameters); } return changedObservables; } private static IEnumerable RemoveSurfaceLineDependentData(IEnumerable calculationScenarios) { var changedObservables = new List(); foreach (MacroStabilityInwardsCalculation calculationScenario in calculationScenarios) { changedObservables.AddRange(RingtoetsCommonDataSynchronizationService.ClearCalculationOutput(calculationScenario)); changedObservables.AddRange(ClearSurfaceLine(calculationScenario.InputParameters)); } return changedObservables; } private static IEnumerable GetCalculationsWithSoilProfileAssigned(MacroStabilityInwardsFailureMechanism failureMechanism, StochasticSoilProfile soilProfile) { IEnumerable calculationScenarios = failureMechanism.Calculations .Cast() .Where(pcs => ReferenceEquals(pcs.InputParameters.StochasticSoilProfile, soilProfile)); return calculationScenarios; } private static IEnumerable ClearSurfaceLine(MacroStabilityInwardsInput input) { input.SurfaceLine = null; return new[] { input }; } private static IEnumerable ClearStochasticSoilModel(MacroStabilityInwardsInput input) { input.StochasticSoilModel = null; input.StochasticSoilProfile = null; return new[] { input }; } private static IEnumerable ClearStochasticSoilProfile(MacroStabilityInwardsInput input) { input.StochasticSoilProfile = null; return new[] { input }; } private static IEnumerable ClearHydraulicBoundaryLocation(MacroStabilityInwardsInput input) { if (input.HydraulicBoundaryLocation != null) { input.HydraulicBoundaryLocation = null; return new[] { input }; } return Enumerable.Empty(); } } }