Hooks

Suggest edits
Documentation > Language

Contents

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 Hooks to save or display results generated on remote environments. Hooks are conceived to perform an action upon completion of the task they are attached to.

How to plug a hook to a task 🔗

Let's consider this simple workflow:

val i = Val[Int]
val hello = ScalaTask("i = i * 2") set (
  inputs += i,
  outputs += i
)

val h = ToStringHook()

DirectSampling(
  evaluation = (hello hook h),
  sampling = i in (0 to 9)
)

The hook h is plugged to the end of the hello task. Everytime hello finishes, the hook h is executed. Multiple hooks can also be plugged to the same task as shown in this next example:

val i = Val[Int]

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

val h1 = ToStringHook()
val h2 = ToStringHook()
val h3 = ToStringHook()

(hello hook (h1, h2, h3))

Hooks come in various declinations, with different actions on the results. The available hooks are described hereafter.

Hooks to write into files 🔗

Write a string 🔗

Similarly to the AppendToFileHook, any string can be appended to a file, using the more general AppendToFileHook. The appended strings can be a combination of variables from the dataflow and plain text.

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

Write an entire file 🔗

Use the AppendFileFileHook as well.

val file = Val[File]
val h = AppendToFileHook("${file.content}", "/path/of/the/file")

Write into a CSV file 🔗

The CSVHook takes data from the dataflow and appends it to a file formatted as CSV:

val i = Val[Int]

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

The path is expanded using the variables from the dataflow (expressions between ${} are evaluated and replaced).

The optional last parameter of AppendToCSVFileHook is the list of variables to write to the CSV file. The default behaviour when this list is not specified is to dump all the variables from the dataflow to the file. You can restrict this behaviour by listing only the variables you want to save.

Some additional optional parameters can be passed to the CSVHook.
Setting csvHeader = "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.
arrayOnRaw = true forces the flattening of the input lists such that all variables values are written to a single row/line of the CSV file.
For instance:
val i = Val[Int]
val j = Val[Array[Int]]

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


Hook to copy a file 🔗

The CopyFileHook makes it possible to copy a file/directory from the dataflow 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 in the console, use the ToStringHook:

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

val h = ToStringHook(i, j)

If no variable is specified, ToStringHook displays the whole dataflow.

Display results in the stdout 🔗

To display a string formed of variables and plain text in the console, use the DisplayHook. 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).