src/Pure/PIDE/isar_document.scala
author wenzelm
Sat Sep 03 18:08:09 2011 +0200 (2011-09-03 ago)
changeset 44673 2fa51ac191bc
parent 44661 383c9d758a56
child 44676 7de87f1ae965
permissions -rw-r--r--
Document.remove_versions on ML side;
wenzelm@38483
     1
/*  Title:      Pure/PIDE/isar_document.scala
wenzelm@38412
     2
    Author:     Makarius
wenzelm@38412
     3
wenzelm@38567
     4
Protocol message formats for interactive Isar documents.
wenzelm@38412
     5
*/
wenzelm@38412
     6
wenzelm@38412
     7
package isabelle
wenzelm@38412
     8
wenzelm@38412
     9
wenzelm@38412
    10
object Isar_Document
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@38567
    29
wenzelm@38567
    30
  /* toplevel transactions */
wenzelm@38567
    31
wenzelm@38567
    32
  sealed abstract class Status
wenzelm@44613
    33
  case object Unprocessed extends Status
wenzelm@38567
    34
  case class Forked(forks: Int) extends Status
wenzelm@38567
    35
  case object Finished extends Status
wenzelm@38567
    36
  case object Failed extends Status
wenzelm@38567
    37
wenzelm@38581
    38
  def command_status(markup: List[Markup]): Status =
wenzelm@38567
    39
  {
wenzelm@38567
    40
    val forks = (0 /: markup) {
wenzelm@38581
    41
      case (i, Markup(Markup.FORKED, _)) => i + 1
wenzelm@38581
    42
      case (i, Markup(Markup.JOINED, _)) => i - 1
wenzelm@38567
    43
      case (i, _) => i
wenzelm@38567
    44
    }
wenzelm@38567
    45
    if (forks != 0) Forked(forks)
wenzelm@38581
    46
    else if (markup.exists(_.name == Markup.FAILED)) Failed
wenzelm@38581
    47
    else if (markup.exists(_.name == Markup.FINISHED)) Finished
wenzelm@38567
    48
    else Unprocessed
wenzelm@38567
    49
  }
wenzelm@38887
    50
wenzelm@44613
    51
  sealed case class Node_Status(unprocessed: Int, running: Int, finished: Int, failed: Int)
wenzelm@44613
    52
wenzelm@44613
    53
  def node_status(
wenzelm@44613
    54
    state: Document.State, version: Document.Version, node: Document.Node): Node_Status =
wenzelm@44613
    55
  {
wenzelm@44613
    56
    var unprocessed = 0
wenzelm@44613
    57
    var running = 0
wenzelm@44613
    58
    var finished = 0
wenzelm@44613
    59
    var failed = 0
wenzelm@44613
    60
    node.commands.foreach(command =>
wenzelm@44613
    61
      command_status(state.command_state(version, command).status) match {
wenzelm@44613
    62
        case Unprocessed => unprocessed += 1
wenzelm@44613
    63
        case Forked(_) => running += 1
wenzelm@44613
    64
        case Finished => finished += 1
wenzelm@44613
    65
        case Failed => failed += 1
wenzelm@44613
    66
      })
wenzelm@44613
    67
    Node_Status(unprocessed, running, finished, failed)
wenzelm@44613
    68
  }
wenzelm@44613
    69
wenzelm@38887
    70
wenzelm@39441
    71
  /* result messages */
wenzelm@39439
    72
wenzelm@39439
    73
  def clean_message(body: XML.Body): XML.Body =
wenzelm@39439
    74
    body filter { case XML.Elem(Markup(Markup.NO_REPORT, _), _) => false case _ => true } map
wenzelm@39439
    75
      { case XML.Elem(markup, ts) => XML.Elem(markup, clean_message(ts)) case t => t }
wenzelm@39439
    76
wenzelm@39441
    77
  def message_reports(msg: XML.Tree): List[XML.Elem] =
