Documentation > Developers
While OpenMOLE's core code is not intended to be directly accessible to most users, an easy-to-use transparent and flexible API has
been developed for simple development of user extensions to the core. This API allows the user to implement new tasks, methods, samplings.
Concepts of the API 🔗
The primitives for the API are imported by importing contents of packageorg.openmole.core.dsl.extension
.
These primitive provide constructors for
- tasks
- samplings
- hooks
Task extensions 🔗
To define a new task, useTask(name: String)(process: FromContext => Context)
. What the task does is defined by the provided closure, which transforms a FromContext
into a Context
.
You can add implicits in your apply
method to get advanced services (mole services, network services, etc.).
Validation is provided with the validate
method which transforms validation parameters into a sequence of throwables.
For example
object MyTask {
def apply(taskparam: Double,taskval: Val[Double])(implicit moleService: MoleServices,workspace: Workspace, networkService: NetworkService) =
Task("MyTask"){
parameters =>
// do something with from context parameters : here add a constant to a double prototype
Context(taskval -> parameters.context(taskval) + taskparam)
} validate {vp => vp.map{proto => if(proto.v < 0) new Throwable("double proto should be positive") }} set (
(inputs,outputs) += (taskval)
)
}
Sampling extensions 🔗
To implement a sampling, the constructorSampling
takes a function transforming a FromContext
into a sampling results,
which is an Iterator[Iterable[Variable[_]]]
.
For example the following sampling assigns uniformally a sequence of doubles to some prototypes :
object MySampling {
def apply(values: FromContext[Array[[Double]]],prototypes: Val[_]*) = Sampling {
p =>
values.from(p.context).map{ value => prototypes.toList.map{ proto => Variable(proto,value)}}.toIterator
} validate { _ => Seq.empty} inputs {prototypes} prototypes {prototypes}
}