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