Documentation > Run
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
ScalaTaskand 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.
You can embed you own Scala/Java code and libraries in OpenMOLE using an OpenMOLE Plugin.
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 DisplayHook())
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
workDirectoryvariable to access them:
You can have the same behaviour for a file taken as input using the
val content = Val[Double] val read = ScalaTask("""val content = (workDirectory / "myFile.txt").content""") set ( resources += 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 = (workDirectory / "myFile.txt").content""") set ( inputFiles += (myFile, "myFile.txt"), myFile := workDirectory / "myFile.txt", outputs += content )
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
ScalaTaskyou 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
pluginskeyword and the
pluginsOftake 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).
ScalaTaskuses an OpenMOLE plugin containing the namespace
myopenmolepluginthat itself contains the object
Hellois used by the
// 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 DisplayHook(), 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 DisplayHook() )
The execution of this workflow can be distributed using OpenMOLE's environments. Check the page dedicated to environments to learn more on this process.