| author | wenzelm | 
| Wed, 10 Jul 2013 16:25:26 +0200 | |
| changeset 52616 | 3ac2878764f9 | 
| parent 52582 | 31467a4b1466 | 
| child 54442 | c39972ddd672 | 
| permissions | -rw-r--r-- | 
| 
43744
 
2c7e1565b4a3
some support to invoke Scala methods under program control;
 
wenzelm 
parents:  
diff
changeset
 | 
1  | 
/* Title: Pure/System/invoke_scala.scala  | 
| 
 
2c7e1565b4a3
some support to invoke Scala methods under program control;
 
wenzelm 
parents:  
diff
changeset
 | 
2  | 
Author: Makarius  | 
| 
 
2c7e1565b4a3
some support to invoke Scala methods under program control;
 
wenzelm 
parents:  
diff
changeset
 | 
3  | 
|
| 
49173
 
fa01a202399c
eliminated potentially confusing terminology of Scala "layer";
 
wenzelm 
parents: 
44158 
diff
changeset
 | 
4  | 
JVM method invocation service via Isabelle/Scala.  | 
| 
43744
 
2c7e1565b4a3
some support to invoke Scala methods under program control;
 
wenzelm 
parents:  
diff
changeset
 | 
5  | 
*/  | 
| 
 
2c7e1565b4a3
some support to invoke Scala methods under program control;
 
wenzelm 
parents:  
diff
changeset
 | 
6  | 
|
| 
 
2c7e1565b4a3
some support to invoke Scala methods under program control;
 
wenzelm 
parents:  
diff
changeset
 | 
7  | 
package isabelle  | 
| 
 
2c7e1565b4a3
some support to invoke Scala methods under program control;
 
wenzelm 
parents:  
diff
changeset
 | 
8  | 
|
| 
 
2c7e1565b4a3
some support to invoke Scala methods under program control;
 
wenzelm 
parents:  
diff
changeset
 | 
9  | 
|
| 
43751
 
8c7f69f1825b
proper InvocationTargetException.getCause for indirect exceptions;
 
wenzelm 
parents: 
43748 
diff
changeset
 | 
10  | 
import java.lang.reflect.{Method, Modifier, InvocationTargetException}
 | 
| 43748 | 11  | 
import scala.util.matching.Regex  | 
| 
43744
 
2c7e1565b4a3
some support to invoke Scala methods under program control;
 
wenzelm 
parents:  
diff
changeset
 | 
12  | 
|
| 
 
2c7e1565b4a3
some support to invoke Scala methods under program control;
 
wenzelm 
parents:  
diff
changeset
 | 
13  | 
|
| 
 
2c7e1565b4a3
some support to invoke Scala methods under program control;
 
wenzelm 
parents:  
diff
changeset
 | 
14  | 
object Invoke_Scala  | 
| 
 
2c7e1565b4a3
some support to invoke Scala methods under program control;
 
wenzelm 
parents:  
diff
changeset
 | 
