Index: DamClients/DamLive/trunk/src/Deltares.Dam.Application.Live/DamEngineRunner.cs =================================================================== diff -u --- DamClients/DamLive/trunk/src/Deltares.Dam.Application.Live/DamEngineRunner.cs (revision 0) +++ DamClients/DamLive/trunk/src/Deltares.Dam.Application.Live/DamEngineRunner.cs (revision 1630) @@ -0,0 +1,152 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Deltares.Dam.Application.Live.Properties; +using Deltares.Dam.Data; +using Deltares.Dam.Data.DamEngineIo; +using Deltares.DamEngine.Interface; +using Deltares.DamEngine.Io; +using Deltares.DamEngine.Io.XmlInput; +using Deltares.Standard.Application; +using Deltares.Standard.Language; +using Deltares.Standard.Logging; + +namespace Deltares.Dam.Application.Live +{ + public class DamEngineRunner + { + internal const string NoDamxFile = "No .damx file set to load project data from."; + internal const string NoFewsInputFileAvailable = "No FEWS input file available to read the input time series from."; + internal const string NoFewsOutputFileAvailable = "No FEWS output file available to write the result to."; + internal const string ErrorExtractingWorkingFolder = "An error occured while trying to extract the path from the FEWS input file. This error can be solved by setting a working folder"; + internal LogHelper Logger = LogHelper.Create(); + public DamProjectData DamProjectData { get; set; } + + public CalculationParameters CalculationParameters { get; set; } + public TimeSerieCollection OutputTimeSeriesCollection { get; set; } + public TimeSerieCollection InputTimeSeriesCollection { get; set; } + public string StabilityWorkingPath { get; set; } + public string PipingWorkingPath { get; set; } + public string StabilityExePath { get; set; } + public bool UseMStabForCalculation { get; set; } + public double WaterLevelOffset { get; set; } + public string WorkingPath { get; set; } + public int MaxCalculationCores { get; set; } = 1; + protected internal FileInfo DamXFile { get; set; } + protected internal FileInfo FewsInputFile { get; set; } + protected internal FileInfo FewsOutputFile { get; set; } + protected internal FileInfo ParametersFile { get; set; } + public string Filter { get; set; } + public bool HasErrors + { + get { return Logger.HasLoggedExceptions; } + } + + public DamEngineRunner() + { + // WorkingPath will be extracted from file name if available + StabilityWorkingPath = Settings.Default.StabilityWorkingPath; + PipingWorkingPath = Settings.Default.PipingWorkingPath; + StabilityExePath = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), Settings.Default.MStabExePath); + UseMStabForCalculation = Settings.Default.UseMStabForCalculation; + WaterLevelOffset = Settings.Default.WaterLevelOffset; + + } + + public void Initialize() + { + if (DamProjectData == null) + { + if (DamXFile == null) + throw new InvalidOperationException(NoDamxFile); + + DamProjectData = DamProject.LoadData(DamXFile.FullName); + WorkingPath = Path.ChangeExtension(DamXFile.FullName, ".Calc"); + } + + if (InputTimeSeriesCollection == null) + { + if (FewsInputFile == null) + throw new InvalidOperationException(NoFewsInputFileAvailable); + + InputTimeSeriesCollection = TimeSerieCollection.LoadFromFile(FewsInputFile); + } + + if (OutputTimeSeriesCollection == null) + { + if (FewsOutputFile == null) + throw new InvalidOperationException(NoFewsOutputFileAvailable); + + OutputTimeSeriesCollection = InputTimeSeriesCollection.GetShallowCopy(); + } + + if (CalculationParameters == null && ParametersFile != null) + { + // Read calculation parameters, if available + CalculationParameters = CalculationParameters.LoadFromFile(ParametersFile); + } + + // Prepare DamProjetData for serializing to Dam Engine + DamProjectData.SelectedLocationJobs.Clear(); + foreach (Data.Location location in DamProjectData.Locations) + { + var locationJob = new LocationJob() + { + Location = location, Run = true + }; + DamProjectData.LocationJobs.Add(locationJob); + } + DamProjectData.FillOverallSensorData(); + + // Add timeseries + DamProjectData.InputTimeSerieCollection = InputTimeSeriesCollection; + } + + public void Run() + { + Initialize(); + // CreateAndSetWorkingDirectories(); + // DeleteFormerProjectFiles(); + CallDamEngine(); + // WriteResultsToFile(FewsOutputFile.FullName); + } + + private void CallDamEngine() + { + DamProjectData.MaxCalculationCores = MaxCalculationCores; + try + { + Input input = FillXmlInputFromDamUi.CreateInput(DamProjectData); +#if DEBUG + const string inputFilename = "InputFile.xml"; + DamXmlSerialization.SaveInputAsXmlFile(inputFilename, input); +#endif + string inputXml = DamXmlSerialization.SaveInputAsXmlString(input); + + var damEngineInterface = new EngineInterface(inputXml); + string validationMessages = damEngineInterface.Validate(); + // now the validation messages should be deserialized. If any, they should be passed on to the Validator + // and checked for errors. IF errors are found, then no calculation. When no messages or only warnings then + // do calculate. For now, just check length + if (string.IsNullOrEmpty(validationMessages)) + { + string outputXml = damEngineInterface.Run(); + var output = DamXmlSerialization.LoadOutputFromXmlString(outputXml); + FillDamUiFromXmlOutput.AddOutputToDamProjectData(DamProjectData, output); +#if DEBUG + const string outputFilename = "OutputFile.xml"; + DamXmlSerialization.SaveOutputAsXmlFile(outputFilename, output); +#endif + + } + } + catch (Exception e) + { + LogManager.Add(new LogMessage(LogMessageType.FatalError, typeof(EngineInterface), string.Format("{0}", e.Message))); + } + } + } +} Index: DamClients/DamLive/trunk/src/Deltares.DamLive.Tests/DamLiveIntegrationTest.cs =================================================================== diff -u -r1608 -r1630 --- DamClients/DamLive/trunk/src/Deltares.DamLive.Tests/DamLiveIntegrationTest.cs (.../DamLiveIntegrationTest.cs) (revision 1608) +++ DamClients/DamLive/trunk/src/Deltares.DamLive.Tests/DamLiveIntegrationTest.cs (.../DamLiveIntegrationTest.cs) (revision 1630) @@ -46,7 +46,7 @@ private string outputFile; private string projectFile; private string parameterFile; - private ModelRunner runner; + private DamEngineRunner runner; private DamProjectData damProjectData; [TestFixtureSetUp] @@ -323,7 +323,7 @@ // Load the sensor time serie data (see input file for details) var inputTimeSeries = TimeSerieCollection.LoadFromFile(inputFile); - runner = new TestModelRunner + runner = new DamEngineRunner() { WorkingPath = Path.ChangeExtension(projectFile, ".Calc"), StabilityWorkingPath = "stability", @@ -351,8 +351,7 @@ Stability = true } }, - ProjectData = damProjectData, - RunnerDelegate = new SensorModelRunner(), + DamProjectData = damProjectData, FewsOutputFile = new FileInfo(outputFile), InputTimeSeriesCollection = inputTimeSeries }; Index: DamClients/DamLive/trunk/src/Deltares.Dam.Application.Live/Program.cs =================================================================== diff -u -r1545 -r1630 --- DamClients/DamLive/trunk/src/Deltares.Dam.Application.Live/Program.cs (.../Program.cs) (revision 1545) +++ DamClients/DamLive/trunk/src/Deltares.Dam.Application.Live/Program.cs (.../Program.cs) (revision 1630) @@ -158,21 +158,20 @@ } - Logger.LogInfo("v1.0 - Deltares 2014"); + Logger.LogInfo("v1.0 - Deltares 2018"); Logger.LogInfo("Model runner started"); Console.WriteLine(); - var modelRunner = new ModelRunner - { - ParametersFile = new FileInfo(calculationParametersFile), - FewsInputFile = new FileInfo(fewsInputFile), - FewsOutputFile = new FileInfo(fewsOutputFile), - DamXFile = new FileInfo(damXFile), - Filter = filter, - RunnerDelegate = new SensorModelRunner() - }; + var damEngineRunner = new DamEngineRunner() + { + ParametersFile = new FileInfo(calculationParametersFile), + FewsInputFile = new FileInfo(fewsInputFile), + FewsOutputFile = new FileInfo(fewsOutputFile), + DamXFile = new FileInfo(damXFile), + Filter = filter, + }; - modelRunner.Run(); + damEngineRunner.Run(); Logger.LogInfo("Model runner ended"); Index: DamClients/DamLive/trunk/src/Deltares.Dam.Application.Live/Deltares.Dam.Application.Live.csproj =================================================================== diff -u -r1548 -r1630 --- DamClients/DamLive/trunk/src/Deltares.Dam.Application.Live/Deltares.Dam.Application.Live.csproj (.../Deltares.Dam.Application.Live.csproj) (revision 1548) +++ DamClients/DamLive/trunk/src/Deltares.Dam.Application.Live/Deltares.Dam.Application.Live.csproj (.../Deltares.Dam.Application.Live.csproj) (revision 1630) @@ -62,6 +62,13 @@ False ..\..\lib\Authorization\x86\Deltares.Authorization.dll + + ..\..\lib\DamEngine\Deltares.DamEngine.Interface.dll + + + False + ..\..\lib\DamEngine\Deltares.DamEngine.Io.dll + ..\..\lib\DSL-Geographic\Deltares.Geographic.dll @@ -82,6 +89,7 @@ +