Commit 3c58687a authored by Romain Reuillon's avatar Romain Reuillon

[Plugin] enh: first implementation of json output

parent 5e1d5422
......@@ -441,17 +441,18 @@ lazy val onvariableGrouping = OsgiProject(pluginDir, "org.openmole.plugin.groupi
/* Hook */
def allHook = Seq(displayHook, fileHook, modifierHook)
def allHook = Seq(displayHook, fileHook, modifierHook, jsonHook)
lazy val displayHook = OsgiProject(pluginDir, "org.openmole.plugin.hook.display", imports = Seq("*")) dependsOn (openmoleDSL) settings (pluginSettings: _*)
lazy val fileHook = OsgiProject(pluginDir, "org.openmole.plugin.hook.file", imports = Seq("*")) dependsOn(openmoleDSL, replication % "test") settings (
libraryDependencies += Libraries.scalatest
) settings (pluginSettings: _*)
libraryDependencies += Libraries.scalatest) settings (pluginSettings: _*)
lazy val modifierHook = OsgiProject(pluginDir, "org.openmole.plugin.hook.modifier", imports = Seq("*")) dependsOn (openmoleDSL) settings (
libraryDependencies += Libraries.scalatest
) settings (pluginSettings: _*)
libraryDependencies += Libraries.scalatest) settings (pluginSettings: _*)
lazy val jsonHook = OsgiProject(pluginDir, "org.openmole.plugin.hook.json", imports = Seq("*")) dependsOn(openmoleDSL,json, replication % "test") settings (
libraryDependencies += Libraries.scalatest) settings (pluginSettings: _*)
/* Method */
......
......@@ -41,7 +41,7 @@ package object extension {
def Context = org.openmole.core.context.Context
type WritableOutput = org.openmole.core.workflow.tools.WritableOutput
def WritableOutput = org.openmole.core.workflow.tools.WritableOutput
val WritableOutput = org.openmole.core.workflow.tools.WritableOutput
def ExpandedString = org.openmole.core.expansion.ExpandedString
......
......@@ -31,8 +31,8 @@ package composition {
import org.openmole.core.outputmanager.OutputManager
import org.openmole.core.workflow.builder.DefinitionScope
import org.openmole.core.workflow.execution.{ EnvironmentProvider, LocalEnvironmentProvider }
import org.openmole.core.workflow.mole.CSVHook.CSVFormat
import org.openmole.core.workflow.mole.{ CSVHook, FileFormat, FormattedFileHook, Grouping, Hook, MasterCapsule, Mole, MoleCapsule, MoleExecution, MoleExecutionContext, MoleServices, Source }
import org.openmole.core.workflow.mole.CSVHook.CSVOutputFormat
import org.openmole.core.workflow.mole.{ CSVHook, OutputFormat, FormattedFileHook, Grouping, Hook, MasterCapsule, Mole, MoleCapsule, MoleExecution, MoleExecutionContext, MoleServices, Source }
import org.openmole.core.workflow.sampling.Sampling
import org.openmole.core.workflow.task.{ EmptyTask, ExplorationTask, Task }
import org.openmole.core.workflow.tools.{ OptionalArgument, WritableOutput }
......@@ -141,7 +141,7 @@ package composition {
def hook[F](
output: WritableOutput,
values: Seq[Val[_]] = Vector.empty,
format: F = CSVFormat())(implicit definitionScope: DefinitionScope, fileFormat: FileFormat[F]): TaskNode = hook(FormattedFileHook(output = output, values = values, format = format))
format: F = CSVOutputFormat())(implicit definitionScope: DefinitionScope, fileFormat: OutputFormat[F]): TaskNode = hook(FormattedFileHook(output = output, values = values, format = format))
def source(sources: Source*) = copy(sources = this.sources ++ sources)
}
......
......@@ -21,17 +21,17 @@ object CSVHook {
arrayOnRow: Boolean = false,
overwrite: Boolean = false)(implicit name: sourcecode.Name, definitionScope: DefinitionScope): mole.FromContextHook =
FormattedFileHook(
format = CSVFormat(header = header, arrayOnRow = arrayOnRow, overwrite = overwrite),
format = CSVOutputFormat(header = header, arrayOnRow = arrayOnRow, overwrite = overwrite),
output = output,
values = values,
exclude = exclude,
name = Some("CSVHook")
)
object CSVFormat {
object CSVOutputFormat {
implicit def format: FileFormat[CSVFormat] = new FileFormat[CSVFormat] {
override def write(format: CSVFormat, output: WritableOutput, ps: Seq[Val[_]]): FromContext[Unit] = FromContext { p
implicit def format: OutputFormat[CSVOutputFormat] = new OutputFormat[CSVOutputFormat] {
override def write(format: CSVOutputFormat, output: WritableOutput, ps: Seq[Val[_]]): FromContext[Unit] = FromContext { p
import p._
val vs = ps.map(context(_))
......@@ -49,14 +49,14 @@ object CSVHook {
}
}
override def validate(format: CSVFormat) = { p
override def validate(format: CSVOutputFormat) = { p
import p._
format.header.option.toSeq.flatMap(_.validate(inputs))
}
}
}
case class CSVFormat(
case class CSVOutputFormat(
header: OptionalArgument[FromContext[String]] = None,
arrayOnRow: Boolean = false,
overwrite: Boolean = false)
......
......@@ -7,19 +7,19 @@ import org.openmole.core.context._
import org.openmole.core.expansion.FromContext
import org.openmole.core.workflow.builder._
trait FileFormat[T] {
trait OutputFormat[T] {
def write(format: T, output: WritableOutput, ps: Seq[Val[_]]): FromContext[Unit]
def validate(format: T): FromContextHook.ValidateParameters Seq[Throwable]
}
object FormattedFileHook {
def apply[T: FileFormat](
def apply[T: OutputFormat](
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: FileFormat[T]): mole.FromContextHook =
name: Option[String] = None)(implicit valName: sourcecode.Name, definitionScope: DefinitionScope, fileFormat: OutputFormat[T]): mole.FromContextHook =
Hook(name getOrElse "FileFormatHook") { parameters
import parameters._
......
......@@ -22,9 +22,9 @@ import scala.language.implicitConversions
package mole {
trait MolePackage {
def CSVHook = mole.CSVHook
def CSVFormat = mole.CSVHook.CSVFormat
def CSVOutputFormat = mole.CSVHook.CSVOutputFormat
type FileFormat[T] = mole.FileFormat[T]
type OutputFormat[T] = mole.OutputFormat[T]
type FromContextHook = mole.FromContextHook
type FromContextSource = mole.FromContextSource
}
......
......@@ -46,7 +46,7 @@ package tools {
def apply[S](s: S)(implicit expandable: Expandable[S, T]) = expandable.expand(s)
}
type Display = WritableOutput.Display
type Display = tools.WritableOutput.Display
def display(implicit outputRedirection: OutputRedirection): Display = outputRedirection.output
}
......
/*
* 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.json
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()
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.json
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 JSONOutputFormat {
implicit def outputFormat = new OutputFormat[JSONOutputFormat] {
override def write(format: JSONOutputFormat, output: WritableOutput, ps: Seq[Val[_]]): 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))))
}
case WritableOutput.PrintStreamValue(ps) ps.println(pretty(render(variablesToJValue(vs))))
}
}
override def validate(format: JSONOutputFormat): FromContextHook.ValidateParameters Seq[Throwable] = { p Seq() }
}
}
case class JSONOutputFormat()
......@@ -35,21 +35,21 @@ package object directsampling {
type Aggregation = AggregateTask.AggregateVal[_, _]
implicit class DirectSamplingDSL(dsl: DSLContainer[DirectSampling]) extends DSLContainerHook(dsl) {
def hook[T: FileFormat](
def hook[T: OutputFormat](
output: WritableOutput,
values: Seq[Val[_]] = Vector.empty,
format: T = CSVFormat()): DSLContainer[DirectSampling] = {
format: T = CSVOutputFormat()): DSLContainer[DirectSampling] = {
implicit val defScope = dsl.scope
dsl hook FormattedFileHook(output = output, values = values, format = format)
}
}
implicit class ReplicationDSL(dsl: DSLContainer[Replication]) extends DSLContainerHook(dsl) {
def hook[T: FileFormat](
def hook[T: OutputFormat](
output: WritableOutput,
values: Seq[Val[_]] = Vector.empty,
includeSeed: Boolean = false,
format: T = CSVFormat()): DSLContainer[Replication] = {
format: T = CSVOutputFormat()): 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)
......
package org.openmole.plugin.tool
import org.json4s.JsonAST.JValue
import org.json4s.JsonAST.{ JObject, JValue }
import org.openmole.core.context._
import org.openmole.core.exception.UserBadDataError
import shapeless._
package object json {
def variablesToJValue(variables: Seq[Variable[_]]) =
JObject(variables.toList.map { v v.name -> toJSONValue(v.value) })
def toJSONValue(v: Any): org.json4s.JValue = {
import org.json4s._
......@@ -22,12 +24,6 @@ package object json {
}
}
/*
def toJSONdictionary(v: Any): String = {
}
*/
def jValueToVariable(jValue: JValue, v: Val[_]): Variable[_] = {
import org.json4s._
import shapeless._
......
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