Hooks

Suggest edits
Documentation > Language

Contents

What is a Hook? 🔗

Tasks in OpenMOLE are mute pieces of software. They are not conceived to write files, display values, or generally present any side effects at all. The role of a task is to compute some output data from their input data. That's what guarantees that their execution can be deported to other machines.
OpenMOLE introduces a mechanism called Hook to save or display results generated during the execution of a workflow. Hooks are conceived to perform an action on the outputs of the task they are plugged to. Different hooks are available for different actions that need to be performed.

How to plug a hook to a task 🔗

Let's consider this simple workflow:

// Define the variable i
val i = Val[Int]

// Define a task which returns its input value multiplied by 2
val hello = ScalaTask("i = i * 2") set (
  inputs += i,
  outputs += i
)

// Define a Hook
val h = DisplayHook()

// Define an exploration task
DirectSampling(
  evaluation = (hello hook h),
  sampling = i in (0 to 9)
)

The hook h is plugged to the end of the hello task, which means that every time hello finishes, the hook h is executed. Here it means that the dataflow will be printed in the standard output, so all the output values for i will be displayed.

Plugging multiple hooks to the same task 🔗

You can define as many hooks as you want in a workflow, and plug them to the same task or to different tasks. See the following workflow:

val i = Val[Int]

val hello = ScalaTask("val i = 2") set (
  outputs += i
)

val h1 = DisplayHook()
val h2 = DisplayHook()
val h3 = DisplayHook()

(hello hook (h1, h2, h3))

Here the hooks h1, h2, and h3 are all plugged to the hello task and will all be executed upon its completion. However, we should point out that plugging the same hook multiple times to the same task is usually of limited interest...!

Default hooks 🔗

Most OpenMOLE methods come with a default hook to save their results in a properly formatted file. To use these embedded hooks, you can directly give the required arguments (eg the path of the created file) to the hook keyword. For instance, in a direct sampling you can do:

val i = Val[Int]

//Define the "myModel" task
val myModel =
  ScalaTask("val o = i * 2") set (
    inputs += i,
    outputs += (i, o)
  )

// Define the exploration task
DirectSampling(
  evaluation = myModel,
  sampling = i in (0 to 10 by 1)
) hook (workDirectory / "result.csv")

The result of the direct sampling will be saved in the file result.csv.

Hooks to write into files 🔗

Write a string 🔗

Any string can be appended to a file using the hook AppendToFileHook. The appended string can be a combination of variables from the data flow and plain text.

val i = Val[Int]

val h = AppendToFileHook("path/of/the/file.txt", "string ${i} to append")

Write an entire file 🔗

AppendToFileHook can be used to write an entire file as well.

val file = Val[File]
val i = Val[Int]

val h = AppendToFileHook("${file.content}", "path/to/a/file/or/dir${i}.csv")

The path to the new file can be expanded using variables from the data flow (i here for instance). The variables or expressions written between ${} are evaluated and replaced with their value.

Write into a CSV file 🔗

The hook AppendToCSVFileHook takes data from the data flow and appends it to a file formatted as CSV.

val i = Val[Int]

val h = AppendToCSVFileHook("path/to/a/file/or/dir${i}.csv")

Some additional optional parameters can be passed to the AppendToCSVFileHook:
  • values = Seq(i, j) specifies which variables from the data flow should be written in the file. The default behaviour when this list is not specified is to dump all the variables from the dataflow to the file.
  • header = "Col1, Col2, ColZ" customises the header of the CSV file to be created with the string it receives as a parameter. Please note that this only happens if the file doesn't exist when the hook is executed.
  • arrayOnRow = true forces the flattening of input lists such that all list variables are written to a single row/line of the CSV file.
val i = Val[Int]
val j = Val[Array[Int]]

val h = AppendToCSVFileHook("path/to/a/file/or/dir${i}.csv", values = Seq(i, j), header = "i, j", arrayOnRow = true)

Write a matrix into a file 🔗

Some workflows may output two dimensional data, which can be understood as a matrix. For this, the MatrixHook writes matrix-like data to a file.

val matrix = Val[Array[Array[Double]]]

val h = MatrixHook("file.csv", matrix)

Output format will be a CSV file. Data understood as matrix are one and two dimensional arrays of double, int and long.

Hook to copy a file 🔗

The CopyFileHook makes it possible to copy a file or directory from the data flow to a given location on the machine running OpenMOLE.

val file = Val[File]
val i = Val[Int]

val h = CopyFileHook(file, "path/to/copy/the/file${i}.txt")

Hooks to display results 🔗

Display variables 🔗

To display a variable i from the workflow in the standard output, use the hook DisplayHook(i):

val i = Val[Int]
val j = Val[Int]

val h = DisplayHook(i, j)

If no variable is specified in DisplayHook(), the whole data flow will be displayed.

Display strings 🔗

To display a string in the standard output, use the DisplayHook("string"). The string can be formed of plain text and/or variables. You can think of the DisplayHook as an OpenMOLE equivalent to Scala's println.

val i = Val[Int]

val h = DisplayHook("The value of i is ${i}.")

Conditional hooking 🔗

You may want to filter outputs that are redirected to a hook, i.e. do conditional hooking. You can use for that the when keyword, built from a hook and a condition:

val i = Val[Int]

val display = DisplayHook("The value of i is ${i}.") when "i > 0"

Decorators exist for a simpler syntax: ConditionHook(myhook,mycondition) is equivalent to myhook when mycondition and myhook condition mycondition (where the condition can be given as a condition or a string).