src/Pure/PIDE/protocol.scala
author wenzelm
Thu Mar 27 10:43:43 2014 +0100 (2014-03-27)
changeset 56299 8201790fdeb9
parent 56295 a40e67ce4f84
child 56306 0fc032898b05
permissions -rw-r--r--
more careful treatment of multiple command states (eval + prints): merge content that is actually required;
more standard Markup_Tree merge, including trivial cases;
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@46227
    44
  object Status
wenzelm@46227
    45
  {
wenzelm@46227
    46
    val init = Status()
wenzelm@46227
    47
  }
wenzelm@46227
    48
wenzelm@46166
    49
  sealed case class Status(
wenzelm@49036
    50
    private val touched: Boolean = false,
wenzelm@47395
    51
    private val accepted: Boolean = false,
wenzelm@46166
    52
    private val failed: Boolean = false,
wenzelm@49036
    53
    forks: Int = 0,
wenzelm@49036
    54
    runs: Int = 0)
wenzelm@38567
    55
  {
wenzelm@46227
    56
    def + (that: Status): Status =
wenzelm@49036
    57
      Status(touched || that.touched, accepted || that.accepted, failed || that.failed,
wenzelm@49036
    58
        forks + that.forks, runs + that.runs)
wenzelm@49036
    59
wenzelm@49036
    60
    def is_unprocessed: Boolean = accepted && !failed && (!touched || (forks != 0 && runs == 0))
wenzelm@49036
    61
    def is_running: Boolean = runs != 0
wenzelm@49039
    62
    def is_finished: Boolean = !failed && touched && forks == 0 && runs == 0
wenzelm@49036
    63
    def is_failed: Boolean = failed
wenzelm@46166
    64
  }
wenzelm@46166
    65
wenzelm@46166
    66
  def command_status(status: Status, markup: Markup): Status =
wenzelm@46166
    67
    markup match {
wenzelm@50201
    68
      case Markup(Markup.ACCEPTED, _) => status.copy(accepted = true)
wenzelm@50201
    69
      case Markup(Markup.FORKED, _) => status.copy(touched = true, forks = status.forks + 1)
wenzelm@50201
    70
      case Markup(Markup.JOINED, _) => status.copy(forks = status.forks - 1)
wenzelm@50201
    71
      case Markup(Markup.RUNNING, _) => status.copy(touched = true, runs = status.runs + 1)
wenzelm@50201
    72
      case Markup(Markup.FINISHED, _) => status.copy(runs = status.runs - 1)
wenzelm@50201
    73
      case Markup(Markup.FAILED, _) => status.copy(failed = true)
wenzelm@46166
    74
      case _ => status
wenzelm@38567
    75
    }
wenzelm@46166
    76
wenzelm@46166
    77
  def command_status(markups: List[Markup]): Status =
wenzelm@46227
    78
    (Status.init /: markups)(command_status(_, _))
wenzelm@38887
    79
wenzelm@55820
    80
  val command_status_elements =
wenzelm@55820
    81
    Document.Elements(Markup.ACCEPTED, Markup.FORKED, Markup.JOINED, Markup.RUNNING,
wenzelm@55646
    82
      Markup.FINISHED, Markup.FAILED)
wenzelm@55646
    83
wenzelm@55820
    84
  val status_elements =
wenzelm@55646
    85
    command_status_elements + Markup.WARNING + Markup.ERROR
wenzelm@55646
    86
wenzelm@46209
    87
wenzelm@51818
    88
  /* command timing */
wenzelm@51818
    89
wenzelm@51818
    90
  object Command_Timing
wenzelm@51818
    91
  {
wenzelm@52531
    92
    def unapply(props: Properties.T): Option[(Document_ID.Generic, isabelle.Timing)] =
wenzelm@51818
    93
      props match {
wenzelm@51818
    94
        case (Markup.FUNCTION, Markup.COMMAND_TIMING) :: args =>
wenzelm@51818
    95
          (args, args) match {
wenzelm@51818
    96
            case (Position.Id(id), Markup.Timing_Properties(timing)) => Some((id, timing))
wenzelm@51818
    97
            case _ => None
wenzelm@51818
    98
          }
wenzelm@51818
    99
        case _ => None
wenzelm@51818
   100
      }
wenzelm@51818
   101
  }
wenzelm@51818
   102
wenzelm@51818
   103
wenzelm@46209
   104
  /* node status */
wenzelm@46209
   105
wenzelm@46688
   106
  sealed case class Node_Status(
wenzelm@46688
   107
    unprocessed: Int, running: Int, finished: Int, warned: Int, failed: Int)
wenzelm@44866
   108
  {
wenzelm@46688
   109
    def total: Int = unprocessed + running + finished + warned + failed
wenzelm@44866
   110
  }
wenzelm@44613
   111
wenzelm@44613
   112
  def node_status(
wenzelm@44613
   113
    state: Document.State, version: Document.Version, node: Document.Node): Node_Status =
wenzelm@44613
   114
  {
wenzelm@44613
   115
    var unprocessed = 0
wenzelm@44613
   116
    var running = 0
wenzelm@44613
   117
    var finished = 0
wenzelm@46688
   118
    var warned = 0
wenzelm@44613
   119
    var failed = 0
wenzelm@56299
   120
    for {
wenzelm@56299
   121
      command <- node.commands
wenzelm@56299
   122
      st <- state.command_states(version, command)
wenzelm@56299
   123
      status = command_status(st.status)
wenzelm@56299
   124
    } {
wenzelm@56299
   125
      if (status.is_running) running += 1
wenzelm@56299
   126
      else if (status.is_finished) {
wenzelm@56299
   127
        if (st.results.entries.exists(p => is_warning(p._2))) warned += 1
wenzelm@56299
   128
        else finished += 1
wenzelm@56299
   129
      }
wenzelm@56299
   130
      else if (status.is_failed) failed += 1
wenzelm@56299
   131
      else unprocessed += 1
wenzelm@56299
   132
    }
wenzelm@46688
   133
    Node_Status(unprocessed, running, finished, warned, failed)
wenzelm@44613
   134
  }
wenzelm@44613
   135
wenzelm@38887
   136
wenzelm@51533
   137
  /* node timing */
wenzelm@51533
   138
wenzelm@51533
   139
  sealed case class Node_Timing(total: Double, commands: Map[Command, Double])
wenzelm@51533
   140
wenzelm@51533
   141
  val empty_node_timing = Node_Timing(0.0, Map.empty)
wenzelm@51533
   142
wenzelm@51533
   143
  def node_timing(
wenzelm@51533
   144
    state: Document.State,
wenzelm@51533
   145
    version: Document.Version,
wenzelm@51533
   146
    node: Document.Node,
wenzelm@51533
   147
    threshold: Double): Node_Timing =
wenzelm@51533
   148
  {
wenzelm@51533
   149
    var total = 0.0
wenzelm@51533
   150
    var commands = Map.empty[Command, Double]
wenzelm@51533
   151
    for {
wenzelm@51533
   152
      command <- node.commands.iterator
wenzelm@56299
   153
      st <- state.command_states(version, command)
wenzelm@51533
   154
      command_timing =
wenzelm@51533
   155
        (0.0 /: st.status)({
wenzelm@51533
   156
          case (timing, Markup.Timing(t)) => timing + t.elapsed.seconds
wenzelm@51533
   157
          case (timing, _) => timing
wenzelm@51533
   158
        })
wenzelm@51533
   159
    } {
wenzelm@51533
   160
      total += command_timing
wenzelm@51533
   161
      if (command_timing >= threshold) commands += (command -> command_timing)
wenzelm@51533
   162
    }
wenzelm@51533
   163
    Node_Timing(total, commands)
wenzelm@51533
   164
  }
wenzelm@51533
   165
wenzelm@51533
   166
wenzelm@39441
   167
  /* result messages */
wenzelm@39439
   168
wenzelm@55820
   169
  private val clean_elements =
wenzelm@55820
   170
    Document.Elements(Markup.REPORT, Markup.NO_REPORT)
wenzelm@50450
   171
wenzelm@39439
   172
  def clean_message(body: XML.Body): XML.Body =
wenzelm@49445
   173
    body filter {
wenzelm@55646
   174
      case XML.Wrapped_Elem(Markup(name, _), _, _) => !clean_elements(name)
wenzelm@55646
   175
      case XML.Elem(Markup(name, _), _) => !clean_elements(name)
wenzelm@49445
   176
      case _ => true
wenzelm@49650
   177
    } map {
wenzelm@49650
   178
      case XML.Wrapped_Elem(markup, body, ts) => XML.Wrapped_Elem(markup, body, clean_message(ts))
wenzelm@49650
   179
      case XML.Elem(markup, ts) => XML.Elem(markup, clean_message(ts))
wenzelm@49650
   180
      case t => t
wenzelm@49650
   181
    }
wenzelm@39439
   182
wenzelm@49445
   183
  def message_reports(props: Properties.T, body: XML.Body): List[XML.Elem] =
