Run your Scala model

Suggest edits
Documentation > Run

Contents

Simple ScalaTask 🔗

You can program tasks in Scala using the ScalaTask. For instance the following workflow sums all the elements of an array using a ScalaTask and displays the results. 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.
val array = Val[Array[Double]]
val result = Val[Double]

val sum =
  ScalaTask("val result = array.sum") set (
    inputs += array,
    outputs += result,
    array := Array(8.0, 9.0, 10.0)
  )

(sum hook ToStringHook())
You can embed 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.

File Management 🔗

A documentation on file management expose how to handle the input and output files in the case of the ScalaTask. Alternatively, if your Java or Scala code requires some files, you can also add them to the resources and use the workDirectory variable to access them:
val content = Val[Double]

val read =
  ScalaTask("""val content = (workDirectory / "myFile.txt").content""") set (
    resources += workDirectory / "myFile.txt",
    outputs += content
  )
You can have the same behaviour for a file taken as input using the inputFiles function:
val myFile = Val[File]
val content = Val[String]

val read =
  ScalaTask("""val content = (workDirectory / "myFile.txt").content""") set (
    inputFiles += (myFile, "myFile.txt"),
    myFile := workDirectory / "myFile.txt",
    outputs += content
  )
Generally it is easier to directly use the variable containing the file:
val myFile = Val[File]
val content = Val[String]

val read =
  ScalaTask("val content = myFile.content") set (
    myFile := workDirectory / "myFile.txt",
    outputs += content
  )

Plugins 🔗

If you use your own OpenMole plugins and you call some code provided by the plugin in a ScalaTask you have associate the plugin with this task. Indeed OpenMOLE as no way of knowing that the task uses this plugin and the execution on a remote environment will certainly fail. To do so use the plugins keyword and the pluginsOf function. pluginsOf take an object of your plugins in the parameter. It can take a class as well 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 and Hello is used by the ScalaTask:
// Declare the variable
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)
)

DirectSampling(
  evaluation = hello hook ToStringHook(),
  sampling = i in (0 to 2)
)

Workflow example 🔗

Multiple ScalaTasks can be joined to compose a workflow. Lets imagine that you want to perform an expensive computation on every element of an array. For the sake of simplicity, the "expensive computation" is here a multiplication by 2.
val element = Val[Double]
val multiplied = Val[Double]
val result = Val[Double]

val expensive = ScalaTask("val multiplied = element * 2") set (
  inputs += element,
  outputs += multiplied
)

val sum = ScalaTask("val result = multiplied.sum") set (
  inputs += multiplied.toArray,
  outputs += result
)

DirectSampling (
  evaluation = expensive,
  sampling = element in List(8.0, 9.0, 10.0),
  aggregation = sum hook ToStringHook()
)

The execution of this workflow can be distributed using OpenMOLE's environments. Check the page dedicated to environments to learn more on this process.