src/Pure/PIDE/protocol.scala
author wenzelm
Fri Aug 31 13:23:25 2012 +0200 (2012-08-31 ago)
changeset 49036 4680c4046814
parent 49009 15381ea111ec
child 49039 e780d1bf767e
permissions -rw-r--r--
further refinement of command status, to accomodate forked proofs;
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@47388
    20
        Some(pair(long, list(pair(long, 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@46227
    46
  object Status
wenzelm@46227
    47
  {
wenzelm@46227
    48
    val init = Status()
wenzelm@46227
    49
  }
wenzelm@46227
    50
wenzelm@46166
    51
  sealed case class Status(
wenzelm@49036
    52
    private val touched: Boolean = false,
wenzelm@47395
    53
    private val accepted: Boolean = false,
wenzelm@46166
    54
    private val failed: Boolean = false,
wenzelm@49036
    55
    forks: Int = 0,
wenzelm@49036
    56
    runs: Int = 0)
wenzelm@38567
    57
  {
wenzelm@46227
    58
    def + (that: Status): Status =
wenzelm@49036
    59
      Status(touched || that.touched, accepted || that.accepted, failed || that.failed,
wenzelm@49036
    60
        forks + that.forks, runs + that.runs)
wenzelm@49036
    61
wenzelm@49036
    62
    def is_unprocessed: Boolean = accepted && !failed && (!touched || (forks != 0 && runs == 0))
wenzelm@49036
    63
    def is_running: Boolean = runs != 0
wenzelm@49036
    64
    def is_finished: Boolean = !failed && forks == 0 && runs == 0
wenzelm@49036
    65
    def is_failed: Boolean = failed
wenzelm@46166
    66
  }
wenzelm@46166
    67
wenzelm@46166
    68
  val command_status_markup: Set[String] =
wenzelm@49036
    69
    Set(Isabelle_Markup.ACCEPTED, Isabelle_Markup.FORKED, Isabelle_Markup.JOINED,
wenzelm@49036
    70
      Isabelle_Markup.RUNNING, Isabelle_Markup.FINISHED, Isabelle_Markup.FAILED)
wenzelm@46166
    71
wenzelm@46166
    72
  def command_status(status: Status, markup: Markup): Status =
wenzelm@46166
    73
    markup match {
wenzelm@47395
    74
      case Markup(Isabelle_Markup.ACCEPTED, _) => status.copy(accepted = true)
wenzelm@49036
    75
      case Markup(Isabelle_Markup.FORKED, _) => status.copy(touched = true, forks = status.forks + 1)
wenzelm@49036
    76
      case Markup(Isabelle_Markup.JOINED, _) => status.copy(forks = status.forks - 1)
wenzelm@49036
    77
      case Markup(Isabelle_Markup.RUNNING, _) => status.copy(touched = true, runs = status.runs + 1)
wenzelm@49036
    78
      case Markup(Isabelle_Markup.FINISHED, _) => status.copy(runs = status.runs - 1)
wenzelm@46166
    79
      case Markup(Isabelle_Markup.FAILED, _) => status.copy(failed = true)
wenzelm@46166
    80
      case _ => status
wenzelm@38567
    81
    }
wenzelm@46166
    82
wenzelm@46166
    83
  def command_status(markups: List[Markup]): Status =
wenzelm@46227
    84
    (Status.init /: markups)(command_status(_, _))
wenzelm@38887
    85
wenzelm@46209
    86
wenzelm@46209
    87
  /* node status */
wenzelm@46209
    88
wenzelm@46688
    89
  sealed case class Node_Status(
wenzelm@46688
    90
    unprocessed: Int, running: Int, finished: Int, warned: Int, failed: Int)
wenzelm@44866
    91
  {
wenzelm@46688
    92
    def total: Int = unprocessed + running + finished + warned + failed
wenzelm@44866
    93
  }
wenzelm@44613
    94
wenzelm@44613
    95
  def node_status(
wenzelm@44613
    96
    state: Document.State, version: Document.Version, node: Document.Node): Node_Status =
wenzelm@44613
    97
  {
wenzelm@44613
    98
    var unprocessed = 0
wenzelm@44613
    99
    var running = 0
wenzelm@44613
   100
    var finished = 0
wenzelm@46688
   101
    var warned = 0
wenzelm@44613
   102
    var failed = 0
wenzelm@44613
   103
    node.commands.foreach(command =>
wenzelm@46166
   104
      {
wenzelm@46688
   105
        val st = state.command_state(version, command)
wenzelm@46688
   106
        val status = command_status(st.status)
wenzelm@46166
   107
        if (status.is_running) running += 1
wenzelm@46688
   108
        else if (status.is_finished) {
wenzelm@46688
   109
          if (st.results.exists(p => is_warning(p._2))) warned += 1
wenzelm@46688
   110
          else finished += 1
wenzelm@46688
   111
        }
wenzelm@46166
   112
        else if (status.is_failed) failed += 1
wenzelm@46166
   113
        else unprocessed += 1
wenzelm@44613
   114
      })
wenzelm@46688
   115
    Node_Status(unprocessed, running, finished, warned, failed)
wenzelm@44613
   116
  }
wenzelm@44613
   117
wenzelm@38887
   118
wenzelm@39441
   119
  /* result messages */
wenzelm@39439
   120
wenzelm@39439
   121
  def clean_message(body: XML.Body): XML.Body =
wenzelm@45666
   122
    body filter { case XML.Elem(Markup(Isabelle_Markup.NO_REPORT, _), _) => false case _ => true } map
wenzelm@39439
   123
      { case XML.Elem(markup, ts) => XML.Elem(markup, clean_message(ts)) case t => t }
wenzelm@39439
   124
wenzelm@39441
   125
  def message_reports(msg: XML.Tree): List[XML.Elem] =
wenzelm@39441
   126
    msg match {
wenzelm@45666
   127
      case elem @ XML.Elem(Markup(Isabelle_Markup.REPORT, _), _) => List(elem)
wenzelm@39441
   128
      case XML.Elem(_, body) => body.flatMap(message_reports)
wenzelm@39441
   129
      case XML.Text(_) => Nil
wenzelm@39441
   130
    }
wenzelm@39441
   131
wenzelm@39439
   132
wenzelm@39511
   133
  /* specific messages */
wenzelm@39511
   134
wenzelm@39625
   135
 def is_tracing(msg: XML.Tree): Boolean =
wenzelm@39622
   136
    msg match {
wenzelm@45666
   137
      case XML.Elem(Markup(Isabelle_Markup.TRACING, _), _) => true
wenzelm@39622
   138
      case _ => false
wenzelm@39622
   139
    }
wenzelm@39622
   140
wenzelm@39511
   141
  def is_warning(msg: XML.Tree): Boolean =
wenzelm@39511
   142
    msg match {
wenzelm@45666
   143
      case XML.Elem(Markup(Isabelle_Markup.WARNING, _), _) => true
wenzelm@39511
   144
      case _ => false
wenzelm@39511
   145
    }
wenzelm@39511
   146
wenzelm@39511
   147
  def is_error(msg: XML.Tree): Boolean =
wenzelm@39511
   148
    msg match {
wenzelm@45666
   149
      case XML.Elem(Markup(Isabelle_Markup.ERROR, _), _) => true
wenzelm@39511
   150
      case _ => false
wenzelm@39511
   151
    }
wenzelm@38887
   152
wenzelm@39441
   153
  def is_state(msg: XML.Tree): Boolean =
wenzelm@39170
   154
    msg match {
wenzelm@45666
   155
      case XML.Elem(Markup(Isabelle_Markup.WRITELN, _),
wenzelm@45666
   156
        List(XML.Elem(Markup(Isabelle_Markup.STATE, _), _))) => true
wenzelm@39170
   157
      case _ => false
wenzelm@39170
   158
    }
wenzelm@39170
   159
wenzelm@47542
   160
  object Sendback
wenzelm@47542
   161
  {
wenzelm@47542
   162
    def unapply(msg: Any): Option[XML.Body] =
wenzelm@47542
   163
      msg match {
wenzelm@47542
   164
        case XML.Elem(Markup(Isabelle_Markup.SENDBACK, _), body) => Some(body)
wenzelm@47542
   165
        case _ => None
wenzelm@47542
   166
      }
wenzelm@47542
   167
  }
wenzelm@47542
   168
wenzelm@39511
   169
wenzelm@39511
   170
  /* reported positions */
wenzelm@39511
   171
wenzelm@39627
   172
  private val include_pos =
wenzelm@45666
   173
    Set(Isabelle_Markup.BINDING, Isabelle_Markup.ENTITY, Isabelle_Markup.REPORT,
wenzelm@45666
   174
      Isabelle_Markup.POSITION)
wenzelm@39441
   175
wenzelm@39441
   176
  def message_positions(command: Command, message: XML.Elem): Set[Text.Range] =
wenzelm@38887
   177
  {
wenzelm@39441
   178
    def positions(set: Set[Text.Range], tree: XML.Tree): Set[Text.Range] =
wenzelm@38887
   179
      tree match {
wenzelm@39172
   180
        case XML.Elem(Markup(name, Position.Id_Range(id, raw_range)), body)
wenzelm@39172
   181
        if include_pos(name) && id == command.id =>
wenzelm@39172
   182
          val range = command.decode(raw_range).restrict(command.range)
wenzelm@39441
   183
          body.foldLeft(if (range.is_singularity) set else set + range)(positions)
wenzelm@39441
   184
        case XML.Elem(Markup(name, _), body) => body.foldLeft(set)(positions)
wenzelm@39042
   185
        case _ => set
wenzelm@38887
   186
      }
wenzelm@39441
   187
    val set = positions(Set.empty, message)
wenzelm@39170
   188
    if (set.isEmpty && !is_state(message))
wenzelm@39172
   189
      set ++ Position.Range.unapply(message.markup.properties).map(command.decode(_))
wenzelm@39042
   190
    else set
wenzelm@38887
   191
  }
wenzelm@38412
   192
}
wenzelm@38412
   193
wenzelm@38412
   194
wenzelm@45709
   195
trait Protocol extends Isabelle_Process
wenzelm@38412
   196
{
wenzelm@38412
   197
  /* commands */
wenzelm@38412
   198
wenzelm@44644
   199
  def define_command(command: Command): Unit =
wenzelm@45709
   200
    input("Document.define_command",
wenzelm@48705
   201
      Document.ID(command.id), encode(command.name), encode(command.source))
wenzelm@38412
   202
wenzelm@38412
   203
wenzelm@38417
   204
  /* document versions */
wenzelm@38412
   205
wenzelm@47343
   206
  def discontinue_execution() { input("Document.discontinue_execution") }
wenzelm@47343
   207
wenzelm@47343
   208
  def cancel_execution() { input("Document.cancel_execution") }
wenzelm@44612
   209
wenzelm@44481
   210
  def update(old_id: Document.Version_ID, new_id: Document.Version_ID,
wenzelm@44383
   211
    edits: List[Document.Edit_Command])
wenzelm@38412
   212
  {
wenzelm@44157
   213
    val edits_yxml =
wenzelm@43767
   214
    { import XML.Encode._
wenzelm@44383
   215
      def id: T[Command] = (cmd => long(cmd.id))
wenzelm@46737
   216
      def encode_edit(name: Document.Node.Name)
wenzelm@44979
   217
          : T[Document.Node.Edit[(Option[Command], Option[Command]), Command.Perspective]] =
wenzelm@44979
   218
        variant(List(
wenzelm@48755
   219
          { case Document.Node.Clear() => (Nil, Nil) },  // FIXME unused !?
wenzelm@44979
   220
          { case Document.Node.Edits(a) => (Nil, list(pair(option(id), option(id)))(a)) },
wenzelm@48707
   221
          { case Document.Node.Deps(header) =>
wenzelm@46770
   222
              val dir = Isabelle_System.posix_path(name.dir)
wenzelm@48707
   223
              val imports = header.imports.map(_.node)
wenzelm@46770
   224
              // FIXME val uses = deps.uses.map(p => (Isabelle_System.posix_path(p._1), p._2))
wenzelm@48707
   225
              val uses = header.uses
wenzelm@44979
   226
              (Nil,
wenzelm@48707
   227
                pair(Encode.string, pair(Encode.string, pair(list(Encode.string),
wenzelm@48864
   228
                  pair(list(pair(Encode.string,
wenzelm@48864
   229
                    option(pair(pair(Encode.string, list(Encode.string)), list(Encode.string))))),
wenzelm@48707
   230
                  pair(list(pair(Encode.string, bool)), list(Encode.string))))))(
wenzelm@48707
   231
                (dir, (name.theory, (imports, (header.keywords, (uses, header.errors))))))) },
wenzelm@44979
   232
          { case Document.Node.Perspective(a) => (a.commands.map(c => long_atom(c.id)), Nil) }))
wenzelm@48705
   233
      def encode_edits: T[List[Document.Edit_Command]] = list((node_edit: Document.Edit_Command) =>
wenzelm@44979
   234
      {
wenzelm@44979
   235
        val (name, edit) = node_edit
wenzelm@46737
   236
        pair(string, encode_edit(name))(name.node, edit)
wenzelm@44979
   237
      })
wenzelm@48705
   238
      YXML.string_of_body(encode_edits(edits)) }
wenzelm@45709
   239
    input("Document.update", Document.ID(old_id), Document.ID(new_id), edits_yxml)
wenzelm@38412
   240
  }
wenzelm@43748
   241
wenzelm@44673
   242
  def remove_versions(versions: List[Document.Version])
wenzelm@44673
   243
  {
wenzelm@44673
   244
    val versions_yxml =
wenzelm@44673
   245
      { import XML.Encode._
wenzelm@44673
   246
        YXML.string_of_body(list(long)(versions.map(_.id))) }
wenzelm@45709
   247
    input("Document.remove_versions", versions_yxml)
wenzelm@44673
   248
  }
wenzelm@44673
   249
wenzelm@43748
   250
wenzelm@43748
   251
  /* method invocation service */
wenzelm@43748
   252
wenzelm@43748
   253
  def invoke_scala(id: String, tag: Invoke_Scala.Tag.Value, res: String)
wenzelm@43748
   254
  {
wenzelm@45709
   255
    input("Document.invoke_scala", id, tag.toString, res)
wenzelm@43748
   256
  }
wenzelm@38412
   257
}