src/Pure/PIDE/protocol.scala
author wenzelm
Tue Feb 11 15:05:13 2014 +0100 (2014-02-11)
changeset 55429 4a50f9e70dc1
parent 54562 301a721af68b
child 55431 e0f20a44ff9d
permissions -rw-r--r--
report (but ignore) markup within auxiliary files;
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
  val command_status_markup: Set[String] =
wenzelm@50201
    67
    Set(Markup.ACCEPTED, Markup.FORKED, Markup.JOINED, Markup.RUNNING,
wenzelm@50201
    68
      Markup.FINISHED, Markup.FAILED)
wenzelm@46166
    69
wenzelm@46166
    70
  def command_status(status: Status, markup: Markup): Status =
wenzelm@46166
    71
    markup match {
wenzelm@50201
    72
      case Markup(Markup.ACCEPTED, _) => status.copy(accepted = true)
wenzelm@50201
    73
      case Markup(Markup.FORKED, _) => status.copy(touched = true, forks = status.forks + 1)
wenzelm@50201
    74
      case Markup(Markup.JOINED, _) => status.copy(forks = status.forks - 1)
wenzelm@50201
    75
      case Markup(Markup.RUNNING, _) => status.copy(touched = true, runs = status.runs + 1)
wenzelm@50201
    76
      case Markup(Markup.FINISHED, _) => status.copy(runs = status.runs - 1)
wenzelm@50201
    77
      case Markup(Markup.FAILED, _) => status.copy(failed = true)
wenzelm@46166
    78
      case _ => status
wenzelm@38567
    79
    }
wenzelm@46166
    80
wenzelm@46166
    81
  def command_status(markups: List[Markup]): Status =
wenzelm@46227
    82
    (Status.init /: markups)(command_status(_, _))
wenzelm@38887
    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@44613
   117
    node.commands.foreach(command =>
wenzelm@46166
   118
      {
wenzelm@46688
   119
        val st = state.command_state(version, command)
wenzelm@46688
   120
        val status = command_status(st.status)
wenzelm@46166
   121
        if (status.is_running) running += 1
wenzelm@46688
   122
        else if (status.is_finished) {
wenzelm@50507
   123
          if (st.results.entries.exists(p => is_warning(p._2))) warned += 1
wenzelm@46688
   124
          else finished += 1
wenzelm@46688
   125
        }
wenzelm@46166
   126
        else if (status.is_failed) failed += 1
wenzelm@46166
   127
        else unprocessed += 1
wenzelm@44613
   128
      })
wenzelm@46688
   129
    Node_Status(unprocessed, running, finished, warned, failed)
wenzelm@44613
   130
  }
wenzelm@44613
   131
wenzelm@38887
   132
wenzelm@51533
   133
  /* node timing */
wenzelm@51533
   134
wenzelm@51533
   135
  sealed case class Node_Timing(total: Double, commands: Map[Command, Double])
wenzelm@51533
   136
wenzelm@51533
   137
  val empty_node_timing = Node_Timing(0.0, Map.empty)
wenzelm@51533
   138
wenzelm@51533
   139
  def node_timing(
wenzelm@51533
   140
    state: Document.State,
wenzelm@51533
   141
    version: Document.Version,
wenzelm@51533
   142
    node: Document.Node,
wenzelm@51533
   143
    threshold: Double): Node_Timing =
wenzelm@51533
   144
  {
wenzelm@51533
   145
    var total = 0.0
wenzelm@51533
   146
    var commands = Map.empty[Command, Double]
wenzelm@51533
   147
    for {
wenzelm@51533
   148
      command <- node.commands.iterator
wenzelm@51533
   149
      st = state.command_state(version, command)
wenzelm@51533
   150
      command_timing =
wenzelm@51533
   151
        (0.0 /: st.status)({
wenzelm@51533
   152
          case (timing, Markup.Timing(t)) => timing + t.elapsed.seconds
wenzelm@51533
   153
          case (timing, _) => timing
wenzelm@51533
   154
        })
wenzelm@51533
   155
    } {
wenzelm@51533
   156
      total += command_timing
wenzelm@51533
   157
      if (command_timing >= threshold) commands += (command -> command_timing)
wenzelm@51533
   158
    }
wenzelm@51533
   159
    Node_Timing(total, commands)
wenzelm@51533
   160
  }
wenzelm@51533
   161
wenzelm@51533
   162
wenzelm@39441
   163
  /* result messages */
wenzelm@39439
   164
wenzelm@50450
   165
  private val clean = Set(Markup.REPORT, Markup.NO_REPORT)
wenzelm@50450
   166
wenzelm@39439
   167
  def clean_message(body: XML.Body): XML.Body =
wenzelm@49445
   168
    body filter {
wenzelm@50450
   169
      case XML.Wrapped_Elem(Markup(name, _), _, _) => !clean(name)
wenzelm@50450
   170
      case XML.Elem(Markup(name, _), _) => !clean(name)
wenzelm@49445
   171
      case _ => true
wenzelm@49650
   172
    } map {
wenzelm@49650
   173
      case XML.Wrapped_Elem(markup, body, ts) => XML.Wrapped_Elem(markup, body, clean_message(ts))
wenzelm@49650
   174
      case XML.Elem(markup, ts) => XML.Elem(markup, clean_message(ts))
wenzelm@49650
   175
      case t => t
wenzelm@49650
   176
    }
wenzelm@39439
   177
wenzelm@49445
   178
  def message_reports(props: Properties.T, body: XML.Body): List[XML.Elem] =
wenzelm@49445
   179
    body flatMap {
wenzelm@50450
   180
      case XML.Wrapped_Elem(Markup(Markup.REPORT, ps), body, ts) =>
wenzelm@50450
   181
        List(XML.Wrapped_Elem(Markup(Markup.REPORT, props ::: ps), body, ts))
wenzelm@50201
   182
      case XML.Elem(Markup(Markup.REPORT, ps), ts) =>
wenzelm@50201
   183
        List(XML.Elem(Markup(Markup.REPORT, props ::: ps), ts))
wenzelm@49650
   184
      case XML.Wrapped_Elem(_, _, ts) => message_reports(props, ts)
wenzelm@49445
   185
      case XML.Elem(_, ts) => message_reports(props, ts)
wenzelm@39441
   186
      case XML.Text(_) => Nil
wenzelm@39441
   187
    }
wenzelm@39441
   188
wenzelm@39439
   189
wenzelm@39511
   190
  /* specific messages */
wenzelm@39511
   191
wenzelm@50500
   192
  def is_inlined(msg: XML.Tree): Boolean =
wenzelm@50500
   193
    !(is_result(msg) || is_tracing(msg) || is_state(msg))
wenzelm@50500
   194
wenzelm@50500
   195
  def is_result(msg: XML.Tree): Boolean =
wenzelm@50500
   196
    msg match {
wenzelm@50500
   197
      case XML.Elem(Markup(Markup.RESULT, _), _) => true
wenzelm@50500
   198
      case _ => false
wenzelm@50500
   199
    }
wenzelm@50500
   200
wenzelm@50157
   201
  def is_tracing(msg: XML.Tree): Boolean =
wenzelm@39622
   202
    msg match {
wenzelm@50201
   203
      case XML.Elem(Markup(Markup.TRACING, _), _) => true
wenzelm@50201
   204
      case XML.Elem(Markup(Markup.TRACING_MESSAGE, _), _) => true
wenzelm@39622
   205
      case _ => false
wenzelm@39622
   206
    }
wenzelm@39622
   207
wenzelm@52650
   208
  def is_writeln_markup(msg: XML.Tree, name: String): Boolean =
wenzelm@50500
   209
    msg match {
wenzelm@50500
   210
      case XML.Elem(Markup(Markup.WRITELN, _),
wenzelm@52650
   211
        List(XML.Elem(markup, _))) => markup.name == name
wenzelm@50500
   212
      case XML.Elem(Markup(Markup.WRITELN_MESSAGE, _),
wenzelm@52650
   213
        List(XML.Elem(markup, _))) => markup.name == name
wenzelm@50500
   214
      case _ => false
wenzelm@50500
   215
    }
wenzelm@50500
   216
wenzelm@52650
   217
  def is_state(msg: XML.Tree): Boolean = is_writeln_markup(msg, Markup.STATE)
wenzelm@52650
   218
  def is_information(msg: XML.Tree): Boolean = is_writeln_markup(msg, Markup.INFORMATION)
wenzelm@52650
   219
wenzelm@39511
   220
  def is_warning(msg: XML.Tree): Boolean =
wenzelm@39511
   221
    msg match {
wenzelm@50201
   222
      case XML.Elem(Markup(Markup.WARNING, _), _) => true
wenzelm@50201
   223
      case XML.Elem(Markup(Markup.WARNING_MESSAGE, _), _) => true
wenzelm@39511
   224
      case _ => false
wenzelm@39511
   225
    }
wenzelm@39511
   226
wenzelm@39511
   227
  def is_error(msg: XML.Tree): Boolean =
wenzelm@39511
   228
    msg match {
wenzelm@50201
   229
      case XML.Elem(Markup(Markup.ERROR, _), _) => true
wenzelm@50201
   230
      case XML.Elem(Markup(Markup.ERROR_MESSAGE, _), _) => true
wenzelm@39511
   231
      case _ => false
wenzelm@39511
   232
    }
wenzelm@38887
   233
wenzelm@50500
   234
wenzelm@50500
   235
  /* dialogs */
wenzelm@50500
   236
wenzelm@50500
   237
  object Dialog_Args
wenzelm@50500
   238
  {
wenzelm@52531
   239
    def unapply(props: Properties.T): Option[(Document_ID.Generic, Long, String)] =
wenzelm@50500
   240
      (props, props, props) match {
wenzelm@50500
   241
        case (Position.Id(id), Markup.Serial(serial), Markup.Result(result)) =>
wenzelm@50500
   242
          Some((id, serial, result))
wenzelm@50500
   243
        case _ => None
wenzelm@50500
   244
      }
wenzelm@50500
   245
  }
wenzelm@50500
   246
wenzelm@50500
   247
  object Dialog
wenzelm@50500
   248
  {
wenzelm@52531
   249
    def unapply(tree: XML.Tree): Option[(Document_ID.Generic, Long, String)] =
wenzelm@50500
   250
      tree match {
wenzelm@50500
   251
        case XML.Elem(Markup(Markup.DIALOG, Dialog_Args(id, serial, result)), _) =>
wenzelm@50500
   252
          Some((id, serial, result))
wenzelm@50500
   253
        case _ => None
wenzelm@50500
   254
      }
wenzelm@50500
   255
  }
wenzelm@50500
   256
wenzelm@50500
   257
  object Dialog_Result
wenzelm@50500
   258
  {
wenzelm@52531
   259
    def apply(id: Document_ID.Generic, serial: Long, result: String): XML.Elem =
wenzelm@50501
   260
    {
wenzelm@50501
   261
      val props = Position.Id(id) ::: Markup.Serial(serial)
wenzelm@50501
   262
      XML.Elem(Markup(Markup.RESULT, props), List(XML.Text(result)))
wenzelm@50501
   263
    }
wenzelm@50500
   264
wenzelm@50501
   265
    def unapply(tree: XML.Tree): Option[String] =
wenzelm@50500
   266
      tree match {
wenzelm@50501
   267
        case XML.Elem(Markup(Markup.RESULT, _), List(XML.Text(result))) => Some(result)
wenzelm@50500
   268
        case _ => None
wenzelm@50500
   269
      }
wenzelm@50500
   270
  }
wenzelm@39170
   271
wenzelm@39511
   272
wenzelm@39511
   273
  /* reported positions */
wenzelm@39511
   274
wenzelm@50201
   275
  private val include_pos = Set(Markup.BINDING, Markup.ENTITY, Markup.REPORT, Markup.POSITION)
wenzelm@39441
   276
wenzelm@39441
   277
  def message_positions(command: Command, message: XML.Elem): Set[Text.Range] =
wenzelm@38887
   278
  {
wenzelm@49650
   279
    def elem_positions(raw_range: Text.Range, set: Set[Text.Range], body: XML.Body)
wenzelm@49650
   280
      : Set[Text.Range] =
wenzelm@49650
   281
    {
wenzelm@49650
   282
      val range = command.decode(raw_range).restrict(command.range)
wenzelm@49650
   283
      body.foldLeft(if (range.is_singularity) set else set + range)(positions)
wenzelm@49650
   284
    }
wenzelm@50157
   285
wenzelm@39441
   286
    def positions(set: Set[Text.Range], tree: XML.Tree): Set[Text.Range] =
wenzelm@38887
   287
      tree match {
wenzelm@55429
   288
        case XML.Wrapped_Elem(Markup(name, Position.Reported(id, file, range)), _, body)
wenzelm@55429
   289
        if include_pos(name) && id == command.id && file == "" => elem_positions(range, set, body)
wenzelm@49650
   290
wenzelm@55429
   291
        case XML.Elem(Markup(name, Position.Reported(id, file, range)), body)
wenzelm@55429
   292
        if include_pos(name) && id == command.id && file == "" => elem_positions(range, set, body)
wenzelm@49650
   293
wenzelm@49650
   294
        case XML.Wrapped_Elem(_, _, body) => body.foldLeft(set)(positions)
wenzelm@49650
   295
wenzelm@49650
   296
        case XML.Elem(_, body) => body.foldLeft(set)(positions)
wenzelm@49650
   297
wenzelm@39042
   298
        case _ => set
wenzelm@38887
   299
      }
wenzelm@50157
   300
wenzelm@39441
   301
    val set = positions(Set.empty, message)
wenzelm@50500
   302
    if (set.isEmpty)
wenzelm@39172
   303
      set ++ Position.Range.unapply(message.markup.properties).map(command.decode(_))
wenzelm@39042
   304
    else set
wenzelm@38887
   305
  }
wenzelm@38412
   306
}
wenzelm@38412
   307
wenzelm@38412
   308
wenzelm@45709
   309
