src/Pure/PIDE/protocol_handlers.scala
author wenzelm
Thu, 06 Jun 2024 11:53:52 +0200
changeset 80266 d52be75ae60b
parent 75393 87ebf5a50283
child 80300 152d6c58adb3
permissions -rw-r--r--
tuned signature;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
65214
a2ec0db555c7 clarified modules;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/PIDE/protocol_handlers.scala
a2ec0db555c7 clarified modules;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
a2ec0db555c7 clarified modules;
wenzelm
parents:
diff changeset
     3
a2ec0db555c7 clarified modules;
wenzelm
parents:
diff changeset
     4
Management of add-on protocol handlers for PIDE session.
a2ec0db555c7 clarified modules;
wenzelm
parents:
diff changeset
     5
*/
a2ec0db555c7 clarified modules;
wenzelm
parents:
diff changeset
     6
a2ec0db555c7 clarified modules;
wenzelm
parents:
diff changeset
     7
package isabelle
a2ec0db555c7 clarified modules;
wenzelm
parents:
diff changeset
     8
a2ec0db555c7 clarified modules;
wenzelm
parents:
diff changeset
     9
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73359
diff changeset
    10
object Protocol_Handlers {
72217
e35997591c5b strict init of protocol handlers;
wenzelm
parents: 72216
diff changeset
    11
  private def err_handler(exn: Throwable, name: String): Nothing =
e35997591c5b strict init of protocol handlers;
wenzelm
parents: 72216
diff changeset
    12
    error("Failed to initialize protocol handler: " + quote(name) + "\n" + Exn.message(exn))
65214
a2ec0db555c7 clarified modules;
wenzelm
parents:
diff changeset
    13
a2ec0db555c7 clarified modules;
wenzelm
parents:
diff changeset
    14
  sealed case class State(
65215
90a7876d6f4c tuned signature;
wenzelm
parents: 65214
diff changeset
    15
    session: Session,
65214
a2ec0db555c7 clarified modules;
wenzelm
parents:
diff changeset
    16
    handlers: Map[String, Session.Protocol_Handler] = Map.empty,
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73359
diff changeset
    17
    functions: Map[String, Session.Protocol_Function] = Map.empty
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73359
diff changeset
    18
  ) {
65214
a2ec0db555c7 clarified modules;
wenzelm
parents:
diff changeset
    19
    def get(name: String): Option[Session.Protocol_Handler] = handlers.get(name)
a2ec0db555c7 clarified modules;
wenzelm
parents:
diff changeset
    20
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73359
diff changeset
    21
    def init(handler: Session.Protocol_Handler): State = {
65214
a2ec0db555c7 clarified modules;
wenzelm
parents:
diff changeset
    22
      val name = handler.getClass.getName
72156
065dcd80293e provide protocol handlers via isabelle_system_service;
wenzelm
parents: 71970
diff changeset
    23
      try {
065dcd80293e provide protocol handlers via isabelle_system_service;
wenzelm
parents: 71970
diff changeset
    24
        if (handlers.isDefinedAt(name)) error("Duplicate protocol handler: " + name)
065dcd80293e provide protocol handlers via isabelle_system_service;
wenzelm
parents: 71970
diff changeset
    25
        else {
065dcd80293e provide protocol handlers via isabelle_system_service;
wenzelm
parents: 71970
diff changeset
    26
          handler.init(session)
065dcd80293e provide protocol handlers via isabelle_system_service;
wenzelm
parents: 71970
diff changeset
    27
          val dups = for ((a, _) <- handler.functions if functions.isDefinedAt(a)) yield a
065dcd80293e provide protocol handlers via isabelle_system_service;
wenzelm
parents: 71970
diff changeset
    28
          if (dups.nonEmpty) error("Duplicate protocol functions: " + commas_quote(dups))
065dcd80293e provide protocol handlers via isabelle_system_service;
wenzelm
parents: 71970
diff changeset
    29
          copy(handlers = handlers + (name -> handler), functions = functions ++ handler.functions)
65214
a2ec0db555c7 clarified modules;
wenzelm
parents:
diff changeset
    30
        }
72156
065dcd80293e provide protocol handlers via isabelle_system_service;
wenzelm
parents: 71970
diff changeset
    31
      }
72217
e35997591c5b strict init of protocol handlers;
wenzelm
parents: 72216
diff changeset
    32
      catch { case exn: Throwable => err_handler(exn, name) }
65214
a2ec0db555c7 clarified modules;
wenzelm
parents:
diff changeset
    33
    }
a2ec0db555c7 clarified modules;
wenzelm
parents:
diff changeset
    34
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73359
diff changeset
    35
    def init(name: String): State = {
72217
e35997591c5b strict init of protocol handlers;
wenzelm
parents: 72216
diff changeset
    36
      val handler =
71734
713fafb3de79 tuned -- avoid deprecated operations;
wenzelm
parents: 71673
diff changeset
    37
        try {
72217
e35997591c5b strict init of protocol handlers;
wenzelm
parents: 72216
diff changeset
    38
          Class.forName(name).getDeclaredConstructor().newInstance()
e35997591c5b strict init of protocol handlers;
wenzelm
parents: 72216
diff changeset
    39
            .asInstanceOf[Session.Protocol_Handler]
71734
713fafb3de79 tuned -- avoid deprecated operations;
wenzelm
parents: 71673
diff changeset
    40
        }
72217
e35997591c5b strict init of protocol handlers;
wenzelm
parents: 72216
diff changeset
    41
        catch { case exn: Throwable => err_handler(exn, name) }
e35997591c5b strict init of protocol handlers;
wenzelm
parents: 72216
diff changeset
    42
      init(handler)
65214
a2ec0db555c7 clarified modules;
wenzelm
parents:
diff changeset
    43
    }
a2ec0db555c7 clarified modules;
wenzelm
parents:
diff changeset
    44
a2ec0db555c7 clarified modules;
wenzelm
parents:
diff changeset
    45
    def invoke(msg: Prover.Protocol_Output): Boolean =
a2ec0db555c7 clarified modules;
wenzelm
parents:
diff changeset
    46
      msg.properties match {
71673
88dfbc382a3d clarified signature;
wenzelm
parents: 65315
diff changeset
    47
        case (Markup.FUNCTION, a) :: _ if functions.isDefinedAt(a) =>
65214
a2ec0db555c7 clarified modules;
wenzelm
parents:
diff changeset
    48
          try { functions(a)(msg) }
a2ec0db555c7 clarified modules;
wenzelm
parents:
diff changeset
    49
          catch {
a2ec0db555c7 clarified modules;
wenzelm
parents:
diff changeset
    50
            case exn: Throwable =>
a2ec0db555c7 clarified modules;
wenzelm
parents:
diff changeset
    51
              Output.error_message(
a2ec0db555c7 clarified modules;
wenzelm
parents:
diff changeset
    52
                "Failed invocation of protocol function: " + quote(a) + "\n" + Exn.message(exn))
a2ec0db555c7 clarified modules;
wenzelm
parents:
diff changeset
    53
            false
a2ec0db555c7 clarified modules;
wenzelm
parents:
diff changeset
    54
          }
a2ec0db555c7 clarified modules;
wenzelm
parents:
diff changeset
    55
        case _ => false
a2ec0db555c7 clarified modules;
wenzelm
parents:
diff changeset
    56
      }
a2ec0db555c7 clarified modules;
wenzelm
parents:
diff changeset
    57
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73359
diff changeset
    58
    def exit(): State = {
65219
ed4b47b8c7dc misc tuning and simplification;
wenzelm
parents: 65215
diff changeset
    59
      for ((_, handler) <- handlers) handler.exit()
65215
90a7876d6f4c tuned signature;
wenzelm
parents: 65214
diff changeset
    60
      copy(handlers = Map.empty, functions = Map.empty)
65214
a2ec0db555c7 clarified modules;
wenzelm
parents:
diff changeset
    61
    }
a2ec0db555c7 clarified modules;
wenzelm
parents:
diff changeset
    62
  }
a2ec0db555c7 clarified modules;
wenzelm
parents:
diff changeset
    63
65215
90a7876d6f4c tuned signature;
wenzelm
parents: 65214
diff changeset
    64
  def init(session: Session): Protocol_Handlers =
90a7876d6f4c tuned signature;
wenzelm
parents: 65214
diff changeset
    65
    new Protocol_Handlers(session)
65214
a2ec0db555c7 clarified modules;
wenzelm
parents:
diff changeset
    66
}
a2ec0db555c7 clarified modules;
wenzelm
parents:
diff changeset
    67
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73359
diff changeset
    68
