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