Skip to content
Snippets Groups Projects
Commit e1b0a2e2 authored by Etienne Kneuss's avatar Etienne Kneuss
Browse files

Merge branch 'master' of laragit.epfl.ch:projects/leon-2.0

parents ff457ffc c903fa2a
No related branches found
No related tags found
No related merge requests found
...@@ -18,6 +18,7 @@ trait IncrementalSolver { ...@@ -18,6 +18,7 @@ trait IncrementalSolver {
def pop(lvl: Int = 1): Unit def pop(lvl: Int = 1): Unit
def assertCnstr(expression: Expr): Unit def assertCnstr(expression: Expr): Unit
def halt(): Unit
def check: Option[Boolean] def check: Option[Boolean]
def checkAssumptions(assumptions: Set[Expr]): Option[Boolean] def checkAssumptions(assumptions: Set[Expr]): Option[Boolean]
def getModel: Map[Identifier, Expr] def getModel: Map[Identifier, Expr]
...@@ -25,6 +26,7 @@ trait IncrementalSolver { ...@@ -25,6 +26,7 @@ trait IncrementalSolver {
} }
trait NaiveIncrementalSolver extends IncrementalSolverBuilder { trait NaiveIncrementalSolver extends IncrementalSolverBuilder {
def halt(): Unit
def solveSAT(e: Expr): (Option[Boolean], Map[Identifier, Expr]) def solveSAT(e: Expr): (Option[Boolean], Map[Identifier, Expr])
def getNewSolver = new IncrementalSolver { def getNewSolver = new IncrementalSolver {
...@@ -38,6 +40,10 @@ trait NaiveIncrementalSolver extends IncrementalSolverBuilder { ...@@ -38,6 +40,10 @@ trait NaiveIncrementalSolver extends IncrementalSolverBuilder {
stack = stack.drop(lvl) stack = stack.drop(lvl)
} }
def halt() {
NaiveIncrementalSolver.this.halt()
}
def assertCnstr(expression: Expr) { def assertCnstr(expression: Expr) {
stack = (expression :: stack.head) :: stack.tail stack = (expression :: stack.head) :: stack.tail
} }
......
...@@ -8,17 +8,17 @@ import purescala.TypeTrees._ ...@@ -8,17 +8,17 @@ import purescala.TypeTrees._
import scala.sys.error import scala.sys.error
class TimeoutSolver(solver : Solver, timeout : Int) extends Solver(solver.context) with NaiveIncrementalSolver { class TimeoutSolver(solver : Solver with IncrementalSolverBuilder, timeout : Int) extends Solver(solver.context) with IncrementalSolverBuilder {
// I'm making this an inner class to fight the temptation of using it for anything meaningful. // I'm making this an inner class to fight the temptation of using it for anything meaningful.
// We have Akka, these days, which whould be better in any respect for non-trivial things. // We have Akka, these days, which whould be better in any respect for non-trivial things.
private class Timer(callback : () => Unit, maxSecs : Int) extends Thread { private class Timer(onTimeout: => Unit) extends Thread {
private var keepRunning = true private var keepRunning = true
private val asMillis : Long = 1000L * maxSecs private val asMillis : Long = 1000L * timeout
override def run : Unit = { override def run : Unit = {
val startTime : Long = System.currentTimeMillis val startTime : Long = System.currentTimeMillis
var exceeded : Boolean = false var exceeded : Boolean = false
while(!exceeded && keepRunning) { while(!exceeded && keepRunning) {
if(asMillis < (System.currentTimeMillis - startTime)) { if(asMillis < (System.currentTimeMillis - startTime)) {
exceeded = true exceeded = true
...@@ -26,15 +26,23 @@ class TimeoutSolver(solver : Solver, timeout : Int) extends Solver(solver.contex ...@@ -26,15 +26,23 @@ class TimeoutSolver(solver : Solver, timeout : Int) extends Solver(solver.contex
Thread.sleep(10) Thread.sleep(10)
} }
if(exceeded && keepRunning) { if(exceeded && keepRunning) {
callback() onTimeout
} }
} }
def halt : Unit = { def halt : Unit = {
keepRunning = false keepRunning = false
} }
} }
def withTimeout[T](onTimeout: => Unit)(body: => T): T = {
val timer = new Timer(onTimeout)
timer.start
val res = body
timer.halt
res
}
val description = solver.description + ", with timeout" val description = solver.description + ", with timeout"
val name = solver.name + "+to" val name = solver.name + "+to"
...@@ -43,16 +51,67 @@ class TimeoutSolver(solver : Solver, timeout : Int) extends Solver(solver.contex ...@@ -43,16 +51,67 @@ class TimeoutSolver(solver : Solver, timeout : Int) extends Solver(solver.contex
} }
def solve(expression: Expr) : Option[Boolean] = { def solve(expression: Expr) : Option[Boolean] = {
val timer = new Timer(() => solver.halt, timeout) withTimeout(solver.halt) {
timer.start solver.solve(expression)
val res = solver.solve(expression) }
timer.halt }
res
override def solveSAT(expression: Expr): (Option[Boolean], Map[Identifier, Expr]) = {
withTimeout(solver.halt) {
solver.solveSAT(expression)
}
}
override def solveSATWithCores(expression: Expr, assumptions: Set[Expr]): (Option[Boolean], Map[Identifier, Expr], Set[Expr]) = {
withTimeout(solver.halt) {
solver.solveSATWithCores(expression, assumptions)
}
}
def getNewSolver = new IncrementalSolver {
val solver = TimeoutSolver.this.solver.getNewSolver
def push(): Unit = {
solver.push()
}
def pop(lvl: Int = 1): Unit = {
solver.pop(lvl)
}
def assertCnstr(expression: Expr): Unit = {
solver.assertCnstr(expression)
}
def halt(): Unit = {
solver.halt()
}
def check: Option[Boolean] = {
withTimeout(solver.halt){
solver.check
}
}
def checkAssumptions(assumptions: Set[Expr]): Option[Boolean] = {
withTimeout(solver.halt){
solver.checkAssumptions(assumptions)
}
}
def getModel: Map[Identifier, Expr] = {
solver.getModel
}
def getUnsatCore: Set[Expr] = {
solver.getUnsatCore
}
} }
override def init() { override def init() {
solver.init solver.init
} }
override def halt() { override def halt() {
solver.halt solver.halt
} }
......
...@@ -95,7 +95,7 @@ class FairZ3Solver(context : LeonContext) ...@@ -95,7 +95,7 @@ class FairZ3Solver(context : LeonContext)
override def halt() { override def halt() {
super.halt super.halt
if(z3 ne null) { if(z3 ne null) {
z3.softCheckCancel z3.interrupt
} }
} }
...@@ -257,6 +257,10 @@ class FairZ3Solver(context : LeonContext) ...@@ -257,6 +257,10 @@ class FairZ3Solver(context : LeonContext)
frameExpressions = Nil :: frameExpressions frameExpressions = Nil :: frameExpressions
} }
def halt() {
z3.interrupt
}
def pop(lvl: Int = 1) { def pop(lvl: Int = 1) {
// We make sure we discard the expressions guarded by this frame // We make sure we discard the expressions guarded by this frame
solver.assertCnstr(z3.mkNot(frameGuards.head)) solver.assertCnstr(z3.mkNot(frameGuards.head))
......
...@@ -86,6 +86,10 @@ class UninterpretedZ3Solver(context : LeonContext) extends Solver(context) with ...@@ -86,6 +86,10 @@ class UninterpretedZ3Solver(context : LeonContext) extends Solver(context) with
solver.push solver.push
} }
def halt() {
z3.interrupt
}
def pop(lvl: Int = 1) { def pop(lvl: Int = 1) {
solver.pop(lvl) solver.pop(lvl)
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment