src/Pure/System/session.scala
author wenzelm
Sun Aug 29 19:04:29 2010 +0200 (2010-08-29)
changeset 38848 9483bb678d96
parent 38842 f762b33e0821
child 38849 2f198d107aef
permissions -rw-r--r--
use Future.get_finished where this is the intended meaning -- prefer immediate crash over deadlock due to promises that are never fulfilled;
clarified session signalling;
wenzelm@36676
     1
/*  Title:      Pure/System/session.scala
wenzelm@36676
     2
    Author:     Makarius
wenzelm@38221
     3
    Options:    :folding=explicit:collapseFolds=1:
wenzelm@36676
     4
wenzelm@36676
     5
Isabelle session, potentially with running prover.
wenzelm@36676
     6
*/
wenzelm@34777
     7
wenzelm@34871
     8
package isabelle
wenzelm@34777
     9
wenzelm@34777
    10
wenzelm@34820
    11
import scala.actors.TIMEOUT
wenzelm@38226
    12
import scala.actors.Actor
wenzelm@34777
    13
import scala.actors.Actor._
wenzelm@34777
    14
wenzelm@34815
    15
wenzelm@34791
    16
object Session
wenzelm@34791
    17
{
wenzelm@34813
    18
  /* events */
wenzelm@34813
    19
wenzelm@34791
    20
  case object Global_Settings
wenzelm@37849
    21
  case object Perspective
wenzelm@38360
    22
  case class Commands_Changed(set: Set[Command])
wenzelm@34791
    23
}
wenzelm@34791
    24
wenzelm@34777
    25
wenzelm@34851
    26
class Session(system: Isabelle_System)
wenzelm@34777
    27
{
wenzelm@37849
    28
  /* real time parameters */  // FIXME properties or settings (!?)
wenzelm@37849
    29
wenzelm@37849
    30
  // user input (e.g. text edits, cursor movement)
wenzelm@37849
    31
  val input_delay = 300
wenzelm@37849
    32
wenzelm@37849
    33
  // prover output (markup, common messages)
wenzelm@37849
    34
  val output_delay = 100
wenzelm@37849
    35
wenzelm@37849
    36
  // GUI layout updates
wenzelm@37849
    37
  val update_delay = 500
wenzelm@37849
    38
wenzelm@37849
    39
wenzelm@34809
    40
  /* pervasive event buses */
wenzelm@34809
    41
wenzelm@34809
    42
  val global_settings = new Event_Bus[Session.Global_Settings.type]
wenzelm@38423
    43
  val raw_protocol = new Event_Bus[Isabelle_Process.Result]
wenzelm@37065
    44
  val raw_output = new Event_Bus[Isabelle_Process.Result]
wenzelm@38360
    45
  val commands_changed = new Event_Bus[Session.Commands_Changed]
wenzelm@37849
    46
  val perspective = new Event_Bus[Session.Perspective.type]
wenzelm@34809
    47
wenzelm@34809
    48
wenzelm@34778
    49
  /* unique ids */
wenzelm@34778
    50
wenzelm@38363
    51
  private var id_count: Document.ID = 0
wenzelm@38419
    52
  def new_id(): Document.ID = synchronized {
wenzelm@38355
    53
    require(id_count > java.lang.Long.MIN_VALUE)
wenzelm@38355
    54
    id_count -= 1
wenzelm@38355
    55
    id_count
wenzelm@38355
    56
  }
wenzelm@34778
    57
wenzelm@34778
    58
wenzelm@34826
    59
wenzelm@38841
    60
  /** buffered command changes (delay_first discipline) **/
wenzelm@38841
    61
wenzelm@38841
    62
  private case object Stop
wenzelm@38841
    63
wenzelm@38841
    64
  private val command_change_buffer = Simple_Thread.actor("command_change_buffer", daemon = true)
wenzelm@38841
    65
  //{{{
wenzelm@38841
    66
  {
wenzelm@38841
    67
    import scala.compat.Platform.currentTime
wenzelm@38841
    68
wenzelm@38841
    69
    var changed: Set[Command] = Set()
wenzelm@38841
    70
    var flush_time: Option[Long] = None
wenzelm@38841
    71
wenzelm@38841
    72
    def flush_timeout: Long =
wenzelm@38841
    73
      flush_time match {
wenzelm@38841
    74
        case None => 5000L
wenzelm@38841
    75
        case Some(time) => (time - currentTime) max 0
wenzelm@38841
    76
      }
wenzelm@38841
    77
wenzelm@38841
    78
    def flush()
wenzelm@38841
    79
    {
wenzelm@38841
    80
      if (!changed.isEmpty) commands_changed.event(Session.Commands_Changed(changed))
wenzelm@38841
    81
      changed = Set()
wenzelm@38841
    82
      flush_time = None
wenzelm@38841
    83
    }
wenzelm@38841
    84
wenzelm@38841
    85
    def invoke()
wenzelm@38841
    86
    {
wenzelm@38841
    87
      val now = currentTime
wenzelm@38841
    88
      flush_time match {
wenzelm@38841
    89
        case None => flush_time = Some(now + output_delay)
wenzelm@38841
    90
        case Some(time) => if (now >= time) flush()
wenzelm@38841
    91
      }
wenzelm@38841
    92
    }
wenzelm@38841
    93
wenzelm@38841
    94
    var finished = false
wenzelm@38841
    95
    while (!finished) {
wenzelm@38841
    96
      receiveWithin(flush_timeout) {
wenzelm@38841
    97
        case command: Command => changed += command; invoke()
wenzelm@38841
    98
        case TIMEOUT => flush()
wenzelm@38841
    99
        case Stop => finished = true
wenzelm@38841
   100
        case bad => System.err.println("command_change_buffer: ignoring bad message " + bad)
wenzelm@38841
   101
      }
wenzelm@38841
   102
    }
wenzelm@38841
   103
  }
wenzelm@38841
   104
  //}}}
wenzelm@38841
   105
wenzelm@38841
   106
wenzelm@38841
   107
wenzelm@38841
   108
  /** main protocol actor **/
wenzelm@34809
   109
wenzelm@34851
   110
  @volatile private var syntax = new Outer_Syntax(system.symbols)
wenzelm@38569
   111
  def current_syntax(): Outer_Syntax = syntax
wenzelm@34809
   112
wenzelm@38841
   113
  private val global_state = new Volatile(Document.State.init)
wenzelm@38841
   114
  def current_state(): Document.State = global_state.peek()
wenzelm@34816
   115
wenzelm@38841
   116
  private case class Edit_Version(edits: List[Document.Node_Text_Edit])
wenzelm@38221
   117
  private case class Started(timeout: Int, args: List[String])
wenzelm@34777
   118
wenzelm@38639
   119
  private val session_actor = Simple_Thread.actor("session_actor", daemon = true)
wenzelm@38639
   120
  {
wenzelm@34809
   121
    var prover: Isabelle_Process with Isar_Document = null
wenzelm@34809
   122
wenzelm@34809
   123
wenzelm@34809
   124
    /* document changes */
wenzelm@34809
   125
wenzelm@38227
   126
    def handle_change(change: Document.Change)
wenzelm@38221
   127
    //{{{
wenzelm@34809
   128
    {
wenzelm@38848
   129
      val previous = change.previous.get_finished
wenzelm@38848
   130
      val (node_edits, current) = change.result.get_finished
wenzelm@38366
   131
wenzelm@38848
   132
      var former_assignment = global_state.peek().the_assignment(previous).get_finished
wenzelm@38370
   133
      for {
wenzelm@38370
   134
        (name, Some(cmd_edits)) <- node_edits
wenzelm@38370
   135
        (prev, None) <- cmd_edits
wenzelm@38417
   136
        removed <- previous.nodes(name).commands.get_after(prev)
wenzelm@38370
   137
      } former_assignment -= removed
wenzelm@38370
   138
wenzelm@38150
   139
      val id_edits =
wenzelm@38150
   140
        node_edits map {
wenzelm@38150
   141
          case (name, None) => (name, None)
wenzelm@38150
   142
          case (name, Some(cmd_edits)) =>
wenzelm@38370
   143
            val ids =
wenzelm@38150
   144
              cmd_edits map {
wenzelm@38150
   145
                case (c1, c2) =>
wenzelm@38150
   146
                  val id1 = c1.map(_.id)
wenzelm@38150
   147
                  val id2 =
wenzelm@38150
   148
                    c2 match {
wenzelm@38150
   149
                      case None => None
wenzelm@38150
   150
                      case Some(command) =>
wenzelm@38841
   151
                        if (global_state.peek().lookup_command(command.id).isEmpty) {
wenzelm@38841
   152
                          global_state.change(_.define_command(command))
wenzelm@38150
   153
                          prover.define_command(command.id, system.symbols.encode(command.source))
wenzelm@38150
   154
                        }
wenzelm@38150
   155
                        Some(command.id)
wenzelm@38150
   156
                    }
wenzelm@38150
   157
                  (id1, id2)
wenzelm@38150
   158
              }
wenzelm@38370
   159
            (name -> Some(ids))
wenzelm@38150
   160
        }
wenzelm@38841
   161
      global_state.change(_.define_version(current, former_assignment))
wenzelm@38417
   162
      prover.edit_version(previous.id, current.id, id_edits)
wenzelm@34809
   163
    }
wenzelm@38221
   164
    //}}}
wenzelm@34809
   165
wenzelm@34809
   166
wenzelm@34809
   167
    /* prover results */
wenzelm@34809
   168
wenzelm@34836
   169
    def bad_result(result: Isabelle_Process.Result)
wenzelm@34836
   170
    {
wenzelm@37041
   171
      System.err.println("Ignoring prover result: " + result.message.toString)
wenzelm@34836
   172
    }
wenzelm@34836
   173
wenzelm@34809
   174
    def handle_result(result: Isabelle_Process.Result)
wenzelm@38221
   175
    //{{{
wenzelm@34809
   176
    {
wenzelm@38423
   177
      raw_protocol.event(result)
wenzelm@34799
   178
wenzelm@38722
   179
      result.properties match {
wenzelm@38722
   180
        case Position.Id(state_id) =>
wenzelm@38370
   181
          try {
wenzelm@38841
   182
            val st = global_state.change_yield(_.accumulate(state_id, result.message))
wenzelm@38841
   183
            command_change_buffer ! st.command
wenzelm@38370
   184
          }
wenzelm@38414
   185
          catch { case _: Document.State.Fail => bad_result(result) }
wenzelm@38722
   186
        case _ =>
wenzelm@38370
   187
          if (result.is_status) {
wenzelm@38370
   188
            result.body match {
wenzelm@38417
   189
              case List(Isar_Document.Assign(id, edits)) =>
wenzelm@38841
   190
                try { global_state.change(_.assign(id, edits)) }
wenzelm@38414
   191
                catch { case _: Document.State.Fail => bad_result(result) }
wenzelm@38370
   192
              case List(Keyword.Command_Decl(name, kind)) => syntax += (name, kind)
wenzelm@38370
   193
              case List(Keyword.Keyword_Decl(name)) => syntax += name
wenzelm@38370
   194
              case _ => if (!result.is_ready) bad_result(result)
wenzelm@38370
   195
            }
wenzelm@38370
   196
          }
wenzelm@38370
   197
          else if (result.kind == Markup.EXIT) prover = null
wenzelm@38370
   198
          else if (result.is_raw) raw_output.event(result)
wenzelm@38370
   199
          else if (!result.is_system) bad_result(result)  // FIXME syslog for system messages (!?)
wenzelm@34809
   200
        }
wenzelm@34809
   201
    }
wenzelm@38221
   202
    //}}}
wenzelm@34809
   203
wenzelm@34820
   204
wenzelm@34820
   205
    /* prover startup */
wenzelm@34820
   206
wenzelm@34820
   207
    def startup_error(): String =
wenzelm@34820
   208
    {
wenzelm@34820
   209
      val buf = new StringBuilder
wenzelm@34820
   210
      while (
wenzelm@34820
   211
        receiveWithin(0) {
wenzelm@34820
   212
          case result: Isabelle_Process.Result =>
wenzelm@34820
   213
            if (result.is_raw) {
wenzelm@34820
   214
              for (text <- XML.content(result.message))
wenzelm@34820
   215
                buf.append(text)
wenzelm@34820
   216
            }
wenzelm@34820
   217
            true
wenzelm@34820
   218
          case TIMEOUT => false
wenzelm@34820
   219
        }) {}
wenzelm@34820
   220
      buf.toString
wenzelm@34820
   221
    }
wenzelm@34820
   222
wenzelm@34820
   223
    def prover_startup(timeout: Int): Option[String] =
wenzelm@34820
   224
    {
wenzelm@34820
   225
      receiveWithin(timeout) {
wenzelm@34820
   226
        case result: Isabelle_Process.Result
wenzelm@37689
   227
          if result.kind == Markup.INIT =>
wenzelm@34820
   228
          while (receive {
wenzelm@34820
   229
            case result: Isabelle_Process.Result =>
wenzelm@34820
   230
              handle_result(result); !result.is_ready
wenzelm@34820
   231
            }) {}
wenzelm@34820
   232
          None
wenzelm@34820
   233
wenzelm@34820
   234
        case result: Isabelle_Process.Result
wenzelm@37689
   235
          if result.kind == Markup.EXIT =>
wenzelm@34820
   236
          Some(startup_error())
wenzelm@34820
   237
wenzelm@34820
   238
        case TIMEOUT =>  // FIXME clarify
wenzelm@34820
   239
          prover.kill; Some(startup_error())
wenzelm@34820
   240
      }
wenzelm@34820
   241
    }
wenzelm@34809
   242
wenzelm@34809
   243
wenzelm@34809
   244
    /* main loop */
wenzelm@34809
   245
wenzelm@38639
   246
    var finished = false
wenzelm@38639
   247
    while (!finished) {
wenzelm@38639
   248
      receive {
wenzelm@38841
   249
        case Edit_Version(edits) =>
wenzelm@38841
   250
          val previous = global_state.peek().history.tip.current
wenzelm@38841
   251
          val result = Future.fork { Thy_Syntax.text_edits(Session.this, previous.join, edits) }
wenzelm@38841
   252
          val change = global_state.change_yield(_.extend_history(previous, edits, result))
wenzelm@38848
   253
          val this_actor = self; change.current.map(_ => this_actor ! change)
wenzelm@38841
   254
          reply(())
wenzelm@38841
   255
wenzelm@38841
   256
        case change: Document.Change if prover != null =>
wenzelm@38841
   257
          handle_change(change)
wenzelm@38841
   258
wenzelm@38841
   259
        case result: Isabelle_Process.Result =>
wenzelm@38841
   260
          handle_result(result)
wenzelm@38841
   261
wenzelm@38221
   262
        case Started(timeout, args) =>
wenzelm@34777
   263
          if (prover == null) {
wenzelm@34851
   264
            prover = new Isabelle_Process(system, self, args:_*) with Isar_Document
wenzelm@34820
   265
            val origin = sender
wenzelm@34820
   266
            val opt_err = prover_startup(timeout)
wenzelm@34820
   267
            if (opt_err.isDefined) prover = null
wenzelm@34820
   268
            origin ! opt_err
wenzelm@34820
   269
          }
wenzelm@34820
   270
          else reply(None)
wenzelm@34820
   271
wenzelm@38639
   272
        case Stop => // FIXME synchronous!?
wenzelm@34820
   273
          if (prover != null) {
wenzelm@34820
   274
            prover.kill
wenzelm@34820
   275
            prover = null
wenzelm@38639
   276
            finished = true
wenzelm@34777
   277
          }
wenzelm@34777
   278
wenzelm@34820
   279
        case bad if prover != null =>
wenzelm@34777
   280
          System.err.println("session_actor: ignoring bad message " + bad)
wenzelm@34777
   281
      }
wenzelm@34777
   282
    }
wenzelm@34777
   283
  }
wenzelm@34777
   284
wenzelm@34809
   285
wenzelm@37129
   286
wenzelm@38226
   287
  /** main methods **/
wenzelm@34809
   288
wenzelm@38221
   289
  def started(timeout: Int, args: List[String]): Option[String] =
wenzelm@38221
   290
    (session_actor !? Started(timeout, args)).asInstanceOf[Option[String]]
wenzelm@34820
   291
wenzelm@38841
   292
  def stop() { command_change_buffer ! Stop; session_actor ! Stop }
wenzelm@38841
   293
wenzelm@38841
   294
  def edit_version(edits: List[Document.Node_Text_Edit]) { session_actor !? Edit_Version(edits) }
wenzelm@38221
   295
wenzelm@38841
   296
  def snapshot(name: String, pending_edits: List[Text.Edit]): Document.Snapshot =
wenzelm@38841
   297
    global_state.peek().snapshot(name, pending_edits)
wenzelm@34777
   298
}