Utils.scala 3.75 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 9
import de.tum.workflows.toz3.InvariantChecker
import de.tum.workflows.toz3.InvProperties
10
import com.typesafe.scalalogging.LazyLogging
Christian Müller's avatar
Christian Müller committed
11
import de.tum.workflows.MainInvariantsInference.logger
Christian Müller's avatar
Christian Müller committed
12

13
object Utils extends LazyLogging {
14

Christian Müller's avatar
Christian Müller committed
15
  val RESULTSFOLDER = "results"
Christian Müller's avatar
src  
Christian Müller committed
16
  val DEBUG_MODE = true
Christian Müller's avatar
Christian Müller committed
17

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

  def collectChoices(w: Block): List[Fun] = {
    w match {
Christian Müller's avatar
Christian Müller committed
24
      case Loop(steps)                     => steps.flatMap(collectChoices)
25 26 27 28 29 30
      case ForallMayBlock(agents, pred, _) => List(Fun(pred, agents))
      case b: ForallBlock                  => List()
      case NondetChoice(l, r)              => l.flatMap(collectChoices) ++ r.flatMap(collectChoices)
    }
  }

31
  def allchoices(w: Workflow) = {
Christian Müller's avatar
Christian Müller committed
32
    w.steps flatMap collectChoices toSet
Christian Müller's avatar
Christian Müller committed
33
  }
34 35 36 37

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

  def time[R](block: => R) = {
Christian Müller's avatar
Christian Müller committed
40
    val t0 = System.nanoTime()
Christian Müller's avatar
Christian Müller committed
41
    val result = block // call-by-name
Christian Müller's avatar
Christian Müller committed
42
    val t1 = System.nanoTime()
Christian Müller's avatar
Christian Müller committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
    ((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)
  }
Christian Müller's avatar
Christian Müller committed
58
  
59 60
  def write(dir: String, filename:String, prop: String) {
    val file = new File(s"$RESULTSFOLDER/$dir/$filename")
Christian Müller's avatar
Christian Müller committed
61 62 63
    if (file.exists()) {
      file.delete()
    }
Christian Müller's avatar
Christian Müller committed
64 65 66 67
    file.getParentFile.mkdirs()
    val writer = new PrintWriter(file)
    writer.print(prop)
    writer.close()
68
    logger.info(s"Written $file")
Christian Müller's avatar
Christian Müller committed
69
  }
Christian Müller's avatar
Christian Müller committed
70 71 72
  
  def check(name:String, desc:String, inv:Formula, spec:Spec, properties:InvProperties) = {
    val model = if (properties.stubborn) "stubborn" else "causal"
73 74 75 76

    val basename = name.split("/").last
		val filenames = s"${basename}_$model${if (desc.isEmpty()) "" else s"_$desc"}"

Christian Müller's avatar
Christian Müller committed
77
    // do not blow up the formula with auxilliary elimination
Christian Müller's avatar
Christian Müller committed
78
    val (res, graph, labelling, provens, dot, time) =
Christian Müller's avatar
Christian Müller committed
79
      InvariantChecker.checkInvariantFPLabelling(spec, inv, properties)
Christian Müller's avatar
Christian Müller committed
80 81 82 83

    logger.info(s"Invariant was ${inv}")
    logger.info(s"Invariant was ${if (res) "" else "not "}proven (took $time ms)\n")

Christian Müller's avatar
Christian Müller committed
84
    for ((s, i) <- dot.zipWithIndex) {
85
      Utils.write(name,s"${filenames}_$i.dot", s)
Christian Müller's avatar
Christian Müller committed
86 87 88 89
    }
    val strengthenings = for (List(a, b) <- labelling.sliding(2) if (a != b)) yield {
      true
    }
90 91 92 93

    val elabdot = Encoding.toDot(graph, Some(properties, spec))(Map(), Set())
    Utils.write(name, s"${filenames}_elaborated.dot", elabdot)

94
    val labels = (for ((node, inv) <- labelling.last) yield {
Christian Müller's avatar
Christian Müller committed
95
      s"Node ${node}:\n${inv.pretty}\n"
96 97
    }).mkString("\n")
    
98
    Utils.write(name, s"$filenames.invariants", labels)
99
    
Christian Müller's avatar
Christian Müller committed
100
    val wfsize = graph.edges.size - 1
Christian Müller's avatar
Christian Müller committed
101
    val invsizes = labelling.last.map(_._2.opsize)
Christian Müller's avatar
Christian Müller committed
102 103
    val maxsize = invsizes.max
    val avgsize = invsizes.sum / invsizes.size
104
    Utils.write(name, s"$filenames.metrics",
Christian Müller's avatar
Christian Müller committed
105 106
        s"""Name: $name
         Description: $desc
Christian Müller's avatar
Christian Müller committed
107
         Invariant:
Christian Müller's avatar
Christian Müller committed
108
${inv.pretty.lines.map(s => "            " + s).mkString("\n")}
Christian Müller's avatar
Christian Müller committed
109 110 111 112 113 114
         Model: $model
         Result: ${if (!res) "not " else ""}inductive
         WF size: $wfsize
         Time: $time ms
         Proof steps: ${dot.size}
         Strengthenings: ${strengthenings.size}
Christian Müller's avatar
Christian Müller committed
115 116
         Largest Inv: $maxsize
         Average Inv: $avgsize""")
Christian Müller's avatar
Christian Müller committed
117 118 119 120 121 122 123
    res
  }
  
  def check(name:String, desc:String, inv:Formula, properties:InvProperties):Boolean = {
    val spec = ExampleWorkflows.parseExample(name).get
    check(name, desc, inv, spec, properties)
  }
Christian Müller's avatar
Christian Müller committed
124
}