Exemple #1
0
        public async Task <SimulationExport> ExportSimulation(Simulation simulation, ExportRunOptions exportRunOptions, PKSimProject project)
        {
            var projectName      = project.Name;
            var simulationName   = simulation.Name;
            var simulationFolder = Path.Combine(exportRunOptions.OutputFolder, FileHelper.RemoveIllegalCharactersFrom(simulationName));

            DirectoryHelper.CreateDirectory(simulationFolder);

            var simulationExport = new SimulationExport
            {
                Project          = projectName,
                SimulationFolder = simulationFolder,
                Simulation       = simulationName
            };

            var simulationRunOptions = new SimulationRunOptions();

            _lazyLoadTask.Load(simulation);
            _lazyLoadTask.LoadResults(simulation);

            var simulationExportOptions = new SimulationExportOptions
            {
                OutputFolder = simulationFolder,
                ExportMode   = exportRunOptions.ExportMode,
                ProjectName  = projectName,
            };

            if (!simulation.OutputSelections.HasSelection)
            {
                _logger.AddWarning($"Simulation '{simulationName}' does not have any selected output and will not be exported", projectName);
                return(simulationExport);
            }

            if (exportRunOptions.RunSimulation)
            {
                await _simulationExporter.RunAndExport(simulation, simulationRunOptions, simulationExportOptions);
            }

            else if (simulation.HasResults)
            {
                await _simulationExporter.Export(simulation, simulationExportOptions);
            }

            else
            {
                _logger.AddWarning($"Simulation '{simulationName}' does not have any results and will not be exported", projectName);
                return(simulationExport);
            }

            _logger.AddDebug($"Simulation '{simulationName}' exported to '{simulationFolder}'", projectName);
            return(simulationExport);
        }
        protected override async Task Context()
        {
            await base.Context();

            _expectedOutputPath             = Path.Combine(_qualificationConfiguration.OutputFolder, PROJECT_NAME);
            DirectoryHelper.DirectoryExists = s => string.Equals(s, _expectedOutputPath);
            DirectoryHelper.DeleteDirectory = (s, b) => _deletedDirectory = s;

            _simulationName = "S1";
            _simulation     = new Simulation {
                Name = _simulationName
            };
            _individualSimulation = new IndividualSimulation {
                Name = _simulationName
            };
            _input = new Input {
                Project = PROJECT_NAME, Name = _simulationName, SectionId = 2, Type = PKSimBuildingBlockType.Simulation, SectionLevel = 5
            };

            _expectedSimulationPath = Path.Combine(_expectedOutputPath, _simulationName);
            _simulationExport       = new SimulationMapping {
                Project = PROJECT_NAME, Simulation = _simulationName, Path = _expectedSimulationPath
            };
            _simulationExports = new[] { _simulationExport };
            A.CallTo(() => _exportSimulationRunner.ExportSimulationsIn(_project, A <ExportRunOptions> ._))
            .Invokes(x => _exportOptions = x.GetArgument <ExportRunOptions>(1))
            .Returns(_simulationExports);

            _observedData = DomainHelperForSpecs.ObservedData().WithName("OBS");
            _project.AddObservedData(_observedData);
            _projectSnapshot.Simulations     = new[] { _simulation };
            _expectedObservedDataXlsFullPath = Path.Combine(_qualificationConfiguration.ObservedDataFolder, $"{_observedData.Name}{Constants.Filter.XLSX_EXTENSION}");
            _expectedObservedDataCsvFullPath = Path.Combine(_qualificationConfiguration.ObservedDataFolder, $"{_observedData.Name}{Constants.Filter.CSV_EXTENSION}");

            _expectedInputFullPath = Path.Combine(_qualificationConfiguration.InputsFolder, PROJECT_NAME, PKSimBuildingBlockType.Simulation.ToString(), $"{_simulationName}{CoreConstants.Filter.MARKDOWN_EXTENSION}");

            A.CallTo(() => _jsonSerializer.Serialize(A <QualificationMapping> ._, _qualificationConfiguration.MappingFile))
            .Invokes(x => _mapping = x.GetArgument <QualificationMapping>(0));

            _project.AddBuildingBlock(_individualSimulation);
            _qualificationConfiguration.Inputs      = new[] { _input };
            _qualificationConfiguration.Simulations = new[] { _simulationName, };
            _runOptions.Run = true;
        }
Exemple #3
0
        private async Task exportProject(ExportRunOptions runOptions)
        {
            var projectFile = runOptions.ProjectFile;

            if (!FileHelper.FileExists(projectFile))
            {
                throw new OSPSuiteException($"Project file '{projectFile}' does not exist.");
            }

            DirectoryHelper.CreateDirectory(runOptions.OutputFolder);

            _logger.AddInfo($"Starting project export for '{projectFile}'");

            _workspacePersistor.LoadSession(_workspace, projectFile);
            _logger.AddDebug($"Project loaded successfully from '{projectFile}'");

            await ExportSimulationsIn(_workspace.Project, runOptions);

            _logger.AddInfo($"Project export for '{projectFile}' terminated");
        }
