Optimization Server - Run Add Issue Task Using a Scenario URI

When using ScenarioDataExpression, you can choose the URI format. This brings some special features:

  • Only an URI to fetch the scenario data is sent to the worker. The input scenario data are not loaded and transferred as bytes through the OptimizationServer, but they are directly streamed between the Data Service and the worker upon worker demand. This leads to better performance and reduced memory usage.

  • The input scenario data modifications can be persisted directly by the worker (the data are streamed directly to the Data Service), which means that you will not need to declare an output scenario in your task.

The task does the same logic as the previous task example except it uses a Scenario URI to read and update the scenario:

@Configuration
public class Tasks {
    @Bean
    public ScriptedTaskDescription addIssueWithOptimizationServerWithUriTask() {
        ScriptedTaskDescription task = new ScriptedTaskDescription("addIssueWithOptimizationServerWithUri", "Add Issue with OptimizationServer using URI");
        task.setDescription("Add an issue to the selected scenario, using an OptimizationServer task using URI");
        task.getScript()
            .addStatement(AskInputStatement.of("scenario", true, JobInputType.scenarioId(WRITABLE)))
            .addStatement(AskInputStatement.of("Issue Message", true, JobInputType.TEXT, "The text of the issue to add"))
            .addStatement(ExecuteOptimizationServerTaskStatement
                .forTaskId(StringExpression.of("Add Issue with OptimizationServer using URI"))
                .withInput("Issue Message", VariableAccessExpression.of("Issue Message"))
                .withInput("inputCollector", ScenarioDataExpression.of(VariableAccessExpression.of("scenario")).withFormat(ScenarioDataFormat.URI)));
        return task;
    }
}

Compared to the previous task example, note that the call to withFormat(ScenarioDataFormat.URI) has been added and the call to withOutputScenario(...) has been removed.

The OptimizationServer task with id "Add Issue with OptimizationServer using URI" is defined in OptimizationServer tasks configuration in worker.yml as follows:

tasks:
  - id: Add Issue with OptimizationServer using URI
    implementationClassName: com.decisionbrain.gene.execution.worker.AddIssueUsingUriTask
    description: This task adds an issue to the provided scenario
    inputs:
      - name: Issue Message
        type: TEXT
        description: The text of the issue to add
        required: true
      - name: inputCollector
        type: BINARY
        description: Collector with initial data
        required: true

Compared to the previous task example, note that the outputCollector output has been removed.

With the following implementation class:

import com.decisionbrain.gene.worker.component.DbDomCollectorDAO;
import com.decisionbrain.gene.worker.task.GeneTask;
import com.decisionbrain.optimserver.common.parameter.Parameter;
import com.decisionbrain.optimserver.worker.api.ExecutionContext;
import com.example.caplan.model.CapacityPlanning;
import com.example.caplan.model.GeneIssue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

public class AddIssueUsingUriTask extends GeneTask<CapacityPlanning> {
    private static final Logger LOGGER = LoggerFactory.getLogger("AddIssueTask");

    @Override
    public void execute(Parameter input, Parameter output, ExecutionContext context) {

        DbDomCollectorDAO<CapacityPlanning> collectorDAO = createDbDomCollectorDAO(context, "inputCollector");

        // Load only issues
        CapacityPlanning collector = collectorDAO.loadScenario(List.of(GeneIssue.class));

        LOGGER.info("Seeing {} existing issues", collector.getGeneIssues().size());

        GeneIssue issue = collector.createGeneIssue();
        issue.setSeverity("INFO");
        issue.setMessage(context.getInputData("Issue Message").loadAsString());

        // Save only issues
        collectorDAO.saveScenario(collector, List.of(GeneIssue.class));
    }
}