wenzelm@39441
    78
    msg match {
wenzelm@39441
    79
      case elem @ XML.Elem(Markup(Markup.REPORT, _), _) => List(elem)
wenzelm@39441
    80
      case XML.Elem(_, body) => body.flatMap(message_reports)
wenzelm@39441
    81
      case XML.Text(_) => Nil
wenzelm@39441
    82
    }
wenzelm@39441
    83
wenzelm@39439
    84
wenzelm@39511
    85
  /* specific messages */
wenzelm@39511
    86
wenzelm@39625
    87
  def is_ready(msg: XML.Tree): Boolean =
wenzelm@39625
    88
    msg match {
wenzelm@39625
    89
      case XML.Elem(Markup(Markup.STATUS, _),
wenzelm@39625
    90
        List(XML.Elem(Markup(Markup.READY, _), _))) => true
wenzelm@39625
    91
      case _ => false
wenzelm@39625
    92
    }
wenzelm@39625
    93
wenzelm@39625
    94
 def is_tracing(msg: XML.Tree): Boolean =
wenzelm@39622
    95
    msg match {
wenzelm@39622
    96
      case XML.Elem(Markup(Markup.TRACING, _), _) => true
wenzelm@39622
    97
      case _ => false
wenzelm@39622
    98
    }
wenzelm@39622
    99
wenzelm@39511
   100
  def is_warning(msg: XML.Tree): Boolean =
wenzelm@39511
   101
    msg match {
wenzelm@39511
   102
      case XML.Elem(Markup(Markup.WARNING, _), _) => true
wenzelm@39511
   103
      case _ => false
wenzelm@39511
   104
    }
wenzelm@39511
   105
wenzelm@39511
   106
  def is_error(msg: XML.Tree): Boolean =
wenzelm@39511
   107
    msg match {
wenzelm@39511
   108
      case XML.Elem(Markup(Markup.ERROR, _), _) => true
wenzelm@39511
   109
      case _ => false
wenzelm@39511
   110
    }
wenzelm@38887
   111
wenzelm@39441
   112
  def is_state(msg: XML.Tree): Boolean =
wenzelm@39170
   113
    msg match {
wenzelm@39627
   114
      case XML.Elem(Markup(Markup.WRITELN, _),
wenzelm@39627
   115
        List(XML.Elem(Markup(Markup.STATE, _), _))) => true
wenzelm@39170
   116
      case _ => false
wenzelm@39170
   117
    }
wenzelm@39170
   118
wenzelm@39511
   119
wenzelm@39511
   120
  /* reported positions */
wenzelm@39511
   121
wenzelm@39627
   122
  private val include_pos =
wenzelm@39627
   123
    Set(Markup.BINDING, Markup.ENTITY, Markup.REPORT, Markup.POSITION)
wenzelm@39441
   124
wenzelm@39441
   125
  def message_positions(command: Command, message: XML.Elem): Set[Text.Range] =
wenzelm@38887
   126
  {
wenzelm@39441
   127
    def positions(set: Set[Text.Range], tree: XML.Tree): Set[Text.Range] =
wenzelm@38887
   128
      tree match {
wenzelm@39172
   129
        case XML.Elem(Markup(name, Position.Id_Range(id, raw_range)), body)
wenzelm@39172
   130
        if include_pos(name) && id == command.id =>
wenzelm@39172
   131
          val range = command.decode(raw_range).restrict(command.range)
wenzelm@39441
   132
          body.foldLeft(if (range.is_singularity) set else set + range)(positions)
wenzelm@39441
   133
        case XML.Elem(Markup(name, _), body) => body.foldLeft(set)(positions)
wenzelm@39042
   134
        case _ => set
wenzelm@38887
   135
      }
wenzelm@39441
   136
    val set = positions(Set.empty, message)
wenzelm@39170
   137
    if (set.isEmpty && !is_state(message))
wenzelm@39172
   138
      set ++ Position.Range.unapply(message.markup.properties).map(command.decode(_))
wenzelm@39042
   139
    else set
wenzelm@38887
   140
  }
wenzelm@38412
   141
}
wenzelm@38412
   142
wenzelm@38412
   143
wenzelm@38412
   144
