// Copyright (C) Stichting Deltares 2018. 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.DikesAssessmentRegional; using Deltares.DamEngine.Calculators.DikesDesign; using Deltares.DamEngine.Data.General; using Deltares.DamEngine.Data.Standard; 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 { private ProgressDelegate progressDelegate; private SendMessageDelegate sendMessageDelegate; private UserAbortDelegate userAbortDelegate; /// /// 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 { return progressDelegate; } set { progressDelegate = value; } } /// /// Gets or sets the send message delegate. /// /// /// The send message delegate. /// public SendMessageDelegate SendMessageDelegate { get { return sendMessageDelegate; } set { sendMessageDelegate = value; } } /// /// Gets or sets the user abort delegate. /// /// /// The user abort delegate. /// public UserAbortDelegate UserAbortDelegate { get { return userAbortDelegate; } set { userAbortDelegate = value; } } /// /// Gets or sets the dam project data. /// /// /// The dam project data. /// public DamProjectData DamProjectData { get; set; } private void SendMessage(LogMessage logMessage) { SendMessageDelegate?.Invoke(logMessage); } private void Progress(double progress) { ProgressDelegate?.Invoke(progress); } private bool UserAbort() { if (UserAbortDelegate != null) { return UserAbortDelegate(); } else { return false; } } /// /// Validates the model. /// /// Validation messages in an XML string public string Validate() { SendMessage(new LogMessage() { MessageType = LogMessageType.Info, Message = Resources.StartValidation }); 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 }); List calculationMessages = null; switch (DamProjectData.DamProjectType) { case DamProjectType.AssessmentRegional: var regionalScenariosCalculator = new RegionalScenariosCalculation(); regionalScenariosCalculator.RegisterProgress(progressDelegate); regionalScenariosCalculator.Execute(DamProjectData); regionalScenariosCalculator.GetResults(); break; case DamProjectType.Operational: throw new NotImplementedException(); case DamProjectType.Design: DesignCalculator 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; } } }