src/Pure/PIDE/isar_document.scala
author wenzelm
Sat Sep 10 20:22:22 2011 +0200 (2011-09-10 ago)
changeset 44866 0eb8284a64bd
parent 44676 7de87f1ae965
child 44979 68b990e950b1
permissions -rw-r--r--
some color scheme for theory status;
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@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@38581
    55
      case (i, Markup(Markup.FORKED, _)) => i + 1
wenzelm@38581
    56
      case (i, Markup(Markup.JOINED, _)) => i - 1
wenzelm@38567
    57
      case (i, _) => i
wenzelm@38567
    58
    }
wenzelm@38567
    59
    if (forks != 0) Forked(forks)
wenzelm@38581
    60
    else if (markup.exists(_.name == Markup.FAILED)) Failed
wenzelm@38581
    61
    else if (markup.exists(_.name == 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@39439
    91
    body filter { case XML.Elem(Markup(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@39441
    96
      case elem @ XML.Elem(Markup(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@39625
   106
      case XML.Elem(Markup(Markup.STATUS, _),
wenzelm@39625
   107
        List(XML.Elem(Markup(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@39622
   113
      case XML.Elem(Markup(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@39511
   119
      case XML.Elem(Markup(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@39511
   125
      case XML.Elem(Markup(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@39627
   131
      case XML.Elem(Markup(Markup.WRITELN, _),
wenzelm@39627
   132
        List(XML.Elem(Markup(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@39627
   140
    Set(Markup.BINDING, Markup.ENTITY, Markup.REPORT, Markup.POSITION)
wenzelm@39441
   141
wenzelm@39441
   142
  def message_positions(command: Command, message: XML.Elem): Set[Text.Range] =
wenzelm@38887
   143
  {
wenzelm@39441
   144
    def positions(set: Set[Text.Range], tree: XML.Tree): Set[Text.Range] =
wenzelm@38887
   145
      tree match {
wenzelm@39172
   146
        case XML.Elem(Markup(name, Position.Id_Range(id, raw_range)), body)
wenzelm@39172
   147
        if include_pos(name) && id == command.id =>
wenzelm@39172
   148
          val range = command.decode(raw_range).restrict(command.range)
wenzelm@39441
   149
          body.foldLeft(if (range.is_singularity) set else set + range)(positions)
wenzelm@39441
   150
        case XML.Elem(Markup(name, _), body) => body.foldLeft(set)(positions)
wenzelm@39042
   151
        case _ => set
wenzelm@38887
   152
      }
wenzelm@39441
   153
    val set = positions(Set.empty, message)
wenzelm@39170
   154
    if (set.isEmpty && !is_state(message))
wenzelm@39172
   155
      set ++ Position.Range.unapply(message.markup.properties).map(command.decode(_))
wenzelm@39042
   156
    else set
wenzelm@38887
   157
  }
wenzelm@38412
   158
}
wenzelm@38412
   159
wenzelm@38412
   160
wenzelm@38412
   161
trait Isar_Document extends Isabelle_Process
wenzelm@38412
   162
{
wenzelm@38412
   163
  /* commands */
wenzelm@38412
   164
wenzelm@44644
   165
  def define_command(command: Command): Unit =
wenzelm@44644
   166
    input("Isar_Document.define_command",
wenzelm@44644
   167
      Document.ID(command.id), Symbol.encode(command.name), Symbol.encode(command.source))
wenzelm@38412
   168
wenzelm@38412
   169
wenzelm@38417
   170
  /* document versions */
wenzelm@38412
   171
wenzelm@44612
   172
  def cancel_execution()
wenzelm@44612
   173
  {
wenzelm@44612
   174
    input("Isar_Document.cancel_execution")
wenzelm@44612
   175
  }
wenzelm@44612
   176
wenzelm@44436
   177
  def update_perspective(old_id: Document.Version_ID, new_id: Document.Version_ID,
wenzelm@44615
   178
    name: Document.Node.Name, perspective: Command.Perspective)
wenzelm@44436
   179
  {
wenzelm@44436
   180
    val ids =
wenzelm@44436
   181
    { import XML.Encode._
wenzelm@44474
   182
      list(long)(perspective.commands.map(_.id)) }
wenzelm@44436
   183
wenzelm@44615
   184
    input("Isar_Document.update_perspective", Document.ID(old_id), Document.ID(new_id),
wenzelm@44615
   185
      name.node, YXML.string_of_body(ids))
wenzelm@44436
   186
  }
wenzelm@44436
   187
wenzelm@44481
   188
  def update(old_id: Document.Version_ID, new_id: Document.Version_ID,
wenzelm@44383
   189
    edits: List[Document.Edit_Command])
wenzelm@38412
   190
  {
wenzelm@44157
   191
    val edits_yxml =
wenzelm@43767
   192
    { import XML.Encode._
wenzelm@44383
   193
      def id: T[Command] = (cmd => long(cmd.id))
wenzelm@44383
   194
      def encode: T[List[Document.Edit_Command]] =
wenzelm@44615
   195
        list(pair((name => string(name.node)),
wenzelm@44156
   196
          variant(List(
wenzelm@44185
   197
            { case Document.Node.Clear() => (Nil, Nil) },
wenzelm@44383
   198
            { case Document.Node.Edits(a) => (Nil, list(pair(option(id), option(id)))(a)) },
wenzelm@44182
   199
            { case Document.Node.Header(Exn.Res(Thy_Header(a, b, c))) =>
wenzelm@44185
   200
                (Nil, triple(string, list(string), list(pair(string, bool)))(a, b, c)) },
wenzelm@44384
   201
            { case Document.Node.Header(Exn.Exn(e)) => (List(Exn.message(e)), Nil) },
wenzelm@44474
   202
            { case Document.Node.Perspective(a) => (a.commands.map(c => long_atom(c.id)), Nil) }))))
wenzelm@44157
   203
      YXML.string_of_body(encode(edits)) }
wenzelm@38412
   204
wenzelm@44481
   205
    input("Isar_Document.update", Document.ID(old_id), Document.ID(new_id), edits_yxml)
wenzelm@38412
   206
  }
wenzelm@43748
   207
wenzelm@44673
   208
  def remove_versions(versions: List[Document.Version])
wenzelm@44673
   209
  {
wenzelm@44673
   210
    val versions_yxml =
wenzelm@44673
   211
      { import XML.Encode._
wenzelm@44673
   212
        YXML.string_of_body(list(long)(versions.map(_.id))) }
wenzelm@44673
   213
    input("Isar_Document.remove_versions", versions_yxml)
wenzelm@44673
   214
  }
wenzelm@44673
   215
wenzelm@43748
   216
wenzelm@43748
   217
  /* method invocation service */
wenzelm@43748
   218
wenzelm@43748
   219
  def invoke_scala(id: String, tag: Invoke_Scala.Tag.Value, res: String)
wenzelm@43748
   220
  {
wenzelm@43748
   221
    input("Isar_Document.invoke_scala", id, tag.toString, res)
wenzelm@43748
   222
  }
wenzelm@38412
   223
}