trait Isar_Document extends Isabelle_Process
wenzelm@38412
   145
{
wenzelm@38412
   146
  /* commands */
wenzelm@38412
   147
wenzelm@44644
   148
  def define_command(command: Command): Unit =
wenzelm@44644
   149
    input("Isar_Document.define_command",
wenzelm@44644
   150
      Document.ID(command.id), Symbol.encode(command.name), Symbol.encode(command.source))
wenzelm@38412
   151
wenzelm@38412
   152
wenzelm@38417
   153
  /* document versions */
wenzelm@38412
   154
wenzelm@44612
   155
  def cancel_execution()
wenzelm@44612
   156
  {
wenzelm@44612
   157
    input("Isar_Document.cancel_execution")
wenzelm@44612
   158
  }
wenzelm@44612
   159
wenzelm@44436
   160
  def update_perspective(old_id: Document.Version_ID, new_id: Document.Version_ID,
wenzelm@44615
   161
    name: Document.Node.Name, perspective: Command.Perspective)
wenzelm@44436
   162
  {
wenzelm@44436
   163
    val ids =
wenzelm@44436
   164
    { import XML.Encode._
wenzelm@44474
   165
      list(long)(perspective.commands.map(_.id)) }
wenzelm@44436
   166
wenzelm@44615
   167
    input("Isar_Document.update_perspective", Document.ID(old_id), Document.ID(new_id),
wenzelm@44615
   168
      name.node, YXML.string_of_body(ids))
wenzelm@44436
   169
  }
wenzelm@44436
   170
wenzelm@44481
   171
  def update(old_id: Document.Version_ID, new_id: Document.Version_ID,
wenzelm@44383
   172
    edits: List[Document.Edit_Command])
wenzelm@38412
   173
  {
wenzelm@44157
   174
    val edits_yxml =
wenzelm@43767
   175
    { import XML.Encode._
wenzelm@44383
   176
      def id: T[Command] = (cmd => long(cmd.id))
wenzelm@44383
   177
      def encode: T[List[Document.Edit_Command]] =
wenzelm@44615
   178
        list(pair((name => string(name.node)),
wenzelm@44156
   179
          variant(List(
wenzelm@44185
   180
            { case Document.Node.Clear() => (Nil, Nil) },
wenzelm@44383
   181
            { case Document.Node.Edits(a) => (Nil, list(pair(option(id), option(id)))(a)) },
wenzelm@44182
   182
            { case Document.Node.Header(Exn.Res(Thy_Header(a, b, c))) =>
wenzelm@44185
   183
                (Nil, triple(string, list(string), list(pair(string, bool)))(a, b, c)) },
wenzelm@44384
   184
            { case Document.Node.Header(Exn.Exn(e)) => (List(Exn.message(e)), Nil) },
wenzelm@44474
   185
            { case Document.Node.Perspective(a) => (a.commands.map(c => long_atom(c.id)), Nil) }))))
wenzelm@44157
   186
      YXML.string_of_body(encode(edits)) }
wenzelm@38412
   187
wenzelm@44481
   188
    input("Isar_Document.update", Document.ID(old_id), Document.ID(new_id), edits_yxml)
wenzelm@38412
   189
  }
wenzelm@43748
   190
wenzelm@44673
   191
  def remove_versions(versions: List[Document.Version])
wenzelm@44673
   192
  {
wenzelm@44673
   193
    val versions_yxml =
wenzelm@44673
   194
      { import XML.Encode._
wenzelm@44673
   195
        YXML.string_of_body(list(long)(versions.map(_.id))) }
wenzelm@44673
   196
    input("Isar_Document.remove_versions", versions_yxml)
wenzelm@44673
   197
  }
wenzelm@44673
   198
wenzelm@43748
   199
wenzelm@43748
   200
  /* method invocation service */
wenzelm@43748
   201
wenzelm@43748
   202
  def invoke_scala(id: String, tag: Invoke_Scala.Tag.Value, res: String)
wenzelm@43748
   203
  {
wenzelm@43748
   204
    input("Isar_Document.invoke_scala", id, tag.toString, res)
wenzelm@43748
   205
  }
wenzelm@38412
   206
}