class Protocol_Handlers private(session: Session) {
65215
90a7876d6f4c tuned signature;
wenzelm
parents: 65214
diff changeset
    69
  private val state = Synchronized(Protocol_Handlers.State(session))
65214
a2ec0db555c7 clarified modules;
wenzelm
parents:
diff changeset
    70
72216
0d7cd97f6c48 clarified treatment of add-on prover_options;
wenzelm
parents: 72156
diff changeset
    71
  def prover_options(options: Options): Options =
73359
d8a0e996614b tuned --- fewer warnings;
wenzelm
parents: 72217
diff changeset
    72
    state.value.handlers.foldLeft(options) {
d8a0e996614b tuned --- fewer warnings;
wenzelm
parents: 72217
diff changeset
    73
      case (opts, (_, handler)) => handler.prover_options(opts)
d8a0e996614b tuned --- fewer warnings;
wenzelm
parents: 72217
diff changeset
    74
    }
72216
0d7cd97f6c48 clarified treatment of add-on prover_options;
wenzelm
parents: 72156
diff changeset
    75
65219
ed4b47b8c7dc misc tuning and simplification;
wenzelm
parents: 65215
diff changeset
    76
  def get(name: String): Option[Session.Protocol_Handler] = state.value.get(name)
65315
c7097ccbffb7 clarified signature;
wenzelm
parents: 65219
diff changeset
    77
  def init(handler: Session.Protocol_Handler): Unit = state.change(_.init(handler))
c7097ccbffb7 clarified signature;
wenzelm
parents: 65219
diff changeset
    78
  def init(name: String): Unit = state.change(_.init(name))
65219
ed4b47b8c7dc misc tuning and simplification;
wenzelm
parents: 65215
diff changeset
    79
  def invoke(msg: Prover.Protocol_Output): Boolean = state.value.invoke(msg)
ed4b47b8c7dc misc tuning and simplification;
wenzelm
parents: 65215
diff changeset
    80
  def exit(): Unit = state.change(_.exit())
65214
a2ec0db555c7 clarified modules;
wenzelm
parents:
diff changeset
    81
}