trait Protocol extends Isabelle_Process
wenzelm@38412
   310
{
wenzelm@54519
   311
  /* inlined files */
wenzelm@54519
   312
wenzelm@54519
   313
  def define_blob(blob: Bytes): Unit =
wenzelm@54519
   314
    protocol_command_raw("Document.define_blob", Bytes(blob.sha1_digest.toString), blob)
wenzelm@54519
   315
wenzelm@54519
   316
wenzelm@38412
   317
  /* commands */
wenzelm@38412
   318
wenzelm@44644
   319
  def define_command(command: Command): Unit =
wenzelm@54519
   320
  {
wenzelm@54519
   321
    val blobs_yxml =
wenzelm@54519
   322
    { import XML.Encode._
wenzelm@54519
   323
      val encode_blob: T[Command.Blob] =
wenzelm@54519
   324
        variant(List(
wenzelm@54526
   325
          { case Exn.Res((a, b)) =>
wenzelm@54526
   326
              (Nil, pair(string, option(string))((a.node, b.map(_.toString)))) },
wenzelm@54526
   327
          { case Exn.Exn(e) => (Nil, string(Exn.message(e))) }))
wenzelm@54519
   328
      YXML.string_of_body(list(encode_blob)(command.blobs))
wenzelm@54519
   329
    }
wenzelm@52582
   330
    protocol_command("Document.define_command",
wenzelm@54519
   331
      Document_ID(command.id), encode(command.name), blobs_yxml, encode(command.source))
wenzelm@54519
   332
  }
wenzelm@38412
   333
wenzelm@38412
   334
wenzelm@52931
   335
  /* execution */
wenzelm@52931
   336
wenzelm@52931
   337
  def discontinue_execution(): Unit =
wenzelm@52931
   338
    protocol_command("Document.discontinue_execution")
wenzelm@38412
   339
wenzelm@52931
   340
  def cancel_exec(id: Document_ID.Exec): Unit =
wenzelm@52931
   341
    protocol_command("Document.cancel_exec", Document_ID(id))
wenzelm@52931
   342
wenzelm@52931
   343
wenzelm@52931
   344
  /* document versions */
wenzelm@47343
   345
wenzelm@52530
   346
  def update(old_id: Document_ID.Version, new_id: Document_ID.Version,
wenzelm@44383
   347
    edits: List[Document.Edit_Command])
wenzelm@38412
   348
  {
wenzelm@44157
   349
    val edits_yxml =
wenzelm@43767
   350
    { import XML.Encode._
wenzelm@44383
   351
      def id: T[Command] = (cmd => long(cmd.id))
wenzelm@46737
   352
      def encode_edit(name: Document.Node.Name)
wenzelm@52849
   353
          : T[Document.Node.Edit[Command.Edit, Command.Perspective]] =
wenzelm@44979
   354
        variant(List(
wenzelm@44979
   355
          { case Document.Node.Edits(a) => (Nil, list(pair(option(id), option(id)))(a)) },
wenzelm@48707
   356
          { case Document.Node.Deps(header) =>
wenzelm@54515
   357
              val master_dir = Isabelle_System.posix_path(name.master_dir)
wenzelm@48707
   358
              val imports = header.imports.map(_.node)
wenzelm@50128
   359
              val keywords = header.keywords.map({ case (a, b, _) => (a, b) })
wenzelm@44979
   360
              (Nil,
wenzelm@48707
   361
                pair(Encode.string, pair(Encode.string, pair(list(Encode.string),
wenzelm@48864
   362
                  pair(list(pair(Encode.string,
wenzelm@48864
   363
                    option(pair(pair(Encode.string, list(Encode.string)), list(Encode.string))))),
wenzelm@51294
   364
                  list(Encode.string)))))(
wenzelm@54515
   365
                (master_dir, (name.theory, (imports, (keywords, header.errors)))))) },
wenzelm@52849
   366
          { case Document.Node.Perspective(a, b, c) =>
wenzelm@52849
   367
              (bool_atom(a) :: b.commands.map(cmd => long_atom(cmd.id)),
wenzelm@52862
   368
                list(pair(id, pair(Encode.string, list(Encode.string))))(c.dest)) }))
wenzelm@48705
   369
      def encode_edits: T[List[Document.Edit_Command]] = list((node_edit: Document.Edit_Command) =>
wenzelm@44979
   370
      {
wenzelm@44979
   371
        val (name, edit) = node_edit
wenzelm@46737
   372
        pair(string, encode_edit(name))(name.node, edit)
wenzelm@44979
   373
      })
wenzelm@48705
   374
      YXML.string_of_body(encode_edits(edits)) }
wenzelm@52582
   375
    protocol_command("Document.update", Document_ID(old_id), Document_ID(new_id), edits_yxml)
wenzelm@38412
   376
  }
wenzelm@43748
   377
wenzelm@44673
   378
  def remove_versions(versions: List[Document.Version])
wenzelm@44673
   379
  {
wenzelm@44673
   380
    val versions_yxml =
wenzelm@44673
   381
      { import XML.Encode._
wenzelm@44673
   382
        YXML.string_of_body(list(long)(versions.map(_.id))) }
wenzelm@52582
   383
    protocol_command("Document.remove_versions", versions_yxml)
wenzelm@44673
   384
  }
wenzelm@44673
   385
wenzelm@43748
   386
wenzelm@50498
   387
  /* dialog via document content */
wenzelm@50498
   388
wenzelm@52931
   389
  def dialog_result(serial: Long, result: String): Unit =
wenzelm@52582
   390
    protocol_command("Document.dialog_result", Properties.Value.Long(serial), result)
wenzelm@38412
   391
}