Utils.scala 4.72 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

Christian Müller's avatar
Christian Müller committed
8
import de.tum.niwo.toz3.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
import de.tum.niwo.invariants.{InvProperties, InvariantChecker}
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
17
  val Z3CONTEXTELIM = false
Christian Müller's avatar
Christian Müller committed
18

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

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

32
  def allchoices(w: Workflow): Set[Fun] = {
33
    w.steps.flatMap(collectChoices).toSet
Christian Müller's avatar
Christian Müller committed
34
  }
35

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

40
  def time[R](block: => R): (Long, R) = {
Christian Müller's avatar
Christian Müller committed
41
    val t0 = System.nanoTime()
Christian Müller's avatar
Christian Müller committed
42
    val result = block // call-by-name
Christian Müller's avatar
Christian Müller committed
43
    val t1 = System.nanoTime()
Christian Müller's avatar
Christian Müller committed
44 45 46
    ((t1 - t0) / 1000000, result)
  }

Christian Müller's avatar
Christian Müller committed
47 48 49
  private def recclear(folder: File) {
    for (fol <- folder.listFiles() if fol.isDirectory()) {
      recclear(fol)
Christian Müller's avatar
Christian Müller committed
50
    }
Christian Müller's avatar
Christian Müller committed
51 52 53 54
    folder.listFiles().foreach(_.delete())
  }

  def clear() {
Christian Müller's avatar
Christian Müller committed
55 56 57 58
    val fol = new File(RESULTSFOLDER)
    fol.mkdirs()
    recclear(fol)
  }
Christian Müller's avatar
Christian Müller committed
59 60 61 62 63
  def clear(name:String) {
    val fol = new File(RESULTSFOLDER, name)
    fol.mkdirs()
    recclear(fol)
  }
Christian Müller's avatar
Christian Müller committed
64
  
65 66
  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
67 68 69
    if (file.exists()) {
      file.delete()
    }
Christian Müller's avatar
Christian Müller committed
70 71 72 73
    file.getParentFile.mkdirs()
    val writer = new PrintWriter(file)
    writer.print(prop)
    writer.close()
74
    logger.info(s"Written $file")
Christian Müller's avatar
Christian Müller committed
75
  }
Christian Müller's avatar
Christian Müller committed
76

Christian Müller's avatar
Christian Müller committed
77 78 79 80 81 82 83 84 85 86 87

  def check(name:String, desc:String, spec:InvariantSpec, properties:InvProperties):Boolean = {
    clear(name)
    infer(name:String, desc:String, spec:InvariantSpec, properties:InvProperties)
  }

  def check(name:String, desc:String, inv:NISpec => Formula, properties:InvProperties):Boolean = {
    val spec = Examples.parseExampleWF(name).get
    check(name, desc, inv, spec, properties)
  }

Christian Müller's avatar
Christian Müller committed
88
  def check(name: String, desc:String, inv:NISpec => Formula, spec:NISpec, properties: InvProperties):Boolean = {
89

Christian Müller's avatar
Christian Müller committed
90 91
    clear(name)

92 93 94 95 96 97 98 99
    val basename = name.split("/").last
    val filenames = s"$basename${if (desc.isEmpty) "" else s"_$desc"}"

    // initial wf graph
    val graph = WFGraphEncoding.toGraph(spec.w)
    val elabdot = WFGraphEncoding.toDot(graph)(Map(), Set())
    Utils.write(name, s"${filenames}_workflow.dot", elabdot)

Christian Müller's avatar
Christian Müller committed
100
    val invspec = TSConverter.toInvariantSpec(spec, properties, inv)
101
    infer(name, desc, invspec, properties)
Christian Müller's avatar
Christian Müller committed
102
  }
Christian Müller's avatar
Christian Müller committed
103
  
Christian Müller's avatar
Christian Müller committed
104
  private def infer(name:String, desc:String, spec:InvariantSpec, properties:InvProperties):Boolean = {
Christian Müller's avatar
Christian Müller committed
105
    val model = if (properties.stubborn) "stubborn" else "causal"
106 107

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

Christian Müller's avatar
Christian Müller committed
110
    val (res, graph, labelling, provens, dot, time) =
Christian Müller's avatar
Christian Müller committed
111
      InvariantChecker.checkInvariantFPLabelling(spec, properties)
Christian Müller's avatar
Christian Müller committed
112

Christian Müller's avatar
Christian Müller committed
113
    logger.info(s"Invariant was ${spec.inv}")
Christian Müller's avatar
Christian Müller committed
114 115
    logger.info(s"Invariant was ${if (res) "" else "not "}proven (took $time ms)\n")

Christian Müller's avatar
Christian Müller committed
116 117
    if (!res) {
      val headinv = labelling.last(graph.nodes.head)
118
      val satq = And(spec.axioms, Neg(headinv))
Christian Müller's avatar
Christian Müller committed
119 120 121
      logger.info(s"Initial state violating the invariant: ${Z3BSFO.debugmodelBS(satq)}")
    }

Christian Müller's avatar
Christian Müller committed
122
    for ((s, i) <- dot.zipWithIndex) {
123
      Utils.write(name,s"${filenames}_$i.dot", s)
Christian Müller's avatar
Christian Müller committed
124
    }
125
    val strengthenings = for (List(a, b) <- labelling.sliding(2) if a != b) yield {
Christian Müller's avatar
Christian Müller committed
126 127
      true
    }
128 129


130
    val labels = (for ((node, inv) <- labelling.last) yield {
Christian Müller's avatar
Christian Müller committed
131
      s"Node ${node}:\n${inv.pretty}\n"
132
    }).mkString("\n")
Christian Müller's avatar
Christian Müller committed
133

134
    Utils.write(name, s"$filenames.invariants", labels)
Christian Müller's avatar
Christian Müller committed
135

Christian Müller's avatar
Christian Müller committed
136
    val wfsize = graph.edges.size - 1
Christian Müller's avatar
Christian Müller committed
137
    val invsizes = labelling.last.map(_._2.opsize)
Christian Müller's avatar
Christian Müller committed
138 139
    val maxsize = invsizes.max
    val avgsize = invsizes.sum / invsizes.size
140
    Utils.write(name, s"$filenames.metrics",
Christian Müller's avatar
Christian Müller committed
141
        s"""Name: $name
Christian Müller's avatar
Christian Müller committed
142 143 144 145 146 147 148 149 150 151 152 153
        |Description: $desc
        |  Invariant:
        |${spec.inv.pretty.lines.map(s => "            " + s).mkString("\n")}
        |  Model: $model
        |  Result: ${if (!res) "not " else ""}inductive
        |  WF size: $wfsize
        |  Time: $time ms
        |  Proof steps: ${dot.size}
        |  Strengthenings: ${strengthenings.size}
        |  Largest Inv: $maxsize
        |  Average Inv: $avgsize""".stripMargin
    )
Christian Müller's avatar
Christian Müller committed
154 155
    res
  }
Christian Müller's avatar
Christian Müller committed
156
}