Create Scenario from File
This task imports a file content into a new scenario, supporting several formats.
@Bean
public ScriptedTaskDescription scenarioImportTask() {
ScriptedTaskDescription task = new ScriptedTaskDescription("ScenarioImportTask", "Create scenario from file");
task.setDescription("Import a file content into a new scenario, supporting several formats");
setI18nKeys(task, "SCENARIO_IMPORT");
var folder = VariableAccessExpression.ofFolder();
var scenarioFile = VariableAccessExpression.ofFile();
var scenarioCreationParameters = VariableAccessExpression.ofScenarioCreationExpression();
var createdScenarios = VariableAccessExpression.of("createdScenarios");
var issues = VariableAccessExpression.of("issues");
task.getScript()
.addStatement(AskInputStatement.ofVariable(folder, true, JobInputType.FOLDER_IN_WORKSPACE, "The Folder in a Workspace where to import the new Scenario "))
.addStatement(AskInputStatement.ofVariable(scenarioFile, true, ParameterTypes.file("xlsx", "xcsv", "dbrf", "gz", "zip"), "The Scenario file containing the data (.xlsx, .xcsv, .dbrf, .gz, .zip)"))
.addStatement(AskInputStatement.ofVariable(scenarioCreationParameters, true, JobInputType.SCENARIO_CREATION_PARAMETERS, "Parameters to create the Scenario"))
// Create the new scenario using metadata if exists.
.addStatement(SetVariableStatement.of(
createdScenarios.getVariableName(),
ScenarioCreationExpression.of(
scenarioCreationParameters,
folder
)
.withFileForProperties(scenarioFile)
))
// Import the scenarios or delete them if an error occurs
.addStatement(DoStatementOnLockedScenariosOrDeleteThem.of(
createdScenarios,
Block.of(
// Save the file content in the data service
ImportScenarioStatement.of(createdScenarios, scenarioFile).storingIssuesInto(issues.getVariableName()),
// Save the issues to output
SetTaskOutputStatement.of("issues", issues)
)
));
return task;
}