src/Pure/PIDE/protocol.scala
author wenzelm
Sun Feb 26 20:05:14 2012 +0100 (2012-02-26 ago)
changeset 46688 134982ee4ecb
parent 46227 4aa84f84d5e8
child 46737 09ab89658a5d
permissions -rw-r--r--
include warning messages in node status;
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@44661
    20
        Some(pair(long, pair(list(pair(long, option(long))), list(pair(string, 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@46166
    52
    private val finished: Boolean = false,
wenzelm@46166
    53
    private val failed: Boolean = false,
wenzelm@46166
    54
    forks: Int = 0)
wenzelm@38567
    55
  {
wenzelm@46166
    56
    def is_unprocessed: Boolean = !finished && !failed && forks == 0
wenzelm@46166
    57
    def is_running: Boolean = forks != 0
wenzelm@46166
    58
    def is_finished: Boolean = finished && forks == 0
wenzelm@46166
    59
    def is_failed: Boolean = failed && forks == 0
wenzelm@46227
    60
    def + (that: Status): Status =
wenzelm@46227
    61
      Status(finished || that.finished, failed || that.failed, forks + that.forks)
wenzelm@46166
    62
  }
wenzelm@46166
    63
wenzelm@46166
    64
  val command_status_markup: Set[String] =
wenzelm@46166
    65
    Set(Isabelle_Markup.FINISHED, Isabelle_Markup.FAILED,
wenzelm@46166
    66
      Isabelle_Markup.FORKED, Isabelle_Markup.JOINED)
wenzelm@46166
    67
wenzelm@46166
    68
  def command_status(status: Status, markup: Markup): Status =
wenzelm@46166
    69
    markup match {
wenzelm@46166
    70
      case Markup(Isabelle_Markup.FINISHED, _) => status.copy(finished = true)
wenzelm@46166
    71
      case Markup(Isabelle_Markup.FAILED, _) => status.copy(failed = true)
wenzelm@46166
    72
      case Markup(Isabelle_Markup.FORKED, _) => status.copy(forks = status.forks + 1)
wenzelm@46166
    73
      case Markup(Isabelle_Markup.JOINED, _) => status.copy(forks = status.forks - 1)
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@46209
    80
wenzelm@46209
    81
  /* node status */
wenzelm@46209
    82
wenzelm@46688
    83
  sealed case class Node_Status(
wenzelm@46688
    84
    unprocessed: Int, running: Int, finished: Int, warned: Int, failed: Int)
wenzelm@44866
    85
  {
wenzelm@46688
    86
    def total: Int = unprocessed + running + finished + warned + failed
wenzelm@44866
    87
  }
wenzelm@44613
    88
wenzelm@44613
    89
  def node_status(
wenzelm@44613
    90
    state: Document.State, version: Document.Version, node: Document.Node): Node_Status =
wenzelm@44613
    91
  {
wenzelm@44613
    92
    var unprocessed = 0
wenzelm@44613
    93
    var running = 0
wenzelm@44613
    94
    var finished = 0
wenzelm@46688
    95
    var warned = 0
wenzelm@44613
    96
    var failed = 0
wenzelm@44613
    97
    node.commands.foreach(command =>
wenzelm@46166
    98
      {
wenzelm@46688
    99
        val st = state.command_state(version, command)
wenzelm@46688
   100
        val status = command_status(st.status)
wenzelm@46166
   101
        if (status.is_running) running += 1
wenzelm@46688
   102
        else if (status.is_finished) {
wenzelm@46688
   103
          if (st.results.exists(p => is_warning(p._2))) warned += 1
wenzelm@46688
   104
          else finished += 1
wenzelm@46688
   105
        }
wenzelm@46166
   106
        else if (status.is_failed) failed += 1
wenzelm@46166
   107
        else unprocessed += 1
wenzelm@44613
   108
      })
wenzelm@46688
   109
    Node_Status(unprocessed, running, finished, warned, failed)
wenzelm@44613
   110
  }
wenzelm@44613
   111
wenzelm@38887
   112
wenzelm@39441
   113
  /* result messages */
wenzelm@39439
   114
wenzelm@39439
   115
  def clean_message(body: XML.Body): XML.Body =
wenzelm@45666
   116
    body filter { case XML.Elem(Markup(Isabelle_Markup.NO_REPORT, _), _) => false case _ => true } map
wenzelm@39439
   117
      { case XML.Elem(markup, ts) => XML.Elem(markup, clean_message(ts)) case t => t }
wenzelm@39439
   118
wenzelm@39441
   119
  def message_reports(msg: XML.Tree): List[XML.Elem] =
wenzelm@39441
   120
    msg match {
wenzelm@45666
   121
      case elem @ XML.Elem(Markup(Isabelle_Markup.REPORT, _), _) => List(elem)
wenzelm@39441
   122
      case XML.Elem(_, body) => body.flatMap(message_reports)
wenzelm@39441
   123
      case XML.Text(_) => Nil
wenzelm@39441
   124
    }
wenzelm@39441
   125
wenzelm@39439
   126
wenzelm@39511
   127
  /* specific messages */
wenzelm@39511
   128
wenzelm@39625
   129
 def is_tracing(msg: XML.Tree): Boolean =
wenzelm@39622
   130
    msg match {
wenzelm@45666
   131
      case XML.Elem(Markup(Isabelle_Markup.TRACING, _), _) => true
wenzelm@39622
   132
      case _ => false
wenzelm@39622
   133
    }
wenzelm@39622
   134
wenzelm@39511
   135
  def is_warning(msg: XML.Tree): Boolean =
wenzelm@39511
   136
    msg match {
wenzelm@45666
   137
      case XML.Elem(Markup(Isabelle_Markup.WARNING, _), _) => true
wenzelm@39511
   138
      case _ => false
wenzelm@39511
   139
    }
wenzelm@39511
   140
wenzelm@39511
   141
  def is_error(msg: XML.Tree): Boolean =
wenzelm@39511
   142
    msg match {
wenzelm@45666
   143
      case XML.Elem(Markup(Isabelle_Markup.ERROR, _), _) => true
wenzelm@39511
   144
      case _ => false
wenzelm@39511
   145
    }
wenzelm@38887
   146
wenzelm@39441
   147
  def is_state(msg: XML.Tree): Boolean =
wenzelm@39170
   148
    msg match {
wenzelm@45666
   149
      case XML.Elem(Markup(Isabelle_Markup.WRITELN, _),
wenzelm@45666
   150
        List(XML.Elem(Markup(Isabelle_Markup.STATE, _), _))) => true
wenzelm@39170
   151
      case _ => false
wenzelm@39170
   152
    }
wenzelm@39170
   153
wenzelm@39511
   154
wenzelm@39511
   155
  /* reported positions */
wenzelm@39511
   156
wenzelm@39627
   157
  private val include_pos =
wenzelm@45666
   158
    Set(Isabelle_Markup.BINDING, Isabelle_Markup.ENTITY, Isabelle_Markup.REPORT,
wenzelm@45666
   159
      Isabelle_Markup.POSITION)
wenzelm@39441
   160
wenzelm@39441
   161
  def message_positions(command: Command, message: XML.Elem): Set[Text.Range] =
wenzelm@38887
   162
  {
wenzelm@39441
   163
    def positions(set: Set[Text.Range], tree: XML.Tree): Set[Text.Range] =
wenzelm@38887
   164
      tree match {
wenzelm@39172
   165
        case XML.Elem(Markup(name, Position.Id_Range(id, raw_range)), body)
wenzelm@39172
   166
        if include_pos(name) && id == command.id =>
wenzelm@46207
   167
          // FIXME handle message range outside command range (!??!)
wenzelm@39172
   168
          val range = command.decode(raw_range).restrict(command.range)
wenzelm@39441
   169
          body.foldLeft(if (range.is_singularity) set else set + range)(positions)
wenzelm@39441
   170
        case XML.Elem(Markup(name, _), body) => body.foldLeft(set)(positions)
wenzelm@39042
   171
        case _ => set
wenzelm@38887
   172
      }
wenzelm@39441
   173
    val set = positions(Set.empty, message)
wenzelm@39170
   174
    if (set.isEmpty && !is_state(message))
wenzelm@39172
   175
      set ++ Position.Range.unapply(message.markup.properties).map(command.decode(_))
wenzelm@39042
   176
    else set
wenzelm@38887
   177
  }
wenzelm@38412
   178
}
wenzelm@38412
   179
wenzelm@38412
   180
wenzelm@45709
   181
trait Protocol extends Isabelle_Process
wenzelm@38412
   182
{
wenzelm@38412
   183
  /* commands */
wenzelm@38412
   184
wenzelm@44644
   185
  def define_command(command: Command): Unit =
wenzelm@45709
   186
    input("Document.define_command",
wenzelm@44644
   187
      Document.ID(command.id), Symbol.encode(command.name), Symbol.encode(command.source))
wenzelm@38412
   188
wenzelm@38412
   189
wenzelm@38417
   190
  /* document versions */
wenzelm@38412
   191
wenzelm@44612
   192
  def cancel_execution()
wenzelm@44612
   193
  {
wenzelm@45709
   194
    input("Document.cancel_execution")
wenzelm@44612
   195
  }
wenzelm@44612
   196
wenzelm@44436
   197
  def update_perspective(old_id: Document.Version_ID, new_id: Document.Version_ID,
wenzelm@44615
   198
    name: Document.Node.Name, perspective: Command.Perspective)
wenzelm@44436
   199
  {
wenzelm@44436
   200
    val ids =
wenzelm@44436
   201
    { import XML.Encode._
wenzelm@44474
   202
      list(long)(perspective.commands.map(_.id)) }
wenzelm@44436
   203
wenzelm@45709
   204
    input("Document.update_perspective", Document.ID(old_id), Document.ID(new_id),
wenzelm@44615
   205
      name.node, YXML.string_of_body(ids))
wenzelm@44436
   206
  }
wenzelm@44436
   207
wenzelm@44481
   208
  def update(old_id: Document.Version_ID, new_id: Document.Version_ID,
wenzelm@44383
   209
    edits: List[Document.Edit_Command])
wenzelm@38412
   210
  {
wenzelm@44157
   211
    val edits_yxml =
wenzelm@43767
   212
    { import XML.Encode._
wenzelm@44383
   213
      def id: T[Command] = (cmd => long(cmd.id))
wenzelm@44979
   214
      def encode_edit(dir: String)
wenzelm@44979
   215
          : T[Document.Node.Edit[(Option[Command], Option[Command]), Command.Perspective]] =
wenzelm@44979
   216
        variant(List(
wenzelm@44979
   217
          { case Document.Node.Clear() => (Nil, Nil) },
wenzelm@44979
   218
          { case Document.Node.Edits(a) => (Nil, list(pair(option(id), option(id)))(a)) },
wenzelm@44979
   219
          { case Document.Node.Header(Exn.Res(Thy_Header(a, b, c))) =>
wenzelm@44979
   220
              (Nil,
wenzelm@44979
   221
                triple(pair(string, string), list(string), list(pair(string, bool)))(
wenzelm@44979
   222
                  (dir, a), b, c)) },
wenzelm@44979
   223
          { case Document.Node.Header(Exn.Exn(e)) => (List(Exn.message(e)), Nil) },
wenzelm@44979
   224
          { case Document.Node.Perspective(a) => (a.commands.map(c => long_atom(c.id)), Nil) }))
wenzelm@44979
   225
      def encode: T[List[Document.Edit_Command]] = list((node_edit: Document.Edit_Command) =>
wenzelm@44979
   226
      {
wenzelm@44979
   227
        val (name, edit) = node_edit
wenzelm@44979
   228
        val dir = Isabelle_System.posix_path(name.dir)
wenzelm@44979
   229
        pair(string, encode_edit(dir))(name.node, edit)
wenzelm@44979
   230
      })
wenzelm@44157
   231
      YXML.string_of_body(encode(edits)) }
wenzelm@45709
   232
    input("Document.update", Document.ID(old_id), Document.ID(new_id), edits_yxml)
wenzelm@38412
   233
  }
wenzelm@43748
   234
wenzelm@44673
   235
  def remove_versions(versions: List[Document.Version])
wenzelm@44673
   236
  {
wenzelm@44673
   237
    val versions_yxml =
wenzelm@44673
   238
      { import XML.Encode._
wenzelm@44673
   239
        YXML.string_of_body(list(long)(versions.map(_.id))) }
wenzelm@45709
   240
    input("Document.remove_versions", versions_yxml)
wenzelm@44673
   241
  }
wenzelm@44673
   242
wenzelm@43748
   243
wenzelm@43748
   244
  /* method invocation service */
wenzelm@43748
   245
wenzelm@43748
   246
  def invoke_scala(id: String, tag: Invoke_Scala.Tag.Value, res: String)
wenzelm@43748
   247
  {
wenzelm@45709
   248
    input("Document.invoke_scala", id, tag.toString, res)
wenzelm@43748
   249
  }
wenzelm@38412
   250
}