author | wenzelm |
Thu, 20 Aug 2020 14:22:49 +0200 | |
changeset 72179 | 0841895ca438 |
parent 72178 | 2481cdb84832 |
child 72181 | 6241cbbf5a58 |
permissions | -rw-r--r-- |
72176 | 1 |
/* Title: HOL/Tools/Nitpick/kodkod.scala |
2 |
Author: Makarius |
|
3 |
||
4 |
Scala interface for Kodkod. |
|
5 |
*/ |
|
6 |
||
7 |
package isabelle.nitpick |
|
8 |
||
9 |
import isabelle._ |
|
10 |
||
72177 | 11 |
import java.util.concurrent.Executors |
12 |
||
72178
2481cdb84832
more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents:
72177
diff
changeset
|
13 |
import org.antlr.runtime.{ANTLRInputStream, RecognitionException} |
72177 | 14 |
import de.tum.in.isabelle.Kodkodi.{Context, KodkodiLexer, KodkodiParser} |
72176 | 15 |
|
16 |
||
17 |
object Kodkod |
|
18 |
{ |
|
72178
2481cdb84832
more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents:
72177
diff
changeset
|
19 |
sealed case class Result(rc: Int, out: String, err: String) |
72179 | 20 |
{ |
21 |
def ok: Boolean = rc == 0 |
|
22 |
def check: String = |
|
23 |
if (ok) out else error(if (err.isEmpty) "Error" else err) |
|
24 |
} |
|
72178
2481cdb84832
more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents:
72177
diff
changeset
|
25 |
|
72179 | 26 |
def execute(source: String, |
72176 | 27 |
solve_all: Boolean = false, |
28 |
prove: Boolean = false, |
|
29 |
max_solutions: Int = Integer.MAX_VALUE, |
|
30 |
cleanup_inst: Boolean = false, |
|
72178
2481cdb84832
more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents:
72177
diff
changeset
|
31 |
timeout: Time = Time.zero, |
2481cdb84832
more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents:
72177
diff
changeset
|
32 |
max_threads: Int = 1): Result = |
72176 | 33 |
{ |
72178
2481cdb84832
more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents:
72177
diff
changeset
|
34 |
val executor = Executors.newFixedThreadPool(max_threads) |
2481cdb84832
more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents:
72177
diff
changeset
|
35 |
|
2481cdb84832
more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents:
72177
diff
changeset
|
36 |
def executor_kill(): Unit = |
2481cdb84832
more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents:
72177
diff
changeset
|
37 |
if (!executor.isShutdown) Isabelle_Thread.fork() { executor.shutdownNow() } |
2481cdb84832
more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents:
72177
diff
changeset
|
38 |
|
2481cdb84832
more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents:
72177
diff
changeset
|
39 |
class Exit extends Exception |
2481cdb84832
more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents:
72177
diff
changeset
|
40 |
|
72177 | 41 |
val context = |
42 |
new Context { |
|
72178
2481cdb84832
more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents:
72177
diff
changeset
|
43 |
private var rc = 0 |
72177 | 44 |
private val out = new StringBuilder |
45 |
private val err = new StringBuilder |
|
72178
2481cdb84832
more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents:
72177
diff
changeset
|
46 |
|
2481cdb84832
more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents:
72177
diff
changeset
|
47 |
def return_code(i: Int): Unit = synchronized { rc = rc max i} |
72177 | 48 |
override def output(s: String): Unit = synchronized { out ++= s; out += '\n' } |
49 |
override def error(s: String): Unit = synchronized { err ++= s; err += '\n' } |
|
72178
2481cdb84832
more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents:
72177
diff
changeset
|
50 |
override def exit(i: Int): Unit = |
2481cdb84832
more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents:
72177
diff
changeset
|
51 |
synchronized { |
2481cdb84832
more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents:
72177
diff
changeset
|
52 |
return_code(i) |
2481cdb84832
more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents:
72177
diff
changeset
|
53 |
executor_kill() |
2481cdb84832
more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents:
72177
diff
changeset
|
54 |
throw new Exit |
2481cdb84832
more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents:
72177
diff
changeset
|
55 |
} |
2481cdb84832
more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents:
72177
diff
changeset
|
56 |
|
2481cdb84832
more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents:
72177
diff
changeset
|
57 |
def result(): Result = synchronized { Result(rc, out.toString, err.toString) } |
72177 | 58 |
} |
72178
2481cdb84832
more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents:
72177
diff
changeset
|
59 |
|
2481cdb84832
more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents:
72177
diff
changeset
|
60 |
try { |
2481cdb84832
more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents:
72177
diff
changeset
|
61 |
val lexer = new KodkodiLexer(new ANTLRInputStream(Bytes(source).stream)) |
2481cdb84832
more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents:
72177
diff
changeset
|
62 |
val parser = |
2481cdb84832
more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents:
72177
diff
changeset
|
63 |
KodkodiParser.create(context, executor, |
2481cdb84832
more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents:
72177
diff
changeset
|
64 |
false, solve_all, prove, max_solutions, cleanup_inst, lexer) |
72176 | 65 |
|
72178
2481cdb84832
more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents:
72177
diff
changeset
|
66 |
val timeout_request = |
2481cdb84832
more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents:
72177
diff
changeset
|
67 |
if (timeout.is_zero) None |
2481cdb84832
more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents:
72177
diff
changeset
|
68 |
else { |
2481cdb84832
more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents:
72177
diff
changeset
|
69 |
Some(Event_Timer.request(Time.now() + timeout) { |
2481cdb84832
more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents:
72177
diff
changeset
|
70 |
context.error("Ran out of time") |
2481cdb84832
more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents:
72177
diff
changeset
|
71 |
context.return_code(2) |
2481cdb84832
more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents:
72177
diff
changeset
|
72 |
executor_kill() |
2481cdb84832
more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents:
72177
diff
changeset
|
73 |
}) |
72177 | 74 |
} |
72178
2481cdb84832
more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents:
72177
diff
changeset
|
75 |
|
2481cdb84832
more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents:
72177
diff
changeset
|
76 |
try { parser.problems() } |
2481cdb84832
more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents:
72177
diff
changeset
|
77 |
catch { case exn: RecognitionException => parser.reportError(exn) } |
2481cdb84832
more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents:
72177
diff
changeset
|
78 |
|
2481cdb84832
more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents:
72177
diff
changeset
|
79 |
timeout_request.foreach(_.cancel) |
72177 | 80 |
|
72178
2481cdb84832
more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents:
72177
diff
changeset
|
81 |
if (parser.getTokenStream.LA(1) != KodkodiParser.EOF) { |
2481cdb84832
more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents:
72177
diff
changeset
|
82 |
context.error("Error: trailing tokens") |
2481cdb84832
more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents:
72177
diff
changeset
|
83 |
context.exit(1) |
2481cdb84832
more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents:
72177
diff
changeset
|
84 |
} |
2481cdb84832
more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents:
72177
diff
changeset
|
85 |
if (lexer.getNumberOfSyntaxErrors + parser.getNumberOfSyntaxErrors > 0) { |
2481cdb84832
more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents:
72177
diff
changeset
|
86 |
context.exit(1) |
2481cdb84832
more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents:
72177
diff
changeset
|
87 |
} |
2481cdb84832
more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents:
72177
diff
changeset
|
88 |
} |
2481cdb84832
more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents:
72177
diff
changeset
|
89 |
catch { |
2481cdb84832
more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents:
72177
diff
changeset
|
90 |
case _: Exit => |
2481cdb84832
more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents:
72177
diff
changeset
|
91 |
case exn: Throwable => |
2481cdb84832
more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents:
72177
diff
changeset
|
92 |
val message = exn.getMessage |
2481cdb84832
more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents:
72177
diff
changeset
|
93 |
context.error(if (message.isEmpty) exn.toString else "Error: " + message) |
2481cdb84832
more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents:
72177
diff
changeset
|
94 |
context.return_code(1) |
2481cdb84832
more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents:
72177
diff
changeset
|
95 |
} |
72177 | 96 |
|
72178
2481cdb84832
more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents:
72177
diff
changeset
|
97 |
context.result() |
72176 | 98 |
} |
72179 | 99 |
|
100 |
def warmup(): String = |
|
101 |
execute(File.read(Path.explode("$KODKODI/examples/weber3.kki"))).check |
|
72176 | 102 |
} |