diff --git a/VectoCore/VectoCore/Models/Simulation/Impl/SimulatorFactory/IFollowUpSimulatorFactoryCreator.cs b/VectoCore/VectoCore/Models/Simulation/Impl/SimulatorFactory/IFollowUpSimulatorFactoryCreator.cs new file mode 100644 index 0000000000000000000000000000000000000000..7db39559c366e1a564fb52e273fbc232c1e613b7 --- /dev/null +++ b/VectoCore/VectoCore/Models/Simulation/Impl/SimulatorFactory/IFollowUpSimulatorFactoryCreator.cs @@ -0,0 +1,110 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TUGraz.VectoCommon.InputData; +using TUGraz.VectoCommon.Models; +using TUGraz.VectoCore.InputData.FileIO.XML; +using TUGraz.VectoCore.InputData.FileIO.XML.Declaration.DataProvider; +using TUGraz.VectoCore.OutputData; +using TUGraz.VectoCore.OutputData.FileIO; +using TUGraz.VectoCore.OutputData.XML; + +namespace TUGraz.VectoCore.Models.Simulation.Impl.SimulatorFactory +{ + public interface IFollowUpSimulatorFactoryCreator + { + ISimulatorFactory GetNextFactory(); + + IOutputDataWriter CurrentStageOutputDataWriter { get; } + + IDeclarationReport CurrentStageDeclarationReport { get; } + IInputDataProvider CurrentStageInputData { get; } + } + + + public abstract class FollowUpSimulatorFactoryCreator : IFollowUpSimulatorFactoryCreator + { + + public FollowUpSimulatorFactoryCreator(ISimulatorFactoryFactory simFactoryFactory) + { + + } + + #region Implementation of IFollowUpSimulatorFactoryCreator + + public abstract ISimulatorFactory GetNextFactory(); + public virtual IOutputDataWriter CurrentStageOutputDataWriter { get; } = null; + public virtual IDeclarationReport CurrentStageDeclarationReport { get; } = null; + + public virtual IInputDataProvider CurrentStageInputData { get; } = null; + + #endregion + } + + + public class RunInterimAfterPrimary : FollowUpSimulatorFactoryCreator + { + private readonly TempFileOutputWriter _currentStageOutputDataWriter = null; + private readonly IOutputDataWriter _originalReportWriter = null; + private readonly IDeclarationReport _currentStageDeclarationReport = null; + private readonly IXMLInputDataReader _inputDataReader; + private readonly IMultistagePrimaryAndStageInputDataProvider _originalStageInputData; + private readonly ISimulatorFactoryFactory _simFactoryFactory; + private readonly bool _validate; + + public RunInterimAfterPrimary(IMultistagePrimaryAndStageInputDataProvider originalStageInputData, + IOutputDataWriter reportWriter, + ISimulatorFactoryFactory simFactoryFactory, + IXMLInputDataReader inputDataReader, bool validate) : base(simFactoryFactory) + { + _validate = validate; + _simFactoryFactory = simFactoryFactory; + _originalStageInputData = originalStageInputData; + _inputDataReader = inputDataReader; + _originalReportWriter = reportWriter; + _currentStageOutputDataWriter = + new TempFileOutputWriter(reportWriter, ReportType.DeclarationReportManufacturerXML) + ; + _currentStageDeclarationReport = + new XMLDeclarationReportPrimaryVehicle(_currentStageOutputDataWriter, true); + + } + + + + #region Implementation of IFollowUpSimulatorFactoryCreator + + public override ISimulatorFactory GetNextFactory() + { + //Prepare inputdata for next simulation step + var primaryInputData = _inputDataReader.CreateDeclaration(_currentStageOutputDataWriter + .GetDocument(ReportType.DeclarationReportPrimaryVehicleXML).CreateReader()) as IMultistageBusInputDataProvider; + var stageInputData = _originalStageInputData.StageInputData; + + var nextStageInput = new XMLDeclarationVIFInputData(primaryInputData, stageInputData); + + var manStagesCount = + nextStageInput.MultistageJobInputData.JobInputData.ManufacturingStages?.Count ?? -1; + + _originalReportWriter.NumberOfManufacturingStages = manStagesCount; + + return _simFactoryFactory.Factory(ExecutionMode.Declaration, nextStageInput, _originalReportWriter, null, + null, _validate); + } + + #endregion + + + #region Overrides of FollowUpSimulatorFactoryCreator + + public override IInputDataProvider CurrentStageInputData => _originalStageInputData.PrimaryVehicle; + + public override IOutputDataWriter CurrentStageOutputDataWriter => _currentStageOutputDataWriter; + public override IDeclarationReport CurrentStageDeclarationReport => _currentStageDeclarationReport; + + #endregion + + } +} diff --git a/VectoCore/VectoCore/Models/Simulation/Impl/SimulatorFactory/SimulatorFactory.cs b/VectoCore/VectoCore/Models/Simulation/Impl/SimulatorFactory/SimulatorFactory.cs index 8733715099fbd1371d573936ca03c34ddb0d216e..e4c4a34f749a678b39787ae53f4ab0fb5c77c95d 100644 --- a/VectoCore/VectoCore/Models/Simulation/Impl/SimulatorFactory/SimulatorFactory.cs +++ b/VectoCore/VectoCore/Models/Simulation/Impl/SimulatorFactory/SimulatorFactory.cs @@ -66,16 +66,22 @@ namespace TUGraz.VectoCore.Models.Simulation.Impl.SimulatorFactory private static IKernel _kernel; //Kernel is only used when the SimulatorFactory is created with the Factory Method. protected Func<ISimulatorFactory> _followingSimulatorFactoryCreator = null; + protected IFollowUpSimulatorFactoryCreator _followUpSimulatorFactoryCreator = null; protected bool _simulate = true; + public ISimulatorFactory FollowUpSimulatorFactory { get { - if (!CreateFollowUpSimulatorFactory) - return null; + //if (!CreateFollowUpSimulatorFactory) + // return null; + + if (_followUpSimulatorFactoryCreator != null) { + return _followUpSimulatorFactoryCreator.GetNextFactory(); + } Log.Info("Starting next Simulation Step\n"); return _followingSimulatorFactoryCreator?.Invoke(); @@ -92,9 +98,11 @@ namespace TUGraz.VectoCore.Models.Simulation.Impl.SimulatorFactory [Obsolete("Creation of new SimulatorFactories should be done with SimulatorFactoryFactory NInject Factory", false)] public static ISimulatorFactory CreateSimulatorFactory(ExecutionMode mode, IInputDataProvider dataProvider, IOutputDataWriter writer, IDeclarationReport declarationReport = null, IVTPReport vtpReport=null, bool validate = true) { - lock (_kernelLock) { - if (_kernel == null) { - _kernel = new StandardKernel(new VectoNinjectModule()); + if (_kernel == null) { + lock (_kernelLock) { + if (_kernel == null) { + _kernel = new StandardKernel(new VectoNinjectModule()); + } } } return _kernel.Get<ISimulatorFactoryFactory>().Factory(mode, dataProvider, writer, declarationReport, vtpReport, validate); diff --git a/VectoCore/VectoCore/Models/Simulation/Impl/SimulatorFactory/SimulatorFactoryDeclaration.cs b/VectoCore/VectoCore/Models/Simulation/Impl/SimulatorFactory/SimulatorFactoryDeclaration.cs index cbc96b733c139f4b90cdd476be07b7d2e7019110..96effd551804c84a2f46eadca3cd77f5024874d0 100644 --- a/VectoCore/VectoCore/Models/Simulation/Impl/SimulatorFactory/SimulatorFactoryDeclaration.cs +++ b/VectoCore/VectoCore/Models/Simulation/Impl/SimulatorFactory/SimulatorFactoryDeclaration.cs @@ -2,9 +2,11 @@ using System.Collections.Generic; using System.Configuration; using System.Linq; +using System.ServiceModel.PeerResolvers; using System.Text; using System.Threading.Tasks; using System.Xml; +using JetBrains.Annotations; using Ninject; using TUGraz.VectoCommon.Exceptions; using TUGraz.VectoCommon.InputData; @@ -24,6 +26,8 @@ namespace TUGraz.VectoCore.Models.Simulation.Impl.SimulatorFactory { private readonly IXMLInputDataReader _xmlInputDataReader; private readonly ISimulatorFactoryFactory _simFactoryFactory; + private IInputDataProvider _currentStageInputData; + private IDeclarationReport _currentStageDeclarationReport; public SimulatorFactoryDeclaration(IInputDataProvider dataProvider, IOutputDataWriter writer, @@ -36,12 +40,32 @@ namespace TUGraz.VectoCore.Models.Simulation.Impl.SimulatorFactory { _xmlInputDataReader = xmlInputDataReader; _simFactoryFactory = simulatorFactoryFactory; + _currentStageInputData = dataProvider; + _currentStageDeclarationReport = declarationReport; + _followUpSimulatorFactoryCreator = CreateFollowUpFactoryCreator(); + UpdateCurrentStageInput(); _simulate = CanBeSimulated(dataProvider); if (_simulate) { - CreateDeclarationDataReader(dataProvider, declarationReport, vtpReport); + CreateDeclarationDataReader(_currentStageInputData, _currentStageDeclarationReport, vtpReport); } } + /// <summary> + /// Modifies the input and output of the current simulation step based on the Following step + /// </summary> + private void UpdateCurrentStageInput() + { + ReportWriter = _followUpSimulatorFactoryCreator?.CurrentStageOutputDataWriter ?? + ReportWriter; + + _currentStageDeclarationReport = _followUpSimulatorFactoryCreator?.CurrentStageDeclarationReport ?? + _currentStageDeclarationReport; + + _currentStageInputData = _followUpSimulatorFactoryCreator?.CurrentStageInputData ?? + _currentStageInputData; + } + + [UsedImplicitly] public SimulatorFactoryDeclaration(IInputDataProvider dataProvider, IOutputDataWriter writer, IXMLInputDataReader xmlInputDataReader, ISimulatorFactoryFactory simulatorFactoryFactory, bool validate) : this( @@ -75,6 +99,24 @@ namespace TUGraz.VectoCore.Models.Simulation.Impl.SimulatorFactory return true; } + private IFollowUpSimulatorFactoryCreator CreateFollowUpFactoryCreator() + { + switch (_currentStageInputData) { + case IMultistageBusInputDataProvider multistageBusInputDataProvider: + break; + case IMultistagePrimaryAndStageInputDataProvider multistagePrimaryAndStageInputDataProvider: + return new RunInterimAfterPrimary( + multistagePrimaryAndStageInputDataProvider, + ReportWriter, + _simFactoryFactory, _xmlInputDataReader, Validate); + case IMultiStageTypeInputData multiStageTypeInputData: + break; + default: + return null; + } + return null; + } + private void CreateDeclarationDataReader(IInputDataProvider dataProvider, IDeclarationReport declarationReport, IVTPReport vtpReport) { switch (dataProvider) { @@ -105,7 +147,6 @@ namespace TUGraz.VectoCore.Models.Simulation.Impl.SimulatorFactory { var report = declarationReport ?? new XMLDeclarationReportMultistageBusVehicle(ReportWriter); - //DataReader = CreateRunDataReader(multistageVifInputData, report); DataReader = new DeclarationModeMultistageBusVectoRunDataFactory(multistageVifInputData, report); CreateFollowUpSimulatorFactory = true; @@ -118,9 +159,6 @@ namespace TUGraz.VectoCore.Models.Simulation.Impl.SimulatorFactory return _simFactoryFactory.Factory(_mode, nextStageInput, ReportWriter, report, vtpReport, Validate); - //#pragma warning disable 618 -// return CreateSimulatorFactory(_mode, new XMLDeclarationVIFInputData(inputData as IMultistageBusInputDataProvider, null), ReportWriter, report, vtpReport, Validate); -//#pragma warning restore 618 }; } @@ -128,55 +166,49 @@ namespace TUGraz.VectoCore.Models.Simulation.Impl.SimulatorFactory } case IMultistagePrimaryAndStageInputDataProvider multiStagePrimaryAndStageInputData: { - - //Create Temporary Writer to hold the files only in Memory - var tempOutputWriter = new TempFileOutputWriter(ReportWriter, ReportType.DeclarationReportManufacturerXML); - var originalReportWriter = ReportWriter; - ReportWriter = tempOutputWriter; - var tempPrimaryReport = new XMLDeclarationReportPrimaryVehicle(tempOutputWriter, true); + throw new VectoException("Why are we here ? "); + //TODO: REPLACE WITH RunInterimAfterPrimary + //Create Temporary Writer to hold the files only in Memory +#pragma warning disable 162 + var tempOutputWriter = new TempFileOutputWriter(ReportWriter, ReportType.DeclarationReportManufacturerXML); + var originalReportWriter = ReportWriter; + ReportWriter = tempOutputWriter; + var tempPrimaryReport = new XMLDeclarationReportPrimaryVehicle(tempOutputWriter, true); - DataReader = CreateRunDataReader(multiStagePrimaryAndStageInputData.PrimaryVehicle, tempPrimaryReport); + DataReader = CreateRunDataReader(multiStagePrimaryAndStageInputData.PrimaryVehicle, tempPrimaryReport); - CreateFollowUpSimulatorFactory = true; - _followingSimulatorFactoryCreator = () => { - try - { - var primaryInputData = _xmlInputDataReader.CreateDeclaration(tempOutputWriter - .GetDocument(ReportType.DeclarationReportPrimaryVehicleXML).CreateReader()); - - var vifInputData = new XMLDeclarationVIFInputData( - primaryInputData as IMultistageBusInputDataProvider, - multiStagePrimaryAndStageInputData.StageInputData); + CreateFollowUpSimulatorFactory = true; + _followingSimulatorFactoryCreator = () => + { + try + { + var primaryInputData = _xmlInputDataReader.CreateDeclaration(tempOutputWriter + .GetDocument(ReportType.DeclarationReportPrimaryVehicleXML).CreateReader()); - var manStagesCount = - vifInputData.MultistageJobInputData.JobInputData.ManufacturingStages?.Count ?? -1; + var vifInputData = new XMLDeclarationVIFInputData( + primaryInputData as IMultistageBusInputDataProvider, + multiStagePrimaryAndStageInputData.StageInputData); - originalReportWriter.NumberOfManufacturingStages = manStagesCount; + var manStagesCount = + vifInputData.MultistageJobInputData.JobInputData.ManufacturingStages?.Count ?? -1; - return _simFactoryFactory.Factory(_mode, vifInputData, originalReportWriter, null, - vtpReport, Validate); + originalReportWriter.NumberOfManufacturingStages = manStagesCount; -//#pragma warning disable 618 -// var factory = CreateSimulatorFactory(_mode, -//#pragma warning restore 618 -// vifInputData, -// originalReportWriter, -// null, -// vtpReport, -// Validate); + return _simFactoryFactory.Factory(_mode, vifInputData, originalReportWriter, null, + vtpReport, Validate); + } + catch (Exception ex) + { + Log.Error($"Failed to create additional Simulation run: {ex.Message}"); + return null; + } + }; + return; +#pragma warning restore 162 -// return factory; - } - catch (Exception ex) - { - Log.Error($"Failed to create additional Simulation run: {ex.Message}"); - return null; - } - }; - return; - } + } default: throw new VectoException("Unknown InputData for Declaration Mode!"); } diff --git a/VectoCore/VectoCore/Models/Simulation/Impl/SimulatorFactory/SimulatorFactoryEngineering.cs b/VectoCore/VectoCore/Models/Simulation/Impl/SimulatorFactory/SimulatorFactoryEngineering.cs index 74f39e3eeacdf4ef68c036771911b1ff1e039639..c8da6c30de9165b6cd6529f03ab92a79983b69e5 100644 --- a/VectoCore/VectoCore/Models/Simulation/Impl/SimulatorFactory/SimulatorFactoryEngineering.cs +++ b/VectoCore/VectoCore/Models/Simulation/Impl/SimulatorFactory/SimulatorFactoryEngineering.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using JetBrains.Annotations; using TUGraz.VectoCommon.Exceptions; using TUGraz.VectoCommon.InputData; using TUGraz.VectoCommon.Models; @@ -13,6 +14,7 @@ namespace TUGraz.VectoCore.Models.Simulation.Impl.SimulatorFactory { public class SimulatorFactoryEngineering : SimulatorFactory { + [UsedImplicitly] public SimulatorFactoryEngineering(IInputDataProvider dataProvider, IOutputDataWriter writer, bool validate) : base(ExecutionMode.Engineering, writer, validate) { CreateEngineeringDataReader(dataProvider); diff --git a/VectoCore/VectoCore/VectoCore.csproj b/VectoCore/VectoCore/VectoCore.csproj index 1d976bf2bfbcf6a11236e1ea08cdd04bf41e6c56..1765147ccac24422bb9c2b69faef578601d57369 100644 --- a/VectoCore/VectoCore/VectoCore.csproj +++ b/VectoCore/VectoCore/VectoCore.csproj @@ -439,6 +439,7 @@ <Compile Include="Models\SimulationComponent\Impl\ATClutchInfo.cs" /> <Compile Include="Models\Simulation\Impl\PCCEcoRollEngineStopPreprocessor.cs" /> <Compile Include="Models\Simulation\Impl\PCCSegmentPreprocessor.cs" /> + <Compile Include="Models\Simulation\Impl\SimulatorFactory\IFollowUpSimulatorFactoryCreator.cs" /> <Compile Include="Models\Simulation\Impl\SimulatorFactory\SimulatorFactoryDeclaration.cs" /> <Compile Include="Models\Simulation\Impl\SimulatorFactory\SimulatorFactoryEngineering.cs" /> <Compile Include="Models\Simulation\ISimulationPreprocessor.cs" />