src/Pure/PIDE/protocol.scala
author wenzelm
Tue Mar 26 11:26:13 2013 +0100 (2013-03-26)
changeset 51533 3f6280aedbcc
parent 51294 0850d43cb355
child 51818 517f232e867d
permissions -rw-r--r--
dockable window for timing information;
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@44476
    14
  object Assign
wenzelm@44476
    15
  {
wenzelm@44661
    16
    def unapply(text: String): Option[(Document.Version_ID, Document.Assign)] =
wenzelm@44661
    17
      try {
wenzelm@44661
    18
        import XML.Decode._
wenzelm@44661
    19
        val body = YXML.parse_body(text)
wenzelm@47388
    20
        Some(pair(long, list(pair(long, option(long))))(body))
wenzelm@44661
    21
      }
wenzelm@44661
    22
      catch {
wenzelm@44661
    23
        case ERROR(_) => None
wenzelm@44661
    24
        case _: XML.XML_Atom => None
wenzelm@44661
    25
        case _: XML.XML_Body => None
wenzelm@38412
    26
      }
wenzelm@38412
    27
  }
wenzelm@38567
    28
wenzelm@44676
    29
  object Removed
wenzelm@44676
    30
  {
wenzelm@44676
    31
    def unapply(text: String): Option[List[Document.Version_ID]] =
wenzelm@44676
    32
      try {
wenzelm@44676
    33
        import XML.Decode._
wenzelm@44676
    34
        Some(list(long)(YXML.parse_body(text)))
wenzelm@44676
    35
      }
wenzelm@44676
    36
      catch {
wenzelm@44676
    37
        case ERROR(_) => None
wenzelm@44676
    38
        case _: XML.XML_Atom => None
wenzelm@44676
    39
        case _: XML.XML_Body => None
wenzelm@44676
    40
      }
wenzelm@44676
    41
  }
wenzelm@44676
    42
wenzelm@38567
    43
wenzelm@46209
    44
  /* command status */
wenzelm@38567
    45
wenzelm@46227
    46
  object Status
wenzelm@46227
    47
  {
wenzelm@46227
    48
    val init = Status()
wenzelm@46227
    49
  }
wenzelm@46227
    50
wenzelm@46166
    51
  sealed case class Status(
wenzelm@49036
    52
    private val touched: Boolean = false,
wenzelm@47395
    53
    private val accepted: Boolean = false,
wenzelm@46166
    54
    private val failed: Boolean = false,
wenzelm@49036
    55
    forks: Int = 0,
wenzelm@49036
    56
    runs: Int = 0)
wenzelm@38567
    57
  {
wenzelm@46227
    58
    def + (that: Status): Status =
wenzelm@49036
    59
      Status(touched || that.touched, accepted || that.accepted, failed || that.failed,
wenzelm@49036
    60
        forks + that.forks, runs + that.runs)
wenzelm@49036
    61
wenzelm@49036
    62
    def is_unprocessed: Boolean = accepted && !failed && (!touched || (forks != 0 && runs == 0))
wenzelm@49036
    63
    def is_running: Boolean = runs != 0
wenzelm@49039
    64
    def is_finished: Boolean = !failed && touched && forks == 0 && runs == 0
wenzelm@49036
    65
    def is_failed: Boolean = failed
wenzelm@46166
    66
  }
wenzelm@46166
    67
wenzelm@46166
    68
  val command_status_markup: Set[String] =
wenzelm@50201
    69
    Set(Markup.ACCEPTED, Markup.FORKED, Markup.JOINED, Markup.RUNNING,
wenzelm@50201
    70
      Markup.FINISHED, Markup.FAILED)
wenzelm@46166
    71
wenzelm@46166
    72
  def command_status(status: Status, markup: Markup): Status =
wenzelm@46166
    73
    markup match {
wenzelm@50201
    74
      case Markup(Markup.ACCEPTED, _) => status.copy(accepted = true)
wenzelm@50201
    75
      case Markup(Markup.FORKED, _) => status.copy(touched = true, forks = status.forks + 1)
wenzelm@50201
    76
      case Markup(Markup.JOINED, _) => status.copy(forks = status.forks - 1)
wenzelm@50201
    77
      case Markup(Markup.RUNNING, _) => status.copy(touched = true, runs = status.runs + 1)
wenzelm@50201
    78
      case Markup(Markup.FINISHED, _) => status.copy(runs = status.runs - 1)
wenzelm@50201
    79
      case Markup(Markup.FAILED, _) => status.copy(failed = true)
wenzelm@46166
    80
      case _ => status
wenzelm@38567
    81
    }
