Plug your Scala model

Suggest edits
Documentation > Plug

Contents

Simple ScalaTask 🔗

You can write Scala code to be executed in the workflow using the ScalaTask. For instance, the following workflow sums all the elements of an array using a ScalaTask, and displays the result. Similarly, you could use such a task to generate some model parameter values or perform some data analysis. To get more details on the hook part you can check the doc on hooks.

// Define variables
val array = Val[Array[Double]]
val result = Val[Double]

// Define a task
val sum = ScalaTask("val result = array.sum") set (
    inputs += array,
    outputs += result,

    // Default value
    array := Array(8.0, 9.0, 10.0)
)

// Define the workflow
sum hook display

You can also plug you own Scala/Java code and libraries in OpenMOLE using an OpenMOLE Plugin.

Useful functions 🔗

In addition to Scala code, OpenMOLE provides a few useful functions to aggregate data, create files, create random number generators, etc.

File Management 🔗

To learn how to provide pre-existing files to a ScalaTask, how to write into files or use files created by previous tasks in the workflow, you can go to the file management page.

Plugins 🔗

In order to use code from an OpenMole plugin in a task, you need to associate the plugin to the task, otherwise, OpenMOLE will not know how to execute your task. To do so, use the plugins keyword and the pluginsOf function. pluginsOf takes an object from your plugin as parameter, or a class if you use []:
  • for a class: plugins += pluginsOf[namespace.MyClass],
  • for an object: plugins += pluginsOf(namespace.MyObject).
For example, this ScalaTask uses an OpenMOLE plugin containing the namespace myOpenmolePlugin that itself contains the object Hello, which is used by the ScalaTask:

// Declare the variables
val i = Val[Int]
val j = Val[Int]

// Hello task
val hello = ScalaTask("val j = myopenmoleplugin.Hello.world(i)") set (
    inputs += i,
    outputs += (i, j),

    plugins += pluginsOf(myopenmoleplugin.Hello)
)

// Workflow
DirectSampling(
    evaluation = hello hook display,
    sampling = i in (0 to 2)
)