wenzelm@49445
   184
    body flatMap {
wenzelm@50450
   185
      case XML.Wrapped_Elem(Markup(Markup.REPORT, ps), body, ts) =>
wenzelm@50450
   186
        List(XML.Wrapped_Elem(Markup(Markup.REPORT, props ::: ps), body, ts))
wenzelm@50201
   187
      case XML.Elem(Markup(Markup.REPORT, ps), ts) =>
wenzelm@50201
   188
        List(XML.Elem(Markup(Markup.REPORT, props ::: ps), ts))
wenzelm@49650
   189
      case XML.Wrapped_Elem(_, _, ts) => message_reports(props, ts)
wenzelm@49445
   190
      case XML.Elem(_, ts) => message_reports(props, ts)
wenzelm@39441
   191
      case XML.Text(_) => Nil
wenzelm@39441
   192
    }
wenzelm@39441
   193
wenzelm@39439
   194
wenzelm@39511
   195
  /* specific messages */
wenzelm@39511
   196
wenzelm@50500
   197
  def is_inlined(msg: XML.Tree): Boolean =
wenzelm@50500
   198
    !(is_result(msg) || is_tracing(msg) || is_state(msg))
wenzelm@50500
   199
wenzelm@50500
   200
  def is_result(msg: XML.Tree): Boolean =
wenzelm@50500
   201
    msg match {
wenzelm@50500
   202
      case XML.Elem(Markup(Markup.RESULT, _), _) => true
wenzelm@50500
   203
      case _ => false
wenzelm@50500
   204
    }
wenzelm@50500
   205
wenzelm@50157
   206
  def is_tracing(msg: XML.Tree): Boolean =
wenzelm@39622
   207
    msg match {
wenzelm@50201
   208
      case XML.Elem(Markup(Markup.TRACING, _), _) => true
wenzelm@50201
   209
      case XML.Elem(Markup(Markup.TRACING_MESSAGE, _), _) => true
wenzelm@39622
   210
      case _ => false
wenzelm@39622
   211
    }
wenzelm@39622
   212
wenzelm@52650
   213
  def is_writeln_markup(msg: XML.Tree, name: String): Boolean =
wenzelm@50500
   214
    msg match {
wenzelm@50500
   215
      case XML.Elem(Markup(Markup.WRITELN, _),
wenzelm@52650
   216
        List(XML.Elem(markup, _))) => markup.name == name
wenzelm@50500
   217
      case XML.Elem(Markup(Markup.WRITELN_MESSAGE, _),
wenzelm@52650
   218
        List(XML.Elem(markup, _))) => markup.name == name
wenzelm@50500
   219
      case _ => false
wenzelm@50500
   220
    }
wenzelm@50500
   221
wenzelm@52650
   222
  def is_state(msg: XML.Tree): Boolean = is_writeln_markup(msg, Markup.STATE)
wenzelm@52650
   223
  def is_information(msg: XML.Tree): Boolean = is_writeln_markup(msg, Markup.INFORMATION)
wenzelm@52650
   224
wenzelm@39511
   225
  def is_warning(msg: XML.Tree): Boolean =
wenzelm@39511
   226
    msg match {
wenzelm@50201
   227
      case XML.Elem(Markup(Markup.WARNING, _), _) => true
wenzelm@50201
   228
      case XML.Elem(Markup(Markup.WARNING_MESSAGE, _), _) => true
wenzelm@39511
   229
      case _ => false
wenzelm@39511
   230
    }
wenzelm@39511
   231
wenzelm@39511
   232
  def is_error(msg: XML.Tree): Boolean =
wenzelm@39511
   233
    msg match {
wenzelm@50201
   234
      case XML.Elem(Markup(Markup.ERROR, _), _) => true
wenzelm@50201
   235
      case XML.Elem(Markup(Markup.ERROR_MESSAGE, _), _) => true
wenzelm@39511
   236
      case _ => false
wenzelm@39511
   237
    }
wenzelm@38887
   238
wenzelm@50500
   239
wenzelm@50500
   240
  /* dialogs */
wenzelm@50500
   241
wenzelm@50500
   242
  object Dialog_Args
wenzelm@50500
   243
  {
wenzelm@52531
   244
    def unapply(props: Properties.T): Option[(Document_ID.Generic, Long, String)] =
wenzelm@50500
   245
      (props, props, props) match {
wenzelm@50500
   246
        case (Position.Id(id), Markup.Serial(serial), Markup.Result(result)) =>
wenzelm@50500
   247
          Some((id, serial, result))
wenzelm@50500
   248
        case _ => None
wenzelm@50500
   249
      }
wenzelm@50500
   250
  }
