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