Exemple #4
0
        public async Task <SimulationExport[]> ExportSimulationsIn(PKSimProject project, ExportRunOptions exportRunOptions)
        {
            var nameOfSimulationsToExport = (exportRunOptions.Simulations ?? Enumerable.Empty <string>()).ToList();

            if (!nameOfSimulationsToExport.Any())
            {
                nameOfSimulationsToExport.AddRange(project.All <Simulation>().AllNames());
            }

            var simulationExports = new List <SimulationExport>();

            //sequential for now
            foreach (var simulationName in nameOfSimulationsToExport)
            {
                var simulation = project.BuildingBlockByName <Simulation>(simulationName);
                if (simulation == null)
                {
                    _logger.AddWarning($"Simulation '{simulationName}' was not found in project '{project.Name}'", project.Name);
                    continue;
                }

                simulationExports.Add((await ExportSimulation(simulation, exportRunOptions, project)));
            }

            return(simulationExports.ToArray());
        }
Exemple #5
0
 public Task RunBatchAsync(ExportRunOptions runOptions)
 {
     return(Task.Run(() => exportProject(runOptions)));
 }
Exemple #6
0
        public async Task RunBatchAsync(QualificationRunOptions runOptions)
        {
            _snapshotProjectCache.Clear();
            _logger.AddInfo(runOptions.Validate ? "Starting validation run..." : "Starting qualification run...");

            var config = await readConfigurationFrom(runOptions);

            if (config == null)
            {
                throw new QualificationRunException(UnableToLoadQualificationConfigurationFromFile(runOptions.ConfigurationFile));
            }

            var errorMessage = config.Validate().Message;

            if (!string.IsNullOrEmpty(errorMessage))
            {
                throw new QualificationRunException(errorMessage);
            }

            _logger.AddDebug($"Loading project from snapshot file '{config.SnapshotFile}'...");
            var snapshot = await snapshotProjectFromFile(config.SnapshotFile);

            //Ensures that the name of the snapshot is also the name of the project as defined in the configuration
            snapshot.Name = config.Project;
            _logger.AddDebug($"Project {snapshot.Name} loaded from snapshot file '{config.SnapshotFile}'.");

            await performBuildingBlockSwap(snapshot, config.BuildingBlocks);

            await performSimulationParameterSwap(snapshot, config.SimulationParameters);

            //Retrieve charts and validate inputs before exiting validation to ensure that we can throw error messages if an element is not available
            var plots = retrievePlotDefinitionsFrom(snapshot, config);

            validateInputs(snapshot, config);

            if (runOptions.Validate)
            {
                _logger.AddInfo($"Validation run terminated for {snapshot.Name}");
                return;
            }

            var begin   = DateTime.UtcNow;
            var project = await _snapshotTask.LoadProjectFromSnapshot(snapshot);

            _workspace.Project = project;

            var projectOutputFolder = createProjectOutputFolder(config.OutputFolder, project.Name);

            _logger.AddDebug($"Exporting project {project.Name} to '{projectOutputFolder}'", project.Name);

            var exportRunOptions = new ExportRunOptions
            {
                OutputFolder = projectOutputFolder,
                ExportMode   = SimulationExportMode.Xml | SimulationExportMode.Csv
            };

            var simulationExports = await _exportSimulationRunner.ExportSimulationsIn(project, exportRunOptions);

            var simulationMappings = simulationExports.Select(x => simulationMappingFrom(x, config)).ToArray();

            var observedDataMappings = await exportAllObservedData(project, config);

            var inputMappings = exportInputs(project, config);

            var mapping = new QualificationMapping
            {
                SimulationMappings   = simulationMappings,
                ObservedDataMappings = observedDataMappings,
                Plots  = plots,
                Inputs = inputMappings
            };

            await _jsonSerializer.Serialize(mapping, config.MappingFile);

            _logger.AddDebug($"Project mapping for '{project.Name}' exported to '{config.MappingFile}'", project.Name);

            var projectFile = Path.Combine(config.TempFolder, $"{project.Name}{CoreConstants.Filter.PROJECT_EXTENSION}");

            _workspacePersistor.SaveSession(_workspace, projectFile);
            _logger.AddDebug($"Project saved to '{projectFile}'", project.Name);

            var snapshotFile = Path.Combine(config.TempFolder, $"{project.Name}{Constants.Filter.JSON_EXTENSION}");
            await _snapshotTask.ExportModelToSnapshot(project, snapshotFile);

            _logger.AddDebug($"Project snapshot saved to '{snapshotFile}'", project.Name);

            var end       = DateTime.UtcNow;
            var timeSpent = end - begin;

            _logger.AddInfo($"Project '{project.Name}' exported for qualification in {timeSpent.ToDisplay()}", project.Name);
        }
