src/Pure/PIDE/protocol.scala
author wenzelm
Fri Jul 05 16:01:45 2013 +0200 (2013-07-05)
changeset 52531 21f8e0e151f5
parent 52530 99dd8b4ef3fe
child 52563 f9a20c2c3b70
permissions -rw-r--r--
tuned signature;
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@52530
    16
    def unapply(text: String): Option[(Document_ID.Version, Document.Assign)] =
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@50500
   208
  def is_state(msg: XML.Tree): Boolean =
wenzelm@50500
   209
    msg match {
wenzelm@50500
   210
      case XML.Elem(Markup(Markup.WRITELN, _),
wenzelm@50500
   211
        List(XML.Elem(Markup(Markup.STATE, _), _))) => true
wenzelm@50500
   212
      case XML.Elem(Markup(Markup.WRITELN_MESSAGE, _),
wenzelm@50500
   213
        List(XML.Elem(Markup(Markup.STATE, _), _))) => true
wenzelm@50500
   214
      case _ => false
wenzelm@50500
   215
    }
wenzelm@50500
   216
wenzelm@39511
   217
  def is_warning(msg: XML.Tree): Boolean =
wenzelm@39511
   218
    msg match {
wenzelm@50201
   219
      case XML.Elem(Markup(Markup.WARNING, _), _) => true
wenzelm@50201
   220
      case XML.Elem(Markup(Markup.WARNING_MESSAGE, _), _) => true
wenzelm@39511
   221
      case _ => false
wenzelm@39511
   222
    }
wenzelm@39511
   223
wenzelm@39511
   224
  def is_error(msg: XML.Tree): Boolean =
wenzelm@39511
   225
    msg match {
wenzelm@50201
   226
      case XML.Elem(Markup(Markup.ERROR, _), _) => true
wenzelm@50201
   227
      case XML.Elem(Markup(Markup.ERROR_MESSAGE, _), _) => true
wenzelm@39511
   228
      case _ => false
wenzelm@39511
   229
    }
wenzelm@38887
   230
wenzelm@50500
   231
wenzelm@50500
   232
  /* dialogs */
wenzelm@50500
   233
wenzelm@50500
   234
  object Dialog_Args
wenzelm@50500
   235
  {
wenzelm@52531
   236
    def unapply(props: Properties.T): Option[(Document_ID.Generic, Long, String)] =
wenzelm@50500
   237
      (props, props, props) match {
wenzelm@50500
   238
        case (Position.Id(id), Markup.Serial(serial), Markup.Result(result)) =>
wenzelm@50500
   239
          Some((id, serial, result))
wenzelm@50500
   240
        case _ => None
wenzelm@50500
   241
      }
wenzelm@50500
   242
  }
wenzelm@50500
   243
wenzelm@50500
   244
  object Dialog
wenzelm@50500
   245
  {
wenzelm@52531
   246
    def unapply(tree: XML.Tree): Option[(Document_ID.Generic, Long, String)] =
wenzelm@50500
   247
      tree match {
wenzelm@50500
   248
        case XML.Elem(Markup(Markup.DIALOG, Dialog_Args(id, serial, result)), _) =>
wenzelm@50500
   249
          Some((id, serial, result))
wenzelm@50500
   250
        case _ => None
wenzelm@50500
   251
      }
wenzelm@50500
   252
  }
wenzelm@50500
   253
wenzelm@50500
   254
  object Dialog_Result
wenzelm@50500
   255
  {
wenzelm@52531
   256
    def apply(id: Document_ID.Generic, serial: Long, result: String): XML.Elem =
wenzelm@50501
   257
    {
wenzelm@50501
   258
      val props = Position.Id(id) ::: Markup.Serial(serial)
wenzelm@50501
   259
      XML.Elem(Markup(Markup.RESULT, props), List(XML.Text(result)))
wenzelm@50501
   260
    }
wenzelm@50500
   261
wenzelm@50501
   262
    def unapply(tree: XML.Tree): Option[String] =
wenzelm@50500
   263
      tree match {
wenzelm@50501
   264
        case XML.Elem(Markup(Markup.RESULT, _), List(XML.Text(result))) => Some(result)
wenzelm@50500
   265
        case _ => None
wenzelm@50500
   266
      }
wenzelm@50500
   267
  }
wenzelm@39170
   268
wenzelm@39511
   269
wenzelm@39511
   270
  /* reported positions */
wenzelm@39511
   271
wenzelm@50201
   272
  private val include_pos = Set(Markup.BINDING, Markup.ENTITY, Markup.REPORT, Markup.POSITION)
wenzelm@39441
   273
wenzelm@39441
   274
  def message_positions(command: Command, message: XML.Elem): Set[Text.Range] =
wenzelm@38887
   275
  {
wenzelm@49650
   276
    def elem_positions(raw_range: Text.Range, set: Set[Text.Range], body: XML.Body)
wenzelm@49650
   277
      : Set[Text.Range] =
wenzelm@49650
   278
    {
wenzelm@49650
   279
      val range = command.decode(raw_range).restrict(command.range)
wenzelm@49650
   280
      body.foldLeft(if (range.is_singularity) set else set + range)(positions)
wenzelm@49650
   281
    }
wenzelm@50157
   282
wenzelm@39441
   283
    def positions(set: Set[Text.Range], tree: XML.Tree): Set[Text.Range] =
wenzelm@38887
   284
      tree match {
wenzelm@49650
   285
        case XML.Wrapped_Elem(Markup(name, Position.Id_Range(id, range)), _, body)
wenzelm@49650
   286
        if include_pos(name) && id == command.id => elem_positions(range, set, body)
wenzelm@49650
   287
wenzelm@49650
   288
        case XML.Elem(Markup(name, Position.Id_Range(id, range)), body)
wenzelm@49650
   289
        if include_pos(name) && id == command.id => elem_positions(range, set, body)
wenzelm@49650
   290
wenzelm@49650
   291
        case XML.Wrapped_Elem(_, _, body) => body.foldLeft(set)(positions)
wenzelm@49650
   292
wenzelm@49650
   293
        case XML.Elem(_, body) => body.foldLeft(set)(positions)
wenzelm@49650
   294
wenzelm@39042
   295
        case _ => set
wenzelm@38887
   296
      }
wenzelm@50157
   297
wenzelm@39441
   298
    val set = positions(Set.empty, message)
wenzelm@50500
   299
    if (set.isEmpty)
wenzelm@39172
   300
      set ++ Position.Range.unapply(message.markup.properties).map(command.decode(_))
wenzelm@39042
   301
    else set
wenzelm@38887
   302
  }
wenzelm@38412
   303
}
wenzelm@38412
   304
wenzelm@38412
   305
wenzelm@45709
   306
trait Protocol extends Isabelle_Process
wenzelm@38412
   307
{
wenzelm@38412
   308
  /* commands */
wenzelm@38412
   309
wenzelm@44644
   310
  def define_command(command: Command): Unit =
wenzelm@45709
   311
    input("Document.define_command",
wenzelm@52531
   312
      Document_ID(command.id), encode(command.name), encode(command.source))
wenzelm@38412
   313
wenzelm@38412
   314
wenzelm@38417
   315
  /* document versions */
wenzelm@38412
   316
wenzelm@47343
   317
  def discontinue_execution() { input("Document.discontinue_execution") }
wenzelm@47343
   318
wenzelm@47343
   319
  def cancel_execution() { input("Document.cancel_execution") }
wenzelm@44612
   320
wenzelm@52530
   321
  def update(old_id: Document_ID.Version, new_id: Document_ID.Version,
wenzelm@44383
   322
    edits: List[Document.Edit_Command])
wenzelm@38412
   323
  {
wenzelm@44157
   324
    val edits_yxml =
wenzelm@43767
   325
    { import XML.Encode._
wenzelm@44383
   326
      def id: T[Command] = (cmd => long(cmd.id))
wenzelm@46737
   327
      def encode_edit(name: Document.Node.Name)
wenzelm@44979
   328
          : T[Document.Node.Edit[(Option[Command], Option[Command]), Command.Perspective]] =
wenzelm@44979
   329
        variant(List(
wenzelm@48755
   330
          { case Document.Node.Clear() => (Nil, Nil) },  // FIXME unused !?
wenzelm@44979
   331
          { case Document.Node.Edits(a) => (Nil, list(pair(option(id), option(id)))(a)) },
wenzelm@48707
   332
          { case Document.Node.Deps(header) =>
wenzelm@46770
   333
              val dir = Isabelle_System.posix_path(name.dir)
wenzelm@48707
   334
              val imports = header.imports.map(_.node)
wenzelm@50128
   335
              val keywords = header.keywords.map({ case (a, b, _) => (a, b) })
wenzelm@44979
   336
              (Nil,
wenzelm@48707
   337
                pair(Encode.string, pair(Encode.string, pair(list(Encode.string),
wenzelm@48864
   338
                  pair(list(pair(Encode.string,
wenzelm@48864
   339
                    option(pair(pair(Encode.string, list(Encode.string)), list(Encode.string))))),
wenzelm@51294
   340
                  list(Encode.string)))))(
wenzelm@51294
   341
                (dir, (name.theory, (imports, (keywords, header.errors)))))) },
wenzelm@44979
   342
          { case Document.Node.Perspective(a) => (a.commands.map(c => long_atom(c.id)), Nil) }))
wenzelm@48705
   343
      def encode_edits: T[List[Document.Edit_Command]] = list((node_edit: Document.Edit_Command) =>
wenzelm@44979
   344
      {
wenzelm@44979
   345
        val (name, edit) = node_edit
wenzelm@46737
   346
        pair(string, encode_edit(name))(name.node, edit)
wenzelm@44979
   347
      })
wenzelm@48705
   348
      YXML.string_of_body(encode_edits(edits)) }
wenzelm@52531
   349
    input("Document.update", Document_ID(old_id), Document_ID(new_id), edits_yxml)
wenzelm@38412
   350
  }
wenzelm@43748
   351
wenzelm@44673
   352
  def remove_versions(versions: List[Document.Version])
wenzelm@44673
   353
  {
wenzelm@44673
   354
    val versions_yxml =
wenzelm@44673
   355
      { import XML.Encode._
wenzelm@44673
   356
        YXML.string_of_body(list(long)(versions.map(_.id))) }
wenzelm@45709
   357
    input("Document.remove_versions", versions_yxml)
wenzelm@44673
   358
  }
wenzelm@44673
   359
wenzelm@43748
   360
wenzelm@50498
   361
  /* dialog via document content */
wenzelm@50498
   362
wenzelm@50500
   363
  def dialog_result(serial: Long, result: String)
wenzelm@50498
   364
  {
wenzelm@50500
   365
    input("Document.dialog_result", Properties.Value.Long(serial), result)
wenzelm@50498
   366
  }
wenzelm@38412
   367
}