src/Pure/PIDE/protocol.scala
author wenzelm
Tue Apr 01 17:26:32 2014 +0200 (2014-04-01)
changeset 56352 abdc524db8b4
parent 56335 8953d4cc060a
child 56353 ecbdfe30bf7f
permissions -rw-r--r--
more frugal command_status, which is often used in a tight loop;
wenzelm@45709
     1
/*  Title:      Pure/PIDE/protocol.scala
wenzelm@38412
     2
    Author:     Makarius
wenzelm@38412
     3
wenzelm@45709
     4
Protocol message formats for interactive proof documents.
wenzelm@38412
     5
*/
wenzelm@38412
     6
wenzelm@38412
     7
package isabelle
wenzelm@38412
     8
wenzelm@38412
     9
wenzelm@45709
    10
object Protocol
wenzelm@38412
    11
{
wenzelm@38567
    12
  /* document editing */
wenzelm@38412
    13
wenzelm@52563
    14
  object Assign_Update
wenzelm@44476
    15
  {
wenzelm@52563
    16
    def unapply(text: String): Option[(Document_ID.Version, Document.Assign_Update)] =
wenzelm@44661
    17
      try {
wenzelm@44661
    18
        import XML.Decode._
wenzelm@44661
    19
        val body = YXML.parse_body(text)
wenzelm@52527
    20
        Some(pair(long, list(pair(long, list(long))))(body))
wenzelm@44661
    21
      }
wenzelm@44661
    22
      catch {
wenzelm@44661
    23
        case ERROR(_) => None
wenzelm@51987
    24
        case _: XML.Error => None
wenzelm@38412
    25
      }
wenzelm@38412
    26
  }
wenzelm@38567
    27
wenzelm@44676
    28
  object Removed
wenzelm@44676
    29
  {
wenzelm@52530
    30
    def unapply(text: String): Option[List[Document_ID.Version]] =
wenzelm@44676
    31
      try {
wenzelm@44676
    32
        import XML.Decode._
wenzelm@44676
    33
        Some(list(long)(YXML.parse_body(text)))
wenzelm@44676
    34
      }
wenzelm@44676
    35
      catch {
wenzelm@44676
    36
        case ERROR(_) => None
wenzelm@51987
    37
        case _: XML.Error => None
wenzelm@44676
    38
      }
wenzelm@44676
    39
  }
wenzelm@44676
    40
wenzelm@38567
    41
wenzelm@46209
    42
  /* command status */
wenzelm@38567
    43
wenzelm@46166
    44
  sealed case class Status(
wenzelm@56352
    45
    private val touched: Boolean,
wenzelm@56352
    46
    private val accepted: Boolean,
wenzelm@56352
    47
    private val failed: Boolean,
wenzelm@56352
    48
    forks: Int,
wenzelm@56352
    49
    runs: Int)
wenzelm@38567
    50
  {
wenzelm@46227
    51
    def + (that: Status): Status =
wenzelm@49036
    52
      Status(touched || that.touched, accepted || that.accepted, failed || that.failed,
wenzelm@49036
    53
        forks + that.forks, runs + that.runs)
wenzelm@49036
    54
wenzelm@49036
    55
    def is_unprocessed: Boolean = accepted && !failed && (!touched || (forks != 0 && runs == 0))
wenzelm@49036
    56
    def is_running: Boolean = runs != 0
wenzelm@49039
    57
    def is_finished: Boolean = !failed && touched && forks == 0 && runs == 0
wenzelm@49036
    58
    def is_failed: Boolean = failed
wenzelm@46166
    59
  }
wenzelm@46166
    60
wenzelm@56352
    61
  def command_status(markups: Iterator[Markup]): Status =
wenzelm@56352
    62
  {
wenzelm@56352
    63
    var touched = false
wenzelm@56352
    64
    var accepted = false
wenzelm@56352
    65
    var failed = false
wenzelm@56352
    66
    var forks = 0
wenzelm@56352
    67
    var runs = 0
wenzelm@56352
    68
    for { markup <- markups; name = markup.name }
wenzelm@56352
    69
      name match {
wenzelm@56352
    70
        case Markup.ACCEPTED => accepted = true
wenzelm@56352
    71
        case Markup.FORKED => touched = true; forks += 1
wenzelm@56352
    72
        case Markup.JOINED => forks -= 1
wenzelm@56352
    73
        case Markup.RUNNING => touched = true; runs += 1
wenzelm@56352
    74
        case Markup.FINISHED => runs -= 1
wenzelm@56352
    75
        case Markup.FAILED => failed = true
wenzelm@56352
    76
        case _ =>
wenzelm@56352
    77
      }
wenzelm@56352
    78
    Status(touched, accepted, failed, forks, runs)
wenzelm@56352
    79
  }
wenzelm@38887
    80
wenzelm@55820
    81
  val command_status_elements =
wenzelm@55820
    82
    Document.Elements(Markup.ACCEPTED, Markup.FORKED, Markup.JOINED, Markup.RUNNING,
wenzelm@55646
    83
      Markup.FINISHED, Markup.FAILED)
wenzelm@55646
    84
wenzelm@55820
    85
  val status_elements =
wenzelm@55646
    86
    command_status_elements + Markup.WARNING + Markup.ERROR
wenzelm@55646
    87
wenzelm@46209
    88
wenzelm@51818
    89
  /* command timing */
wenzelm@51818
    90
wenzelm@51818
    91
  object Command_Timing
wenzelm@51818
    92
  {
wenzelm@52531
    93
    def unapply(props: Properties.T): Option[(Document_ID.Generic, isabelle.Timing)] =
wenzelm@51818
    94
      props match {
wenzelm@51818
    95
        case (Markup.FUNCTION, Markup.COMMAND_TIMING) :: args =>
wenzelm@51818
    96
          (args, args) match {
wenzelm@51818
    97
            case (Position.Id(id), Markup.Timing_Properties(timing)) => Some((id, timing))
wenzelm@51818
    98
            case _ => None
wenzelm@51818
    99
          }
wenzelm@51818
   100
        case _ => None
wenzelm@51818
   101
      }
wenzelm@51818
   102
  }
wenzelm@51818
   103
wenzelm@51818
   104
wenzelm@46209
   105
  /* node status */
wenzelm@46209
   106
wenzelm@46688
   107
  sealed case class Node_Status(
wenzelm@46688
   108
    unprocessed: Int, running: Int, finished: Int, warned: Int, failed: Int)
wenzelm@44866
   109
  {
wenzelm@46688
   110
    def total: Int = unprocessed + running + finished + warned + failed
wenzelm@44866
   111
  }
wenzelm@44613
   112
wenzelm@44613
   113
  def node_status(
wenzelm@44613
   114
    state: Document.State, version: Document.Version, node: Document.Node): Node_Status =
wenzelm@44613
   115
  {
wenzelm@44613
   116
    var unprocessed = 0
wenzelm@44613
   117
    var running = 0
wenzelm@44613
   118
    var finished = 0
wenzelm@46688
   119
    var warned = 0
wenzelm@44613
   120
    var failed = 0
wenzelm@56299
   121
    for {
wenzelm@56299
   122
      command <- node.commands
wenzelm@56306
   123
      states = state.command_states(version, command)
wenzelm@56352
   124
      status = command_status(states.iterator.flatMap(st => st.status.iterator))
wenzelm@56299
   125
    } {
wenzelm@56299
   126
      if (status.is_running) running += 1
wenzelm@56299
   127
      else if (status.is_finished) {
wenzelm@56306
   128
        val warning = states.exists(st => st.results.entries.exists(p => is_warning(p._2)))
wenzelm@56306
   129
        if (warning) warned += 1 else finished += 1
wenzelm@56299
   130
      }
wenzelm@56299
   131
      else if (status.is_failed) failed += 1
wenzelm@56299
   132
      else unprocessed += 1
wenzelm@56299
   133
    }
wenzelm@46688
   134
    Node_Status(unprocessed, running, finished, warned, failed)
wenzelm@44613
   135
  }
wenzelm@44613
   136
wenzelm@38887
   137
wenzelm@51533
   138
  /* node timing */
wenzelm@51533
   139
wenzelm@51533
   140
  sealed case class Node_Timing(total: Double, commands: Map[Command, Double])
wenzelm@51533
   141
wenzelm@51533
   142
  val empty_node_timing = Node_Timing(0.0, Map.empty)
wenzelm@51533
   143
wenzelm@51533
   144
  def node_timing(
wenzelm@51533
   145
    state: Document.State,
wenzelm@51533
   146
    version: Document.Version,
wenzelm@51533
   147
    node: Document.Node,
wenzelm@51533
   148
    threshold: Double): Node_Timing =
wenzelm@51533
   149
  {
wenzelm@51533
   150
    var total = 0.0
wenzelm@51533
   151
    var commands = Map.empty[Command, Double]
wenzelm@51533
   152
    for {
wenzelm@51533
   153
      command <- node.commands.iterator
wenzelm@56299
   154
      st <- state.command_states(version, command)
wenzelm@51533
   155
      command_timing =
wenzelm@51533
   156
        (0.0 /: st.status)({
wenzelm@51533
   157
          case (timing, Markup.Timing(t)) => timing + t.elapsed.seconds
wenzelm@51533
   158
          case (timing, _) => timing
wenzelm@51533
   159
        })
wenzelm@51533
   160
    } {
wenzelm@51533
   161
      total += command_timing
wenzelm@51533
   162
      if (command_timing >= threshold) commands += (command -> command_timing)
wenzelm@51533
   163
    }
wenzelm@51533
   164
    Node_Timing(total, commands)
wenzelm@51533
   165
  }
wenzelm@51533
   166
wenzelm@51533
   167
wenzelm@39441
   168
  /* result messages */
wenzelm@39439
   169
wenzelm@55820
   170
  private val clean_elements =
wenzelm@55820
   171
    Document.Elements(Markup.REPORT, Markup.NO_REPORT)
wenzelm@50450
   172
wenzelm@39439
   173
  def clean_message(body: XML.Body): XML.Body =
wenzelm@49445
   174
    body filter {
wenzelm@55646
   175
      case XML.Wrapped_Elem(Markup(name, _), _, _) => !clean_elements(name)
wenzelm@55646
   176
      case XML.Elem(Markup(name, _), _) => !clean_elements(name)
wenzelm@49445
   177
      case _ => true
wenzelm@49650
   178
    } map {
wenzelm@49650
   179
      case XML.Wrapped_Elem(markup, body, ts) => XML.Wrapped_Elem(markup, body, clean_message(ts))
wenzelm@49650
   180
      case XML.Elem(markup, ts) => XML.Elem(markup, clean_message(ts))
wenzelm@49650
   181
      case t => t
wenzelm@49650
   182
    }
wenzelm@39439
   183
wenzelm@49445
   184
  def message_reports(props: Properties.T, body: XML.Body): List[XML.Elem] =
wenzelm@49445
   185
    body flatMap {
wenzelm@50450
   186
      case XML.Wrapped_Elem(Markup(Markup.REPORT, ps), body, ts) =>
wenzelm@50450
   187
        List(XML.Wrapped_Elem(Markup(Markup.REPORT, props ::: ps), body, ts))
wenzelm@50201
   188
      case XML.Elem(Markup(Markup.REPORT, ps), ts) =>
wenzelm@50201
   189
        List(XML.Elem(Markup(Markup.REPORT, props ::: ps), ts))
wenzelm@49650
   190
      case XML.Wrapped_Elem(_, _, ts) => message_reports(props, ts)
wenzelm@49445
   191
      case XML.Elem(_, ts) => message_reports(props, ts)
wenzelm@39441
   192
      case XML.Text(_) => Nil
wenzelm@39441
   193
    }
wenzelm@39441
   194
wenzelm@39439
   195
wenzelm@39511
   196
  /* specific messages */
wenzelm@39511
   197
wenzelm@50500
   198
  def is_inlined(msg: XML.Tree): Boolean =
wenzelm@50500
   199
    !(is_result(msg) || is_tracing(msg) || is_state(msg))
wenzelm@50500
   200
wenzelm@50500
   201
  def is_result(msg: XML.Tree): Boolean =
wenzelm@50500
   202
    msg match {
wenzelm@50500
   203
      case XML.Elem(Markup(Markup.RESULT, _), _) => true
wenzelm@50500
   204
      case _ => false
wenzelm@50500
   205
    }
wenzelm@50500
   206
wenzelm@50157
   207
  def is_tracing(msg: XML.Tree): Boolean =
wenzelm@39622
   208
    msg match {
wenzelm@50201
   209
      case XML.Elem(Markup(Markup.TRACING, _), _) => true
wenzelm@50201
   210
      case XML.Elem(Markup(Markup.TRACING_MESSAGE, _), _) => true
wenzelm@39622
   211
      case _ => false
wenzelm@39622
   212
    }
wenzelm@39622
   213
wenzelm@52650
   214
  def is_writeln_markup(msg: XML.Tree, name: String): Boolean =
wenzelm@50500
   215
    msg match {
wenzelm@50500
   216
      case XML.Elem(Markup(Markup.WRITELN, _),
wenzelm@52650
   217
        List(XML.Elem(markup, _))) => markup.name == name
wenzelm@50500
   218
      case XML.Elem(Markup(Markup.WRITELN_MESSAGE, _),
wenzelm@52650
   219
        List(XML.Elem(markup, _))) => markup.name == name
wenzelm@50500
   220
      case _ => false
wenzelm@50500
   221
    }
wenzelm@50500
   222
wenzelm@52650
   223
  def is_state(msg: XML.Tree): Boolean = is_writeln_markup(msg, Markup.STATE)
wenzelm@52650
   224
  def is_information(msg: XML.Tree): Boolean = is_writeln_markup(msg, Markup.INFORMATION)
wenzelm@52650
   225
wenzelm@39511
   226
  def is_warning(msg: XML.Tree): Boolean =
wenzelm@39511
   227
    msg match {
wenzelm@50201
   228
      case XML.Elem(Markup(Markup.WARNING, _), _) => true
wenzelm@50201
   229
      case XML.Elem(Markup(Markup.WARNING_MESSAGE, _), _) => true
wenzelm@39511
   230
      case _ => false
wenzelm@39511
   231
    }
wenzelm@39511
   232
wenzelm@39511
   233
  def is_error(msg: XML.Tree): Boolean =
wenzelm@39511
   234
    msg match {
wenzelm@50201
   235
      case XML.Elem(Markup(Markup.ERROR, _), _) => true
wenzelm@50201
   236
      case XML.Elem(Markup(Markup.ERROR_MESSAGE, _), _) => true
wenzelm@39511
   237
      case _ => false
wenzelm@39511
   238
    }
wenzelm@38887
   239
wenzelm@50500
   240
wenzelm@50500
   241
  /* dialogs */
wenzelm@50500
   242
wenzelm@50500
   243
  object Dialog_Args
wenzelm@50500
   244
  {
wenzelm@52531
   245
    def unapply(props: Properties.T): Option[(Document_ID.Generic, Long, String)] =
wenzelm@50500
   246
      (props, props, props) match {
wenzelm@50500
   247
        case (Position.Id(id), Markup.Serial(serial), Markup.Result(result)) =>
wenzelm@50500
   248
          Some((id, serial, result))
wenzelm@50500
   249
        case _ => None
wenzelm@50500
   250
      }
wenzelm@50500
   251
  }
wenzelm@50500
   252
wenzelm@50500
   253
  object Dialog
wenzelm@50500
   254
  {
wenzelm@52531
   255
    def unapply(tree: XML.Tree): Option[(Document_ID.Generic, Long, String)] =
wenzelm@50500
   256
      tree match {
wenzelm@50500
   257
        case XML.Elem(Markup(Markup.DIALOG, Dialog_Args(id, serial, result)), _) =>
wenzelm@50500
   258
          Some((id, serial, result))
wenzelm@50500
   259
        case _ => None
wenzelm@50500
   260
      }
wenzelm@50500
   261
  }
wenzelm@50500
   262
wenzelm@50500
   263
  object Dialog_Result
wenzelm@50500
   264
  {
wenzelm@52531
   265
    def apply(id: Document_ID.Generic, serial: Long, result: String): XML.Elem =
wenzelm@50501
   266
    {
wenzelm@50501
   267
      val props = Position.Id(id) ::: Markup.Serial(serial)
wenzelm@50501
   268
      XML.Elem(Markup(Markup.RESULT, props), List(XML.Text(result)))
wenzelm@50501
   269
    }
wenzelm@50500
   270
wenzelm@50501
   271
    def unapply(tree: XML.Tree): Option[String] =
wenzelm@50500
   272
      tree match {
wenzelm@50501
   273
        case XML.Elem(Markup(Markup.RESULT, _), List(XML.Text(result))) => Some(result)
wenzelm@50500
   274
        case _ => None
wenzelm@50500
   275
      }
wenzelm@50500
   276
  }
wenzelm@39170
   277
wenzelm@39511
   278
wenzelm@39511
   279
  /* reported positions */
wenzelm@39511
   280
wenzelm@55646
   281
  private val position_elements =
wenzelm@55820
   282
    Document.Elements(Markup.BINDING, Markup.ENTITY, Markup.REPORT, Markup.POSITION)
wenzelm@39441
   283
wenzelm@55433
   284
  def message_positions(
wenzelm@56295
   285
    valid_id: Document_ID.Generic => Boolean,
wenzelm@55433
   286
    chunk: Command.Chunk,
wenzelm@55433
   287
    message: XML.Elem): Set[Text.Range] =
wenzelm@38887
   288
  {
wenzelm@55433
   289
    def elem_positions(props: Properties.T, set: Set[Text.Range]): Set[Text.Range] =
wenzelm@55433
   290
      props match {
wenzelm@55884
   291
        case Position.Reported(id, file_name, symbol_range)
wenzelm@56295
   292
        if valid_id(id) && file_name == chunk.file_name =>
wenzelm@55884
   293
          chunk.incorporate(symbol_range) match {
wenzelm@55822
   294
            case Some(range) => set + range
wenzelm@55548
   295
            case _ => set
wenzelm@55548
   296
          }
wenzelm@55433
   297
        case _ => set
wenzelm@55433
   298
      }
wenzelm@50157
   299
wenzelm@39441
   300
    def positions(set: Set[Text.Range], tree: XML.Tree): Set[Text.Range] =
wenzelm@38887
   301
      tree match {
wenzelm@55433
   302
        case XML.Wrapped_Elem(Markup(name, props), _, body) =>
wenzelm@55646
   303
          body.foldLeft(if (position_elements(name)) elem_positions(props, set) else set)(positions)
wenzelm@55433
   304
        case XML.Elem(Markup(name, props), body) =>
wenzelm@55646
   305
          body.foldLeft(if (position_elements(name)) elem_positions(props, set) else set)(positions)
wenzelm@55433
   306
        case XML.Text(_) => set
wenzelm@38887
   307
      }
wenzelm@50157
   308
wenzelm@39441
   309
    val set = positions(Set.empty, message)
wenzelm@55433
   310
    if (set.isEmpty) elem_positions(message.markup.properties, set)
wenzelm@39042
   311
    else set
wenzelm@38887
   312
  }
wenzelm@38412
   313
}
wenzelm@38412
   314
