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