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