// Copyright (C) Stichting Deltares 2025. All rights reserved.
//
// This file is part of the application DAM - UI.
//
// DAM - UI 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;
using System.Collections.Generic;
using System.ComponentModel;
using System.Xml.Serialization;
using Deltares.Dam.Data.UISupport;
using Deltares.Standard;
using Deltares.Standard.Attributes;
using Deltares.Standard.EventPublisher;
using Deltares.Standard.Language;
using Deltares.Standard.Validation;
namespace Deltares.Dam.Data;
///
/// Represents the calculation specifications at project level for DAM.
/// These are the main choices that specify the calculation
///
public class DamProjectCalculationSpecification : IVisibleEnabled, IDomain, IDisposable, ICloneable
{
private static AnalysisType selectedAnalysisType = AnalysisType.NoAdaption;
private FailureMechanismSystemType failureMechanismSystemType;
private PipingModelType pipingModelType = PipingModelType.Wti2017;
private StabilityModelType stabilityModelType = StabilityModelType.UpliftVan;
private Enum calculationModel = StabilityModelType.UpliftVan;
private DamStabilityParameters damStabilityParameters = new();
private Boolean firstTime = true;
public DamProjectCalculationSpecification()
{
failureMechanismSystemType = FailureMechanismSystemType.StabilityInside;
damStabilityParameters.SlipCircleDefinition.Specification = this;
DataEventPublisher.OnAfterChange += DataEventPublisher_OnAfterChange;
}
public DamProjectCalculationSpecification Clone()
{
var damFailureMechanismCalculation = new DamProjectCalculationSpecification();
if (damStabilityParameters != null)
{
damFailureMechanismCalculation.DamStabilityParameters = damStabilityParameters.Clone();
}
damFailureMechanismCalculation.Assign(this);
return damFailureMechanismCalculation;
}
public override string ToString()
{
var description = "";
description += $"{FailureMechanismSystemType}";
if ((FailureMechanismSystemType == FailureMechanismSystemType.StabilityInside) ||
(FailureMechanismSystemType == FailureMechanismSystemType.StabilityOutside))
{
//interface?
description += $" ({StabilityModelType})";
}
return description;
}
///
/// The stability parameters.
///
public DamStabilityParameters DamStabilityParameters
{
get
{
return damStabilityParameters;
}
set
{
damStabilityParameters = value;
DataEventPublisher.AfterChange(this);
}
}
///
/// The failure mechanism system type.
///
[Label("Failure mechanism")]
[PropertyOrder(1, 0)]
public FailureMechanismSystemType FailureMechanismSystemType
{
get
{
return failureMechanismSystemType;
}
set
{
// Make sure the set is done for the very first time too even when the value has not changed (MWDAM-1199).
if (failureMechanismSystemType != value || firstTime)
{
firstTime = false;
DataEventPublisher.BeforeChange(this, "FailureMechanismSystemType");
failureMechanismSystemType = value;
// To solve MWDAM-592, remember the current pipingmodeltype as this gets reset by setting the
// calculationmodel. Only switch it back when needed.
PipingModelType oldPipingModelType = pipingModelType;
foreach (Enum possibleModel in GetDomain("CalculationModel"))
{
CalculationModel = possibleModel;
break;
}
if (failureMechanismSystemType == FailureMechanismSystemType.Piping)
{
PipingModelType = oldPipingModelType;
}
DataEventPublisher.AfterChange(this, "FailureMechanismSystemType");
}
}
}
///
/// The dam project type.
///
[Browsable(false)] public static DamProjectType DamProjectType { get; set; }
///
/// The piping model type.
///
[Browsable(false)]
public PipingModelType PipingModelType
{
get
{
return pipingModelType;
}
set
{
pipingModelType = value;
if (failureMechanismSystemType == FailureMechanismSystemType.Piping)
{
CalculationModel = pipingModelType;
}
}
}
///
/// The stability model type.
///
[Browsable(false)]
public StabilityModelType StabilityModelType
{
get
{
return stabilityModelType;
}
set
{
stabilityModelType = value;
if (failureMechanismSystemType != FailureMechanismSystemType.Piping)
{
CalculationModel = value;
}
}
}
///
/// The calculationmodel is only needed to support the selection of the modeltype in the UI. The dropdownlist
/// in the UI depends on this. This set can be filled with any proper types (for piping, stabilty etc) for any
/// mechanism instead of the fixed types per mechanism.
///
[XmlIgnore]
[Label("Model")]
[PropertyOrder(1, 1)]
public Enum CalculationModel
{
get
{
return calculationModel;
}
set
{
DataEventPublisher.BeforeChange(this, "CalculationModel");
calculationModel = value;
if (value is PipingModelType)
{
pipingModelType = (PipingModelType) value;
}
else
{
stabilityModelType = (StabilityModelType) value;
if (stabilityModelType != StabilityModelType.Bishop)
{
damStabilityParameters.SearchMethod = StabilitySearchMethod.BeeSwarm;
}
}
DataEventPublisher.AfterChange(this, "CalculationModel");
}
}
///
/// Gets or sets the type of the selected analysis.
///
///
/// The type of the selected analysis.
///
[Label("Analysis type")]
public static AnalysisType SelectedAnalysisType
{
get
{
return selectedAnalysisType;
}
set
{
// Create local instance of DamProjectCalculationSpecification because event handlers need sender
var r = new DamProjectCalculationSpecification();
DataEventPublisher.BeforeChange(r, "SelectedAnalysisType");
selectedAnalysisType = value;
DataEventPublisher.AfterChange(r, "SelectedAnalysisType");
}
}
[XmlIgnore] [Browsable(false)] public IVisibleEnabledProvider VisibleEnabledProvider { get; set; }
public void Dispose()
{
DataEventPublisher.OnAfterChange -= DataEventPublisher_OnAfterChange;
}
public ICollection GetDomain(string property)
{
switch (property)
{
case "CalculationModel":
return ConfigurationManager.Instance.GetAvailableMechanismModels(DamProjectType, failureMechanismSystemType);
case "PipingModelType":
return ConfigurationManager.Instance.GetAvailableMechanismModels(DamProjectType, FailureMechanismSystemType.Piping);
case "FailureMechanismSystemType":
return ConfigurationManager.Instance.GetAvailableFailureMechanisms(DamProjectType);
case "SelectedAnalysisType":
return new[]
{
AnalysisType.AdaptGeometry,
AnalysisType.NoAdaption
};
default: return null;
}
}
public bool IsVisible(string property)
{
if (Equals(property, nameof(StabilityModelType)))
{
switch (FailureMechanismSystemType)
{
case FailureMechanismSystemType.StabilityInside: return true;
case FailureMechanismSystemType.StabilityOutside: return false;
case FailureMechanismSystemType.Piping: return false;
default: return true;
}
}
switch (property)
{
case "SelectedAnalysisType": return Location.DamProjectType == DamProjectType.Design;
case "DamStabilityParameters": return FailureMechanismSystemType == FailureMechanismSystemType.StabilityInside ||
FailureMechanismSystemType == FailureMechanismSystemType.StabilityOutside;
default: return true;
}
}
public bool IsEnabled(string property)
{
if (Equals(nameof(CalculationModel), property)
&& FailureMechanismSystemType == FailureMechanismSystemType.StabilityOutside)
{
return false; // Disable model selection when stability outside is active, as only Bishop is currently supported.
}
switch (property)
{
default: return true;
}
}
private void DataEventPublisher_OnAfterChange(object sender, PublishEventArgs e)
{
if (sender == damStabilityParameters)
{
DataEventPublisher.AfterChange(this);
}
}
private void Assign(DamProjectCalculationSpecification damFailureMechanismCalculation)
{
failureMechanismSystemType = damFailureMechanismCalculation.FailureMechanismSystemType;
calculationModel = damFailureMechanismCalculation.CalculationModel;
pipingModelType = damFailureMechanismCalculation.PipingModelType;
stabilityModelType = damFailureMechanismCalculation.StabilityModelType;
if (DamStabilityParameters != null)
{
DamStabilityParameters.Assign(damFailureMechanismCalculation.DamStabilityParameters);
}
else
{
DamStabilityParameters = null;
}
}
}