// Copyright (C) Stichting Deltares 2024. All rights reserved.
//
// This file is part of the Dam Engine.
//
// The Dam Engine is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero 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 Deltares.DamEngine.Calculators.DikesDesign;
using Deltares.DamEngine.Calculators.DikesOperational;
using Deltares.DamEngine.Data.General;
using Deltares.DamEngine.Data.Standard.Calculation;
using Deltares.DamEngine.Data.Standard.Logging;
using Deltares.DamEngine.Interface.Properties;
using Deltares.DamEngine.Io;
using Deltares.DamEngine.Io.XmlInput;
using Deltares.DamEngine.Io.XmlOutput;
namespace Deltares.DamEngine.Interface;
///
/// The main interface class for accessing the Dam Engine
///
public class EngineInterface
{
///
/// Initializes a new instance of the class.
///
/// Xml string containing the model input.
public EngineInterface(string modelInput)
{
if (String.IsNullOrEmpty(modelInput))
{
throw new EngineInterfaceException(Resources.Error_EmptyInputString);
}
Input input = DamXmlSerialization.LoadInputFromXmlString(modelInput);
DamProjectData = FillDamFromXmlInput.CreateDamProjectData(input);
SendMessage(new LogMessage
{
MessageType = LogMessageType.Info,
Message = Resources.ModelAccepted
});
}
///
/// Gets or sets the progress delegate.
///
///
/// The progress delegate.
///
public ProgressDelegate ProgressDelegate { get; set; }
///
/// Gets or sets the send message delegate.
///
///
/// The send message delegate.
///
public SendMessageDelegate SendMessageDelegate { get; set; }
///
/// Gets or sets the user abort delegate.
///
///
/// The user abort delegate.
///
public UserAbortDelegate UserAbortDelegate { get; set; }
///
/// Gets or sets the dam project data.
///
///
/// The dam project data.
///
public DamProjectData DamProjectData { get; set; }
///
/// Validates the model.
///
/// Validation messages in an XML string
public string Validate()
{
SendMessage(new LogMessage
{
MessageType = LogMessageType.Info,
Message = Resources.StartValidation
});
List validationMessages = null;
try
{
bool isOperational = DamProjectData.DamProjectType == DamProjectType.Operational;
bool isPiping = DamProjectData.DamProjectCalculationSpecification.CurrentSpecification.FailureMechanismSystemType == FailureMechanismSystemType.Piping;
DamProjectData.Dike.Validate(isOperational, isPiping);
}
catch (Exception e)
{
var validationMessage = new LogMessage(LogMessageType.Error, null, e.Message);
validationMessages = new List
{
validationMessage
};
DamProjectData.CalculationMessages = new List();
}
// Local validation can give messages and these have to be added to the results but note that there
// already can be messages added by instantiation of the interface too. So add the local to the global
if (validationMessages != null && validationMessages.Count > 0)
{
if (DamProjectData != null && DamProjectData.CalculationMessages != null)
{
DamProjectData.CalculationMessages.AddRange(validationMessages);
}
}
// Now check if there are any messages at all, if so return them.
if (DamProjectData != null && DamProjectData.CalculationMessages != null && DamProjectData.CalculationMessages.Count > 0)
{
Output output = FillXmlOutputFromDam.CreateOutput(DamProjectData);
string outputXml = DamXmlSerialization.SaveOutputAsXmlString(output);
SendMessage(new LogMessage
{
MessageType = LogMessageType.Info,
Message = Resources.EndValidation
});
return outputXml;
}
SendMessage(new LogMessage
{
MessageType = LogMessageType.Info,
Message = Resources.EndValidation
});
return null;
}
///
/// Runs this instance.
///
/// Returns the output in an XML string
public string Run()
{
string outputXml;
SendMessage(new LogMessage
{
MessageType = LogMessageType.Info,
Message = Resources.StartCalculation
});
switch (DamProjectData.DamProjectType)
{
case DamProjectType.Operational:
var operationalCalculator = new OperationalCalculator();
operationalCalculator.Execute(DamProjectData);
break;
case DamProjectType.Design:
var designCalculator = new DesignCalculator();
designCalculator.RegisterProgress(ProgressDelegate);
designCalculator.Execute(DamProjectData);
break;
}
Output output = FillXmlOutputFromDam.CreateOutput(DamProjectData);
outputXml = DamXmlSerialization.SaveOutputAsXmlString(output);
SendMessage(new LogMessage
{
MessageType = LogMessageType.Info,
Message = Resources.EndCalculation
});
return outputXml;
}
private void SendMessage(LogMessage logMessage)
{
SendMessageDelegate?.Invoke(logMessage);
}
private void Progress(double progress)
{
ProgressDelegate?.Invoke(progress);
}
private bool UserAbort()
{
if (UserAbortDelegate != null)
{
return UserAbortDelegate();
}
return false;
}
}