Utils.scala 1.5 KB
Newer Older
Christian Müller's avatar
Christian Müller committed
1 2
package de.tum.workflows

3
import de.tum.workflows.blocks._
Christian Müller's avatar
Christian Müller committed
4
import de.tum.workflows.foltl.FOLTL._
Christian Müller's avatar
Christian Müller committed
5 6
import java.io.File
import java.io.PrintWriter
Christian Müller's avatar
Christian Müller committed
7

Christian Müller's avatar
Christian Müller committed
8
object Utils {
9

Christian Müller's avatar
Christian Müller committed
10 11
  val RESULTSFOLDER = "results"

12
  def mkString[T](string: Iterable[T], start: String, mid: String, end: String) = {
Christian Müller's avatar
Christian Müller committed
13 14
    if (string.isEmpty) "" else string.mkString(start, mid, end)
  }
15 16 17 18 19 20 21 22 23 24

  def collectChoices(w: Block): List[Fun] = {
    w match {
      case Loop(steps)                     => steps.flatMap(collectChoices _)
      case ForallMayBlock(agents, pred, _) => List(Fun(pred, agents))
      case b: ForallBlock                  => List()
      case NondetChoice(l, r)              => l.flatMap(collectChoices) ++ r.flatMap(collectChoices)
    }
  }

25 26
  def allchoices(w: Workflow) = {
    w.steps flatMap (collectChoices _) toSet
Christian Müller's avatar
Christian Müller committed
27
  }
28 29 30 31

  def indentLines(s: String) = {
    s.lines.map("  " + _).mkString("\n")
  }
Christian Müller's avatar
Christian Müller committed
32 33

  def time[R](block: => R) = {
Christian Müller's avatar
Christian Müller committed
34
    val t0 = System.nanoTime()
Christian Müller's avatar
Christian Müller committed
35
    val result = block // call-by-name
Christian Müller's avatar
Christian Müller committed
36
    val t1 = System.nanoTime()
Christian Müller's avatar
Christian Müller committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
    ((t1 - t0) / 1000000, result)
  }

  def clear() {
    def recclear(folder: File) {
      for (fol <- folder.listFiles() if fol.isDirectory()) {
        recclear(fol)
      }

      folder.listFiles().foreach(_.delete())
    }
    val fol = new File(RESULTSFOLDER)
    fol.mkdirs()
    recclear(fol)
  }

  def write(name: String, prop: String) {
    val file = new File(s"$RESULTSFOLDER/$name")
    file.getParentFile.mkdirs()
    val writer = new PrintWriter(file)
    writer.print(prop)
    writer.close()
Christian Müller's avatar
Christian Müller committed
59
  }
Christian Müller's avatar
Christian Müller committed
60
}