wenzelm@50500
   251
wenzelm@50500
   252
  object Dialog
wenzelm@50500
   253
  {
wenzelm@52531
   254
    def unapply(tree: XML.Tree): Option[(Document_ID.Generic, Long, String)] =
wenzelm@50500
   255
      tree match {
wenzelm@50500
   256
        case XML.Elem(Markup(Markup.DIALOG, Dialog_Args(id, serial, result)), _) =>
wenzelm@50500
   257
          Some((id, serial, result))
wenzelm@50500
   258
        case _ => None
wenzelm@50500
   259
      }
wenzelm@50500
   260
  }
wenzelm@50500
   261
wenzelm@50500
   262
  object Dialog_Result
wenzelm@50500
   263
  {
wenzelm@52531
   264
    def apply(id: Document_ID.Generic, serial: Long, result: String): XML.Elem =
wenzelm@50501
   265
    {
wenzelm@50501
   266
      val props = Position.Id(id) ::: Markup.Serial(serial)
wenzelm@50501
   267
      XML.Elem(Markup(Markup.RESULT, props), List(XML.Text(result)))
wenzelm@50501
   268
    }
wenzelm@50500
   269
wenzelm@50501
   270
    def unapply(tree: XML.Tree): Option[String] =
wenzelm@50500
   271
      tree match {
wenzelm@50501
   272
        case XML.Elem(Markup(Markup.RESULT, _), List(XML.Text(result))) => Some(result)
wenzelm@50500
   273
        case _ => None
wenzelm@50500
   274
      }
wenzelm@50500
   275
  }
wenzelm@39170
   276
wenzelm@39511
   277
wenzelm@39511
   278
  /* reported positions */
wenzelm@39511
   279
wenzelm@55646
   280
  private val position_elements =
wenzelm@55820
   281
    Document.Elements(Markup.BINDING, Markup.ENTITY, Markup.REPORT, Markup.POSITION)
wenzelm@39441
   282
wenzelm@55433
   283
  def message_positions(
wenzelm@56295
   284
    valid_id: Document_ID.Generic => Boolean,
wenzelm@55433
   285
    chunk: Command.Chunk,
wenzelm@55433
   286
    message: XML.Elem): Set[Text.Range] =
wenzelm@38887
   287
  {
wenzelm@55433
   288
    def elem_positions(props: Properties.T, set: Set[Text.Range]): Set[Text.Range] =
wenzelm@55433
   289
      props match {
wenzelm@55884
   290
        case Position.Reported(id, file_name, symbol_range)
wenzelm@56295
   291
        if valid_id(id) && file_name == chunk.file_name =>
wenzelm@55884
   292
          chunk.incorporate(symbol_range) match {
wenzelm@55822
   293
            case Some(range) => set + range
wenzelm@55548
   294
            case _ => set
wenzelm@55548
   295
          }
wenzelm@55433
   296
        case _ => set
wenzelm@55433
   297
      }
wenzelm@50157
   298
wenzelm@39441
   299
    def positions(set: Set[Text.Range], tree: XML.Tree): Set[Text.Range] =
wenzelm@38887
   300
      tree match {
wenzelm@55433
   301
        case XML.Wrapped_Elem(Markup(name, props), _, body) =>
wenzelm@55646
   302
          body.foldLeft(if (position_elements(name)) elem_positions(props, set) else set)(positions)
wenzelm@55433
   303
        case XML.Elem(Markup(name, props), body) =>
wenzelm@55646
   304
          body.foldLeft(if (position_elements(name)) elem_positions(props, set) else set)(positions)
wenzelm@55433
   305
        case XML.Text(_) => set
wenzelm@38887
   306
      }
wenzelm@50157
   307
wenzelm@39441
   308
    val set = positions(Set.empty, message)
wenzelm@55433
   309
    if (set.isEmpty) elem_positions(message.markup.properties, set)
wenzelm@39042
   310
    else set
wenzelm@38887
   311
  }
wenzelm@38412
   312
}
wenzelm@38412
   313
wenzelm@38412
   314
wenzelm@45709
   315
trait Protocol extends Isabelle_Process
wenzelm@38412
   316
{
wenzelm@54519
   317
  /* inlined files */
wenzelm@54519
   318
wenzelm@55783
   319
  def define_blob(blob: Document.Blob): Unit =
wenzelm@55783
   320
    protocol_command_raw(
wenzelm@55783
   321
      "Document.define_blob", Bytes(blob.bytes.sha1_digest.toString), blob.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
}