15  | 
{
 | 
| 43748 | 16  | 
/* method reflection */  | 
17  | 
||
18  | 
  private val Ext = new Regex("(.*)\\.([^.]*)")
 | 
|
| 
43744
 
2c7e1565b4a3
some support to invoke Scala methods under program control;
 
wenzelm 
parents:  
diff
changeset
 | 
19  | 
  private val STRING = Class.forName("java.lang.String")
 | 
| 
 
2c7e1565b4a3
some support to invoke Scala methods under program control;
 
wenzelm 
parents:  
diff
changeset
 | 
20  | 
|
| 43748 | 21  | 
private def get_method(name: String): String => String =  | 
22  | 
    name match {
 | 
|
23  | 
case Ext(class_name, method_name) =>  | 
|
24  | 
val m =  | 
|
25  | 
          try { Class.forName(class_name).getMethod(method_name, STRING) }
 | 
|
26  | 
          catch {
 | 
|
| 
43751
 
8c7f69f1825b
proper InvocationTargetException.getCause for indirect exceptions;
 
wenzelm 
parents: 
43748 
diff
changeset
 | 
27  | 
case _: ClassNotFoundException | _: NoSuchMethodException =>  | 
| 
 
8c7f69f1825b
proper InvocationTargetException.getCause for indirect exceptions;
 
wenzelm 
parents: 
43748 
diff
changeset
 | 
28  | 
              error("No such method: " + quote(name))
 | 
| 43748 | 29  | 
}  | 
30  | 
        if (!Modifier.isStatic(m.getModifiers)) error("Not at static method: " + m.toString)
 | 
|
| 
43751
 
8c7f69f1825b
proper InvocationTargetException.getCause for indirect exceptions;
 
wenzelm 
parents: 
43748 
diff
changeset
 | 
31  | 
        if (m.getReturnType != STRING) error("Bad method return type: " + m.toString)
 | 
| 43748 | 32  | 
|
| 
43751
 
8c7f69f1825b
proper InvocationTargetException.getCause for indirect exceptions;
 
wenzelm 
parents: 
43748 
diff
changeset
 | 
33  | 
        (arg: String) => {
 | 
| 
 
8c7f69f1825b
proper InvocationTargetException.getCause for indirect exceptions;
 
wenzelm 
parents: 
43748 
diff
changeset
 | 
34  | 
          try { m.invoke(null, arg).asInstanceOf[String] }
 | 
| 
 
8c7f69f1825b
proper InvocationTargetException.getCause for indirect exceptions;
 
wenzelm 
parents: 
43748 
diff
changeset
 | 
35  | 
          catch {
 | 
| 
 
8c7f69f1825b
proper InvocationTargetException.getCause for indirect exceptions;
 
wenzelm 
parents: 
43748 
diff
changeset
 | 
36  | 
case e: InvocationTargetException if e.getCause != null =>  | 
| 
 
8c7f69f1825b
proper InvocationTargetException.getCause for indirect exceptions;
 
wenzelm 
parents: 
43748 
diff
changeset
 | 
37  | 
throw e.getCause  | 
| 
 
8c7f69f1825b
proper InvocationTargetException.getCause for indirect exceptions;
 
wenzelm 
parents: 
43748 
diff
changeset
 | 
38  | 
}  | 
| 
 
8c7f69f1825b
proper InvocationTargetException.getCause for indirect exceptions;
 
wenzelm 
parents: 
43748 
diff
changeset
 | 
39  | 
}  | 
| 43748 | 40  | 
      case _ => error("Malformed method name: " + quote(name))
 | 
41  | 
}  | 
|
42  | 
||
43  | 
||
44  | 
/* method invocation */  | 
|
45  | 
||
46  | 
object Tag extends Enumeration  | 
|
| 
43744
 
2c7e1565b4a3
some support to invoke Scala methods under program control;
 
wenzelm 
parents:  
diff
changeset
 | 
47  | 
  {
 | 
| 43748 | 48  | 
    val NULL = Value("0")
 | 
49  | 
    val OK = Value("1")
 | 
|
50  | 
    val ERROR = Value("2")
 | 
|
51  | 
    val FAIL = Value("3")
 | 
|
| 49470 | 52  | 
    val INTERRUPT = Value("4")
 | 
| 43748 | 53  | 
}  | 
| 
43744
 
2c7e1565b4a3
some support to invoke Scala methods under program control;
 
wenzelm 
parents:  
diff
changeset
 | 
54  | 
|
| 43748 | 55  | 
def method(name: String, arg: String): (Tag.Value, String) =  | 
56  | 
    Exn.capture { get_method(name) } match {
 | 
|
57  | 
case Exn.Res(f) =>  | 
|
58  | 
        Exn.capture { f(arg) } match {
 | 
|
59  | 
case Exn.Res(null) => (Tag.NULL, "")  | 
|
60  | 
case Exn.Res(res) => (Tag.OK, res)  | 
|
| 49470 | 61  | 
case Exn.Exn(_: InterruptedException) => (Tag.INTERRUPT, "")  | 
| 44158 | 62  | 
case Exn.Exn(e) => (Tag.ERROR, Exn.message(e))  | 
| 43748 | 63  | 
}  | 
| 44158 | 64  | 
case Exn.Exn(e) => (Tag.FAIL, Exn.message(e))  | 
| 43748 | 65  | 
}  | 
| 
43744
 
2c7e1565b4a3
some support to invoke Scala methods under program control;
 
wenzelm 
parents:  
diff
changeset
 | 
66  | 
}  | 
| 
52111
 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 
wenzelm 
parents: 
49470 
diff
changeset
 | 