Exemple #7
0
        public async Task RunBatchAsync(QualificationRunOptions runOptions)
        {
            _snapshotProjectCache.Clear();
            _logger.AddInfo(runOptions.Validate ? "Starting validation run..." : "Starting qualification run...");

            var config = await readConfigurationFrom(runOptions);

            if (config == null)
            {
                throw new QualificationRunException(UnableToLoadQualificationConfigurationFromFile(runOptions.ConfigurationFile));
            }

            var errorMessage = config.Validate().Message;

            if (!string.IsNullOrEmpty(errorMessage))
            {
                throw new QualificationRunException(errorMessage);
            }

            _logger.AddDebug($"Loading project from snapshot file '{config.SnapshotFile}'...", categoryName: config.Project);
            var snapshot = await snapshotProjectFromFile(config.SnapshotFile);

            //Ensures that the name of the snapshot is also the name of the project as defined in the configuration
            snapshot.Name = config.Project;
            _logger.AddDebug($"Project {snapshot.Name} loaded from snapshot file '{config.SnapshotFile}'.", categoryName: snapshot.Name);

            await performBuildingBlockSwap(snapshot, config.BuildingBlocks);

            await performSimulationParameterSwap(snapshot, config.SimulationParameters);

            //Retrieve charts and validate inputs before exiting validation to ensure that we can throw error messages if an element is not available
            var plots = retrievePlotDefinitionsFrom(snapshot, config);

            validateInputs(snapshot, config);

            if (runOptions.Validate)
            {
                _logger.AddInfo($"Validation run terminated for {snapshot.Name}", categoryName: snapshot.Name);
                return;
            }

            var begin   = DateTime.UtcNow;
            var project = await _snapshotTask.LoadProjectFromSnapshotAsync(snapshot, runOptions.Run);

            _workspace.Project = project;

            var projectOutputFolder = createProjectOutputFolder(config.OutputFolder, project.Name);

            _logger.AddDebug($"Exporting project {project.Name} to '{projectOutputFolder}'", project.Name);

            var exportRunOptions = new ExportRunOptions
            {
                OutputFolder = projectOutputFolder,
                //We run the output, this is for the old matlab implementation where we need xml. Otherwise, we only need pkml export
                ExportMode = runOptions.Run ? SimulationExportMode.Xml | SimulationExportMode.Csv : SimulationExportMode.Pkml,

                Simulations = config.Simulations,

                //We only want to export what is required in this case
                ExportAllSimulationsIfListIsEmpty = false
            };

            //Using absolute path for simulation folder. We need them to be relative
            var simulationMappings = await _exportSimulationRunner.ExportSimulationsIn(project, exportRunOptions);

            simulationMappings.Each(x => x.Path = relativePath(x.Path, config.OutputFolder));

            var observedDataMappings = await exportAllObservedData(project, config);

            var inputMappings = exportInputs(project, config);

            var mapping = new QualificationMapping
            {
                SimulationMappings   = simulationMappings,
                ObservedDataMappings = observedDataMappings,
                Plots  = plots,
                Inputs = inputMappings
            };

            await _jsonSerializer.Serialize(mapping, config.MappingFile);

            _logger.AddDebug($"Project mapping for '{project.Name}' exported to '{config.MappingFile}'", project.Name);

            if (runOptions.ExportProjectFiles)
            {
                var projectFile = Path.Combine(config.TempFolder, $"{project.Name}{CoreConstants.Filter.PROJECT_EXTENSION}");
                _workspacePersistor.SaveSession(_workspace, projectFile);
                _logger.AddDebug($"Project saved to '{projectFile}'", project.Name);

                var snapshotFile = Path.Combine(config.TempFolder, $"{project.Name}{Constants.Filter.JSON_EXTENSION}");
                await _snapshotTask.ExportModelToSnapshotAsync(project, snapshotFile);

                _logger.AddDebug($"Project snapshot saved to '{snapshotFile}'", project.Name);
            }

            var end       = DateTime.UtcNow;
            var timeSpent = end - begin;

            _logger.AddInfo($"Project '{project.Name}' exported for qualification in {timeSpent.ToDisplay()}", project.Name);
        }
        public async Task <SimulationMapping> ExportSimulation(Simulation simulation, ExportRunOptions exportRunOptions, PKSimProject project)
        {
            var projectName      = project.Name;
            var simulationName   = simulation.Name;
            var simulationFile   = FileHelper.RemoveIllegalCharactersFrom(simulationName);
            var simulationFolder = Path.Combine(exportRunOptions.OutputFolder, simulationFile);

            DirectoryHelper.CreateDirectory(simulationFolder);

            var simulationExport = new SimulationMapping
            {
                Project        = projectName,
                Path           = simulationFolder,
                Simulation     = simulationName,
                SimulationFile = simulationFile
            };

            var simulationRunOptions = new SimulationRunOptions();

            _lazyLoadTask.Load(simulation);
            _lazyLoadTask.LoadResults(simulation);

            var simulationExportOptions = new SimulationExportOptions
            {
                OutputFolder = simulationFolder,
                ExportMode   = exportRunOptions.ExportMode,
                ProjectName  = projectName,
            };

            if (exportRunOptions.RunSimulation)
            {
                await _simulationExporter.RunAndExport(simulation, simulationRunOptions, simulationExportOptions);
            }

            else
            {
                await _simulationExporter.Export(simulation, simulationExportOptions);
            }

            _logger.AddDebug($"Simulation '{simulationName}' exported to '{simulationFolder}'", projectName);
            return(simulationExport);
        }