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

3 4
import de.tum.niwo.blocks._
import de.tum.niwo.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

8
import de.tum.niwo.toz3.{InvProperties, InvariantChecker, Z3BSFO}
9
import com.typesafe.scalalogging.LazyLogging
Christian Müller's avatar
Christian Müller committed
10
import de.tum.niwo.graphs.WFGraphEncoding
Christian Müller's avatar
Christian Müller committed
11

12
object Utils extends LazyLogging {
13

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

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

21
  def collectChoices(w: WFBlock): List[Fun] = {
22
    w match {
23 24
      case l:WFLoop                           => l.steps.flatMap(collectChoices)
      case nd:WFNondetChoice                   => nd.left.flatMap(collectChoices) ++ nd.right.flatMap(collectChoices)
25
      case ForallMayWFBlock(agents, pred, _) => List(Fun(pred, agents))
26
      case _: ForallWFBlock                  => List()
27 28 29
    }
  }

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

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

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

    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
76
    val (res, graph, labelling, provens, dot, time) =
Christian Müller's avatar
Christian Müller committed
77
      InvariantChecker.checkInvariantFPLabelling(spec, inv, properties)
Christian Müller's avatar
Christian Müller committed
78 79 80 81

    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
82 83
    if (!res) {
      val headinv = labelling.last(graph.nodes.head)
84
      val satq = And(spec.axioms, Neg(headinv))
Christian Müller's avatar
Christian Müller committed
85 86 87
      logger.info(s"Initial state violating the invariant: ${Z3BSFO.debugmodelBS(satq)}")
    }

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

Christian Müller's avatar
Christian Müller committed
95 96 97
    // FIXME move this
//    val elabdot = WFGraphEncoding.toDot(graph)(Map(), Set())
//    Utils.write(name, s"${filenames}_elaborated.dot", elabdot)
98

99
    val labels = (for ((node, inv) <- labelling.last) yield {
Christian Müller's avatar
Christian Müller committed
100
      s"Node ${node}:\n${inv.pretty}\n"
101 102
    }).mkString("\n")
    
103
    Utils.write(name, s"$filenames.invariants", labels)
104
    
Christian Müller's avatar
Christian Müller committed
105
    val wfsize = graph.edges.size - 1
Christian Müller's avatar
Christian Müller committed
106
    val invsizes = labelling.last.map(_._2.opsize)
Christian Müller's avatar
Christian Müller committed
107 108
    val maxsize = invsizes.max
    val avgsize = invsizes.sum / invsizes.size
109
    Utils.write(name, s"$filenames.metrics",
Christian Müller's avatar
Christian Müller committed
110 111
        s"""Name: $name
         Description: $desc
Christian Müller's avatar
Christian Müller committed
112
         Invariant:
Christian Müller's avatar
Christian Müller committed
113
${inv.pretty.lines.map(s => "            " + s).mkString("\n")}
Christian Müller's avatar
Christian Müller committed
114 115 116 117 118 119
         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
120 121
         Largest Inv: $maxsize
         Average Inv: $avgsize""")
Christian Müller's avatar
Christian Müller committed
122 123 124 125
    res
  }
  
  def check(name:String, desc:String, inv:Formula, properties:InvProperties):Boolean = {
126
    val spec = Examples.parseExampleWF(name).get
Christian Müller's avatar
Christian Müller committed
127 128
    check(name, desc, inv, spec, properties)
  }
Christian Müller's avatar
Christian Müller committed
129
}