Commit 9a6e9aa6 authored by Romain Reuillon's avatar Romain Reuillon

[Core] enh: add method data to output format

parent 82f7e181
......@@ -441,7 +441,7 @@ lazy val onvariableGrouping = OsgiProject(pluginDir, "org.openmole.plugin.groupi
/* Hook */
def allHook = Seq(displayHook, fileHook, modifierHook, jsonHook)
def allHook = Seq(displayHook, fileHook, modifierHook, jsonHook, omrHook)
lazy val displayHook = OsgiProject(pluginDir, "org.openmole.plugin.hook.display", imports = Seq("*")) dependsOn (openmoleDSL) settings (pluginSettings: _*)
......@@ -454,6 +454,9 @@ lazy val modifierHook = OsgiProject(pluginDir, "org.openmole.plugin.hook.modifie
lazy val jsonHook = OsgiProject(pluginDir, "org.openmole.plugin.hook.json", imports = Seq("*")) dependsOn(openmoleDSL,json, replication % "test") settings (
libraryDependencies += Libraries.scalatest) settings (pluginSettings: _*)
lazy val omrHook = OsgiProject(pluginDir, "org.openmole.plugin.hook.omr", imports = Seq("*")) dependsOn(openmoleDSL,json, replication % "test") settings (
libraryDependencies += Libraries.scalatest) settings (pluginSettings: _*)
/* Method */
......
......@@ -141,7 +141,7 @@ package composition {
def hook[F](
output: WritableOutput,
values: Seq[Val[_]] = Vector.empty,
format: F = CSVOutputFormat(append = true))(implicit definitionScope: DefinitionScope, fileFormat: OutputFormat[F]): TaskNode = hook(FormattedFileHook(output = output, values = values, format = format))
format: F = CSVOutputFormat(append = true))(implicit definitionScope: DefinitionScope, fileFormat: OutputFormat[F, Any]): TaskNode = hook(FormattedFileHook(output = output, values = values, format = format))
def source(sources: Source*) = copy(sources = this.sources ++ sources)
}
......
......@@ -30,8 +30,8 @@ object CSVHook {
object CSVOutputFormat {
implicit def format: OutputFormat[CSVOutputFormat] = new OutputFormat[CSVOutputFormat] {
override def write(format: CSVOutputFormat, output: WritableOutput, variables: Seq[Variable[_]]): FromContext[Unit] = FromContext { p
implicit def format: OutputFormat[CSVOutputFormat, Any] = new OutputFormat[CSVOutputFormat, Any] {
override def write(format: CSVOutputFormat, output: WritableOutput, variables: Seq[Variable[_]], method: Any): FromContext[Unit] = FromContext { p
import p._
def headerLine = format.header.map(_.from(context)) getOrElse csv.header(variables.map(_.prototype), variables.map(_.value), arrayOnRow = format.arrayOnRow)
......
......@@ -8,20 +8,21 @@ import org.openmole.core.exception.UserBadDataError
import org.openmole.core.expansion.FromContext
import org.openmole.core.workflow.builder._
trait OutputFormat[T] {
def write(format: T, output: WritableOutput, variables: Seq[Variable[_]]): FromContext[Unit]
trait OutputFormat[T, -M] {
def write(format: T, output: WritableOutput, variables: Seq[Variable[_]], method: M): FromContext[Unit]
def validate(format: T): FromContextHook.ValidateParameters Seq[Throwable]
def extension: String
}
object FormattedFileHook {
def apply[T: OutputFormat](
def apply[T, M](
format: T,
output: WritableOutput,
values: Seq[Val[_]] = Vector.empty,
exclude: Seq[Val[_]] = Vector.empty,
name: Option[String] = None)(implicit valName: sourcecode.Name, definitionScope: DefinitionScope, fileFormat: OutputFormat[T]): mole.FromContextHook =
method: M = None,
name: Option[String] = None)(implicit valName: sourcecode.Name, definitionScope: DefinitionScope, fileFormat: OutputFormat[T, M]): mole.FromContextHook =
Hook(name getOrElse "FileFormatHook") { parameters
import parameters._
......@@ -30,7 +31,7 @@ object FormattedFileHook {
val ps = { if (values.isEmpty) context.values.map { _.prototype }.toVector else values }.filter { v !excludeSet.contains(v.name) }
val variables = ps.map(p context.variable(p).getOrElse(throw new UserBadDataError(s"Variable $p not found in hook $this")))
fileFormat.write(format, output, variables).from(context)
fileFormat.write(format, output, variables, method).from(context)
context
} validate { p
......
......@@ -24,7 +24,7 @@ package mole {
def CSVHook = mole.CSVHook
def CSVOutputFormat = mole.CSVHook.CSVOutputFormat
type OutputFormat[T] = mole.OutputFormat[T]
type OutputFormat[T, D] = mole.OutputFormat[T, D]
type FromContextHook = mole.FromContextHook
type FromContextSource = mole.FromContextSource
}
......
......@@ -37,8 +37,7 @@ class HookSpec extends FlatSpec with Matchers {
"A hook" should "intercept the execution of a task" in {
val executed = new AtomicInteger(0)
val p = Val[String]("p")
val p = Val[String]
val t1 =
TestTask { _ + (p "test") } set (
......@@ -66,7 +65,6 @@ class HookSpec extends FlatSpec with Matchers {
val t1 =
TestTask { _ + (p "test") } set (
name := "Test",
outputs += p
)
......@@ -83,4 +81,10 @@ class HookSpec extends FlatSpec with Matchers {
executed should equal(true)
}
"Display hook" should "be accepted" in {
val t1 = EmptyTask()
val ex = t1 hook display
ex.run
}
}
......@@ -7,8 +7,8 @@ import org.openmole.plugin.tool.json._
object JSONOutputFormat {
implicit def outputFormat = new OutputFormat[JSONOutputFormat] {
override def write(format: JSONOutputFormat, output: WritableOutput, variables: Seq[Variable[_]]): FromContext[Unit] = FromContext { p
implicit def outputFormat = new OutputFormat[JSONOutputFormat, Any] {
override def write(format: JSONOutputFormat, output: WritableOutput, variables: Seq[Variable[_]], method: Any): FromContext[Unit] = FromContext { p
import p._
import org.json4s._
import org.json4s.jackson.JsonMethods._
......
/*
* Copyright (C) 2015 Romain Reuillon
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.openmole.plugin.hook.omr
import org.openmole.core.pluginmanager._
import org.openmole.core.preference.ConfigurationInfo
import org.osgi.framework.BundleContext
class Activator extends PluginInfoActivator {
override def keyWordTraits = List()
override def stop(context: BundleContext): Unit = {
PluginInfo.unregister(this)
ConfigurationInfo.unregister(this)
}
override def start(context: BundleContext): Unit = {
import org.openmole.core.pluginmanager.KeyWord._
val keyWords: Vector[KeyWord] =
Vector( // TaskKeyWord(objectName(JSONHook)),
// OtherKeyWord(objectName(JSONOutputFormat))
)
PluginInfo.register(this, Vector(this.getClass.getPackage), keyWords = keyWords)
ConfigurationInfo.register(
this,
ConfigurationInfo.list()
)
}
}
\ No newline at end of file
//package org.openmole.plugin.hook.omr
//
//import org.openmole.core.dsl._
//import org.openmole.core.dsl.extension._
//import org.openmole.core.workflow.mole.FromContextHook
//import org.openmole.plugin.tool.json._
//
//object OMROutputFormat {
//
// implicit def outputFormat = new OutputFormat[OMROutputFormat, Any] {
// override def write(format: OMROutputFormat, output: WritableOutput, variables: Seq[Variable[_]]): FromContext[Unit] = FromContext { p ⇒
// import p._
// import org.json4s._
// import org.json4s.jackson.JsonMethods._
//
// implicit val formats = DefaultFormats
//
// output match {
// case WritableOutput.FileValue(file) ⇒
// file.from(context).withPrintStream(append = false, create = true) { ps ⇒
// ps.print(compact(render(variablesToJValue(variables))))
// }
// case WritableOutput.StreamValue(ps, prelude) ⇒
// prelude.foreach(ps.print)
// ps.println(pretty(render(variablesToJValue(variables))))
// }
// }
//
// override def validate(format: OMROutputFormat): FromContextHook.ValidateParameters ⇒ Seq[Throwable] = { p ⇒ Seq() }
// override def extension = ".omr"
// }
//
//}
//
//case class OMROutputFormat()
......@@ -35,24 +35,24 @@ package object directsampling {
type Aggregation = AggregateTask.AggregateVal[_, _]
implicit class DirectSamplingDSL(dsl: DSLContainer[DirectSampling]) extends DSLContainerHook(dsl) {
def hook[T: OutputFormat](
def hook[T](
output: WritableOutput,
values: Seq[Val[_]] = Vector.empty,
format: T = CSVOutputFormat(append = true)): DSLContainer[DirectSampling] = {
format: T = CSVOutputFormat(append = true))(implicit outputFormat: OutputFormat[T, DirectSampling]): DSLContainer[DirectSampling] = {
implicit val defScope = dsl.scope
dsl hook FormattedFileHook(output = output, values = values, format = format)
dsl hook FormattedFileHook(output = output, values = values, format = format, method = dsl.data)
}
}
implicit class ReplicationDSL(dsl: DSLContainer[Replication]) extends DSLContainerHook(dsl) {
def hook[T: OutputFormat](
def hook[T](
output: WritableOutput,
values: Seq[Val[_]] = Vector.empty,
includeSeed: Boolean = false,
format: T = CSVOutputFormat(append = true)): DSLContainer[Replication] = {
format: T = CSVOutputFormat(append = true))(implicit outputFormat: OutputFormat[T, Replication]): DSLContainer[Replication] = {
implicit val defScope = dsl.scope
val exclude = if (!includeSeed) Seq(dsl.data.seed) else Seq()
dsl hook FormattedFileHook(output = output, values = values, exclude = exclude, format = format)
dsl hook FormattedFileHook(output = output, values = values, exclude = exclude, format = format, method = dsl.data)
}
}
......
......@@ -11,7 +11,7 @@ import org.openmole.plugin.tool.pattern._
import org.scalatest._
import org.openmole.core.workflow.test._
class PatternCompositionSpec extends FlatSpec with Matchers {
class DirectSamplingSpec extends FlatSpec with Matchers {
import org.openmole.core.workflow.test.Stubs._
"Direct sampling" should "transmit explored inputs" in {
......@@ -193,4 +193,15 @@ class PatternCompositionSpec extends FlatSpec with Matchers {
mole.run
}
"Direct samplings" should "accept display hook" in {
val l = Val[Double]
val model = EmptyTask() set (inputs += l)
DirectSampling(
model,
ExplicitSampling(l, Seq(1.0, 2.0))
) hook display
}
}
\ No newline at end of file
......@@ -22,13 +22,17 @@ import org.openmole.core.dsl.extension._
object SavePopulationHook {
case class EvolutionData()
def evolutionData(t: EvolutionWorkflow) = EvolutionData()
def resultVariables(t: EvolutionWorkflow) = FromContext { p
import p._
context.variable(t.generationPrototype).toSeq ++
t.operations.result(context(t.populationPrototype).toVector, context(t.statePrototype)).from(context)
}
def hook[F](t: EvolutionWorkflow, output: WritableOutput, frequency: Option[Long], last: Boolean, format: F)(implicit name: sourcecode.Name, definitionScope: DefinitionScope, outputFormat: OutputFormat[F]) = {
def hook[F](t: EvolutionWorkflow, output: WritableOutput, frequency: Option[Long], last: Boolean, format: F)(implicit name: sourcecode.Name, definitionScope: DefinitionScope, outputFormat: OutputFormat[F, EvolutionData]) = {
Hook("SavePopulationHook") { p
import p._
......@@ -44,7 +48,7 @@ object SavePopulationHook {
output match {
case WritableOutput.FileValue(dir)
saveFile(dir) match {
case Some(outputFile) outputFormat.write(format, outputFile.from(context), resultVariables(t).from(context)).from(context)
case Some(outputFile) outputFormat.write(format, outputFile.from(context), resultVariables(t).from(context), evolutionData(t)).from(context)
case None
}
case o
......@@ -55,7 +59,7 @@ object SavePopulationHook {
case _ false
}
if (save) outputFormat.write(format, o, resultVariables(t).from(context)).from(context)
if (save) outputFormat.write(format, o, resultVariables(t).from(context), evolutionData(t)).from(context)
}
context
......@@ -63,7 +67,7 @@ object SavePopulationHook {
}
def apply[T, F: OutputFormat](algorithm: T, output: WritableOutput, frequency: OptionalArgument[Long] = None, last: Boolean = false, format: F = CSVOutputFormat(unrollArray = true))(implicit wfi: WorkflowIntegration[T], name: sourcecode.Name, definitionScope: DefinitionScope) = {
def apply[T, F](algorithm: T, output: WritableOutput, frequency: OptionalArgument[Long] = None, last: Boolean = false, format: F = CSVOutputFormat(unrollArray = true))(implicit wfi: WorkflowIntegration[T], name: sourcecode.Name, definitionScope: DefinitionScope, outputFormat: OutputFormat[F, EvolutionData]) = {
val t = wfi(algorithm)
hook(t, output, frequency.option, last = last, format = format)
}
......@@ -72,14 +76,14 @@ object SavePopulationHook {
object SaveLastPopulationHook {
def apply[T, F](algorithm: T, output: WritableOutput, format: F = CSVOutputFormat(unrollArray = true))(implicit wfi: WorkflowIntegration[T], name: sourcecode.Name, definitionScope: DefinitionScope, outputFormat: OutputFormat[F]) = {
def apply[T, F](algorithm: T, output: WritableOutput, format: F = CSVOutputFormat(unrollArray = true))(implicit wfi: WorkflowIntegration[T], name: sourcecode.Name, definitionScope: DefinitionScope, outputFormat: OutputFormat[F, SavePopulationHook.EvolutionData]) = {
val t = wfi(algorithm)
Hook("SaveLastPopulationHook") { p
import p._
import org.openmole.core.csv
outputFormat.write(format, output, SavePopulationHook.resultVariables(t).from(context)).from(context)
outputFormat.write(format, output, SavePopulationHook.resultVariables(t).from(context), SavePopulationHook.evolutionData(t)).from(context)
context
} set (inputs += (t.populationPrototype, t.statePrototype))
......
......@@ -99,7 +99,7 @@ package object evolution {
implicit def workflowIntegration = WorkflowIntegration[DSLContainer[EvolutionWorkflow]](_.data)
implicit class EvolutionMethodContainer(dsl: DSLContainer[EvolutionWorkflow]) extends DSLContainerHook(dsl) {
def hook[F: OutputFormat](output: WritableOutput, frequency: OptionalArgument[Long] = None, last: Boolean = false, format: F = CSVOutputFormat()): DSLContainer[EvolutionWorkflow] = {
def hook[F](output: WritableOutput, frequency: OptionalArgument[Long] = None, last: Boolean = false, format: F = CSVOutputFormat())(implicit outputFormat: OutputFormat[F, SavePopulationHook.EvolutionData]): DSLContainer[EvolutionWorkflow] = {
implicit val defScope = dsl.scope
dsl.hook(SavePopulationHook(dsl, output, frequency = frequency, last = last, format = format))
}
......
......@@ -5,7 +5,7 @@ import org.openmole.core.dsl.extension._
object MorrisHook {
def apply[F](dsl: DSLContainer[Sensitivity.MorrisParams], output: WritableOutput, format: F = CSVOutputFormat())(implicit name: sourcecode.Name, definitionScope: DefinitionScope, outputFormat: OutputFormat[F]) =
def apply[F](dsl: DSLContainer[Sensitivity.MorrisParams], output: WritableOutput, format: F = CSVOutputFormat())(implicit name: sourcecode.Name, definitionScope: DefinitionScope, outputFormat: OutputFormat[F, Sensitivity.MorrisParams]) =
Hook("MorrisHook") { p
import p._
import WritableOutput._
......@@ -14,13 +14,13 @@ object MorrisHook {
output match {
case FileValue(dirFC)
Sensitivity.writeResults(format, FileValue(dirFC / s"mu${outputFormat.extension}"), inputs, dsl.data.outputs, Morris.mu(_, _)).from(context)
Sensitivity.writeResults(format, FileValue(dirFC / s"muStar${outputFormat.extension}"), inputs, dsl.data.outputs, Morris.muStar(_, _)).from(context)
Sensitivity.writeResults(format, FileValue(dirFC / s"sigma${outputFormat.extension}"), inputs, dsl.data.outputs, Morris.sigma(_, _)).from(context)
Sensitivity.writeResults(format, dsl.data, FileValue(dirFC / s"mu${outputFormat.extension}"), inputs, dsl.data.outputs, Morris.mu(_, _)).from(context)
Sensitivity.writeResults(format, dsl.data, FileValue(dirFC / s"muStar${outputFormat.extension}"), inputs, dsl.data.outputs, Morris.muStar(_, _)).from(context)
Sensitivity.writeResults(format, dsl.data, FileValue(dirFC / s"sigma${outputFormat.extension}"), inputs, dsl.data.outputs, Morris.sigma(_, _)).from(context)
case StreamValue(ps, prelude)
Sensitivity.writeResults(format, StreamValue(ps, Some(prelude.getOrElse("") + "mu\n")), inputs, dsl.data.outputs, Morris.mu(_, _)).from(context)
Sensitivity.writeResults(format, StreamValue(ps, Some("muStar\n")), inputs, dsl.data.outputs, Morris.muStar(_, _)).from(context)
Sensitivity.writeResults(format, StreamValue(ps, Some("sigma\n")), inputs, dsl.data.outputs, Morris.sigma(_, _)).from(context)
Sensitivity.writeResults(format, dsl.data, StreamValue(ps, Some(prelude.getOrElse("") + "mu\n")), inputs, dsl.data.outputs, Morris.mu(_, _)).from(context)
Sensitivity.writeResults(format, dsl.data, StreamValue(ps, Some("muStar\n")), inputs, dsl.data.outputs, Morris.muStar(_, _)).from(context)
Sensitivity.writeResults(format, dsl.data, StreamValue(ps, Some("sigma\n")), inputs, dsl.data.outputs, Morris.sigma(_, _)).from(context)
}
context
}
......
......@@ -5,7 +5,7 @@ import org.openmole.core.dsl.extension._
object SaltelliHook {
def apply[F](dsl: DSLContainer[Sensitivity.SaltelliParams], output: WritableOutput, format: F = CSVOutputFormat())(implicit name: sourcecode.Name, definitionScope: DefinitionScope, outputFormat: OutputFormat[F]) =
def apply[F](dsl: DSLContainer[Sensitivity.SaltelliParams], output: WritableOutput, format: F = CSVOutputFormat())(implicit name: sourcecode.Name, definitionScope: DefinitionScope, outputFormat: OutputFormat[F, Sensitivity.SaltelliParams]) =
Hook("SaltelliHook") { p
import p._
import WritableOutput._
......@@ -14,11 +14,11 @@ object SaltelliHook {
output match {
case FileValue(dirFC)
Sensitivity.writeResults(format, FileValue(dirFC / s"firstOrderIndices${outputFormat.extension}"), inputs, dsl.data.outputs, Saltelli.firstOrder(_, _)).from(context)
Sensitivity.writeResults(format, FileValue(dirFC / s"totalOrderIndices${outputFormat.extension}"), inputs, dsl.data.outputs, Saltelli.totalOrder(_, _)).from(context)
Sensitivity.writeResults(format, dsl.data, FileValue(dirFC / s"firstOrderIndices${outputFormat.extension}"), inputs, dsl.data.outputs, Saltelli.firstOrder(_, _)).from(context)
Sensitivity.writeResults(format, dsl.data, FileValue(dirFC / s"totalOrderIndices${outputFormat.extension}"), inputs, dsl.data.outputs, Saltelli.totalOrder(_, _)).from(context)
case StreamValue(ps, prelude)
Sensitivity.writeResults(format, StreamValue(ps, Some(prelude.getOrElse("") + "first order\n")), inputs, dsl.data.outputs, Saltelli.firstOrder(_, _)).from(context)
Sensitivity.writeResults(format, StreamValue(ps, Some("total order\n")), inputs, dsl.data.outputs, Saltelli.totalOrder(_, _)).from(context)
Sensitivity.writeResults(format, dsl.data, StreamValue(ps, Some(prelude.getOrElse("") + "first order\n")), inputs, dsl.data.outputs, Saltelli.firstOrder(_, _)).from(context)
Sensitivity.writeResults(format, dsl.data, StreamValue(ps, Some("total order\n")), inputs, dsl.data.outputs, Saltelli.totalOrder(_, _)).from(context)
}
context
......
......@@ -53,7 +53,7 @@ package object sensitivity {
}
def writeResults[F](format: F, output: WritableOutput, inputs: Seq[Val[_]], outputs: Seq[Val[_]], coefficient: (Val[_], Val[_]) Val[_])(implicit outputFormat: OutputFormat[F]) = FromContext { p
def writeResults[F, D](format: F, method: D, output: WritableOutput, inputs: Seq[Val[_]], outputs: Seq[Val[_]], coefficient: (Val[_], Val[_]) Val[_])(implicit outputFormat: OutputFormat[F, D]) = FromContext { p
import p._
def results = outputs.map { o
......@@ -64,7 +64,7 @@ package object sensitivity {
def allVals = Seq(Val[String]("output")) ++ inputs
val data = (results.transpose zip allVals).map { case (value, v) => Variable.unsecure(v.array, value) }
outputFormat.write(format, output, data).from(context)
outputFormat.write(format, output, data, method = method).from(context)
}
......@@ -77,7 +77,7 @@ package object sensitivity {
* @param dsl
*/
implicit class SaltelliMethodContainer(dsl: DSLContainer[Sensitivity.SaltelliParams]) extends DSLContainerHook(dsl) {
def hook[F: OutputFormat](output: WritableOutput, format: F = CSVOutputFormat()): DSLContainer[Sensitivity.SaltelliParams] = {
def hook[F](output: WritableOutput, format: F = CSVOutputFormat())(implicit outputFormat: OutputFormat[F, Sensitivity.SaltelliParams]): DSLContainer[Sensitivity.SaltelliParams] = {
implicit val defScope = dsl.scope
dsl hook SaltelliHook(dsl, output, format)
}
......@@ -89,7 +89,7 @@ package object sensitivity {
* @param dsl
*/
implicit class MorrisMethodContainer(dsl: DSLContainer[Sensitivity.MorrisParams]) extends DSLContainerHook(dsl) {
def hook[F: OutputFormat](output: WritableOutput, format: F = CSVOutputFormat()): DSLContainer[Sensitivity.MorrisParams] = {
def hook[F](output: WritableOutput, format: F = CSVOutputFormat())(implicit outputFormat: OutputFormat[F, Sensitivity.MorrisParams]): DSLContainer[Sensitivity.MorrisParams] = {
implicit val defScope = dsl.scope
dsl hook MorrisHook(dsl, output, format)
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment