src/Pure/PIDE/protocol.scala
author wenzelm
Fri Dec 14 12:09:08 2012 +0100 (2012-12-14)
changeset 50507 9605b0d93d1e
parent 50501 6f41f1646617
child 51293 05b1bbae748d
permissions -rw-r--r--
more formal class Command.Results;
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@39441
   119
  /* result messages */
wenzelm@39439
   120
wenzelm@50450
   121
  private val clean = Set(Markup.REPORT, Markup.NO_REPORT)
wenzelm@50450
   122
wenzelm@39439
   123
  def clean_message(body: XML.Body): XML.Body =
wenzelm@49445
   124
    body filter {
wenzelm@50450
   125
      case XML.Wrapped_Elem(Markup(name, _), _, _) => !clean(name)
wenzelm@50450
   126
      case XML.Elem(Markup(name, _), _) => !clean(name)
wenzelm@49445
   127
      case _ => true
wenzelm@49650
   128
    } map {
wenzelm@49650
   129
      case XML.Wrapped_Elem(markup, body, ts) => XML.Wrapped_Elem(markup, body, clean_message(ts))
wenzelm@49650
   130
      case XML.Elem(markup, ts) => XML.Elem(markup, clean_message(ts))
wenzelm@49650
   131
      case t => t
wenzelm@49650
   132
    }
wenzelm@39439
   133
wenzelm@49445
   134
  def message_reports(props: Properties.T, body: XML.Body): List[XML.Elem] =
wenzelm@49445
   135
    body flatMap {
wenzelm@50450
   136
      case XML.Wrapped_Elem(Markup(Markup.REPORT, ps), body, ts) =>
wenzelm@50450
   137
        List(XML.Wrapped_Elem(Markup(Markup.REPORT, props ::: ps), body, ts))
wenzelm@50201
   138
      case XML.Elem(Markup(Markup.REPORT, ps), ts) =>
wenzelm@50201
   139
        List(XML.Elem(Markup(Markup.REPORT, props ::: ps), ts))
wenzelm@49650
   140
      case XML.Wrapped_Elem(_, _, ts) => message_reports(props, ts)
wenzelm@49445
   141
      case XML.Elem(_, ts) => message_reports(props, ts)
wenzelm@39441
   142
      case XML.Text(_) => Nil
wenzelm@39441
   143
    }
wenzelm@39441
   144
wenzelm@39439
   145
wenzelm@39511
   146
  /* specific messages */
wenzelm@39511
   147
wenzelm@50500
   148
  def is_inlined(msg: XML.Tree): Boolean =
wenzelm@50500
   149
    !(is_result(msg) || is_tracing(msg) || is_state(msg))
wenzelm@50500
   150
wenzelm@50500
   151
  def is_result(msg: XML.Tree): Boolean =
wenzelm@50500
   152
    msg match {
wenzelm@50500
   153
      case XML.Elem(Markup(Markup.RESULT, _), _) => true
wenzelm@50500
   154
      case _ => false
wenzelm@50500
   155
    }
wenzelm@50500
   156
wenzelm@50157
   157
  def is_tracing(msg: XML.Tree): Boolean =
wenzelm@39622
   158
    msg match {
wenzelm@50201
   159
      case XML.Elem(Markup(Markup.TRACING, _), _) => true
wenzelm@50201
   160
      case XML.Elem(Markup(Markup.TRACING_MESSAGE, _), _) => true
wenzelm@39622
   161
      case _ => false
wenzelm@39622
   162
    }
wenzelm@39622
   163
wenzelm@50500
   164
  def is_state(msg: XML.Tree): Boolean =
wenzelm@50500
   165
    msg match {
wenzelm@50500
   166
      case XML.Elem(Markup(Markup.WRITELN, _),
wenzelm@50500
   167
        List(XML.Elem(Markup(Markup.STATE, _), _))) => true
wenzelm@50500
   168
      case XML.Elem(Markup(Markup.WRITELN_MESSAGE, _),
wenzelm@50500
   169
        List(XML.Elem(Markup(Markup.STATE, _), _))) => true
wenzelm@50500
   170
      case _ => false
wenzelm@50500
   171
    }
wenzelm@50500
   172
wenzelm@39511
   173
  def is_warning(msg: XML.Tree): Boolean =
wenzelm@39511
   174
    msg match {
wenzelm@50201
   175
      case XML.Elem(Markup(Markup.WARNING, _), _) => true
wenzelm@50201
   176
      case XML.Elem(Markup(Markup.WARNING_MESSAGE, _), _) => true
wenzelm@39511
   177
      case _ => false
wenzelm@39511
   178
    }
wenzelm@39511
   179
wenzelm@39511
   180
  def is_error(msg: XML.Tree): Boolean =
wenzelm@39511
   181
    msg match {
wenzelm@50201
   182
      case XML.Elem(Markup(Markup.ERROR, _), _) => true
wenzelm@50201
   183
      case XML.Elem(Markup(Markup.ERROR_MESSAGE, _), _) => true
wenzelm@39511
   184
      case _ => false
wenzelm@39511
   185
    }
wenzelm@38887
   186
wenzelm@50500
   187
wenzelm@50500
   188
  /* dialogs */
wenzelm@50500
   189
wenzelm@50500
   190
  object Dialog_Args
wenzelm@50500
   191
  {
wenzelm@50500
   192
    def unapply(props: Properties.T): Option[(Document.ID, Long, String)] =
wenzelm@50500
   193
      (props, props, props) match {
wenzelm@50500
   194
        case (Position.Id(id), Markup.Serial(serial), Markup.Result(result)) =>
wenzelm@50500
   195
          Some((id, serial, result))
wenzelm@50500
   196
        case _ => None
wenzelm@50500
   197
      }
wenzelm@50500
   198
  }
wenzelm@50500
   199
wenzelm@50500
   200
  object Dialog
wenzelm@50500
   201
  {
wenzelm@50500
   202
    def unapply(tree: XML.Tree): Option[(Document.ID, Long, String)] =
wenzelm@50500
   203
      tree match {
wenzelm@50500
   204
        case XML.Elem(Markup(Markup.DIALOG, Dialog_Args(id, serial, result)), _) =>
wenzelm@50500
   205
          Some((id, serial, result))
wenzelm@50500
   206
        case _ => None
wenzelm@50500
   207
      }
wenzelm@50500
   208
  }
wenzelm@50500
   209
wenzelm@50500
   210
  object Dialog_Result
wenzelm@50500
   211
  {
wenzelm@50501
   212
    def apply(id: Document.ID, serial: Long, result: String): XML.Elem =
wenzelm@50501
   213
    {
wenzelm@50501
   214
      val props = Position.Id(id) ::: Markup.Serial(serial)
wenzelm@50501
   215
      XML.Elem(Markup(Markup.RESULT, props), List(XML.Text(result)))
wenzelm@50501
   216
    }
wenzelm@50500
   217
wenzelm@50501
   218
    def unapply(tree: XML.Tree): Option[String] =
wenzelm@50500
   219
      tree match {
wenzelm@50501
   220
        case XML.Elem(Markup(Markup.RESULT, _), List(XML.Text(result))) => Some(result)
wenzelm@50500
   221
        case _ => None
wenzelm@50500
   222
      }
wenzelm@50500
   223
  }
wenzelm@39170
   224
wenzelm@39511
   225
wenzelm@39511
   226
  /* reported positions */
wenzelm@39511
   227
wenzelm@50201
   228
  private val include_pos = Set(Markup.BINDING, Markup.ENTITY, Markup.REPORT, Markup.POSITION)
wenzelm@39441
   229
wenzelm@39441
   230
  def message_positions(command: Command, message: XML.Elem): Set[Text.Range] =
wenzelm@38887
   231
  {
wenzelm@49650
   232
    def elem_positions(raw_range: Text.Range, set: Set[Text.Range], body: XML.Body)
wenzelm@49650
   233
      : Set[Text.Range] =
wenzelm@49650
   234
    {
wenzelm@49650
   235
      val range = command.decode(raw_range).restrict(command.range)
wenzelm@49650
   236
      body.foldLeft(if (range.is_singularity) set else set + range)(positions)
wenzelm@49650
   237
    }
wenzelm@50157
   238
wenzelm@39441
   239
    def positions(set: Set[Text.Range], tree: XML.Tree): Set[Text.Range] =
wenzelm@38887
   240
      tree match {
wenzelm@49650
   241
        case XML.Wrapped_Elem(Markup(name, Position.Id_Range(id, range)), _, body)
wenzelm@49650
   242
        if include_pos(name) && id == command.id => elem_positions(range, set, body)
wenzelm@49650
   243
wenzelm@49650
   244
        case XML.Elem(Markup(name, Position.Id_Range(id, range)), body)
wenzelm@49650
   245
        if include_pos(name) && id == command.id => elem_positions(range, set, body)
wenzelm@49650
   246
wenzelm@49650
   247
        case XML.Wrapped_Elem(_, _, body) => body.foldLeft(set)(positions)
wenzelm@49650
   248
wenzelm@49650
   249
        case XML.Elem(_, body) => body.foldLeft(set)(positions)
wenzelm@49650
   250
wenzelm@39042
   251
        case _ => set
wenzelm@38887
   252
      }
wenzelm@50157
   253
wenzelm@39441
   254
    val set = positions(Set.empty, message)
wenzelm@50500
   255
    if (set.isEmpty)
wenzelm@39172
   256
      set ++ Position.Range.unapply(message.markup.properties).map(command.decode(_))
wenzelm@39042
   257
    else set
wenzelm@38887
   258
  }
wenzelm@38412
   259
}
wenzelm@38412
   260
wenzelm@38412
   261
wenzelm@45709
   262
trait Protocol extends Isabelle_Process
wenzelm@38412
   263
{
wenzelm@38412
   264
  /* commands */
wenzelm@38412
   265
wenzelm@44644
   266
  def define_command(command: Command): Unit =
wenzelm@45709
   267
    input("Document.define_command",
wenzelm@48705
   268
      Document.ID(command.id), encode(command.name), encode(command.source))
wenzelm@38412
   269
wenzelm@38412
   270
wenzelm@38417
   271
  /* document versions */
wenzelm@38412
   272
wenzelm@47343
   273
  def discontinue_execution() { input("Document.discontinue_execution") }
wenzelm@47343
   274
wenzelm@47343
   275
  def cancel_execution() { input("Document.cancel_execution") }
wenzelm@44612
   276
wenzelm@44481
   277
  def update(old_id: Document.Version_ID, new_id: Document.Version_ID,
wenzelm@44383
   278
    edits: List[Document.Edit_Command])
wenzelm@38412
   279
  {
wenzelm@44157
   280
    val edits_yxml =
wenzelm@43767
   281
    { import XML.Encode._
wenzelm@44383
   282
      def id: T[Command] = (cmd => long(cmd.id))
wenzelm@46737
   283
      def encode_edit(name: Document.Node.Name)
wenzelm@44979
   284
          : T[Document.Node.Edit[(Option[Command], Option[Command]), Command.Perspective]] =
wenzelm@44979
   285
        variant(List(
wenzelm@48755
   286
          { case Document.Node.Clear() => (Nil, Nil) },  // FIXME unused !?
wenzelm@44979
   287
          { case Document.Node.Edits(a) => (Nil, list(pair(option(id), option(id)))(a)) },
wenzelm@48707
   288
          { case Document.Node.Deps(header) =>
wenzelm@46770
   289
              val dir = Isabelle_System.posix_path(name.dir)
wenzelm@48707
   290
              val imports = header.imports.map(_.node)
wenzelm@50128
   291
              val keywords = header.keywords.map({ case (a, b, _) => (a, b) })
wenzelm@46770
   292
              // FIXME val uses = deps.uses.map(p => (Isabelle_System.posix_path(p._1), p._2))
wenzelm@48707
   293
              val uses = header.uses
wenzelm@44979
   294
              (Nil,
wenzelm@48707
   295
                pair(Encode.string, pair(Encode.string, pair(list(Encode.string),
wenzelm@48864
   296
                  pair(list(pair(Encode.string,
wenzelm@48864
   297
                    option(pair(pair(Encode.string, list(Encode.string)), list(Encode.string))))),
wenzelm@48707
   298
                  pair(list(pair(Encode.string, bool)), list(Encode.string))))))(
wenzelm@50128
   299
                (dir, (name.theory, (imports, (keywords, (uses, header.errors))))))) },
wenzelm@44979
   300
          { case Document.Node.Perspective(a) => (a.commands.map(c => long_atom(c.id)), Nil) }))
wenzelm@48705
   301
      def encode_edits: T[List[Document.Edit_Command]] = list((node_edit: Document.Edit_Command) =>
wenzelm@44979
   302
      {
wenzelm@44979
   303
        val (name, edit) = node_edit
wenzelm@46737
   304
        pair(string, encode_edit(name))(name.node, edit)
wenzelm@44979
   305
      })
wenzelm@48705
   306
      YXML.string_of_body(encode_edits(edits)) }
wenzelm@45709
   307
    input("Document.update", Document.ID(old_id), Document.ID(new_id), edits_yxml)
wenzelm@38412
   308
  }
wenzelm@43748
   309
wenzelm@44673
   310
  def remove_versions(versions: List[Document.Version])
wenzelm@44673
   311
  {
wenzelm@44673
   312
    val versions_yxml =
wenzelm@44673
   313
      { import XML.Encode._
wenzelm@44673
   314
        YXML.string_of_body(list(long)(versions.map(_.id))) }
wenzelm@45709
   315
    input("Document.remove_versions", versions_yxml)
wenzelm@44673
   316
  }
wenzelm@44673
   317
wenzelm@43748
   318
wenzelm@50498
   319
  /* dialog via document content */
wenzelm@50498
   320
wenzelm@50500
   321
  def dialog_result(serial: Long, result: String)
wenzelm@50498
   322
  {
wenzelm@50500
   323
    input("Document.dialog_result", Properties.Value.Long(serial), result)
wenzelm@50498
   324
  }
wenzelm@50498
   325
wenzelm@50498
   326
wenzelm@43748
   327
  /* method invocation service */
wenzelm@43748
   328
wenzelm@43748
   329
  def invoke_scala(id: String, tag: Invoke_Scala.Tag.Value, res: String)
wenzelm@43748
   330
  {
wenzelm@45709
   331
    input("Document.invoke_scala", id, tag.toString, res)
wenzelm@43748
   332
  }
wenzelm@38412
   333
}