wenzelm@46166
    82
wenzelm@46166
    83
  def command_status(markups: List[Markup]): Status =
wenzelm@46227
    84
    (Status.init /: markups)(command_status(_, _))
wenzelm@38887
    85
wenzelm@46209
    86
wenzelm@46209
    87
  /* node status */
wenzelm@46209
    88
wenzelm@46688
    89
  sealed case class Node_Status(
wenzelm@46688
    90
    unprocessed: Int, running: Int, finished: Int, warned: Int, failed: Int)
wenzelm@44866
    91
  {
wenzelm@46688
    92
    def total: Int = unprocessed + running + finished + warned + failed
wenzelm@44866
    93
  }
wenzelm@44613
    94
wenzelm@44613
    95
  def node_status(
wenzelm@44613
    96
    state: Document.State, version: Document.Version, node: Document.Node): Node_Status =
wenzelm@44613
    97
  {
wenzelm@44613
    98
    var unprocessed = 0
wenzelm@44613
    99
    var running = 0
wenzelm@44613
   100
    var finished = 0
wenzelm@46688
   101
    var warned = 0
wenzelm@44613
   102
    var failed = 0
wenzelm@44613
   103
    node.commands.foreach(command =>
wenzelm@46166
   104
      {
wenzelm@46688
   105
        val st = state.command_state(version, command)
wenzelm@46688
   106
        val status = command_status(st.status)
wenzelm@46166
   107
        if (status.is_running) running += 1
wenzelm@46688
   108
        else if (status.is_finished) {
wenzelm@50507
   109
          if (st.results.entries.exists(p => is_warning(p._2))) warned += 1
wenzelm@46688
   110
          else finished += 1
wenzelm@46688
   111
        }
wenzelm@46166
   112
        else if (status.is_failed) failed += 1
wenzelm@46166
   113
        else unprocessed += 1
wenzelm@44613
   114
      })
wenzelm@46688
   115
    Node_Status(unprocessed, running, finished, warned, failed)
wenzelm@44613
   116
  }
wenzelm@44613
   117
wenzelm@38887
   118
wenzelm@51533
   119
  /* node timing */
wenzelm@51533
   120
wenzelm@51533
   121
  sealed case class Node_Timing(total: Double, commands: Map[Command, Double])
wenzelm@51533
   122
wenzelm@51533
   123
  val empty_node_timing = Node_Timing(0.0, Map.empty)
wenzelm@51533
   124
wenzelm@51533
   125
  def node_timing(
wenzelm@51533
   126
    state: Document.State,
wenzelm@51533
   127
    version: Document.Version,
wenzelm@51533
   128
    node: Document.Node,
wenzelm@51533
   129
    threshold: Double): Node_Timing =
wenzelm@51533
   130
  {
wenzelm@51533
   131
    var total = 0.0
wenzelm@51533
   132
    var commands = Map.empty[Command, Double]
wenzelm@51533
   133
    for {
wenzelm@51533
   134
      command <- node.commands.iterator
wenzelm@51533
   135
      st = state.command_state(version, command)
wenzelm@51533
   136
      command_timing =
wenzelm@51533
   137
        (0.0 /: st.status)({
wenzelm@51533
   138
          case (timing, Markup.Timing(t)) => timing + t.elapsed.seconds
wenzelm@51533
   139
          case (timing, _) => timing
wenzelm@51533
   140
        })
wenzelm@51533
   141
    } {
wenzelm@51533
   142
      total += command_timing
wenzelm@51533
   143
      if (command_timing >= threshold) commands += (command -> command_timing)
wenzelm@51533
   144
    }
wenzelm@51533
   145
    Node_Timing(total, commands)
wenzelm@51533
   146
  }
wenzelm@51533
   147
wenzelm@51533
   148
wenzelm@39441
   149
  /* result messages */
wenzelm@39439
   150
wenzelm@50450
   151
  private val clean = Set(Markup.REPORT, Markup.NO_REPORT)
wenzelm@50450
   152
wenzelm@39439
   153
  def clean_message(body: XML.Body): XML.Body =
wenzelm@49445
   154
    body filter {
wenzelm@50450
   155
      case XML.Wrapped_Elem(Markup(name, _), _, _) => !clean(name)
wenzelm@50450
   156
      case XML.Elem(Markup(name, _), _) => !clean(name)
wenzelm@49445
   157
      case _ => true
wenzelm@49650
   158
    } map {
wenzelm@49650
   159
      case XML.Wrapped_Elem(markup, body, ts) => XML.Wrapped_Elem(markup, body, clean_message(ts))
wenzelm@49650
   160
      case XML.Elem(markup, ts) => XML.Elem(markup, clean_message(ts))
wenzelm@49650
   161
      case t => t
wenzelm@49650
   162
    }
wenzelm@39439
   163
wenzelm@49445
   164
  def message_reports(props: Properties.T, body: XML.Body): List[XML.Elem] =
wenzelm@49445
   165
    body flatMap {
wenzelm@50450
   166
      case XML.Wrapped_Elem(Markup(Markup.REPORT, ps), body, ts) =>
wenzelm@50450
   167
        List(XML.Wrapped_Elem(Markup(Markup.REPORT, props ::: ps), body, ts))
wenzelm@50201
   168
      case XML.Elem(Markup(Markup.REPORT, ps), ts) =>
wenzelm@50201
   169
        List(XML.Elem(Markup(Markup.REPORT, props ::: ps), ts))
wenzelm@49650
   170
      case XML.Wrapped_Elem(_, _, ts) => message_reports(props, ts)
wenzelm@49445
   171
      case XML.Elem(_, ts) => message_reports(props, ts)
wenzelm@39441
   172
      case XML.Text(_) => Nil
wenzelm@39441
   173
    }
wenzelm@39441
   174
wenzelm@39439
   175
wenzelm@39511
   176
  /* specific messages */
wenzelm@39511
   177
wenzelm@50500
   178
  def is_inlined(msg: XML.Tree): Boolean =
wenzelm@50500
   179
    !(is_result(msg) || is_tracing(msg) || is_state(msg))
wenzelm@50500
   180
wenzelm@50500
   181
  def is_result(msg: XML.Tree): Boolean =
wenzelm@50500
   182
    msg match {
wenzelm@50500
   183
      case XML.Elem(Markup(Markup.RESULT, _), _) => true
wenzelm@50500
   184
      case _ => false
wenzelm@50500
   185
    }
wenzelm@50500
   186
wenzelm@50157
   187
  def is_tracing(msg: XML.Tree): Boolean =
wenzelm@39622
   188
    msg match {
wenzelm@50201
   189
      case XML.Elem(Markup(Markup.TRACING, _), _) => true
wenzelm@50201
   190
      case XML.Elem(Markup(Markup.TRACING_MESSAGE, _), _) => true
wenzelm@39622
   191
      case _ => false
wenzelm@39622
   192
    }
wenzelm@39622
   193
wenzelm@50500
   194
  def is_state(msg: XML.Tree): Boolean =
wenzelm@50500
   195
    msg match {
wenzelm@50500
   196
      case XML.Elem(Markup(Markup.WRITELN, _),
wenzelm@50500
   197
        List(XML.Elem(Markup(Markup.STATE, _), _))) => true
wenzelm@50500
   198
      case XML.Elem(Markup(Markup.WRITELN_MESSAGE, _),
wenzelm@50500
   199
        List(XML.Elem(Markup(Markup.STATE, _), _))) => true
wenzelm@50500
   200
      case _ => false
wenzelm@50500
   201
    }
wenzelm@50500
   202
wenzelm@39511
   203
  def is_warning(msg: XML.Tree): Boolean =
wenzelm@39511
   204
    msg match {
wenzelm@50201
   205
      case XML.Elem(Markup(Markup.WARNING, _), _) => true
wenzelm@50201
   206
      case XML.Elem(Markup(Markup.WARNING_MESSAGE, _), _) => true
wenzelm@39511
   207
      case _ => false
wenzelm@39511
   208
    }
wenzelm@39511
   209
wenzelm@39511
   210
  def is_error(msg: XML.Tree): Boolean =
wenzelm@39511
   211
    msg match {
wenzelm@50201
   212
      case XML.Elem(Markup(Markup.ERROR, _), _) => true
wenzelm@50201
   213
      case XML.Elem(Markup(Markup.ERROR_MESSAGE, _), _) => true
wenzelm@39511
   214
      case _ => false
wenzelm@39511
   215
    }
wenzelm@38887
   216
wenzelm@50500
   217
wenzelm@50500
   218
  /* dialogs */
wenzelm@50500
   219
wenzelm@50500
   220
  object Dialog_Args
wenzelm@50500
   221
  {
wenzelm@50500
   222
    def unapply(props: Properties.T): Option[(Document.ID, Long, String)] =
wenzelm@50500
   223
      (props, props, props) match {
wenzelm@50500
   224
        case (Position.Id(id), Markup.Serial(serial), Markup.Result(result)) =>
wenzelm@50500
   225
          Some((id, serial, result))
wenzelm@50500
   226
        case _ => None
wenzelm@50500
   227
      }
wenzelm@50500
   228
  }
wenzelm@50500
   229
wenzelm@50500
   230
  object Dialog
wenzelm@50500
   231
  {
wenzelm@50500
   232
    def unapply(tree: XML.Tree): Option[(Document.ID, Long, String)] =
wenzelm@50500
   233
      tree match {
wenzelm@50500
   234
        case XML.Elem(Markup(Markup.DIALOG, Dialog_Args(id, serial, result)), _) =>
wenzelm@50500
   235
          Some((id, serial, result))
wenzelm@50500
   236
        case _ => None
wenzelm@50500
   237
      }
wenzelm@50500
   238
  }
wenzelm@50500
   239
wenzelm@50500
   240
  object Dialog_Result
wenzelm@50500
   241
  {
wenzelm@50501
   242
    def apply(id: Document.ID, serial: Long, result: String): XML.Elem =
wenzelm@50501
   243
    {
wenzelm@50501
   244
      val props = Position.Id(id) ::: Markup.Serial(serial)
wenzelm@50501
   245
      XML.Elem(Markup(Markup.RESULT, props), List(XML.Text(result)))
wenzelm@50501
   246
    }
wenzelm@50500
   247
wenzelm@50501
   248
    def unapply(tree: XML.Tree): Option[String] =
wenzelm@50500
   249
      tree match {
wenzelm@50501
   250
        case XML.Elem(Markup(Markup.RESULT, _), List(XML.Text(result))) => Some(result)
wenzelm@50500
   251
        case _ => None
wenzelm@50500
   252
      }
wenzelm@50500
   253
  }
wenzelm@39170
   254
wenzelm@39511
   255
wenzelm@39511
   256
  /* reported positions */
wenzelm@39511
   257
wenzelm@50201
   258
  private val include_pos = Set(Markup.BINDING, Markup.ENTITY, Markup.REPORT, Markup.POSITION)
wenzelm@39441
   259
wenzelm@39441
   260
  def message_positions(command: Command, message: XML.Elem): Set[Text.Range] =
wenzelm@38887
   261
  {
wenzelm@49650
   262
    def elem_positions(raw_range: Text.Range, set: Set[Text.Range], body: XML.Body)
wenzelm@49650
   263
      : Set[Text.Range] =
wenzelm@49650
   264
    {
wenzelm@49650
   265
      val range = command.decode(raw_range).restrict(command.range)
wenzelm@49650
   266
      body.foldLeft(if (range.is_singularity) set else set + range)(positions)
wenzelm@49650
   267
    }
wenzelm@50157
   268
wenzelm@39441
   269
    def positions(set: Set[Text.Range], tree: XML.Tree): Set[Text.Range] =
wenzelm@38887
   270
      tree match {
wenzelm@49650
   271
        case XML.Wrapped_Elem(Markup(name, Position.Id_Range(id, range)), _, body)
wenzelm@49650
   272
        if include_pos(name) && id == command.id => elem_positions(range, set, body)
wenzelm@49650
   273
wenzelm@49650
   274
        case XML.Elem(Markup(name, Position.Id_Range(id, range)), body)
wenzelm@49650
   275
        if include_pos(name) && id == command.id => elem_positions(range, set, body)
wenzelm@49650
   276
wenzelm@49650
   277
        case XML.Wrapped_Elem(_, _, body) => body.foldLeft(set)(positions)
wenzelm@49650
   278
wenzelm@49650
   279
        case XML.Elem(_, body) => body.foldLeft(set)(positions)
wenzelm@49650
   280
wenzelm@39042
   281
        case _ => set
wenzelm@38887
   282
      }
wenzelm@50157
   283
wenzelm@39441
   284
    val set = positions(Set.empty, message)
wenzelm@50500
   285
    if (set.isEmpty)
wenzelm@39172
   286
      set ++ Position.Range.unapply(message.markup.properties).map(command.decode(_))
wenzelm@39042
   287
    else set
wenzelm@38887
   288
  }
wenzelm@38412
   289
}
wenzelm@38412
   290
wenzelm@38412
   291
wenzelm@45709
   292
trait Protocol extends Isabelle_Process
wenzelm@38412
   293
{
wenzelm@38412
   294
  /* commands */
wenzelm@38412
   295
wenzelm@44644
   296
  def define_command(command: Command): Unit =
wenzelm@45709
   297
    input("Document.define_command",
wenzelm@48705
   298
      Document.ID(command.id), encode(command.name), encode(command.source))
wenzelm@38412
   299
wenzelm@38412
   300
wenzelm@38417
   301
  /* document versions */
wenzelm@38412
   302
wenzelm@47343
   303
  def discontinue_execution() { input("Document.discontinue_execution") }
wenzelm@47343
   304
wenzelm@47343
   305
  def cancel_execution() { input("Document.cancel_execution") }
wenzelm@44612
   306
wenzelm@44481
   307
  def update(old_id: Document.Version_ID, new_id: Document.Version_ID,
wenzelm@44383
   308
    edits: List[Document.Edit_Command])
wenzelm@38412
   309
  {
wenzelm@44157
   310
    val edits_yxml =
wenzelm@43767
   311
    { import XML.Encode._
wenzelm@44383
   312
      def id: T[Command] = (cmd => long(cmd.id))
wenzelm@46737
   313
      def encode_edit(name: Document.Node.Name)
wenzelm@44979
   314
          : T[Document.Node.Edit[(Option[Command], Option[Command]), Command.Perspective]] =
wenzelm@44979
   315
        variant(List(
wenzelm@48755
   316
          { case Document.Node.Clear() => (Nil, Nil) },  // FIXME unused !?
wenzelm@44979
   317
          { case Document.Node.Edits(a) => (Nil, list(pair(option(id), option(id)))(a)) },
wenzelm@48707
   318
          { case Document.Node.Deps(header) =>
wenzelm@46770
   319
              val dir = Isabelle_System.posix_path(name.dir)
wenzelm@48707
   320
              val imports = header.imports.map(_.node)
wenzelm@50128
   321
              val keywords = header.keywords.map({ case (a, b, _) => (a, b) })
wenzelm@44979
   322
              (Nil,
wenzelm@48707
   323
                pair(Encode.string, pair(Encode.string, pair(list(Encode.string),
wenzelm@48864
   324
                  pair(list(pair(Encode.string,
wenzelm@48864
   325
                    option(pair(pair(Encode.string, list(Encode.string)), list(Encode.string))))),
wenzelm@51294
   326
                  list(Encode.string)))))(
wenzelm@51294
   327
                (dir, (name.theory, (imports, (keywords, header.errors)))))) },
wenzelm@44979
   328
          { case Document.Node.Perspective(a) => (a.commands.map(c => long_atom(c.id)), Nil) }))
wenzelm@48705
   329
      def encode_edits: T[List[Document.Edit_Command]] = list((node_edit: Document.Edit_Command) =>
wenzelm@44979
   330
      {
wenzelm@44979
   331
        val (name, edit) = node_edit
wenzelm@46737
   332
        pair(string, encode_edit(name))(name.node, edit)
wenzelm@44979
   333
      })
wenzelm@48705
   334
      YXML.string_of_body(encode_edits(edits)) }
wenzelm@45709
   335
    input("Document.update", Document.ID(old_id), Document.ID(new_id), edits_yxml)
wenzelm@38412
   336
  }
wenzelm@43748
   337
wenzelm@44673
   338
  def remove_versions(versions: List[Document.Version])
wenzelm@44673
   339
  {
wenzelm@44673
   340
    val versions_yxml =
wenzelm@44673
   341
      { import XML.Encode._
wenzelm@44673
   342
        YXML.string_of_body(list(long)(versions.map(_.id))) }
wenzelm@45709
   343
    input("Document.remove_versions", versions_yxml)
wenzelm@44673
   344
  }
wenzelm@44673
   345
wenzelm@43748
   346
wenzelm@50498
   347
  /* dialog via document content */
wenzelm@50498
   348
wenzelm@50500
   349
  def dialog_result(serial: Long, result: String)
wenzelm@50498
   350
  {
wenzelm@50500
   351
    input("Document.dialog_result", Properties.Value.Long(serial), result)
wenzelm@50498
   352
  }
wenzelm@50498
   353
wenzelm@50498
   354
wenzelm@43748
   355
  /* method invocation service */
wenzelm@43748
   356
wenzelm@43748
   357
  def invoke_scala(id: String, tag: Invoke_Scala.Tag.Value, res: String)
wenzelm@43748
   358
  {
wenzelm@45709
   359
    input("Document.invoke_scala", id, tag.toString, res)
wenzelm@43748
   360
  }
wenzelm@38412
   361
}