Commit 55b23c98 authored by Romain Reuillon's avatar Romain Reuillon

[Tool] enh: simplify method signature

parent d3f66d76
......@@ -40,9 +40,13 @@ object CSVHook {
output match {
case WritableOutput.FileValue(file)
val f = file.from(context)
if (format.overwrite && !f.isEmpty) f.delete()
val create = format.overwrite || f.isEmpty
val h = if (f.isEmpty) Some(headerLine) else None
f.withPrintStream(append = true, create = true) { ps csv.writeVariablesToCSV(ps, h, vs, format.arrayOnRow) }
if (create) f.withPrintStream(create = true) { ps csv.writeVariablesToCSV(ps, h, vs, format.arrayOnRow) }
else f.withPrintStream(append = true, create = true) { ps csv.writeVariablesToCSV(ps, h, vs, format.arrayOnRow) }
case WritableOutput.PrintStreamValue(ps)
val header = Some(headerLine)
csv.writeVariablesToCSV(ps, header, vs, format.arrayOnRow)
......
......@@ -47,7 +47,7 @@ object SavePopulationHook {
output match {
case WritableOutput.FileValue(dir)
(dir / ExpandedString("population${" + t.generationPrototype.name + "}.csv")).from(context).withPrintStream(overwrite = false, create = true) { ps
(dir / ExpandedString("population${" + t.generationPrototype.name + "}.csv")).from(context).withPrintStream(create = true) { ps
csv.writeVariablesToCSV(
ps,
Some(headerLine),
......@@ -88,7 +88,7 @@ object SaveLastPopulationHook {
val values = SavePopulationHook.resultVariables(t).from(context).map(_.value)
def headerLine = csv.header(SavePopulationHook.resultVariables(t).from(context).map(_.prototype.array), values)
file.from(context).withPrintStream(overwrite = true, create = true) { ps
file.from(context).withPrintStream(create = true) { ps
csv.writeVariablesToCSV(
ps,
Some(headerLine),
......
......@@ -16,15 +16,15 @@ object MorrisHook {
val dir = dirFC.from(context)
dir.mkdirs()
(dir / "mu.csv").withPrintStream(overwrite = true) { ps
(dir / "mu.csv").withPrintStream() { ps
Sensitivity.writeResults(ps, inputs, dsl.data.outputs, Morris.mu(_, _)).from(context)
}
(dir / "muStar.csv").withPrintStream(overwrite = true) { ps
(dir / "muStar.csv").withPrintStream() { ps
Sensitivity.writeResults(ps, inputs, dsl.data.outputs, Morris.muStar(_, _)).from(context)
}
(dir / "sigma.csv").withPrintStream(overwrite = true) { ps
(dir / "sigma.csv").withPrintStream() { ps
Sensitivity.writeResults(ps, inputs, dsl.data.outputs, Morris.sigma(_, _)).from(context)
}
case WritableOutput.PrintStreamValue(ps)
......
......@@ -15,11 +15,11 @@ object SaltelliHook {
case WritableOutput.FileValue(dirFC)
val dir = dirFC.from(context)
(dir / "firstOrderIndices.csv").withPrintStream(overwrite = true, create = true) { ps
(dir / "firstOrderIndices.csv").withPrintStream(create = true) { ps
Sensitivity.writeResults(ps, inputs, dsl.data.outputs, Saltelli.firstOrder(_, _)).from(context)
}
(dir / "totalOrderIndices.csv").withPrintStream(overwrite = true) { ps
(dir / "totalOrderIndices.csv").withPrintStream() { ps
Sensitivity.writeResults(ps, inputs, dsl.data.outputs, Saltelli.totalOrder(_, _)).from(context)
}
case WritableOutput.PrintStreamValue(ps)
......
......@@ -419,10 +419,9 @@ package file {
def withOutputStream[T] = withClosable[OutputStream, T](bufferedOutputStream())(_)
def withPrintStream[T](append: Boolean = false, overwrite: Boolean = true, create: Boolean = false) = {
def arg = append || !overwrite
def withPrintStream[T](append: Boolean = false, create: Boolean = false) = {
if (create) file.createParentDir
withClosable[PrintStream, T](new PrintStream(file.bufferedOutputStream(arg))) _
withClosable[PrintStream, T](new PrintStream(file.bufferedOutputStream(append = append))) _
}
def withFileOutputStream[T] = withClosable[FileOutputStream, T](new FileOutputStream(file))(_)
......
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