Template Task

Suggest edits
Documentation > Utility Tasks

TemplateTask & TemplateFileTask 🔗

The TemplateTask and the TemplateFileTask are designed to generate input files for a model, by replacing some part of a template using OpenMOLE variables. A template contains the file content and some OpenMOLE expressions withing ${}.
A TemplateTask usage looks as follow:
val x = Val[Int]
val parameterFile = Val[File]

val template =
  TemplateTask(
    """
    |param1 = 9
    |param2 = ${x}
    |""".stripMargin,
    parameterFile
  ) set (
    inputs += x
  )

val container =
  ContainerTask("ubuntu", "cat /tmp/parameter.txt") set (
    inputFiles += (parameterFile, "/tmp/parameter.txt")
  )

DirectSampling(
  evaluation = template -- container,
  sampling = x in (0 to 10)
)

In this example the a file is generated for each value of x by the TemplateTask. This file is then provided to the task container.
The TemplateFileTask get the template content from a file. For instance you could consider a file named template.json, containing some templated json:
{
  "quantity":            ${quantity},
  "minimumSize":         6,
  "maximumSize":         8
}

You can then use the TemplateFileTask to generate the input file for you model, as follow:
val quantity = Val[Int]
val parameterFile = Val[File]

val template =
  TemplateFileTask(
    workDirectory / "template.json",
    parameterFile
  ) set (
    inputs += quantity
  )

val container =
  ContainerTask("ubuntu", "cat /tmp/parameter.json") set (
    inputFiles += (parameterFile, "/tmp/parameter.json")
  )

DirectSampling(
  evaluation = template -- container,
  sampling = quantity in (0 to 10)
)