wenzelm@38412
   315
wenzelm@45709
   316
trait Protocol extends Isabelle_Process
wenzelm@38412
   317
{
wenzelm@54519
   318
  /* inlined files */
wenzelm@54519
   319
wenzelm@56335
   320
  def define_blob(digest: SHA1.Digest, bytes: Bytes): Unit =
wenzelm@56335
   321
    protocol_command_raw("Document.define_blob", Bytes(digest.toString), bytes)
wenzelm@54519
   322
wenzelm@54519
   323
wenzelm@38412
   324
  /* commands */
wenzelm@38412
   325
wenzelm@44644
   326
  def define_command(command: Command): Unit =
wenzelm@54519
   327
  {
wenzelm@54519
   328
    val blobs_yxml =
wenzelm@54519
   329
    { import XML.Encode._
wenzelm@54519
   330
      val encode_blob: T[Command.Blob] =
wenzelm@54519
   331
        variant(List(
wenzelm@54526
   332
          { case Exn.Res((a, b)) =>
wenzelm@55431
   333
              (Nil, pair(string, option(string))((a.node, b.map(p => p._1.toString)))) },
wenzelm@54526
   334
          { case Exn.Exn(e) => (Nil, string(Exn.message(e))) }))
wenzelm@54519
   335
      YXML.string_of_body(list(encode_blob)(command.blobs))
wenzelm@54519
   336
    }
wenzelm@52582
   337
    protocol_command("Document.define_command",
wenzelm@54519
   338
      Document_ID(command.id), encode(command.name), blobs_yxml, encode(command.source))
wenzelm@54519
   339
  }
wenzelm@38412
   340
wenzelm@38412
   341
wenzelm@52931
   342
  /* execution */
wenzelm@52931
   343
wenzelm@52931
   344
  def discontinue_execution(): Unit =
wenzelm@52931
   345
    protocol_command("Document.discontinue_execution")
wenzelm@38412
   346
wenzelm@52931
   347
  def cancel_exec(id: Document_ID.Exec): Unit =
wenzelm@52931
   348
    protocol_command("Document.cancel_exec", Document_ID(id))
wenzelm@52931
   349
wenzelm@52931
   350
wenzelm@52931
   351
  /* document versions */
wenzelm@47343
   352
wenzelm@52530
   353
  def update(old_id: Document_ID.Version, new_id: Document_ID.Version,
wenzelm@44383
   354
    edits: List[Document.Edit_Command])
wenzelm@38412
   355
  {
wenzelm@44157
   356
    val edits_yxml =
wenzelm@43767
   357
    { import XML.Encode._
wenzelm@44383
   358
      def id: T[Command] = (cmd => long(cmd.id))
wenzelm@46737
   359
      def encode_edit(name: Document.Node.Name)
wenzelm@52849
   360
          : T[Document.Node.Edit[Command.Edit, Command.Perspective]] =
wenzelm@44979
   361
        variant(List(
wenzelm@44979
   362
          { case Document.Node.Edits(a) => (Nil, list(pair(option(id), option(id)))(a)) },
wenzelm@48707
   363
          { case Document.Node.Deps(header) =>
wenzelm@54515
   364
              val master_dir = Isabelle_System.posix_path(name.master_dir)
wenzelm@48707
   365
              val imports = header.imports.map(_.node)
wenzelm@50128
   366
              val keywords = header.keywords.map({ case (a, b, _) => (a, b) })
wenzelm@44979
   367
              (Nil,
wenzelm@48707
   368
                pair(Encode.string, pair(Encode.string, pair(list(Encode.string),
wenzelm@48864
   369
                  pair(list(pair(Encode.string,
wenzelm@48864
   370
                    option(pair(pair(Encode.string, list(Encode.string)), list(Encode.string))))),
wenzelm@51294
   371
                  list(Encode.string)))))(
wenzelm@54515
   372
                (master_dir, (name.theory, (imports, (keywords, header.errors)))))) },
wenzelm@52849
   373
          { case Document.Node.Perspective(a, b, c) =>
wenzelm@52849
   374
              (bool_atom(a) :: b.commands.map(cmd => long_atom(cmd.id)),
wenzelm@52862
   375
                list(pair(id, pair(Encode.string, list(Encode.string))))(c.dest)) }))
wenzelm@48705
   376
      def encode_edits: T[List[Document.Edit_Command]] = list((node_edit: Document.Edit_Command) =>
wenzelm@44979
   377
      {
wenzelm@44979
   378
        val (name, edit) = node_edit
wenzelm@46737
   379
        pair(string, encode_edit(name))(name.node, edit)
wenzelm@44979
   380
      })
wenzelm@48705
   381
      YXML.string_of_body(encode_edits(edits)) }
wenzelm@52582
   382
    protocol_command("Document.update", Document_ID(old_id), Document_ID(new_id), edits_yxml)
wenzelm@38412
   383
  }
wenzelm@43748
   384
wenzelm@44673
   385
  def remove_versions(versions: List[Document.Version])
wenzelm@44673
   386
  {
wenzelm@44673
   387
    val versions_yxml =
wenzelm@44673
   388
      { import XML.Encode._
wenzelm@44673
   389
        YXML.string_of_body(list(long)(versions.map(_.id))) }
wenzelm@52582
   390
    protocol_command("Document.remove_versions", versions_yxml)
wenzelm@44673
   391
  }
wenzelm@44673
   392
wenzelm@43748
   393
wenzelm@50498
   394
  /* dialog via document content */
wenzelm@50498
   395
wenzelm@52931
   396
  def dialog_result(serial: Long, result: String): Unit =
wenzelm@52582
   397
    protocol_command("Document.dialog_result", Properties.Value.Long(serial), result)
wenzelm@38412
   398
}