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