Commit be7c0bff authored by Romain Reuillon's avatar Romain Reuillon

[Plugin] enh: implement json outpout for evolution

parent d8ddbe64
......@@ -31,11 +31,10 @@ object CSVHook {
object CSVOutputFormat {
implicit def format: OutputFormat[CSVOutputFormat] = new OutputFormat[CSVOutputFormat] {
override def write(format: CSVOutputFormat, output: WritableOutput, ps: Seq[Val[_]]): FromContext[Unit] = FromContext { p
override def write(format: CSVOutputFormat, output: WritableOutput, variables: Seq[Variable[_]]): FromContext[Unit] = FromContext { p
import p._
val vs = ps.map(context(_))
def headerLine = format.header.map(_.from(context)) getOrElse csv.header(ps, vs, format.arrayOnRow)
def headerLine = format.header.map(_.from(context)) getOrElse csv.header(variables.map(_.prototype), variables, format.arrayOnRow)
output match {
case WritableOutput.FileValue(file)
......@@ -44,12 +43,12 @@ object CSVHook {
val h = if (f.isEmpty) Some(headerLine) else None
if (create) f.atomicWithPrintStream { ps csv.writeVariablesToCSV(ps, h, vs, format.arrayOnRow) }
else f.withPrintStream(append = true, create = true) { ps csv.writeVariablesToCSV(ps, h, vs, format.arrayOnRow) }
if (create) f.atomicWithPrintStream { ps csv.writeVariablesToCSV(ps, h, variables.map(_.value), format.arrayOnRow) }
else f.withPrintStream(append = true, create = true) { ps csv.writeVariablesToCSV(ps, h, variables.map(_.value), format.arrayOnRow) }
case WritableOutput.PrintStreamValue(ps)
val header = Some(headerLine)
csv.writeVariablesToCSV(ps, header, vs, format.arrayOnRow)
csv.writeVariablesToCSV(ps, header, variables, format.arrayOnRow)
}
}
......
......@@ -4,11 +4,12 @@ import org.openmole.core.workflow.dsl._
import org.openmole.core.workflow.mole
import org.openmole.core.workflow.tools.WritableOutput
import org.openmole.core.context._
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, ps: Seq[Val[_]]): FromContext[Unit]
def write(format: T, output: WritableOutput, variables: Seq[Variable[_]]): FromContext[Unit]
def validate(format: T): FromContextHook.ValidateParameters Seq[Throwable]
def extension: String
}
......@@ -27,8 +28,9 @@ object FormattedFileHook {
val excludeSet = exclude.map(_.name).toSet
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, ps).from(context)
fileFormat.write(format, output, variables).from(context)
context
} validate { p
......
......@@ -8,21 +8,20 @@ import org.openmole.plugin.tool.json._
object JSONOutputFormat {
implicit def outputFormat = new OutputFormat[JSONOutputFormat] {
override def write(format: JSONOutputFormat, output: WritableOutput, ps: Seq[Val[_]]): FromContext[Unit] = FromContext { p
override def write(format: JSONOutputFormat, output: WritableOutput, variables: Seq[Variable[_]]): FromContext[Unit] = FromContext { p
import p._
import org.json4s._
import org.json4s.jackson.JsonMethods._
implicit val formats = DefaultFormats
val vs = ps.flatMap(v context.variable[Any](v.name).toSeq)
output match {
case WritableOutput.FileValue(file)
file.from(context).withPrintStream(append = false, create = true) { ps
ps.print(compact(render(variablesToJValue(vs))))
ps.print(compact(render(variablesToJValue(variables))))
}
case WritableOutput.PrintStreamValue(ps) ps.println(pretty(render(variablesToJValue(vs))))
case WritableOutput.PrintStreamValue(ps)
ps.println(pretty(render(variablesToJValue(variables))))
}
}
......
......@@ -28,10 +28,9 @@ object SavePopulationHook {
t.operations.result(context(t.populationPrototype).toVector, context(t.statePrototype)).from(context)
}
def hook[F](t: EvolutionWorkflow, output: WritableOutput, frequency: OptionalArgument[Long], format: F = CSVOutputFormat(overwrite = true))(implicit name: sourcecode.Name, definitionScope: DefinitionScope, outputFormat: OutputFormat[F]) = {
def hook[F](t: EvolutionWorkflow, output: WritableOutput, frequency: OptionalArgument[Long], format: F)(implicit name: sourcecode.Name, definitionScope: DefinitionScope, outputFormat: OutputFormat[F]) = {
Hook("SavePopulationHook") { p
import p._
import org.openmole.core.csv
def save =
frequency.option match {
......@@ -42,27 +41,12 @@ object SavePopulationHook {
}
if (save) {
val values = resultVariables(t).from(context).map(_.value)
def headerLine = csv.header(resultVariables(t).from(context).map(_.prototype.array), values)
output match {
case WritableOutput.FileValue(dir)
(dir / ExpandedString("population${" + t.generationPrototype.name + "}.csv")).from(context).withPrintStream(overwrite = false, create = true) { ps
csv.writeVariablesToCSV(
ps,
Some(headerLine),
values
)
}
case WritableOutput.PrintStreamValue(ps)
csv.writeVariablesToCSV(
ps,
Some(headerLine),
values
)
val outputFile = dir / ExpandedString("population${" + t.generationPrototype.name + "}" + outputFormat.extension)
outputFormat.write(format, outputFile, resultVariables(t).from(context)).from(context)
case o outputFormat.write(format, o, resultVariables(t).from(context)).from(context)
}
}
context
......@@ -70,9 +54,9 @@ object SavePopulationHook {
}
def apply[T](algorithm: T, output: WritableOutput, frequency: OptionalArgument[Long] = None)(implicit wfi: WorkflowIntegration[T], name: sourcecode.Name, definitionScope: DefinitionScope) = {
def apply[T, F: OutputFormat](algorithm: T, output: WritableOutput, frequency: OptionalArgument[Long] = None, format: F = CSVOutputFormat(overwrite = true))(implicit wfi: WorkflowIntegration[T], name: sourcecode.Name, definitionScope: DefinitionScope) = {
val t = wfi(algorithm)
hook(t, output, frequency)
hook(t, output, frequency, format)
}
}
......
......@@ -99,9 +99,9 @@ package object evolution {
implicit def workflowIntegration = WorkflowIntegration[DSLContainer[EvolutionWorkflow]](_.data)
implicit class EvolutionMethodContainer(dsl: DSLContainer[EvolutionWorkflow]) extends DSLContainerHook(dsl) {
def hook(output: WritableOutput, frequency: OptionalArgument[Long] = None): DSLContainer[EvolutionWorkflow] = {
def hook[F: OutputFormat](output: WritableOutput, frequency: OptionalArgument[Long] = None, format: F = CSVOutputFormat(overwrite = true)): DSLContainer[EvolutionWorkflow] = {
implicit val defScope = dsl.scope
dsl.hook(SavePopulationHook(dsl, output, frequency))
dsl.hook(SavePopulationHook(dsl, output, frequency, format))
}
}
......
......@@ -431,9 +431,9 @@ package file {
def atomicWithPrintStream[T](f: PrintStream T) = {
file.createParentDirectory
val tmpFile = java.io.File.createTempFile("printstream", ".tmp", file.getParentFile)
val tmpFile = java.io.File.createTempFile("stream", ".tmp", file.getParentFile)
try {
val printStream = new PrintStream(file.bufferedOutputStream())
val printStream = new PrintStream(tmpFile.bufferedOutputStream())
try f(printStream)
finally printStream.close()
}
......
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