67  | 
|
| 
 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 
wenzelm 
parents: 
49470 
diff
changeset
 | 
68  | 
|
| 
 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 
wenzelm 
parents: 
49470 
diff
changeset
 | 
69  | 
/* protocol handler */  | 
| 
 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 
wenzelm 
parents: 
49470 
diff
changeset
 | 
70  | 
|
| 
 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 
wenzelm 
parents: 
49470 
diff
changeset
 | 
71  | 
class Invoke_Scala extends Session.Protocol_Handler  | 
| 
 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 
wenzelm 
parents: 
49470 
diff
changeset
 | 
72  | 
{
 | 
| 
 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 
wenzelm 
parents: 
49470 
diff
changeset
 | 
73  | 
private var futures = Map.empty[String, java.util.concurrent.Future[Unit]]  | 
| 
 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 
wenzelm 
parents: 
49470 
diff
changeset
 | 
74  | 
|
| 
 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 
wenzelm 
parents: 
49470 
diff
changeset
 | 
75  | 
private def fulfill(prover: Session.Prover,  | 
| 
 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 
wenzelm 
parents: 
49470 
diff
changeset
 | 
76  | 
id: String, tag: Invoke_Scala.Tag.Value, res: String): Unit = synchronized  | 
| 
 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 
wenzelm 
parents: 
49470 
diff
changeset
 | 
77  | 
  {
 | 
| 
 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 
wenzelm 
parents: 
49470 
diff
changeset
 | 
78  | 
    if (futures.isDefinedAt(id)) {
 | 
| 52582 | 79  | 
      prover.protocol_command("Invoke_Scala.fulfill", id, tag.toString, res)
 | 
| 
52111
 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 
wenzelm 
parents: 
49470 
diff
changeset
 | 
80  | 
futures -= id  | 
| 
 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 
wenzelm 
parents: 
49470 
diff
changeset
 | 
81  | 
}  | 
| 
 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 
wenzelm 
parents: 
49470 
diff
changeset
 | 
82  | 
}  | 
| 
 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 
wenzelm 
parents: 
49470 
diff
changeset
 | 
83  | 
|
| 
 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 
wenzelm 
parents: 
49470 
diff
changeset
 | 
84  | 
private def cancel(prover: Session.Prover,  | 
| 
 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 
wenzelm 
parents: 
49470 
diff
changeset
 | 
85  | 
id: String, future: java.util.concurrent.Future[Unit])  | 
| 
 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 
wenzelm 
parents: 
49470 
diff
changeset
 | 
86  | 
  {
 | 
| 
 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 
wenzelm 
parents: 
49470 
diff
changeset
 | 
87  | 
future.cancel(true)  | 
| 
 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 
wenzelm 
parents: 
49470 
diff
changeset
 | 
88  | 
fulfill(prover, id, Invoke_Scala.Tag.INTERRUPT, "")  | 
| 
 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 
wenzelm 
parents: 
49470 
diff
changeset
 | 
89  | 
}  | 
| 
 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 
wenzelm 
parents: 
49470 
diff
changeset
 | 
90  | 
|
| 
 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 
wenzelm 
parents: 
49470 
diff
changeset
 | 
91  | 
private def invoke_scala(  | 
| 
 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 
wenzelm 
parents: 
49470 
diff
changeset
 | 
92  | 
prover: Session.Prover, output: Isabelle_Process.Output): Boolean = synchronized  | 
| 
 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 
wenzelm 
parents: 
49470 
diff
changeset
 | 
93  | 
  {
 | 
| 
 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 
wenzelm 
parents: 
49470 
diff
changeset
 | 
94  | 
    output.properties match {
 | 
| 
 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 
wenzelm 
parents: 
49470 
diff
changeset
 | 
95  | 
case Markup.Invoke_Scala(name, id) =>  | 
| 
 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 
wenzelm 
parents: 
49470 
diff
changeset
 | 
96  | 
futures += (id ->  | 
| 
 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 
wenzelm 
parents: 
49470 
diff
changeset
 | 
97  | 
default_thread_pool.submit(() =>  | 
| 
 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 
wenzelm 
parents: 
49470 
diff
changeset
 | 
98  | 
            {
 | 
| 
 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 
wenzelm 
parents: 
49470 
diff
changeset
 | 
99  | 
val arg = XML.content(output.body)  | 
| 
 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 
wenzelm 
parents: 
49470 
diff
changeset
 | 
100  | 
val (tag, result) = Invoke_Scala.method(name, arg)  | 
| 
 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 
wenzelm 
parents: 
49470 
diff
changeset
 | 
101  | 
fulfill(prover, id, tag, result)  | 
| 
 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 
wenzelm 
parents: 
49470 
diff
changeset
 | 
102  | 
}))  | 
| 
 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 
wenzelm 
parents: 
49470 
diff
changeset
 | 
103  | 
true  | 
| 
 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 
wenzelm 
parents: 
49470 
diff
changeset
 | 
104  | 
case _ => false  | 
| 
 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 
wenzelm 
parents: 
49470 
diff
changeset
 | 
105  | 
}  | 
| 
 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 
wenzelm 
parents: 
49470 
diff
changeset
 | 
106  | 
}  | 
| 
 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 
wenzelm 
parents: 
49470 
diff
changeset
 | 
107  | 
|
| 
 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 
wenzelm 
parents: 
49470 
diff
changeset
 | 
108  | 
private def cancel_scala(  | 
| 
 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 
wenzelm 
parents: 
49470 
diff
changeset
 | 
109  | 
prover: Session.Prover, output: Isabelle_Process.Output): Boolean = synchronized  | 
| 
 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 
wenzelm 
parents: 
49470 
diff
changeset
 | 
110  | 
  {
 | 
| 
 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 
wenzelm 
parents: 
49470 
diff
changeset
 | 
111  | 
    output.properties match {
 | 
| 
 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 
wenzelm 
parents: 
49470 
diff
changeset
 | 
112  | 
case Markup.Cancel_Scala(id) =>  | 
| 
 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 
wenzelm 
parents: 
49470 
diff
changeset
 | 
113  | 
        futures.get(id) match {
 | 
| 
 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 
wenzelm 
parents: 
49470 
diff
changeset
 | 
114  | 
case Some(future) => cancel(prover, id, future)  | 
| 
 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 
wenzelm 
parents: 
49470 
diff
changeset
 | 
115  | 
case None =>  | 
| 
 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 
wenzelm 
parents: 
49470 
diff
changeset
 | 
116  | 
}  | 
| 
 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 
wenzelm 
parents: 
49470 
diff
changeset
 | 
117  | 
true  | 
| 
 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 
wenzelm 
parents: 
49470 
diff
changeset
 | 
118  | 
case _ => false  | 
| 
 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 
wenzelm 
parents: 
49470 
diff
changeset
 | 
119  | 
}  | 
| 
 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 
wenzelm 
parents: 
49470 
diff
changeset
 | 
120  | 
}  | 
| 
 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 
wenzelm 
parents: 
49470 
diff
changeset
 | 
121  | 
|
| 
 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 
wenzelm 
parents: 
49470 
diff
changeset
 | 
122  | 
override def stop(prover: Session.Prover): Unit = synchronized  | 
| 
 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 
wenzelm 
parents: 
49470 
diff
changeset
 | 
123  | 
  {
 | 
| 
 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 
wenzelm 
parents: 
49470 
diff
changeset
 | 
124  | 
for ((id, future) <- futures) cancel(prover, id, future)  | 
| 
 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 
wenzelm 
parents: 
49470 
diff
changeset
 | 
125  | 
futures = Map.empty  | 
| 
 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 
wenzelm 
parents: 
49470 
diff
changeset
 | 
126  | 
}  | 
| 
 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 
wenzelm 
parents: 
49470 
diff
changeset
 | 
127  | 
|
| 
 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 
wenzelm 
parents: 
49470 
diff
changeset
 | 
128  | 
val functions = Map(  | 
| 
 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 
wenzelm 
parents: 
49470 
diff
changeset
 | 
129  | 
Markup.INVOKE_SCALA -> invoke_scala _,  | 
| 
 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 
wenzelm 
parents: 
49470 
diff
changeset
 | 
130  | 
Markup.CANCEL_SCALA -> cancel_scala _)  | 
| 
 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 
wenzelm 
parents: 
49470 
diff
changeset
 | 
131  | 
}  | 
| 
 
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
 
wenzelm 
parents: 
49470 
diff
changeset
 | 
132  |